diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2020-03-31 21:14:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-31 21:14:05 +0200 |
commit | 9134bb9cfb81297d00ccd6d41fe975e853fca2e5 (patch) | |
tree | 7477f7d63057de8cb8912ea1741243f43a5ca4f4 /tests | |
parent | 40898871a9bde4353edc21423b0e8bfe03f68414 (diff) | |
download | Nim-9134bb9cfb81297d00ccd6d41fe975e853fca2e5.tar.gz |
macros for proc types, macros for types (#13778)
* new minor feature: macros for proc types, to be documented * Finished the implementation and added tests * [skip ci] Describe the new custom pragmas in the manual and the changelog Co-authored-by: Zahary Karadjov <zahary@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/pragmas/tcustom_pragma.nim | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/tests/pragmas/tcustom_pragma.nim b/tests/pragmas/tcustom_pragma.nim index 719af4d50..2d970aba1 100644 --- a/tests/pragmas/tcustom_pragma.nim +++ b/tests/pragmas/tcustom_pragma.nim @@ -1,6 +1,6 @@ {.experimental: "notnil".} -import macros +import macros, asyncmacro, asyncfutures block: template myAttr() {.pragma.} @@ -249,3 +249,89 @@ block: var e {.fooBar("foo", 123, 'u').}: int doAssert(hasCustomPragma(e, fooBar)) doAssert(getCustomPragmaVal(e, fooBar).c == 123) + +block: + macro expectedAst(expectedRepr: static[string], input: untyped): untyped = + assert input.treeRepr & "\n" == expectedRepr + return input + + const procTypeAst = """ +ProcTy + FormalParams + Empty + IdentDefs + Ident "x" + Ident "int" + Empty + Pragma + Ident "async" +""" + + type + Foo = proc (x: int) {.expectedAst(procTypeAst), async.} + + static: assert Foo is proc(x: int): Future[void] + + const asyncProcTypeAst = """ +ProcTy + FormalParams + BracketExpr + Ident "Future" + Ident "void" + IdentDefs + Ident "s" + Ident "string" + Empty + Pragma +""" + + type + Bar = proc (s: string) {.async, expectedAst(asyncProcTypeAst).} + + static: assert Bar is proc(x: string): Future[void] + + const typeAst = """ +TypeDef + PragmaExpr + Ident "Baz" + Pragma + Empty + ObjectTy + Empty + Empty + RecList + IdentDefs + Ident "x" + Ident "string" + Empty +""" + + type + Baz {.expectedAst(typeAst).} = object + x: string + + static: assert Baz.x is string + + const procAst = """ +ProcDef + Ident "bar" + Empty + Empty + FormalParams + Ident "string" + IdentDefs + Ident "s" + Ident "string" + Empty + Empty + Empty + StmtList + ReturnStmt + Ident "s" +""" + + proc bar(s: string): string {.expectedAst(procAst).} = + return s + + static: assert bar("x") == "x" + |