summary refs log tree commit diff stats
path: root/tests/js/testobjs.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/js/testobjs.nim')
-rw-r--r--tests/js/testobjs.nim73
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/js/testobjs.nim b/tests/js/testobjs.nim
new file mode 100644
index 000000000..b61d06471
--- /dev/null
+++ b/tests/js/testobjs.nim
@@ -0,0 +1,73 @@
+discard """
+  output: '''{"columns":[{"t":null},{"t":null}]}
+{"columns":[{"t":null},{"t":null}]}
+'''
+"""
+
+## Tests javascript object generation
+
+type
+  Kg = distinct float
+  Price = int
+  Item = object of RootObj
+    weight: Kg
+    price: Price
+    desc: cstring
+  Person = object of RootObj
+    name: cstring
+    age: int
+    item: Item
+  Test = object
+    name: cstring
+  Recurse[T] = object
+    data: T
+    next: ref Recurse[T]
+
+var
+  test = Test(name: "Jorden")
+  sword = Item(desc: "pointy", weight: Kg(10.0),
+                price: Price(50))
+  knight = Person(name: "robert", age: 19, item: sword)
+  recurse4 = (ref Recurse[int])(data: 4, next: nil)
+  recurse3 = (ref Recurse[int])(data: 3, next: recurse4)
+  recurse2 = (ref Recurse[int])(data: 2, next: recurse3)
+  recurse1 = Recurse[int](data: 1, next: recurse2)
+
+
+doAssert test.name == cstring"Jorden"
+doAssert knight.age == 19
+doAssert knight.item.price == 50
+doAssert recurse1.next.next.data == 3
+
+# bug #6035
+proc toJson*[T](data: T): cstring {.importc: "JSON.stringify".}
+
+type
+  Column = object
+    t: ref Column
+
+  Test2 = object
+    columns: seq[Column]
+
+var test1 = Test2(columns: @[Column(t: nil), Column(t: nil)])
+let test2 = test1
+
+echo toJSON(test1)
+echo toJSON(test2)
+
+block issue10005:
+  type
+    Player = ref object of RootObj
+      id*: string
+      nickname*: string
+      color*: string
+
+  proc newPlayer(nickname: string, color: string): Player =
+    let pl = Player(color: "#123", nickname: nickname)
+    return Player(
+        id: "foo",
+        nickname: nickname,
+        color: color,
+    )
+
+  doAssert newPlayer("foo", "#1232").nickname == "foo"