[chore] Rework util spec, rework util.secondsToClock: round seconds to minutes in 00:00 mode + spec

Most of the tests in util_spec were the wrong way around.
It's `assert(expected, given)`.
pull/3349/head
Frans de Jonge 7 years ago
parent 1508fe27c9
commit 0fa090ee47

@ -80,8 +80,9 @@ function util.secondsToClock(seconds, withoutSeconds)
return "00:00:00"; return "00:00:00";
end end
else else
local round = withoutSeconds and require("optmath").round or math.floor
local hours = string.format("%02.f", math.floor(seconds / 3600)); local hours = string.format("%02.f", math.floor(seconds / 3600));
local mins = string.format("%02.f", math.floor(seconds / 60 - (hours * 60))); local mins = string.format("%02.f", round(seconds / 60 - (hours * 60)));
if withoutSeconds then if withoutSeconds then
return hours .. ":" .. mins return hours .. ":" .. mins
end end

@ -7,205 +7,202 @@ describe("util module", function()
end) end)
it("should strip punctuations around word", function() it("should strip punctuations around word", function()
assert.is_equal(util.stripePunctuations("\"hello world\""), "hello world") assert.is_equal("hello world", util.stripePunctuations("\"hello world\""))
assert.is_equal(util.stripePunctuations("\"hello world?\""), "hello world") assert.is_equal("hello world", util.stripePunctuations("\"hello world?\""))
assert.is_equal(util.stripePunctuations("\"hello, world?\""), "hello, world") assert.is_equal("hello, world", util.stripePunctuations("\"hello, world?\""))
assert.is_equal(util.stripePunctuations("“你好“"), "你好") assert.is_equal("你好", util.stripePunctuations("“你好“"))
assert.is_equal(util.stripePunctuations("“你好?“"), "你好") assert.is_equal("你好", util.stripePunctuations("“你好?“"))
assert.is_equal(util.stripePunctuations(""), "") assert.is_equal("", util.stripePunctuations(""))
assert.is_equal(util.stripePunctuations(nil), nil) assert.is_nil(util.stripePunctuations(nil))
end) end)
it("should split string with patterns", function() describe("gsplit()", function()
local sentence = "Hello world, welcome to KOReader!" it("should split string with patterns", function()
local words = {} local sentence = "Hello world, welcome to KOReader!"
for word in util.gsplit(sentence, "%s+", false) do local words = {}
table.insert(words, word) for word in util.gsplit(sentence, "%s+", false) do
end table.insert(words, word)
assert.are_same(words, {"Hello", "world,", "welcome", "to", "KOReader!"}) end
end) assert.are_same({"Hello", "world,", "welcome", "to", "KOReader!"}, words)
end)
it("should split command line arguments with quotation", function() it("should split command line arguments with quotation", function()
local command = "./sdcv -nj \"words\" \"a lot\" 'more or less' --data-dir=dict" local command = "./sdcv -nj \"words\" \"a lot\" 'more or less' --data-dir=dict"
local argv = {} local argv = {}
for arg1 in util.gsplit(command, "[\"'].-[\"']", true) do for arg1 in util.gsplit(command, "[\"'].-[\"']", true) do
for arg2 in util.gsplit(arg1, "^[^\"'].-%s+", true) do for arg2 in util.gsplit(arg1, "^[^\"'].-%s+", true) do
for arg3 in util.gsplit(arg2, "[\"']", false) do for arg3 in util.gsplit(arg2, "[\"']", false) do
local trimed = arg3:gsub("^%s*(.-)%s*$", "%1") local trimed = arg3:gsub("^%s*(.-)%s*$", "%1")
if trimed ~= "" then if trimed ~= "" then
table.insert(argv, trimed) table.insert(argv, trimed)
end
end end
end end
end end
end assert.are_same({"./sdcv", "-nj", "words", "a lot", "more or less", "--data-dir=dict"}, argv)
assert.are_same(argv, {"./sdcv", "-nj", "words", "a lot", "more or less", "--data-dir=dict"}) end)
end) it("should split string with dashes", function()
local words = {}
it("should split with splitter", function() for word in util.gsplit("a-b-c-d", "-", false) do
local words = {} table.insert(words, word)
for word in util.gsplit("a-b-c-d", "-", false) do end
table.insert(words, word) assert.are_same({"a", "b", "c", "d"}, words)
end end)
assert.are_same(words, {"a", "b", "c", "d"}) it("should split string with dashes with final dash", function()
end) local words = {}
for word in util.gsplit("a-b-c-d-", "-", false) do
it("should also split with splitter", function() table.insert(words, word)
local words = {} end
for word in util.gsplit("a-b-c-d-", "-", false) do assert.are_same({"a", "b", "c", "d"}, words)
table.insert(words, word) end)
end
assert.are_same(words, {"a", "b", "c", "d"})
end)
it("should split line into words", function()
local words = util.splitToWords("one two,three four . five")
assert.are_same(words, {
"one",
" ",
"two",
",",
"three",
" ",
"four",
" . ",
"five",
})
end)
it("should split ancient greek words", function()
local words = util.splitToWords("Λαρισαῖος Λευκοθέα Λιγυαστάδης.")
assert.are_same(words, {
"Λαρισαῖος",
" ",
"Λευκοθέα",
" ",
"Λιγυαστάδης",
"."
})
end)
it("should split Chinese words", function()
local words = util.splitToWords("彩虹是通过太阳光的折射引起的。")
assert.are_same(words, {
"","","","","","","","","","","","","","","",
})
end) end)
it("should split words of multilingual text", function() describe("splitToWords()", function()
local words = util.splitToWords("BBC纪录片") it("should split line into words", function()
assert.are_same(words, {"BBC", "", "", ""}) local words = util.splitToWords("one two,three four . five")
assert.are_same({
"one",
" ",
"two",
",",
"three",
" ",
"four",
" . ",
"five",
}, words)
end)
it("should split ancient greek words", function()
local words = util.splitToWords("Λαρισαῖος Λευκοθέα Λιγυαστάδης.")
assert.are_same({
"Λαρισαῖος",
" ",
"Λευκοθέα",
" ",
"Λιγυαστάδης",
"."
}, words)
end)
it("should split Chinese words", function()
local words = util.splitToWords("彩虹是通过太阳光的折射引起的。")
assert.are_same({
"","","","","","","","","","","","","","","",
}, words)
end)
it("should split words of multilingual text", function()
local words = util.splitToWords("BBC纪录片")
assert.are_same({"BBC", "", "", ""}, words)
end)
end) end)
it("should split text to line - unicode", function() describe("splitToChars()", function()
local text = "Pójdźże, chmurność glück schließen Štěstí neštěstí. Uñas gavilán" it("should split text to line - unicode", function()
local word = "" local text = "Pójdźże, chmurność glück schließen Štěstí neštěstí. Uñas gavilán"
local table_of_words = {} local word = ""
local c local table_of_words = {}
local table_chars = util.splitToChars(text) local c
for i = 1, #table_chars do local table_chars = util.splitToChars(text)
c = table_chars[i] for i = 1, #table_chars do
word = word .. c c = table_chars[i]
if util.isSplittable(c) then word = word .. c
table.insert(table_of_words, word) if util.isSplittable(c) then
word = "" table.insert(table_of_words, word)
word = ""
end
if i == #table_chars then table.insert(table_of_words, word) end
end end
if i == #table_chars then table.insert(table_of_words, word) end assert.are_same({
end "Pójdźże, ",
assert.are_same(table_of_words, { "chmurność ",
"Pójdźże, ", "glück ",
"chmurność ", "schließen ",
"glück ", "Štěstí ",
"schließen ", "neštěstí. ",
"Štěstí ", "Uñas ",
"neštěstí. ", "gavilán",
"Uñas ", }, table_of_words)
"gavilán", end)
}) it("should split text to line - CJK", function()
end) local text = "彩虹是通过太阳光的折射引起的。"
local word = ""
it("should split text to line - CJK", function() local table_of_words = {}
local text = "彩虹是通过太阳光的折射引起的。" local c
local word = "" local table_chars = util.splitToChars(text)
local table_of_words = {} for i = 1, #table_chars do
local c c = table_chars[i]
local table_chars = util.splitToChars(text) word = word .. c
for i = 1, #table_chars do if util.isSplittable(c) then
c = table_chars[i] table.insert(table_of_words, word)
word = word .. c word = ""
if util.isSplittable(c) then end
table.insert(table_of_words, word) if i == #table_chars then table.insert(table_of_words, word) end
word = ""
end end
if i == #table_chars then table.insert(table_of_words, word) end assert.are_same({
end "","","","","","","","","","","","","","","",
assert.are_same(table_of_words, { }, table_of_words)
"","","","","","","","","","","","","","","", end)
}) it("should split text to line with next_c - unicode", function()
end) local text = "Ce test : 1) est très simple ; 2 ) simple comme ( 2/2 ) > 50 % ? ok."
local word = ""
it("should split text to line with next_c - unicode", function() local table_of_words = {}
local text = "Ce test : 1) est très simple ; 2 ) simple comme ( 2/2 ) > 50 % ? ok." local c, next_c
local word = "" local table_chars = util.splitToChars(text)
local table_of_words = {} for i = 1, #table_chars do
local c, next_c c = table_chars[i]
local table_chars = util.splitToChars(text) next_c = i < #table_chars and table_chars[i+1] or nil
for i = 1, #table_chars do word = word .. c
c = table_chars[i] if util.isSplittable(c, next_c) then
next_c = i < #table_chars and table_chars[i+1] or nil table.insert(table_of_words, word)
word = word .. c word = ""
if util.isSplittable(c, next_c) then end
table.insert(table_of_words, word) if i == #table_chars then table.insert(table_of_words, word) end
word = ""
end end
if i == #table_chars then table.insert(table_of_words, word) end assert.are_same({
end "Ce ",
assert.are_same(table_of_words, { "test : ",
"Ce ", "1) ",
"test : ", "est ",
"1) ", "très ",
"est ", "simple ; ",
"très ", "2 ) ",
"simple ; ", "simple ",
"2 ) ", "comme ",
"simple ", "( ",
"comme ", "2/2 ) > ",
"( ", "50 % ? ",
"2/2 ) > ", "ok."
"50 % ? ", }, table_of_words)
"ok." end)
}) it("should split text to line with next_c and prev_c - unicode", function()
end) local text = "Ce test : 1) est « très simple » ; 2 ) simple comme ( 2/2 ) > 50 % ? ok."
local word = ""
it("should split text to line with next_c and prev_c - unicode", function() local table_of_words = {}
local text = "Ce test : 1) est « très simple » ; 2 ) simple comme ( 2/2 ) > 50 % ? ok." local c, next_c, prev_c
local word = "" local table_chars = util.splitToChars(text)
local table_of_words = {} for i = 1, #table_chars do
local c, next_c, prev_c c = table_chars[i]
local table_chars = util.splitToChars(text) next_c = i < #table_chars and table_chars[i+1] or nil
for i = 1, #table_chars do prev_c = i > 1 and table_chars[i-1] or nil
c = table_chars[i] word = word .. c
next_c = i < #table_chars and table_chars[i+1] or nil if util.isSplittable(c, next_c, prev_c) then
prev_c = i > 1 and table_chars[i-1] or nil table.insert(table_of_words, word)
word = word .. c word = ""
if util.isSplittable(c, next_c, prev_c) then end
table.insert(table_of_words, word) if i == #table_chars then table.insert(table_of_words, word) end
word = ""
end end
if i == #table_chars then table.insert(table_of_words, word) end assert.are_same({
end "Ce ",
assert.are_same(table_of_words, { "test : ",
"Ce ", "1) ",
"test : ", "est ",
"1) ", "« très ",
"est ", "simple » ; ",
"« très ", "2 ) ",
"simple » ; ", "simple ",
"2 ) ", "comme ",
"simple ", "( 2/2 ) > 50 % ? ",
"comme ", "ok."
"( 2/2 ) > 50 % ? ", }, table_of_words)
"ok." end)
})
end) end)
it("should split file path and name", function() it("should split file path and name", function()
@ -222,7 +219,7 @@ describe("util module", function()
test(nil, "", "") test(nil, "", "")
test("a/b", "a/", "b") test("a/b", "a/", "b")
test("/b", "/", "b") test("/b", "/", "b")
assert.are_same(util.splitFilePathName("/a/b/c.txt"), "/a/b/") assert.are_same("/a/b/", util.splitFilePathName("/a/b/c.txt"))
end) end)
it("should split file name and suffix", function() it("should split file name and suffix", function()
@ -239,74 +236,113 @@ describe("util module", function()
test("/a/.txt", "/a/", "txt") test("/a/.txt", "/a/", "txt")
test(nil, "", "") test(nil, "", "")
test("", "", "") test("", "", "")
assert.are_same(util.splitFileNameSuffix("a.txt"), "a") assert.are_same("a", util.splitFileNameSuffix("a.txt"))
end) end)
it("should replace invalid UTF-8 characters with an underscore", function() describe("fixUtf8()", function()
assert.is_equal(util.fixUtf8("\127 \128 \194\127 ", "_"), "\127 _ _\127 ") it("should replace invalid UTF-8 characters with an underscore", function()
end) assert.is_equal("\127 _ _\127 ", util.fixUtf8("\127 \128 \194\127 ", "_"))
end)
it("should replace invalid UTF-8 characters with multiple characters", function() it("should replace invalid UTF-8 characters with multiple characters", function()
assert.is_equal(util.fixUtf8("\127 \128 \194\127 ", "__"), "\127 __ __\127 ") assert.is_equal("\127 __ __\127 ", util.fixUtf8("\127 \128 \194\127 ", "__"))
end) end)
it("should replace invalid UTF-8 characters with empty char", function() it("should replace invalid UTF-8 characters with empty char", function()
assert.is_equal(util.fixUtf8("\127 \128 \194\127 ", ""), "\127 \127 ") assert.is_equal("\127 \127 ", util.fixUtf8("\127 \128 \194\127 ", ""))
end) end)
it("should not replace valid UTF-8 <20> character", function() it("should not replace valid UTF-8 <20> character", function()
assert.is_equal(util.fixUtf8("<EFBFBD>valid <20> char <20>", "__"), "<EFBFBD>valid <20> char <20>") assert.is_equal("<EFBFBD>valid <20> char <20>", util.fixUtf8("<EFBFBD>valid <20> char <20>", "__"))
end) end)
it("should not replace valid UTF-8 characters", function() it("should not replace valid UTF-8 characters", function()
assert.is_equal(util.fixUtf8("\99 \244\129\130\190", "_"), "\99 \244\129\130\190") assert.is_equal("\99 \244\129\130\190", util.fixUtf8("\99 \244\129\130\190", "_"))
end) end)
it("should not replace valid UTF-8 characters Polish chars", function() it("should not replace valid UTF-8 characters Polish chars", function()
assert.is_equal(util.fixUtf8("Pójdźże źółć", "_"), "Pójdźże źółć") assert.is_equal("Pójdźże źółć", util.fixUtf8("Pójdźże źółć", "_"))
end) end)
it("should not replace valid UTF-8 characters German chars", function() it("should not replace valid UTF-8 characters German chars", function()
assert.is_equal(util.fixUtf8("glück schließen", "_"), "glück schließen") assert.is_equal("glück schließen", util.fixUtf8("glück schließen", "_"))
end)
end) end)
it("should split input to array", function() describe("splitToArray()", function()
assert.are_same(util.splitToArray("100\tabc\t\tdef\tghi200\t", "\t", true), it("should split input to array", function()
{"100", "abc", "", "def", "ghi200"}) assert.are_same({"100", "abc", "", "def", "ghi200"},
end) util.splitToArray("100\tabc\t\tdef\tghi200\t", "\t", true))
end)
it("should also split input to array", function() it("should also split input to array", function()
assert.are_same(util.splitToArray("abcabcabcabca", "a", true), assert.are_same({"", "bc", "bc", "bc", "bc"},
{"", "bc", "bc", "bc", "bc"}) util.splitToArray("abcabcabcabca", "a", true))
end) end)
it("should split input to array without empty entities", function() it("should split input to array without empty entities", function()
assert.are_same(util.splitToArray("100 abc def ghi200 ", " ", false), assert.are_same({"100", "abc", "def", "ghi200"},
{"100", "abc", "def", "ghi200"}) util.splitToArray("100 abc def ghi200 ", " ", false))
end)
end) end)
it("should guess it is not HTML and let is as is", function() describe("htmlToPlainTextIfHtml()", function()
local s = "if (i < 0 && j < 0) j = i&amp;" it("should guess it is not HTML and let is as is", function()
assert.is_equal(util.htmlToPlainTextIfHtml(s), s) local s = "if (i < 0 && j < 0) j = i&amp;"
assert.is_equal(s, util.htmlToPlainTextIfHtml(s))
end)
it("should guess it is HTML and convert it to text", function()
assert.is_equal("Making unit tests is fun & nécéssaire",
util.htmlToPlainTextIfHtml("<div> <br> Making <b>unit&nbsp;tests</b> is <i class='notreally'>fun &amp; n&#xE9;c&#233;ssaire</i><br/> </div>"))
end)
it("should guess it is double encoded HTML and convert it to text", function()
assert.is_equal("Deux parties.\nPrologue.Désespérée, elle le tue...\nPremière partie. Sur la route & dans la nuit",
util.htmlToPlainTextIfHtml("Deux parties.&lt;br&gt;Prologue.Désespérée, elle le tue...&lt;br&gt;Première partie. Sur la route &amp;amp; dans la nuit"))
end)
end) end)
it("should guess it is HTML and convert it to text", function()
assert.is_equal(util.htmlToPlainTextIfHtml("<div> <br> Making <b>unit&nbsp;tests</b> is <i class='notreally'>fun &amp; n&#xE9;c&#233;ssaire</i><br/> </div>"), describe("isEmptyDir()", function()
"Making unit tests is fun & nécéssaire") it("should return true on empty dir", function()
end) assert.is_true(util.isEmptyDir(DataStorage:getDataDir() .. "/data/dict")) -- should be empty during unit tests
it("should guess it is double encoded HTML and convert it to text", function() end)
assert.is_equal(util.htmlToPlainTextIfHtml("Deux parties.&lt;br&gt;Prologue.Désespérée, elle le tue...&lt;br&gt;Première partie. Sur la route &amp;amp; dans la nuit"), it("should return false on non-empty dir", function()
"Deux parties.\nPrologue.Désespérée, elle le tue...\nPremière partie. Sur la route & dans la nuit") assert.is_false(util.isEmptyDir(DataStorage:getDataDir())) -- should contain subdirectories
end) end)
it("should return true on empty dir", function() it("should return nil on non-existent dir", function()
assert.is_equal(util.isEmptyDir(DataStorage:getDataDir() .. "/data/dict"), -- should be empty during unit tests assert.is_nil(util.isEmptyDir("/this/is/just/some/nonsense/really/this/should/not/exist"))
true) end)
end)
it("should return false on non-empty dir", function()
assert.is_equal(util.isEmptyDir(DataStorage:getDataDir()), -- should contain subdirectories
false)
end) end)
it("should return nil on non-existent dir", function()
assert.is_equal(util.isEmptyDir("/this/is/just/some/nonsense/really/this/should/not/exist"), describe("secondsToClock()", function()
nil) it("should convert seconds to 00:00 format", function()
assert.is_equal("00:00",
util.secondsToClock(0, true))
assert.is_equal("00:01",
util.secondsToClock(60, true))
end)
it("should round seconds to minutes in 00:00 format", function()
assert.is_equal("00:01",
util.secondsToClock(89, true))
assert.is_equal("00:02",
util.secondsToClock(90, true))
assert.is_equal("00:02",
util.secondsToClock(110, true))
assert.is_equal("00:02",
util.secondsToClock(120, true))
end)
it("should convert seconds to 00:00:00 format", function()
assert.is_equal("00:00:00",
util.secondsToClock(0))
assert.is_equal("00:01:00",
util.secondsToClock(60))
assert.is_equal("00:01:29",
util.secondsToClock(89))
assert.is_equal("00:01:30",
util.secondsToClock(90))
assert.is_equal("00:01:50",
util.secondsToClock(110))
assert.is_equal("00:02:00",
util.secondsToClock(120))
end)
end) end)
end) end)

Loading…
Cancel
Save