summary refs log tree commit diff stats
path: root/tests/collections
diff options
context:
space:
mode:
authorFabian Keller <bluenote10@users.noreply.github.com>2017-12-14 14:02:13 +0100
committerAndreas Rumpf <rumpf_a@web.de>2017-12-14 14:02:13 +0100
commit6df6ec27ec573fc7f619f7bf9fece6d6b0dc931f (patch)
treef9eadca2e1b46112ec11da257cb664010a369a8d /tests/collections
parentbc1123536e36a222dc3bf65c40c6ceee07da6499 (diff)
downloadNim-6df6ec27ec573fc7f619f7bf9fece6d6b0dc931f.tar.gz
Improved collection-to-string behavior (#6825)
Diffstat (limited to 'tests/collections')
-rw-r--r--tests/collections/tcollections_to_string.nim106
-rw-r--r--tests/collections/ttables.nim2
-rw-r--r--tests/collections/ttablesref.nim4
3 files changed, 109 insertions, 3 deletions
diff --git a/tests/collections/tcollections_to_string.nim b/tests/collections/tcollections_to_string.nim
new file mode 100644
index 000000000..6cc8a84ff
--- /dev/null
+++ b/tests/collections/tcollections_to_string.nim
@@ -0,0 +1,106 @@
+discard """
+  exitcode: 0
+  output: ""
+"""
+import sets
+import tables
+import deques
+import lists
+import critbits
+
+# Tests for tuples
+doAssert $(1, 2, 3) == "(Field0: 1, Field1: 2, Field2: 3)"
+doAssert $("1", "2", "3") == """(Field0: "1", Field1: "2", Field2: "3")"""
+doAssert $('1', '2', '3') == """(Field0: '1', Field1: '2', Field2: '3')"""
+
+# Tests for seqs
+doAssert $(@[1, 2, 3]) == "@[1, 2, 3]"
+doAssert $(@["1", "2", "3"]) == """@["1", "2", "3"]"""
+doAssert $(@['1', '2', '3']) == """@['1', '2', '3']"""
+
+# Tests for sets
+doAssert $(toSet([1])) == "{1}"
+doAssert $(toSet(["1"])) == """{"1"}"""
+doAssert $(toSet(['1'])) == """{'1'}"""
+doAssert $(toOrderedSet([1, 2, 3])) == "{1, 2, 3}"
+doAssert $(toOrderedSet(["1", "2", "3"])) == """{"1", "2", "3"}"""
+doAssert $(toOrderedSet(['1', '2', '3'])) == """{'1', '2', '3'}"""
+
+# Tests for tables
+doAssert $({1: "1", 2: "2"}.toTable) == """{1: "1", 2: "2"}"""
+doAssert $({"1": 1, "2": 2}.toTable) == """{"1": 1, "2": 2}"""
+
+# Tests for deques
+block:
+  var d = initDeque[int]()
+  d.addLast(1)
+  doAssert $d == "[1]"
+block:
+  var d = initDeque[string]()
+  d.addLast("1")
+  doAssert $d == """["1"]"""
+block:
+  var d = initDeque[char]()
+  d.addLast('1')
+  doAssert $d == "['1']"
+
+# Tests for lists
+block:
+  var l = initDoublyLinkedList[int]()
+  l.append(1)
+  l.append(2)
+  l.append(3)
+  doAssert $l == "[1, 2, 3]"
+block:
+  var l = initDoublyLinkedList[string]()
+  l.append("1")
+  l.append("2")
+  l.append("3")
+  doAssert $l == """["1", "2", "3"]"""
+block:
+  var l = initDoublyLinkedList[char]()
+  l.append('1')
+  l.append('2')
+  l.append('3')
+  doAssert $l == """['1', '2', '3']"""
+
+# Tests for critbits
+block:
+  var t: CritBitTree[int]
+  t["a"] = 1
+  doAssert $t == "{a: 1}"
+block:
+  var t: CritBitTree[string]
+  t["a"] = "1"
+  doAssert $t == """{a: "1"}"""
+block:
+  var t: CritBitTree[char]
+  t["a"] = '1'
+  doAssert $t == "{a: '1'}"
+
+
+# Test escaping behavior
+block:
+  var s = ""
+  s.addQuoted('\0')
+  s.addQuoted('\31')
+  s.addQuoted('\127')
+  s.addQuoted('\255')
+  doAssert s == "'\\x00''\\x1F''\\x7F''\\xFF'"
+block:
+  var s = ""
+  s.addQuoted('\\')
+  s.addQuoted('\'')
+  s.addQuoted('\"')
+  doAssert s == """'\\''\'''\"'"""
+
+# Test customized element representation
+type CustomString = object
+
+proc addQuoted(s: var string, x: CustomString) =
+  s.add("<CustomString>")
+
+block:
+  let s = @[CustomString()]
+  doAssert $s == "@[<CustomString>]"
+
diff --git a/tests/collections/ttables.nim b/tests/collections/ttables.nim
index 01dab44fc..2b8af5bd9 100644
--- a/tests/collections/ttables.nim
+++ b/tests/collections/ttables.nim
@@ -48,7 +48,7 @@ block tableTest1:
     for y in 0..1:
       assert t[(x,y)] == $x & $y
   assert($t ==
-    "{(x: 0, y: 1): 01, (x: 0, y: 0): 00, (x: 1, y: 0): 10, (x: 1, y: 1): 11}")
+    "{(x: 0, y: 1): \"01\", (x: 0, y: 0): \"00\", (x: 1, y: 0): \"10\", (x: 1, y: 1): \"11\"}")
 
 block tableTest2:
   var t = initTable[string, float]()
diff --git a/tests/collections/ttablesref.nim b/tests/collections/ttablesref.nim
index 12af1ccbb..a4030e0dc 100644
--- a/tests/collections/ttablesref.nim
+++ b/tests/collections/ttablesref.nim
@@ -47,7 +47,7 @@ block tableTest1:
     for y in 0..1:
       assert t[(x,y)] == $x & $y
   assert($t ==
-    "{(x: 0, y: 1): 01, (x: 0, y: 0): 00, (x: 1, y: 0): 10, (x: 1, y: 1): 11}")
+    "{(x: 0, y: 1): \"01\", (x: 0, y: 0): \"00\", (x: 1, y: 0): \"10\", (x: 1, y: 1): \"11\"}")
 
 block tableTest2:
   var t = newTable[string, float]()
@@ -139,7 +139,7 @@ proc orderedTableSortTest() =
 block anonZipTest:
   let keys = @['a','b','c']
   let values = @[1, 2, 3]
-  doAssert "{a: 1, b: 2, c: 3}" == $ toTable zip(keys, values)
+  doAssert "{'a': 1, 'b': 2, 'c': 3}" == $ toTable zip(keys, values)
 
 block clearTableTest:
   var t = newTable[string, float]()