summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-06-15 04:22:43 -0700
committerGitHub <noreply@github.com>2020-06-15 13:22:43 +0200
commitd51beb7b20c7670a17769b30e721fe67761f98e6 (patch)
tree8cf5004199932ce1952515365928d07004378a2d /tests
parentbf604c6829f87a551dc3b5ad836679be4ab53789 (diff)
downloadNim-d51beb7b20c7670a17769b30e721fe67761f98e6.tar.gz
make `fromJson/toJson` work with `array[range, typ]`, + 1 bugfix (#14669)
* make toJson more robust

* properly handle array
Diffstat (limited to 'tests')
-rw-r--r--tests/stdlib/tjsonutils.nim26
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/stdlib/tjsonutils.nim b/tests/stdlib/tjsonutils.nim
index 01c6aa05a..fca980dc9 100644
--- a/tests/stdlib/tjsonutils.nim
+++ b/tests/stdlib/tjsonutils.nim
@@ -13,12 +13,26 @@ proc testRoundtrip[T](t: T, expected: string) =
 import tables
 import strtabs
 
+type Foo = ref object
+  id: int
+
+proc `==`(a, b: Foo): bool =
+  a.id == b.id
+
 template fn() = 
   block: # toJson, jsonTo
     type Foo = distinct float
     testRoundtrip('x', """120""")
     when not defined(js):
       testRoundtrip(cast[pointer](12345)): """12345"""
+      when nimvm:
+        discard
+        # bugs:
+        # Error: unhandled exception: 'intVal' is not accessible using discriminant 'kind' of type 'TNode' [
+        # Error: VM does not support 'cast' from tyNil to tyPointer
+      else:
+        testRoundtrip(pointer(nil)): """0"""
+        testRoundtrip(cast[pointer](nil)): """0"""
 
     # causes workaround in `fromJson` potentially related to
     # https://github.com/nim-lang/Nim/issues/12282
@@ -40,5 +54,17 @@ template fn() =
     testRoundtrip(a):
       """[1.1,"fo",120,[10,11],[true,false],[{"mode":"modeCaseSensitive","table":{"y":"Y","z":"Z"}},{"mode":"modeCaseSensitive","table":{}}],[0,3],-4,{"foo":0.5,"bar":{"a1":"abc"},"bar2":null}]"""
 
+  block:
+    # edge case when user defined `==` doesn't handle `nil` well, eg:
+    # https://github.com/nim-lang/nimble/blob/63695f490728e3935692c29f3d71944d83bb1e83/src/nimblepkg/version.nim#L105
+    testRoundtrip(@[Foo(id: 10), nil]): """[{"id":10},null]"""
+
+  block:
+    type Foo = enum f1, f2, f3, f4, f5
+    type Bar = enum b1, b2, b3, b4
+    let a = [f2: b2, f3: b3, f4: b4]
+    doAssert b2.ord == 1 # explains the `1`
+    testRoundtrip(a): """[1,2,3]"""
+
 static: fn()
 fn()