function assertf(stmt, fmt, ...) if not stmt then error(string.format(fmt,...)) end end describe("smr imageboard parser #parsers",function() it("should load without error",function() local parser = require("parser_imageboard") end) it("should accept a string and return a string",function() local parser = require("parser_imageboard") local input = "Hello, world!" local output = parser(input) assert(type(output) == "string","Expected string, got: %s",type(output)) end) it("should spoiler text in asterisks ",function() local parser = require("parser_imageboard") local input = "Hello, **world**!" local output = parser(input) local expected = [[

Hello, world!

]] assertf(output == expected, "Expected\n%s\ngot\n%s\n", expected, output) end) it("should spoiler text in [spoiler] tags",function() local parser = require("parser_imageboard") local input = "Hello, [spoiler]world[/spoiler]!" local output = parser(input) local expected = [[

Hello, world!

]] assertf(output == expected, "Expected\n%s\ngot\n%s\n", expected, output) end) it("should italicize words in double single quotes ('')",function() local parser = require("parser_imageboard") local input = "Hello, ''world''!" local output = parser(input) local expected = [[

Hello, world!

]] assertf(output == expected, "Expected\n%s\ngot\n%s\n", expected, output) end) it("should bold words in tripple single quotes (''')",function() local parser = require("parser_imageboard") local input = "Hello, '''world'''!" local output = parser(input) local expected = [[

Hello, world!

]] assertf(output == expected, "Expected\n%s\ngot\n%s\n", expected, output) end) it("should underline words in double underscores (__)",function() local parser = require("parser_imageboard") local input = "Hello, __world__!" local output = parser(input) local expected = [[

Hello, world!

]] assertf(output == expected, "Expected\n%s\ngot\n%s\n", expected, output) end) it("should make a heading out of things in double equals(==)",function() local parser = require("parser_imageboard") local input = "Hello, ==world==!" local output = parser(input) local expected = [[

Hello,

world

!

]] assertf(output == expected, "Expected\n%s\ngot\n%s\n", expected, output) end) it("should strikethrough words in double tildes (~~)",function() local parser = require("parser_imageboard") local input = "Hello, ~~world~~!" local output = parser(input) local expected = [[

Hello, world!

]] assertf(output == expected, "Expected\n%s\ngot\n%s\n", expected, output) end) it("should codify words in [code] tags",function() local parser = require("parser_imageboard") local input = "Hello, [code]world[/code]!" local output = parser(input) local expected = [[

Hello,

world
!

]] assertf(output == expected, "Expected\n%s\ngot\n%s\n", expected, output) end) it("should greentext lines that start with >",function() local parser = require("parser_imageboard") local input = "Hello,\n> world!" local output = parser(input) local expected = [[

Hello,

> world!

]] assertf(output == expected, "Expected\n%s\ngot\n%s\n", expected, output) end) it("should pinktext lines that start with <",function() local parser = require("parser_imageboard") local input = "Hello,\n< world!" local output = parser(input) local expected = [[

Hello,

< world!

]] assertf(output == expected, "Expected\n%s\ngot\n%s\n", expected, output) end) it("should allow for bold+italic text",function() local parser = require("parser_imageboard") local input = "Hello,'''''world!'''''" local output = parser(input) local expected = [[

Hello,world!

]] end) local formatting = { {"**","**"}, {"[spoiler]","[/spoiler]"}, {"''","''"}, {"'''","'''"}, {"__","__"}, {"==","=="}, {"~~","~~"}, {"[code]","[/code]"} } local formatting_line = {"> ", "< "} for k,v in pairs(formatting) do for i = 1, 50 do it("should not break with " .. i .. " " .. v[1] .. " indicators in a row ",function() local parser = require("parser_imageboard") local input = "Hello, " .. string.rep(v[1],i) .. " world!" local start_time = os.clock() local output = parser(input) local end_time = os.clock() assert(end_time - start_time < 1, "Took too long") end) end end for i = 1, 50 do it("Should withstand a random string of " .. i .. " formatters and words. ",function() local parser = require("parser_imageboard") local input = {} local function random_text() if math.random() > 0.5 then return "Hello" else return "world" end end local function random_wrap(text) local rngwrap = formatting[math.random(#formatting)] return rngwrap[1] .. text .. rngwrap[2] end local function random_text_recursive(i) if i == 0 then return "" end local j = math.random() if j < 0.33 then return random_text_recursive(i-1) .. random_wrap(random_text()) elseif j < 0.66 then return random_wrap(random_text() .. random_text_recursive(i-1)) .. random_wrap(random_text()) else return random_wrap(random_text() .. random_text_recursive(i - 1)) end end input = random_text_recursive(i) local start_time = os.clock() local output = parser(input) local end_time = os.clock() assert(end_time - start_time < 1, "Took too long") end) end for _,file_name in ipairs{ "Beauty_and_the_Banchou_1" } do it("should parser " .. file_name,function() local parser = require("parser_imageboard") local input = require("spec.parser_tests." .. file_name) local output = parser(input) --print("output:",output) end) end end)