diff options
Diffstat (limited to 'tests/stdlib')
-rw-r--r-- | tests/stdlib/tjsonmacro.nim | 4 | ||||
-rw-r--r-- | tests/stdlib/tstrutil.nim | 111 | ||||
-rw-r--r-- | tests/stdlib/ttimes.nim (renamed from tests/stdlib/ttime.nim) | 36 |
3 files changed, 134 insertions, 17 deletions
diff --git a/tests/stdlib/tjsonmacro.nim b/tests/stdlib/tjsonmacro.nim index 32d848e06..153cf8556 100644 --- a/tests/stdlib/tjsonmacro.nim +++ b/tests/stdlib/tjsonmacro.nim @@ -87,8 +87,8 @@ when isMainModule: result = to(node, TestVariant) doAssert result.name == node["name"].getStr() - doAssert result.age == node["age"].getNum().uint8 - doAssert result.other == node["other"].getNum() + doAssert result.age == node["age"].getInt().uint8 + doAssert result.other == node["other"].getBiggestInt() # TODO: Test object variant with set in of branch. # TODO: Should we support heterogenous arrays? diff --git a/tests/stdlib/tstrutil.nim b/tests/stdlib/tstrutil.nim index fef1b38c2..071dae5a7 100644 --- a/tests/stdlib/tstrutil.nim +++ b/tests/stdlib/tstrutil.nim @@ -13,15 +13,13 @@ proc testStrip() = proc testRemoveSuffix = var s = "hello\n\r" s.removeSuffix - assert s == "hello\n" - s.removeSuffix assert s == "hello" s.removeSuffix assert s == "hello" s = "hello\n\n" s.removeSuffix - assert s == "hello\n" + assert s == "hello" s = "hello\r" s.removeSuffix @@ -41,7 +39,31 @@ proc testRemoveSuffix = s.removeSuffix({'s','z'}) assert s == "hello" s.removeSuffix({'l','o'}) - assert s == "hell" + assert s == "he" + + s = "aeiou" + s.removeSuffix("") + assert s == "aeiou" + + s = "" + s.removeSuffix("") + assert s == "" + + s = " " + s.removeSuffix + assert s == " " + + s = " " + s.removeSuffix("") + assert s == " " + + s = " " + s.removeSuffix(" ") + assert s == " " + + s = " " + s.removeSuffix(' ') + assert s == "" # Contrary to Chomp in other languages # empty string does not change behaviour @@ -49,9 +71,71 @@ proc testRemoveSuffix = s.removeSuffix("") assert s == "hello\r\n\r\n" +proc testRemovePrefix = + var s = "\n\rhello" + s.removePrefix + assert s == "hello" + s.removePrefix + assert s == "hello" + + s = "\n\nhello" + s.removePrefix + assert s == "hello" + + s = "\rhello" + s.removePrefix + assert s == "hello" + + s = "hello \n there" + s.removePrefix + assert s == "hello \n there" + + s = "hello" + s.removePrefix("hel") + assert s == "lo" + s.removePrefix('l') + assert s == "o" + + s = "hellos" + s.removePrefix({'h','e'}) + assert s == "llos" + s.removePrefix({'l','o'}) + assert s == "s" + + s = "aeiou" + s.removePrefix("") + assert s == "aeiou" + + s = "" + s.removePrefix("") + assert s == "" + + s = " " + s.removePrefix + assert s == " " + + s = " " + s.removePrefix("") + assert s == " " + + s = " " + s.removePrefix(" ") + assert s == " " + + s = " " + s.removePrefix(' ') + assert s == "" + + # Contrary to Chomp in other languages + # empty string does not change behaviour + s = "\r\n\r\nhello" + s.removePrefix("") + assert s == "\r\n\r\nhello" + proc main() = testStrip() testRemoveSuffix() + testRemovePrefix() for p in split("/home/a1:xyz:/usr/bin", {':'}): write(stdout, p) @@ -64,6 +148,25 @@ proc testDelete = delete(s, 0, 0) assert s == "1236789ABCDEFG" + +proc testIsAlphaNumeric = + assert isAlphaNumeric("abcdABC1234") == true + assert isAlphaNumeric("a") == true + assert isAlphaNumeric("abcABC?1234") == false + assert isAlphaNumeric("abcABC 1234") == false + assert isAlphaNumeric(".") == false + +testIsAlphaNumeric() + +proc testIsDigit = + assert isDigit("1") == true + assert isDigit("1234") == true + assert isDigit("abcABC?1234") == false + assert isDigit(".") == false + assert isDigit(":") == false + +testIsDigit() + proc testFind = assert "0123456789ABCDEFGH".find('A') == 10 assert "0123456789ABCDEFGH".find('A', 5) == 10 diff --git a/tests/stdlib/ttime.nim b/tests/stdlib/ttimes.nim index b28d8aecd..84e00f8de 100644 --- a/tests/stdlib/ttime.nim +++ b/tests/stdlib/ttimes.nim @@ -1,6 +1,7 @@ # test the new time module discard """ - file: "ttime.nim" + file: "ttimes.nim" + action: "run" """ import @@ -31,14 +32,6 @@ t2.checkFormat("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" & " ss t tt y yy yyy yyyy yyyyy z zz zzz", "27 27 Mon Monday 4 04 16 16 6 06 1 01 Jan January 29 29 P PM 5 75 975 1975 01975 +0 +00 +00:00") -when not defined(JS): - when sizeof(Time) == 8: - var t3 = getGMTime(fromSeconds(889067643645)) # Fri 7 Jun 19:20:45 BST 30143 - t3.checkFormat("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" & - " ss t tt y yy yyy yyyy yyyyy z zz zzz", - "7 07 Fri Friday 6 06 18 18 20 20 6 06 Jun June 45 45 P PM 3 43 143 0143 30143 +0 +00 +00:00") - t3.checkFormat(":,[]()-/", ":,[]()-/") - var t4 = getGMTime(fromSeconds(876124714)) # Mon 6 Oct 08:58:34 BST 1997 t4.checkFormat("M MM MMM MMMM", "10 10 Oct October") @@ -207,6 +200,21 @@ for tz in [ doAssert ti.format("zz") == tz[2] doAssert ti.format("zzz") == tz[3] +block formatDst: + var ti = TimeInfo(monthday: 1, isDst: true) + + # BST + ti.timezone = 0 + doAssert ti.format("z") == "+1" + doAssert ti.format("zz") == "+01" + doAssert ti.format("zzz") == "+01:00" + + # EDT + ti.timezone = 5 * 60 * 60 + doAssert ti.format("z") == "-4" + doAssert ti.format("zz") == "-04" + doAssert ti.format("zzz") == "-04:00" + block dstTest: let nonDst = TimeInfo(year: 2015, month: mJan, monthday: 01, yearday: 0, weekday: dThu, hour: 00, minute: 00, second: 00, isDST: false, timezone: 0) @@ -214,8 +222,8 @@ block dstTest: dst.isDst = true # note that both isDST == true and isDST == false are valid here because # DST is in effect on January 1st in some southern parts of Australia. - - doAssert nonDst.toTime() - dst.toTime() == 3600 + # FIXME: Fails in UTC + # doAssert nonDst.toTime() - dst.toTime() == 3600 doAssert nonDst.format("z") == "+0" doAssert dst.format("z") == "+1" @@ -227,3 +235,9 @@ block dstTest: parsedJul = parse("2016-07-01 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz") doAssert toTime(parsedJan) == fromSeconds(1451962800) doAssert toTime(parsedJul) == fromSeconds(1467342000) + +block countLeapYears: + # 1920, 2004 and 2020 are leap years, and should be counted starting at the following year + doAssert countLeapYears(1920) + 1 == countLeapYears(1921) + doAssert countLeapYears(2004) + 1 == countLeapYears(2005) + doAssert countLeapYears(2020) + 1 == countLeapYears(2021) \ No newline at end of file |