testobjs.nim 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. discard """
  2. output: '''{"columns":[{"t":null},{"t":null}]}
  3. {"columns":[{"t":null},{"t":null}]}
  4. '''
  5. """
  6. ## Tests javascript object generation
  7. type
  8. Kg = distinct float
  9. Price = int
  10. Item = object of RootObj
  11. weight: Kg
  12. price: Price
  13. desc: cstring
  14. Person = object of RootObj
  15. name: cstring
  16. age: int
  17. item: Item
  18. Test = object
  19. name: cstring
  20. Recurse[T] = object
  21. data: T
  22. next: ref Recurse[T]
  23. var
  24. test = Test(name: "Jorden")
  25. sword = Item(desc: "pointy", weight: Kg(10.0),
  26. price: Price(50))
  27. knight = Person(name: "robert", age: 19, item: sword)
  28. recurse4 = (ref Recurse[int])(data: 4, next: nil)
  29. recurse3 = (ref Recurse[int])(data: 3, next: recurse4)
  30. recurse2 = (ref Recurse[int])(data: 2, next: recurse3)
  31. recurse1 = Recurse[int](data: 1, next: recurse2)
  32. doAssert test.name == cstring"Jorden"
  33. doAssert knight.age == 19
  34. doAssert knight.item.price == 50
  35. doAssert recurse1.next.next.data == 3
  36. # bug #6035
  37. proc toJson*[T](data: T): cstring {.importc: "JSON.stringify".}
  38. type
  39. Column = object
  40. t: ref Column
  41. Test2 = object
  42. columns: seq[Column]
  43. var test1 = Test2(columns: @[Column(t: nil), Column(t: nil)])
  44. let test2 = test1
  45. echo toJSON(test1)
  46. echo toJSON(test2)
  47. block issue10005:
  48. type
  49. Player = ref object of RootObj
  50. id*: string
  51. nickname*: string
  52. color*: string
  53. proc newPlayer(nickname: string, color: string): Player =
  54. let pl = Player(color: "#123", nickname: nickname)
  55. return Player(
  56. id: "foo",
  57. nickname: nickname,
  58. color: color,
  59. )
  60. doAssert newPlayer("foo", "#1232").nickname == "foo"