various3.nim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. discard """
  2. matrix: "--mm:refc"
  3. output: '''
  4. Future is no longer empty, 42
  5. '''
  6. """
  7. import threadpool
  8. proc foo: string = "Dog"
  9. var x: FlowVar[string] = spawn foo()
  10. doAssert(^x == "Dog")
  11. block:
  12. type
  13. Box = object
  14. case empty: bool
  15. of false:
  16. contents: string
  17. else:
  18. discard
  19. var obj = Box(empty: false, contents: "Hello")
  20. doAssert obj.contents == "Hello"
  21. var obj2 = Box(empty: true)
  22. doAssertRaises(FieldDefect):
  23. echo(obj2.contents)
  24. import json
  25. doAssert parseJson("null").kind == JNull
  26. doAssert parseJson("true").kind == JBool
  27. doAssert parseJson("42").kind == JInt
  28. doAssert parseJson("3.14").kind == JFloat
  29. doAssert parseJson("\"Hi\"").kind == JString
  30. doAssert parseJson("""{ "key": "value" }""").kind == JObject
  31. doAssert parseJson("[1, 2, 3, 4]").kind == JArray
  32. import json
  33. let data = """
  34. {"username": "Dominik"}
  35. """
  36. let obj = parseJson(data)
  37. doAssert obj.kind == JObject
  38. doAssert obj["username"].kind == JString
  39. doAssert obj["username"].str == "Dominik"
  40. block:
  41. proc count10(): int =
  42. for i in 0 ..< 10:
  43. result.inc
  44. doAssert count10() == 10
  45. type
  46. Point = tuple[x, y: int]
  47. var point = (5, 10)
  48. var point2 = (x: 5, y: 10)
  49. type
  50. Human = object
  51. name: string
  52. age: int
  53. var jeff = Human(name: "Jeff", age: 23)
  54. var amy = Human(name: "Amy", age: 20)
  55. import asyncdispatch
  56. var future = newFuture[int]()
  57. doAssert(not future.finished)
  58. future.callback =
  59. proc (future: Future[int]) =
  60. echo("Future is no longer empty, ", future.read)
  61. future.complete(42)
  62. import asyncdispatch, asyncfile
  63. when false:
  64. var file = openAsync("")
  65. let dataFut = file.readAll()
  66. dataFut.callback =
  67. proc (future: Future[string]) =
  68. echo(future.read())
  69. asyncdispatch.runForever()
  70. import asyncdispatch, asyncfile, os
  71. proc readFiles() {.async.} =
  72. # --- Changed to getTempDir here.
  73. var file = openAsync(getTempDir() / "test.txt", fmReadWrite)
  74. let data = await file.readAll()
  75. echo(data)
  76. await file.write("Hello!\n")
  77. waitFor readFiles()