summary refs log tree commit diff stats
path: root/tests/stdlib
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@gmail.com>2017-04-08 22:06:57 +0200
committerDominik Picheta <dominikpicheta@gmail.com>2017-04-08 22:06:57 +0200
commit658467a31f34110006fde3bd0ef949dd819a5601 (patch)
tree02caeb44b89dffab2d0b379dd9ed4ed4388ab7b0 /tests/stdlib
parentcc223ff7d89a1f56d4cc0ff5b266a4e7d5d15a4d (diff)
downloadNim-658467a31f34110006fde3bd0ef949dd819a5601.tar.gz
Improve error messages and add tests for the JSON macro.
Diffstat (limited to 'tests/stdlib')
-rw-r--r--tests/stdlib/tjsonmacro.nim100
1 files changed, 75 insertions, 25 deletions
diff --git a/tests/stdlib/tjsonmacro.nim b/tests/stdlib/tjsonmacro.nim
index 806cbadc6..f0f0e6b56 100644
--- a/tests/stdlib/tjsonmacro.nim
+++ b/tests/stdlib/tjsonmacro.nim
@@ -2,33 +2,33 @@ discard """
   file: "tjsonmacro.nim"
   output: ""
 """
-import json, macros, strutils
-
-type
-  Point[T] = object
-    x, y: T
-
-  ReplayEventKind* = enum
-    FoodAppeared, FoodEaten, DirectionChanged
-
-  ReplayEvent* = object
-    time*: float
-    case kind*: ReplayEventKind
-    of FoodAppeared, FoodEaten:
-      foodPos*: Point[float]
-    of DirectionChanged:
-      playerPos*: float
-
-  Replay* = ref object
-    events*: seq[ReplayEvent]
-    test: int
-    test2: string
-    test3: bool
-    testNil: string
+import json, strutils
 
 when isMainModule:
   # Tests inspired by own use case (with some additional tests).
   # This should succeed.
+  type
+    Point[T] = object
+      x, y: T
+
+    ReplayEventKind* = enum
+      FoodAppeared, FoodEaten, DirectionChanged
+
+    ReplayEvent* = object
+      time*: float
+      case kind*: ReplayEventKind
+      of FoodAppeared, FoodEaten:
+        foodPos*: Point[float]
+      of DirectionChanged:
+        playerPos*: float
+
+    Replay* = ref object
+      events*: seq[ReplayEvent]
+      test: int
+      test2: string
+      test3: bool
+      testNil: string
+
   var x = Replay(
     events: @[
       ReplayEvent(
@@ -53,7 +53,57 @@ when isMainModule:
   doAssert y.test == 18827361
   doAssert y.test2 == "hello world"
   doAssert y.test3
-  doAssert y.testNil == nil
+  doAssert y.testNil.isNil
+
+  # TODO: Test for custom object variants (without an enum).
+  # TODO: Test for object variant with an else branch.
 
   # Tests that verify the error messages for invalid data.
-  # TODO:
\ No newline at end of file
+  block:
+    type
+      Person = object
+        name: string
+        age: int
+
+    var node = %{
+      "name": %"Dominik"
+    }
+
+    try:
+      discard to(node, Person)
+      doAssert false
+    except KeyError as exc:
+      doAssert("age" in exc.msg)
+    except:
+      doAssert false
+
+    node["age"] = %false
+
+    try:
+      discard to(node, Person)
+      doAssert false
+    except JsonKindError as exc:
+      doAssert("age" in exc.msg)
+    except:
+      doAssert false
+
+    type
+      PersonAge = enum
+        Fifteen, Sixteen
+
+      PersonCase = object
+        name: string
+        case age: PersonAge
+        of Fifteen:
+          discard
+        of Sixteen:
+          id: string
+
+    try:
+      discard to(node, PersonCase)
+      doAssert false
+    except JsonKindError as exc:
+      doAssert("age" in exc.msg)
+    except:
+      doAssert false
+