summary refs log tree commit diff stats
path: root/tests/usingstmt
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-01-13 02:10:03 +0100
committerAraq <rumpf_a@web.de>2014-01-13 02:10:03 +0100
commit20b5f31c03fb556ec0aa2428a40adbac004d8987 (patch)
tree58086941e7d6bb8f480ca1173a95722ada9435b2 /tests/usingstmt
parent51ee524109cf7e3e86c676bc1676063a01bfd979 (diff)
downloadNim-20b5f31c03fb556ec0aa2428a40adbac004d8987.tar.gz
new tester; all tests categorized
Diffstat (limited to 'tests/usingstmt')
-rw-r--r--tests/usingstmt/tusingstatement.nim89
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/usingstmt/tusingstatement.nim b/tests/usingstmt/tusingstatement.nim
new file mode 100644
index 000000000..a33aced4c
--- /dev/null
+++ b/tests/usingstmt/tusingstatement.nim
@@ -0,0 +1,89 @@
+discard """
+  file: "tusingstatement.nim"
+  output: "Using test.Closing test."
+"""
+
+import 
+  macros
+
+# This macro mimics the using statement from C#
+#
+# It's kept only as a test for the macro system
+# Nimrod's destructors offer a mechanism for automatic 
+# disposal of resources.
+#
+macro autoClose(e: expr): stmt {.immediate.} =
+  let e = callsite()
+  if e.len != 3:
+    error "Using statement: unexpected number of arguments. Got " &
+      $e.len & ", expected: 1 or more variable assignments and a block"
+
+  var args = e
+  var body = e[2]
+  
+  var 
+    variables : seq[PNimrodNode]
+    closingCalls : seq[PNimrodNode]
+
+  newSeq(variables, 0)
+  newSeq(closingCalls, 0)
+  
+  for i in countup(1, args.len-2):
+    if args[i].kind == nnkExprEqExpr:
+      var varName = args[i][0]
+      var varValue = args[i][1]
+ 
+      var varAssignment = newNimNode(nnkIdentDefs)
+      varAssignment.add(varName)
+      varAssignment.add(newNimNode(nnkEmpty)) # empty means no type
+      varAssignment.add(varValue)
+      variables.add(varAssignment)
+
+      closingCalls.add(newCall(!"close", varName))
+    else:
+      error "Using statement: Unexpected expression. Got " &
+        $args[i].kind & " instead of assignment."
+  
+  var varSection = newNimNode(nnkVarSection)
+  varSection.add(variables)
+
+  var finallyBlock = newNimNode(nnkStmtList)
+  finallyBlock.add(closingCalls)
+
+  # XXX: Use a template here once getAst is working properly
+  var targetAst = parseStmt"""block:
+    var
+      x = foo()
+      y = bar()
+
+    try:
+      body()
+
+    finally:
+      close x
+      close y
+  """
+
+  targetAst[0][1][0] = varSection
+  targetAst[0][1][1][0] = body
+  targetAst[0][1][1][1][0] = finallyBlock
+  
+  result = targetAst
+
+type 
+  TResource* = object
+    field*: string
+
+proc openResource(param: string): TResource =
+  result.field = param
+
+proc close(r: var TResource) =
+  write(stdout, "Closing " & r.field & ".")
+
+proc use(r: var TResource) =
+  write(stdout, "Using " & r.field & ".")
+
+autoClose(r = openResource("test")):
+  use r
+
+