summary refs log tree commit diff stats
path: root/tests/async/tasync_misc.nim
blob: 298fa64a96ad2287fb7c813aecf1e5a486c9de07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
discard """
  exitcode: 0
  output: "ok"
"""

import json, asyncdispatch
block: #6100
  let done = newFuture[int]()
  done.complete(1)

  proc asyncSum: Future[int] {.async.} =
    for _ in 1..1_000_000:
      result += await done

  let res = waitFor asyncSum()
  doAssert(res == 1_000_000)

block: #7985
  proc getData(): Future[JsonNode] {.async.} =
    result = %*{"value": 1}

  type
    MyData = object
      value: BiggestInt

  proc main() {.async.} =
    let data = to(await(getData()), MyData)
    doAssert($data == "(value: 1)")

  waitFor(main())

block: #8399
  proc bar(): Future[string] {.async.} = discard

  proc foo(line: string) {.async.} =
    var res =
      case line[0]
      of '+', '-': @[]
      of '$': (let x = await bar(); @[""])
      else: @[]

    doAssert(res == @[""])

  waitFor foo("$asd")

block: # nkCheckedFieldExpr
  proc bar(): Future[JsonNode] {.async.} =
    return newJInt(5)

  proc foo() {.async.} =
    let n = 10 + (await bar()).num
    doAssert(n == 15)

  waitFor foo()

echo "ok"