diff options
author | Araq <rumpf_a@web.de> | 2014-01-13 01:05:09 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2014-01-13 01:05:09 +0100 |
commit | 9ab1a3c7248993769a752526139424017c913ffa (patch) | |
tree | 2e766bb5a46e47e4d6b7aa8954c1fdb53da91f2c /tests/showoff | |
parent | 429d337ef6f812ce976c903d500c7d4f0d616501 (diff) | |
download | Nim-9ab1a3c7248993769a752526139424017c913ffa.tar.gz |
added test cases from strange loop event
Diffstat (limited to 'tests/showoff')
-rw-r--r-- | tests/showoff/tformatopt.nim | 57 | ||||
-rw-r--r-- | tests/showoff/thello2.nim | 11 | ||||
-rw-r--r-- | tests/showoff/thtml1.nim | 11 | ||||
-rw-r--r-- | tests/showoff/thtml2.nim | 37 | ||||
-rw-r--r-- | tests/showoff/tonce.nim | 22 | ||||
-rw-r--r-- | tests/showoff/tquasiquote.nim | 14 |
6 files changed, 152 insertions, 0 deletions
diff --git a/tests/showoff/tformatopt.nim b/tests/showoff/tformatopt.nim new file mode 100644 index 000000000..f33ed6921 --- /dev/null +++ b/tests/showoff/tformatopt.nim @@ -0,0 +1,57 @@ +discard """ + output: '''(a: 3 +b: 4 +s: abc +)''' +""" + +import macros + +proc invalidFormatString() = + echo "invalidFormatString" + +template formatImpl(handleChar: expr) = + var i = 0 + while i < f.len: + if f[i] == '$': + case f[i+1] + of '1'..'9': + var j = 0 + i += 1 + while f[i] in {'0'..'9'}: + j = j * 10 + ord(f[i]) - ord('0') + i += 1 + result.add(a[j-1]) + else: + invalidFormatString() + else: + result.add(handleChar(f[i])) + i += 1 + +proc `%`*(f: string, a: openArray[string]): string = + template identity(x: expr): expr = x + result = "" + formatImpl(identity) + +macro optFormat{`%`(f, a)}(f: string{lit}, a: openArray[string]): expr = + result = newNimNode(nnkBracket) + let f = f.strVal + formatImpl(newLit) + result = nestList(!"&", result) + +template optAdd1{x = y; add(x, z)}(x, y, z: string) = + x = y & z + +proc `/&` [T: object](x: T): string = + result = "(" + for name, value in fieldPairs(x): + result.add("$1: $2\n" % [name, $value]) + result.add(")") + +type + MyObject = object + a, b: int + s: string + +let obj = MyObject(a: 3, b: 4, s: "abc") +echo(/&obj) diff --git a/tests/showoff/thello2.nim b/tests/showoff/thello2.nim new file mode 100644 index 000000000..d2e2f6227 --- /dev/null +++ b/tests/showoff/thello2.nim @@ -0,0 +1,11 @@ +discard """ + output: '''(a: 3, b: 4, s: abc)''' +""" + +type + MyObject = object + a, b: int + s: string + +let obj = MyObject(a: 3, b: 4, s: "abc") +echo obj diff --git a/tests/showoff/thtml1.nim b/tests/showoff/thtml1.nim new file mode 100644 index 000000000..cd95c7971 --- /dev/null +++ b/tests/showoff/thtml1.nim @@ -0,0 +1,11 @@ +discard """ + output: "<br>" +""" + +template htmlTag(tag: expr) {.immediate.} = + proc tag(): string = "<" & astToStr(tag) & ">" + +htmlTag(br) +htmlTag(html) + +echo br() diff --git a/tests/showoff/thtml2.nim b/tests/showoff/thtml2.nim new file mode 100644 index 000000000..8a451ebf1 --- /dev/null +++ b/tests/showoff/thtml2.nim @@ -0,0 +1,37 @@ +discard """ + output: "<html><head><title>now look at this</title></head><body><ul><li>Nimrod is quite capable</li></ul></body></html>" +""" + +import strutils + +template html(name: expr, matter: stmt) {.immediate.} = + proc name(): string = + result = "<html>" + matter + result.add("</html>") + +template nestedTag(tag: expr) {.immediate.} = + template tag(matter: stmt) {.immediate.} = + result.add("<" & astToStr(tag) & ">") + matter + result.add("</" & astToStr(tag) & ">") + +template simpleTag(tag: expr) {.immediate.} = + template tag(matter: expr) {.immediate.} = + result.add("<$1>$2</$1>" % [astToStr(tag), matter]) + +nestedTag body +nestedTag head +nestedTag ul +simpleTag title +simpleTag li + + +html mainPage: + head: + title "now look at this" + body: + ul: + li "Nimrod is quite capable" + +echo mainPage() diff --git a/tests/showoff/tonce.nim b/tests/showoff/tonce.nim new file mode 100644 index 000000000..6fc372e87 --- /dev/null +++ b/tests/showoff/tonce.nim @@ -0,0 +1,22 @@ +discard """ + output: '''first call of p +some call of p +new instantiation +some call of p''' +""" + +template once(body: stmt) = + var x {.global.} = false + if not x: + x = true + body + +proc p() = + once: + echo "first call of p" + echo "some call of p" + +p() +once: + echo "new instantiation" +p() diff --git a/tests/showoff/tquasiquote.nim b/tests/showoff/tquasiquote.nim new file mode 100644 index 000000000..df7fccc33 --- /dev/null +++ b/tests/showoff/tquasiquote.nim @@ -0,0 +1,14 @@ +discard """ + outputsub: '''tquasiquote.nim(14,8): Check failed: 1 > 2''' +""" + +import macros + +macro check(ex: expr): stmt = + var info = ex.lineInfo + var expString = ex.toStrLit + result = quote do: + if not `ex`: + echo `info`, ": Check failed: ", `expString` + +check 1 > 2 |