diff options
Diffstat (limited to 'tests/macros')
-rw-r--r-- | tests/macros/tbindsym.nim | 25 | ||||
-rw-r--r-- | tests/macros/tbugs.nim | 90 | ||||
-rw-r--r-- | tests/macros/tdumptree.nim | 3 | ||||
-rw-r--r-- | tests/macros/tgentemplates.nim | 35 |
4 files changed, 151 insertions, 2 deletions
diff --git a/tests/macros/tbindsym.nim b/tests/macros/tbindsym.nim new file mode 100644 index 000000000..e1e3b5112 --- /dev/null +++ b/tests/macros/tbindsym.nim @@ -0,0 +1,25 @@ +discard """ + output: '''TFoo +TBar''' +""" + +# bug #1319 + +import macros + +type + TTextKind = enum + TFoo, TBar + +macro test: stmt = + var x = @[TFoo, TBar] + result = newStmtList() + for i in x: + result.add newCall(newIdentNode("echo"), + case i + of TFoo: + bindSym("TFoo") + of TBar: + bindSym("TBar")) + +test() diff --git a/tests/macros/tbugs.nim b/tests/macros/tbugs.nim new file mode 100644 index 000000000..3db851dd1 --- /dev/null +++ b/tests/macros/tbugs.nim @@ -0,0 +1,90 @@ +discard """ +msg: '''a +s +d +f +TTaa +TTaa +TTaa +TTaa +true +true +nil''' + +output: '''test +2''' +""" + +type + Foo = object + s: char + +iterator test2(f: string): Foo = + for i in f: + yield Foo(s: i) + +macro test(): stmt = + for i in test2("asdf"): + echo i.s + +test() + + +# bug 1297 + +import macros + +type TType = tuple[s: string] + +macro echotest(): stmt = + var t: TType + t.s = "" + t.s.add("test") + result = newCall(newIdentNode("echo"), newStrLitNode(t.s)) + +echotest() + +# bug #1103 + +type + Td = tuple + a:string + b:int + +proc get_data(d: Td) : string {.compileTime.} = + result = d.a # Works if a literal string is used here. + # Bugs if line A or B is active. Works with C + result &= "aa" # A + #result.add("aa") # B + #result = result & "aa" # C + +macro m(s:static[Td]) : stmt = + echo get_data(s) + echo get_data(s) + result = newEmptyNode() + +const s=("TT", 3) +m(s) +m(s) + +# bug #933 + +proc nilcheck(): PNimrodNode {.compileTime.} = + echo(result == nil) # true + echo(result.isNil) # true + echo(repr(result)) # nil + +macro testnilcheck(): stmt = + result = newNimNode(nnkStmtList) + discard nilcheck() + +testnilcheck() + +# bug #1323 + +proc calc(): array[1, int] = + result[0].inc() + result[0].inc() + +const c = calc() +echo c[0] diff --git a/tests/macros/tdumptree.nim b/tests/macros/tdumptree.nim index 5299a94e3..e5160b7ba 100644 --- a/tests/macros/tdumptree.nim +++ b/tests/macros/tdumptree.nim @@ -1,6 +1,5 @@ discard """ -disabled: true -output: '''StmtList +msg: '''StmtList VarSection IdentDefs Ident !"x" diff --git a/tests/macros/tgentemplates.nim b/tests/macros/tgentemplates.nim new file mode 100644 index 000000000..a7727c597 --- /dev/null +++ b/tests/macros/tgentemplates.nim @@ -0,0 +1,35 @@ +# bug #1140 + +import parseutils, macros + +proc parse_until_symbol(node: PNimrodNode, value: string, index: var int): bool {.compiletime.} = + var splitValue: string + var read = value.parseUntil(splitValue, '$', index) + + # when false: + if false: + var identifier: string + read = value.parseWhile(identifier, {}, index) + node.add newCall("add", ident("result"), newCall("$", ident(identifier))) + + if splitValue.len > 0: + node.insert node.len, newCall("add", ident("result"), newStrLitNode(splitValue)) + +proc parse_template(node: PNimrodNode, value: string) {.compiletime.} = + var index = 0 + while index < value.len and + parse_until_symbol(node, value, index): discard + +macro tmpli*(body: expr): stmt = + result = newStmtList() + result.add parseExpr("result = \"\"") + result.parse_template body[1].strVal + + +proc actual: string = tmpli html""" + <p>Test!</p> + """ + +proc another: string = tmpli html""" + <p>what</p> + """ |