various3.nim 1.9 KB

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