summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2012-03-15 14:15:37 +0200
committerZahary Karadjov <zahary@gmail.com>2012-03-15 14:15:37 +0200
commit6975ba401b365a286336dd3dd66112395206c53e (patch)
tree9a123537a73a8d6c0d992ddb754beead0c47f98a /tests
parentd1d5fc8254eb376de64106314686f2e8e7db459c (diff)
downloadNim-6975ba401b365a286336dd3dd66112395206c53e.tar.gz
fix for template redefinition. test cases added.
Diffstat (limited to 'tests')
-rw-r--r--tests/compile/tredef.nim29
-rw-r--r--tests/reject/tprocredef.nim9
-rwxr-xr-xtests/run/toverl2.nim14
-rw-r--r--tests/run/tunittests.nim2
-rw-r--r--tests/run/utemplates.nim32
5 files changed, 81 insertions, 5 deletions
diff --git a/tests/compile/tredef.nim b/tests/compile/tredef.nim
new file mode 100644
index 000000000..02d1f7776
--- /dev/null
+++ b/tests/compile/tredef.nim
@@ -0,0 +1,29 @@
+template foo(a: int, b: string) = nil
+foo(1, "test")
+
+proc bar(a: int, b: string) = nil
+bar(1, "test")
+
+template foo(a: int, b: string) = bar(a, b)
+foo(1, "test")
+
+block:
+  proc bar(a: int, b: string) = nil
+  template foo(a: int, b: string) = nil
+  foo(1, "test")
+  bar(1, "test")
+  
+proc baz =
+  proc foo(a: int, b: string) = nil
+  proc foo(b: string) =
+    template bar(a: int, b: string) = nil
+    bar(1, "test")
+    
+  foo("test")
+
+  block:
+    proc foo(b: string) = nil
+    foo("test")
+    foo(1, "test")
+
+baz()
diff --git a/tests/reject/tprocredef.nim b/tests/reject/tprocredef.nim
new file mode 100644
index 000000000..86ed92b62
--- /dev/null
+++ b/tests/reject/tprocredef.nim
@@ -0,0 +1,9 @@
+discard """
+  file: "tprocredef.nim"
+  line: 8
+  errormsg: "redefinition of \'foo\'"
+"""
+
+proc foo(a: int, b: string) = nil
+proc foo(a: int, b: string) = nil
+
diff --git a/tests/run/toverl2.nim b/tests/run/toverl2.nim
index 49b17da4d..dd9f075a8 100755
--- a/tests/run/toverl2.nim
+++ b/tests/run/toverl2.nim
@@ -1,6 +1,6 @@
 discard """
   file: "toverl2.nim"
-  output: "true012"
+  output: "true012innertrue"
 """
 # Test new overloading resolution rules
 
@@ -14,14 +14,20 @@ iterator toverl2(x: int): int =
   while res < x: 
     yield res
     inc(res)
-    
+
 var
   pp: proc (x: bool): string = toverl2
+
 stdout.write(pp(true))
+
 for x in toverl2(3): 
   stdout.write(toverl2(x))
-stdout.write("\n")
-#OUT true012
 
+block:
+  proc toverl2(x: int): string = return "inner"
+  stdout.write(toverl2(5))
+  stdout.write(true)
 
+stdout.write("\n")
+#OUT true012innertrue
 
diff --git a/tests/run/tunittests.nim b/tests/run/tunittests.nim
index fa7fe5075..b2ec10cdc 100644
--- a/tests/run/tunittests.nim
+++ b/tests/run/tunittests.nim
@@ -1,2 +1,2 @@
-import uclosures
+import uclosures, utemplates
 
diff --git a/tests/run/utemplates.nim b/tests/run/utemplates.nim
new file mode 100644
index 000000000..8fd3d242a
--- /dev/null
+++ b/tests/run/utemplates.nim
@@ -0,0 +1,32 @@
+import unittest
+
+template t(a: int): expr = "int"
+template t(a: string): expr = "string"
+
+test "templates can be overloaded":
+  check t(10) == "int"
+  check t("test") == "string"
+
+test "previous definitions can be further overloaded or hidden in local scopes":
+  template t(a: bool): expr = "bool"
+
+  check t(true) == "bool"
+  check t(10) == "int"
+  
+  template t(a: int): expr = "inner int"
+  check t(10) == "inner int"
+  check t("test") == "string"
+
+test "templates can be redefined multiple times":
+  template customAssert(cond: bool, msg: string): stmt =
+    if not cond: fail(msg)
+
+  template assertion_failed(body: stmt) =
+    template fail(msg: string): stmt = body
+
+  assertion_failed: check msg == "first fail path"
+  customAssert false, "first fail path"
+
+  assertion_failed: check msg == "second fail path"  
+  customAssert false, "second fail path"
+