diff options
Diffstat (limited to 'tests/macros')
-rw-r--r-- | tests/macros/tbugs.nim | 18 | ||||
-rw-r--r-- | tests/macros/tclosuremacro.nim | 2 | ||||
-rw-r--r-- | tests/macros/tdebugstmt.nim | 2 | ||||
-rw-r--r-- | tests/macros/tgensym.nim | 4 | ||||
-rw-r--r-- | tests/macros/tgetimpl.nim | 20 | ||||
-rw-r--r-- | tests/macros/tgettype.nim | 20 | ||||
-rw-r--r-- | tests/macros/tlexerex.nim | 16 | ||||
-rw-r--r-- | tests/macros/tmacro2.nim | 4 | ||||
-rw-r--r-- | tests/macros/tmacrogenerics.nim | 2 | ||||
-rw-r--r-- | tests/macros/tmacrostmt.nim | 2 | ||||
-rw-r--r-- | tests/macros/tnodecompare.nim | 39 | ||||
-rw-r--r-- | tests/macros/tprintf.nim | 20 | ||||
-rw-r--r-- | tests/macros/tquotewords.nim | 4 | ||||
-rw-r--r-- | tests/macros/treturnsempty.nim | 12 | ||||
-rw-r--r-- | tests/macros/tsametype.nim | 41 | ||||
-rw-r--r-- | tests/macros/tstaticparamsmacro.nim | 74 | ||||
-rw-r--r-- | tests/macros/ttryparseexpr.nim | 1 | ||||
-rw-r--r-- | tests/macros/tvtable.nim | 2 | ||||
-rw-r--r-- | tests/macros/typesapi2.nim | 4 |
19 files changed, 263 insertions, 24 deletions
diff --git a/tests/macros/tbugs.nim b/tests/macros/tbugs.nim index 1ecb0d4cc..1bfce6bc4 100644 --- a/tests/macros/tbugs.nim +++ b/tests/macros/tbugs.nim @@ -9,7 +9,10 @@ TTaa TTaa true true -nil''' +nil +42 +false +true''' output: '''test 2''' @@ -88,3 +91,16 @@ proc calc(): array[1, int] = const c = calc() echo c[0] + + +# bug #3046 + +macro sampleMacroInt(i: int): stmt = + echo i.intVal + +macro sampleMacroBool(b: bool): stmt = + echo b.boolVal + +sampleMacroInt(42) +sampleMacroBool(false) +sampleMacroBool(system.true) diff --git a/tests/macros/tclosuremacro.nim b/tests/macros/tclosuremacro.nim index d5d9b656c..cf51949ed 100644 --- a/tests/macros/tclosuremacro.nim +++ b/tests/macros/tclosuremacro.nim @@ -29,7 +29,7 @@ proc doWithOneAndTwo(f: (int, int) -> int): int = echo twoParams(proc (a, b): auto = a + b) echo twoParams((x, y) => x + y) -echo oneParam(x => x+5) +echo oneParam(x => x+5) echo noParams(() => 3) diff --git a/tests/macros/tdebugstmt.nim b/tests/macros/tdebugstmt.nim index 00c55ccd8..421f8fd14 100644 --- a/tests/macros/tdebugstmt.nim +++ b/tests/macros/tdebugstmt.nim @@ -18,7 +18,7 @@ macro debug(n: varargs[expr]): stmt = # add a call to the statement list that writes ": " add(result, newCall("write", newIdentNode("stdout"), newStrLitNode(": "))) # add a call to the statement list that writes the expressions value: - add(result, newCall("writeln", newIdentNode("stdout"), n[i])) + add(result, newCall("writeLine", newIdentNode("stdout"), n[i])) var a: array [0..10, int] diff --git a/tests/macros/tgensym.nim b/tests/macros/tgensym.nim index b3aef0a2c..a4d1a3606 100644 --- a/tests/macros/tgensym.nim +++ b/tests/macros/tgensym.nim @@ -1,6 +1,6 @@ -import rawsockets, asyncdispatch, macros +import nativesockets, asyncdispatch, macros var p = newDispatcher() -var sock = newAsyncRawSocket() +var sock = newAsyncNativeSocket() proc convertReturns(node, retFutureSym: NimNode): NimNode {.compileTime.} = case node.kind diff --git a/tests/macros/tgetimpl.nim b/tests/macros/tgetimpl.nim new file mode 100644 index 000000000..d38492934 --- /dev/null +++ b/tests/macros/tgetimpl.nim @@ -0,0 +1,20 @@ +discard """ + msg: '''"muhaha" +proc poo(x, y: int) = + echo ["poo"]''' +""" + +import macros + +const + foo = "muhaha" + +proc poo(x, y: int) = + echo "poo" + +macro m(x: typed): untyped = + echo repr x.symbol.getImpl + result = x + +discard m foo +discard m poo diff --git a/tests/macros/tgettype.nim b/tests/macros/tgettype.nim new file mode 100644 index 000000000..0eab6c0a0 --- /dev/null +++ b/tests/macros/tgettype.nim @@ -0,0 +1,20 @@ +discard """ +msg: '''ObjectTy(Sym(Model), RecList(Sym(name), Sym(password))) +BracketExpr(Sym(typeDesc), Sym(User))''' +""" +import strutils, macros + +type + Model = object of RootObj + User = object of Model + name : string + password : string + +macro testUser: expr = + return newLit(User.getType.lispRepr) + +macro testGeneric(T: typedesc[Model]): expr = + return newLit(T.getType.lispRepr) + +echo testUser +echo User.testGeneric diff --git a/tests/macros/tlexerex.nim b/tests/macros/tlexerex.nim new file mode 100644 index 000000000..d348a4bcc --- /dev/null +++ b/tests/macros/tlexerex.nim @@ -0,0 +1,16 @@ + +import macros + +macro match*(s: cstring|string; pos: int; sections: untyped): untyped = + for sec in sections.children: + expectKind sec, nnkOfBranch + expectLen sec, 2 + result = newStmtList() + +when isMainModule: + var input = "the input" + var pos = 0 + match input, pos: + of r"[a-zA-Z_]\w+": echo "an identifier" + of r"\d+": echo "an integer" + of r".": echo "something else" diff --git a/tests/macros/tmacro2.nim b/tests/macros/tmacro2.nim index 8515322d5..17f312925 100644 --- a/tests/macros/tmacro2.nim +++ b/tests/macros/tmacro2.nim @@ -4,7 +4,7 @@ discard """ import macros, strutils -proc testBlock(): string {.compileTime.} = +proc testBlock(): string {.compileTime.} = block myBlock: while true: echo "inner block" @@ -21,7 +21,7 @@ macro mac(n: expr): expr = s = s.replace("l", "!!") result = newStrLitNode("Your value sir: '$#'" % [s]) -const s = testBlock() +const s = testBlock() const t = mac("HEllo World") echo s, " ", t diff --git a/tests/macros/tmacrogenerics.nim b/tests/macros/tmacrogenerics.nim index b886f4fa5..919a15b46 100644 --- a/tests/macros/tmacrogenerics.nim +++ b/tests/macros/tmacrogenerics.nim @@ -16,7 +16,7 @@ macro makeBar(A, B: typedesc): typedesc = echo "instantiation ", counter, " with ", A.name, " and ", B.name result = A -type +type Bar[T, U] = makeBar(T, U) var bb1: Bar[int, float] diff --git a/tests/macros/tmacrostmt.nim b/tests/macros/tmacrostmt.nim index d9c70197d..23e2f358c 100644 --- a/tests/macros/tmacrostmt.nim +++ b/tests/macros/tmacrostmt.nim @@ -13,7 +13,7 @@ of r"[\+\-\*\?]+": return tkOperator else: return tkUnknown - + case_token: inc i #bug #488 diff --git a/tests/macros/tnodecompare.nim b/tests/macros/tnodecompare.nim new file mode 100644 index 000000000..ef25ae370 --- /dev/null +++ b/tests/macros/tnodecompare.nim @@ -0,0 +1,39 @@ +discard """ +output: '''1 +0 +1 +0 +1 +0 +1 +0''' +""" + +import macros + +macro test(a: typed, b: typed): expr = + newLit(a == b) + +echo test(1, 1) +echo test(1, 2) + +type + Obj = object of RootObj + Other = object of RootObj + +echo test(Obj, Obj) +echo test(Obj, Other) + +var a, b: int + +echo test(a, a) +echo test(a, b) + +macro test2: expr = + newLit(bindSym"Obj" == bindSym"Obj") + +macro test3: expr = + newLit(bindSym"Obj" == bindSym"Other") + +echo test2() +echo test3() diff --git a/tests/macros/tprintf.nim b/tests/macros/tprintf.nim index c8fb51cdc..94cbfbc2b 100644 --- a/tests/macros/tprintf.nim +++ b/tests/macros/tprintf.nim @@ -2,15 +2,15 @@ discard """ file: "tprintf.nim" output: "Andreas Rumpf" """ -# Test a printf proc - -proc printf(file: TFile, args: openarray[string]) = - var i = 0 - while i < args.len: - write(file, args[i]) - inc(i) - -printf(stdout, ["Andreas ", "Rumpf\n"]) -#OUT Andreas Rumpf +# Test a printf proc + +proc printf(file: TFile, args: openarray[string]) = + var i = 0 + while i < args.len: + write(file, args[i]) + inc(i) + +printf(stdout, ["Andreas ", "Rumpf\n"]) +#OUT Andreas Rumpf diff --git a/tests/macros/tquotewords.nim b/tests/macros/tquotewords.nim index 76b8d8af7..7a575f541 100644 --- a/tests/macros/tquotewords.nim +++ b/tests/macros/tquotewords.nim @@ -6,13 +6,13 @@ discard """ import macros -macro quoteWords(n: expr): expr {.immediate.} = +macro quoteWords(n: expr): expr {.immediate.} = let n = callsite() result = newNimNode(nnkBracket, n) for i in 1..n.len-1: expectKind(n[i], nnkIdent) result.add(toStrLit(n[i])) - + const myWordList = quoteWords(this, an, example) diff --git a/tests/macros/treturnsempty.nim b/tests/macros/treturnsempty.nim new file mode 100644 index 000000000..7af26a747 --- /dev/null +++ b/tests/macros/treturnsempty.nim @@ -0,0 +1,12 @@ +discard """ + errormsg: "type mismatch" + line: 11 +""" +# bug #2372 +macro foo(dummy: int): stmt = + discard + +proc takeStr(s: string) = echo s + +takeStr foo(12) + diff --git a/tests/macros/tsametype.nim b/tests/macros/tsametype.nim new file mode 100644 index 000000000..6baa34751 --- /dev/null +++ b/tests/macros/tsametype.nim @@ -0,0 +1,41 @@ +discard """ +output: '''1 +0 +1 +0 +1 +0 +1 +0 +1 +0''' +""" + +import macros + +macro same(a: typedesc, b: typedesc): expr = + newLit(a.getType[1].sameType b.getType[1]) + +echo same(int, int) +echo same(int, float) + +type + SomeInt = int + DistinctInt = distinct int + SomeFloat = float + DistinctFloat = distinct float + +echo same(int, SomeInt) +echo same(int, DistinctInt) +echo same(float, SomeFloat) +echo same(float, DistinctFloat) + +type + Obj = object of RootObj + SubObj = object of Obj + Other = object of RootObj + +echo same(Obj, Obj) +echo same(int, Obj) +echo same(SubObj, SubObj) +echo same(Other, Obj) diff --git a/tests/macros/tstaticparamsmacro.nim b/tests/macros/tstaticparamsmacro.nim new file mode 100644 index 000000000..f6ecb3a9f --- /dev/null +++ b/tests/macros/tstaticparamsmacro.nim @@ -0,0 +1,74 @@ +discard """ + msg: '''letters +aa +bb +numbers +11 +22 +AST a +[(11, 22), (33, 44)] +AST b +(e: [55, 66], f: [77, 88]) +55 +10 +20Test +20 +''' +""" + +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) + +type + Foo[N: static[int], Z: static[string]] = object + +macro staticIntMacro(f: static[int]): stmt = echo f +staticIntMacro 10 + +var + x: Foo[20, "Test"] + +macro genericMacro[N; Z: static[string]](f: Foo[N, Z], ll = 3, zz = 12): stmt = + echo N, Z + +genericMacro x + +template genericTemplate[N, Z](f: Foo[N, Z], ll = 3, zz = 12): int = N + +static: + echo genericTemplate(x) + diff --git a/tests/macros/ttryparseexpr.nim b/tests/macros/ttryparseexpr.nim index af932eb7d..c7bbc8e5b 100644 --- a/tests/macros/ttryparseexpr.nim +++ b/tests/macros/ttryparseexpr.nim @@ -15,5 +15,6 @@ const valid = 45 a = test("foo&&") b = test("valid") + c = test("\"") # bug #2504 echo a, " ", b diff --git a/tests/macros/tvtable.nim b/tests/macros/tvtable.nim index 3e3b9c0e6..cc5d7a5d9 100644 --- a/tests/macros/tvtable.nim +++ b/tests/macros/tvtable.nim @@ -17,7 +17,7 @@ type # an untyped table to store the proc pointers # it's also possible to use a strongly typed tuple here VTable = array[0..1, pointer] - + TBase = object {.inheritable.} vtbl: ptr VTable diff --git a/tests/macros/typesapi2.nim b/tests/macros/typesapi2.nim index 016295ba4..2e59d2154 100644 --- a/tests/macros/typesapi2.nim +++ b/tests/macros/typesapi2.nim @@ -1,4 +1,4 @@ -# tests to see if a symbol returned from macros.getType() can +# tests to see if a symbol returned from macros.getType() can # be used as a type import macros @@ -20,7 +20,7 @@ static: assert iii is TestFN proc foo11 : testTypesym(void) = echo "HI!" -static: assert foo11 is proc():void +static: assert foo11 is (proc():void {.nimcall.}) var sss: testTypesym(seq[int]) static: assert sss is seq[int] |