diff options
Diffstat (limited to 'tests')
25 files changed, 410 insertions, 56 deletions
diff --git a/tests/async/tasyncawait.nim b/tests/async/tasyncawait.nim index bde5bf8c8..91dfb7932 100644 --- a/tests/async/tasyncawait.nim +++ b/tests/async/tasyncawait.nim @@ -14,30 +14,31 @@ const var clientCount = 0 -proc sendMessages(disp: PDispatcher, client: TSocketHandle): PFuture[int] {.async.} = +proc sendMessages(disp: PDispatcher, client: TSocketHandle) {.async.} = for i in 0 .. <messagesToSend: - discard await disp.send(client, "Message " & $i & "\c\L") + await disp.send(client, "Message " & $i & "\c\L") -proc launchSwarm(disp: PDispatcher, port: TPort): PFuture[int] {.async.} = +proc launchSwarm(disp: PDispatcher, port: TPort) {.async.} = for i in 0 .. <swarmSize: - var sock = socket() + var sock = disp.socket() + #disp.register(sock) - discard await disp.connect(sock, "localhost", port) + await disp.connect(sock, "localhost", port) when true: - discard await sendMessages(disp, sock) - sock.close() + await sendMessages(disp, sock) + disp.close(sock) else: # Issue #932: https://github.com/Araq/Nimrod/issues/932 var msgFut = sendMessages(disp, sock) msgFut.callback = proc () = - sock.close() + disp.close(sock) -proc readMessages(disp: PDispatcher, client: TSocketHandle): PFuture[int] {.async.} = +proc readMessages(disp: PDispatcher, client: TSocketHandle) {.async.} = while true: var line = await disp.recvLine(client) if line == "": - client.close() + disp.close(client) clientCount.inc break else: @@ -46,16 +47,18 @@ proc readMessages(disp: PDispatcher, client: TSocketHandle): PFuture[int] {.asyn else: doAssert false -proc createServer(disp: PDispatcher, port: TPort): PFuture[int] {.async.} = - var server = socket() +proc createServer(disp: PDispatcher, port: TPort) {.async.} = + var server = disp.socket() #disp.register(server) server.bindAddr(port) server.listen() while true: - discard readMessages(disp, await disp.accept(server)) + var client = await disp.accept(server) + readMessages(disp, client) + # TODO: Test: readMessages(disp, await disp.accept(server)) -discard disp.createServer(TPort(10335)) -discard disp.launchSwarm(TPort(10335)) +disp.createServer(TPort(10335)) +disp.launchSwarm(TPort(10335)) while true: disp.poll() if clientCount == swarmSize: break diff --git a/tests/bind/tbindtypedesc.nim b/tests/bind/tinvalidbindtypedesc.nim index d6fbae537..7d97d2e0d 100644 --- a/tests/bind/tbindtypedesc.nim +++ b/tests/bind/tinvalidbindtypedesc.nim @@ -1,6 +1,5 @@ discard """ line: 11 - file: "tbindtypedesc.nim" errormsg: "type mismatch: got (typedesc[float], string)" """ diff --git a/tests/effects/teffects1.nim b/tests/effects/teffects1.nim index b72e8b00c..0014cff46 100644 --- a/tests/effects/teffects1.nim +++ b/tests/effects/teffects1.nim @@ -1,5 +1,5 @@ discard """ - line: 1855 + line: 1913 file: "system.nim" errormsg: "can raise an unlisted exception: ref EIO" """ diff --git a/tests/generics/tbadgenericlambda.nim b/tests/generics/tbadgenericlambda.nim new file mode 100644 index 000000000..5e406cacc --- /dev/null +++ b/tests/generics/tbadgenericlambda.nim @@ -0,0 +1,7 @@ +discard """ + msg: "nested proc can have generic parameters only when" + line: 6 +""" + +let x = proc (x, y): auto = x + y + diff --git a/tests/generics/tgenericlambda.nim b/tests/generics/tgenericlambda.nim index f7aafe1d9..eb6ada3e5 100644 --- a/tests/generics/tgenericlambda.nim +++ b/tests/generics/tgenericlambda.nim @@ -1,5 +1,5 @@ discard """ - output: "10\n10\n1\n2\n3" + output: "10\n10\n1\n2\n3\n15" """ proc test(x: proc (a, b: int): int) = @@ -16,3 +16,8 @@ proc foreach[T](s: seq[T], body: proc(x: T)) = foreach(@[1,2,3]) do (x): echo x +proc foo = + let x = proc (a, b: int): auto = a + b + echo x(5, 10) + +foo() diff --git a/tests/generics/tgenericshardcases.nim b/tests/generics/tgenericshardcases.nim index 2ef63bc20..e3b805db6 100644 --- a/tests/generics/tgenericshardcases.nim +++ b/tests/generics/tgenericshardcases.nim @@ -14,7 +14,8 @@ macro selectType(a, b: typedesc): typedesc = type Foo[T] = object data1: array[T.high, int] - data2: array[typeNameLen(T), float] # data3: array[0..T.typeNameLen, selectType(float, int)] + data2: array[typeNameLen(T), float] + data3: array[0..T.typeNameLen, selectType(float, int)] MyEnum = enum A, B, C, D @@ -27,10 +28,15 @@ echo high(f1.data2) # (MyEnum.len = 6) - 1 == 5 echo high(f2.data1) # 127 - 1 == 126 echo high(f2.data2) # int8.len - 1 == 3 -#static: -# assert high(f1.data1) == ord(D) -# assert high(f1.data2) == 6 # length of MyEnum +static: + assert high(f1.data1) == ord(C) + assert high(f1.data2) == 5 # length of MyEnum minus one, because we used T.high -# assert high(f2.data1) == 127 -# assert high(f2.data2) == 4 # length of int8 + assert high(f2.data1) == 126 + assert high(f2.data2) == 3 + + assert high(f1.data3) == 6 # length of MyEnum + assert high(f2.data3) == 4 # length of int8 + + assert f2.data3[0] is float diff --git a/tests/generics/tlateboundstatic.nim b/tests/generics/tlateboundstatic.nim new file mode 100644 index 000000000..f68f95f8d --- /dev/null +++ b/tests/generics/tlateboundstatic.nim @@ -0,0 +1,16 @@ +discard """ + msg: "array[0..3, int]" +""" + +type + KK[I: static[int]] = object + x: array[I, int] + +proc foo(a: static[string]): KK[a.len] = + result.x[0] = 12 + +var x = foo "test" + +import typetraits +static: echo x.x.type.name + diff --git a/tests/generics/tsigtypeop.nim b/tests/generics/tsigtypeop.nim new file mode 100644 index 000000000..4c863cba1 --- /dev/null +++ b/tests/generics/tsigtypeop.nim @@ -0,0 +1,9 @@ +type + Vec3[T] = array[3, T] + +proc foo(x: Vec3, y: Vec3.T, z: x.T): x.type.T = + return 10 + +var y: Vec3[int] = [1, 2, 3] +var z: int = foo(y, 3, 4) + diff --git a/tests/iter/tchainediterators.nim b/tests/iter/tchainediterators.nim new file mode 100644 index 000000000..18d096761 --- /dev/null +++ b/tests/iter/tchainediterators.nim @@ -0,0 +1,38 @@ +discard """ + output: '''16 +32 +48 +64 +128 +192 +''' +""" + +iterator gaz(it: iterator{.inline.}): type(it) = + for x in it: + yield x*2 + +iterator baz(it: iterator{.inline.}) = + for x in gaz(it): + yield x*2 + +type T1 = auto + +iterator bar(it: iterator: T1{.inline.}): T1 = + for x in baz(it): + yield x*2 + +iterator foo[T](x: iterator: T{.inline.}): T = + for e in bar(x): + yield e*2 + +var s = @[1, 2, 3] + +# pass an interator several levels deep: +for x in s.items.foo: + echo x + +# use some complex iterator as an input for another one: +for x in s.items.baz.foo: + echo x + diff --git a/tests/iter/titerable.nim b/tests/iter/titerable.nim new file mode 100644 index 000000000..3ec79f68d --- /dev/null +++ b/tests/iter/titerable.nim @@ -0,0 +1,26 @@ +discard """ + output: '''2 +4 +6 +4 +8 +12 +''' +""" + +iterator map[T, U](s: iterator:T{.inline.}, f: proc(x: T): U): U = + for e in s: yield f(e) + +template toSeq(s: expr): expr = + var res = newSeq[type(s)](0) + for e in s: res.add(e) + res + +var s1 = @[1, 2, 3] +for x in map(s1.items, proc (a:int): int = a*2): + echo x + +var s2 = toSeq(map(s1.items, proc (a:int): int = a*4)) +for x in s2: + echo x + diff --git a/tests/metatype/tstaticparams.nim b/tests/metatype/tstaticparams.nim index b1377443b..6d7c569e0 100644 --- a/tests/metatype/tstaticparams.nim +++ b/tests/metatype/tstaticparams.nim @@ -1,6 +1,6 @@ discard """ file: "tstaticparams.nim" - output: "abracadabra\ntest\n3" + output: "abracadabra\ntest\n3\n15\n4\n2" """ type @@ -11,8 +11,11 @@ type data: array[I, T] TA1[T; I: static[int]] = array[I, T] - # TA2[T; I: static[int]] = array[0..I, T] - # TA3[T; I: static[int]] = array[I-1, T] + TA2[T; I: static[int]] = array[0..I, T] + TA3[T; I: static[int]] = array[I-1, T] + + TObj = object + x: TA3[int, 3] proc takeFoo(x: TFoo) = echo "abracadabra" @@ -26,6 +29,30 @@ echo high(y.data) var t1: TA1[float, 1] - # t2: TA2[string, 4] - # t3: TA3[int, 10] + t2: TA2[string, 4] + t3: TA3[int, 10] + t4: TObj + +# example from the manual: +type + Matrix[M,N: static[int]; T] = array[0..(M*N - 1), T] + # Note how `Number` is just a type constraint here, while + # `static[int]` requires us to supply a compile-time int value + + AffineTransform2D[T] = Matrix[3, 3, T] + AffineTransform3D[T] = Matrix[4, 4, T] + +var m: AffineTransform3D[float] +echo high(m) + +proc getRows(mtx: Matrix): int = + result = mtx.M + +echo getRows(m) + +# issue 997 +type TTest[T: static[int], U: static[int]] = array[0..T*U, int] +type TTestSub[N: static[int]] = TTest[1, N] +var z: TTestSub[2] +echo z.high diff --git a/tests/metatype/tusertypeclasses.nim b/tests/metatype/tusertypeclasses.nim index 5b04c490f..a5d575dbf 100644 --- a/tests/metatype/tusertypeclasses.nim +++ b/tests/metatype/tusertypeclasses.nim @@ -26,7 +26,7 @@ foo 10 foo "test" foo(@[TObj(x: 10), TObj(x: 20)]) -proc intval(x: int) = discard +proc intval(x: int): int = 10 # check real and virtual fields type @@ -34,7 +34,8 @@ type T.x y(T) intval T.y - + let z = intval(T.y) + proc y(x: TObj): int = 10 proc testFoo(x: TFoo) = discard diff --git a/tests/metatype/udtcmanual.nim b/tests/metatype/udtcmanual.nim index f22bd6ac6..dd44298dc 100644 --- a/tests/metatype/udtcmanual.nim +++ b/tests/metatype/udtcmanual.nim @@ -25,7 +25,7 @@ type C.len is Ordinal items(c) is iterator for value in C: - value.type is T + type(value) is T proc takesIntContainer(c: Container[int]) = for e in c: echo e diff --git a/tests/overload/tissue966.nim b/tests/overload/tissue966.nim new file mode 100644 index 000000000..2911348cf --- /dev/null +++ b/tests/overload/tissue966.nim @@ -0,0 +1,12 @@ +discard """ + errormsg: "type mismatch: got (PTest)" +""" + +type + PTest = ref object + +proc test(x: PTest, y: int) = nil + +var buf: PTest +buf.test() + diff --git a/tests/parser/tstrongspaces.nim b/tests/parser/tstrongspaces.nim new file mode 100644 index 000000000..91506daf0 --- /dev/null +++ b/tests/parser/tstrongspaces.nim @@ -0,0 +1,52 @@ +#! strongSpaces + +discard """ + output: '''35 +77 +(Field0: 1, Field1: 2, Field2: 2) +ha +true +tester args +all +all args +''' +""" + +echo 2+5 * 5 + +let foo = 77 +echo $foo + +echo (1, 2, 2) + +template `&`(a, b: int): expr = a and b +template `|`(a, b: int): expr = a - b +template `++`(a, b: int): expr = a + b == 8009 + +when true: + let b = 66 + let c = 90 + let bar = 8000 + if foo+4 * 4 == 8 and b&c | 9 ++ + bar: + echo "ho" + else: + echo "ha" + + let booA = foo+4 * 4 - b&c | 9 + + bar + # is parsed as + let booB = ((foo+4)*4) - ((b&c) | 9) + bar + + echo booA == booB + + +template `|`(a, b): expr = (if a.len > 0: a else: b) + +const + tester = "tester" + args = "args" + +echo tester & " " & args|"all" +echo "all" | tester & " " & args +echo "all"|tester & " " & args diff --git a/tests/static/tstaticparammacro.nim b/tests/static/tstaticparammacro.nim new file mode 100644 index 000000000..7fb9e2014 --- /dev/null +++ b/tests/static/tstaticparammacro.nim @@ -0,0 +1,52 @@ +discard """ + msg: '''letters +aa +bb +numbers +11 +22 +AST a +[(11, 22), (33, 44)] +AST b +(e: [55, 66], f: [77, 88]) +55 +''' +""" + +import macros + +type + TConfig = tuple + letters: seq[string] + numbers:seq[int] + +const data: Tconfig = (@["aa", "bb"], @[11, 22]) + +macro mymacro(data: static[TConfig]): stmt = + echo "letters" + for s in items(data.letters): + echo s + echo "numbers" + for n in items(data.numbers): + echo n + +mymacro(data) + +type + Ta = seq[tuple[c:int, d:int]] + Tb = tuple[e:seq[int], f:seq[int]] + +const + a : Ta = @[(11, 22), (33, 44)] + b : Tb = (@[55,66], @[77, 88]) + +macro mA(data: static[Ta]): stmt = + echo "AST a \n", repr(data) + +macro mB(data: static[Tb]): stmt = + echo "AST b \n", repr(data) + echo data.e[0] + +mA(a) +mB(b) + diff --git a/tests/stdlib/tpegs.nim b/tests/stdlib/tpegs.nim index 7775091a1..6e488bab4 100644 --- a/tests/stdlib/tpegs.nim +++ b/tests/stdlib/tpegs.nim @@ -72,7 +72,7 @@ type rule: TNode ## the rule that the symbol refers to TNode {.final, shallow.} = object case kind: TPegKind - of pkEmpty..pkWhitespace: discard + of pkEmpty..pkWhitespace: nil of pkTerminal, pkTerminalIgnoreCase, pkTerminalIgnoreStyle: term: string of pkChar, pkGreedyRepChar: ch: char of pkCharChoice, pkGreedyRepSet: charChoice: ref set[char] diff --git a/tests/system/alloc.nim b/tests/system/alloc.nim index 665b448ac..7abefec2a 100644 --- a/tests/system/alloc.nim +++ b/tests/system/alloc.nim @@ -2,44 +2,51 @@ var x: ptr int x = cast[ptr int](alloc(7)) assert x != nil - -x = alloc(int, 3) +x = cast[ptr int](x.realloc(2)) assert x != nil x.dealloc() -x = alloc0(int, 4) +x = createU(int, 3) +assert x != nil +x.free() + +x = create(int, 4) assert cast[ptr array[4, int]](x)[0] == 0 assert cast[ptr array[4, int]](x)[1] == 0 assert cast[ptr array[4, int]](x)[2] == 0 assert cast[ptr array[4, int]](x)[3] == 0 -x = cast[ptr int](x.realloc(2)) -assert x != nil - -x = x.reallocType(4) +x = x.resize(4) assert x != nil -x.dealloc() +x.free() x = cast[ptr int](allocShared(100)) assert x != nil deallocShared(x) -x = allocShared(int, 3) +x = createSharedU(int, 3) assert x != nil -x.deallocShared() +x.freeShared() -x = allocShared0(int, 3) +x = createShared(int, 3) assert x != nil assert cast[ptr array[3, int]](x)[0] == 0 assert cast[ptr array[3, int]](x)[1] == 0 assert cast[ptr array[3, int]](x)[2] == 0 -x = cast[ptr int](reallocShared(x, 2)) assert x != nil +x = cast[ptr int](x.resizeShared(2)) +assert x != nil +x.freeShared() -x = reallocType(x, 12) +x = create(int, 10) assert x != nil +x = x.resize(12) +assert x != nil +x.dealloc() -x = reallocSharedType(x, 1) +x = createShared(int, 1) +assert x != nil +x = x.resizeShared(1) assert x != nil -x.deallocShared() +x.freeShared() diff --git a/tests/template/tissue909.nim b/tests/template/tissue909.nim new file mode 100644 index 000000000..5b57a3558 --- /dev/null +++ b/tests/template/tissue909.nim @@ -0,0 +1,16 @@ +import macros + +template baz() = + proc bar() = + var x = 5 + iterator foo(): int {.closure.} = + echo x + var y = foo + discard y() + +macro test(): stmt = + result = getAst(baz()) + echo(treeRepr(result)) + +test() +bar() diff --git a/tests/template/tissue993.nim b/tests/template/tissue993.nim new file mode 100644 index 000000000..d39f43942 --- /dev/null +++ b/tests/template/tissue993.nim @@ -0,0 +1,21 @@ + +type pnode* = ref object of tobject + +template litNode (name, ty): stmt = + type name* = ref object of PNode + val*: ty +litNode PIntNode, int + +import json + +template withKey*(j: PJsonNode; key: string; varname: expr; + body:stmt): stmt {.immediate.} = + if j.hasKey(key): + let varname{.inject.}= j[key] + block: + body + +var j = parsejson("{\"zzz\":1}") +withkey(j, "foo", x): + echo(x) + diff --git a/tests/testament/htmlgen.nim b/tests/testament/htmlgen.nim index 74d8811b8..89d56c693 100644 --- a/tests/testament/htmlgen.nim +++ b/tests/testament/htmlgen.nim @@ -174,6 +174,10 @@ proc generateJson*(filename: string, commit: int) = on A.name = B.name and A.category = B.category where A.[commit] = ? and B.[commit] = ? and A.machine = ? and A.result != B.result""" + selResults = """select + name, category, target, action, result, expected, given + from TestResult + where [commit] = ?""" var db = open(connection="testament.db", user="testament", password="", database="testament") let lastCommit = db.getCommit(commit) @@ -189,6 +193,20 @@ proc generateJson*(filename: string, commit: int) = outfile.writeln("""{"total": $#, "passed": $#, "skipped": $#""" % data) + let results = newJArray() + for row in db.rows(sql(selResults), lastCommit): + var obj = newJObject() + obj["name"] = %row[0] + obj["category"] = %row[1] + obj["target"] = %row[2] + obj["action"] = %row[3] + obj["result"] = %row[4] + obj["expected"] = %row[5] + obj["given"] = %row[6] + results.add(obj) + outfile.writeln(""", "results": """) + outfile.write(results.pretty) + if not previousCommit.isNil: let diff = newJArray() diff --git a/tests/testament/tester.nim b/tests/testament/tester.nim index fac97cf2a..923cd7518 100644 --- a/tests/testament/tester.nim +++ b/tests/testament/tester.nim @@ -11,7 +11,7 @@ import parseutils, strutils, pegs, os, osproc, streams, parsecfg, json, - marshal, backend, parseopt, specs, htmlgen, browsers + marshal, backend, parseopt, specs, htmlgen, browsers, terminal const resultsFile = "testresults.html" @@ -109,6 +109,12 @@ proc addResult(r: var TResults, test: TTest, expected = expected, given = given) r.data.addf("$#\t$#\t$#\t$#", name, expected, given, $success) + if success notin {reSuccess, reIgnored}: + styledEcho styleBright, fgRed, "^^^ [", $success, "]" + styledEcho styleDim, "EXPECTED:" + echo expected + styledEcho styleDim, "GIVEN:" + echo given proc cmpMsgs(r: var TResults, expected, given: TSpec, test: TTest) = if strip(expected.msg) notin strip(given.msg): diff --git a/tests/vm/tstaticprintseq.nim b/tests/vm/tstaticprintseq.nim index 99a56d161..4575f3af1 100644 --- a/tests/vm/tstaticprintseq.nim +++ b/tests/vm/tstaticprintseq.nim @@ -4,7 +4,17 @@ discard """ 3 1 2 -3''' +3 +1 +2 +3 +1 +2 +3 +aa +bb +aa +bb''' """ const s = @[1,2,3] @@ -19,3 +29,27 @@ static: for e in s: echo e +macro bar(x: static[seq[int]]): stmt = + for e in x: + echo e + +bar s +bar(@[1, 2, 3]) + +type + TData = tuple + letters: seq[string] + numbers: seq[int] + +const data: TData = (@["aa", "bb"], @[11, 22]) + +static: + var m = data + for x in m.letters: + echo x + +macro ff(d: static[TData]): stmt = + for x in d.letters: + echo x + +ff(data) diff --git a/tests/vm/twrongconst.nim b/tests/vm/twrongconst.nim index e5b8a15bd..5c0c80f9f 100644 --- a/tests/vm/twrongconst.nim +++ b/tests/vm/twrongconst.nim @@ -1,10 +1,9 @@ discard """ - output: "Error: cannot evaluate at compile time: x" - line: 10 + errormsg: "cannot evaluate at compile time: x" + line: 9 """ -var x: array[100, char] +var x: array[100, char] template Foo : expr = x[42] - const myConst = foo diff --git a/tests/vm/twrongwhen.nim b/tests/vm/twrongwhen.nim index 085bb6fb6..d67e42883 100644 --- a/tests/vm/twrongwhen.nim +++ b/tests/vm/twrongwhen.nim @@ -1,9 +1,9 @@ discard """ - output: "Error: cannot evaluate at compile time: x" + errormsg: "cannot evaluate at compile time: x" line: 7 """ -proc bla(x:int) = +proc bla(x:int) = when x == 0: echo "oops" else: |