summary refs log tree commit diff stats
path: root/tests/template
diff options
context:
space:
mode:
Diffstat (limited to 'tests/template')
-rw-r--r--tests/template/mcan_access_hidden_field.nim9
-rw-r--r--tests/template/mtempl5.nim10
-rw-r--r--tests/template/tcan_access_hidden_field.nim9
-rw-r--r--tests/template/thygienictempl.nim18
-rw-r--r--tests/template/tmodulealias.nim19
-rw-r--r--tests/template/tstempl.nim24
-rw-r--r--tests/template/ttempl.nim27
-rw-r--r--tests/template/ttempl2.nim19
-rw-r--r--tests/template/ttempl3.nim58
-rw-r--r--tests/template/ttempl4.nim8
-rw-r--r--tests/template/ttempl5.nim5
-rw-r--r--tests/template/ttemplreturntype.nim4
-rw-r--r--tests/template/utemplates.nim32
13 files changed, 242 insertions, 0 deletions
diff --git a/tests/template/mcan_access_hidden_field.nim b/tests/template/mcan_access_hidden_field.nim
new file mode 100644
index 000000000..bf3592701
--- /dev/null
+++ b/tests/template/mcan_access_hidden_field.nim
@@ -0,0 +1,9 @@
+
+type
+  Foo* = object
+    fooa, foob: int
+
+proc createFoo*(a, b: int): Foo = Foo(fooa: a, foob: b)
+
+template geta*(f: Foo): expr = f.fooa
+
diff --git a/tests/template/mtempl5.nim b/tests/template/mtempl5.nim
new file mode 100644
index 000000000..51e8461b8
--- /dev/null
+++ b/tests/template/mtempl5.nim
@@ -0,0 +1,10 @@
+
+var 
+  gx = 88
+  gy = 44
+  
+template templ*(): int =
+  bind gx, gy
+  gx + gy
+  
+
diff --git a/tests/template/tcan_access_hidden_field.nim b/tests/template/tcan_access_hidden_field.nim
new file mode 100644
index 000000000..a6f6490cc
--- /dev/null
+++ b/tests/template/tcan_access_hidden_field.nim
@@ -0,0 +1,9 @@
+discard """
+  output: 33
+"""
+
+import mcan_access_hidden_field
+
+var myfoo = createFoo(33, 44)
+
+echo myfoo.geta
diff --git a/tests/template/thygienictempl.nim b/tests/template/thygienictempl.nim
new file mode 100644
index 000000000..3cdbac40e
--- /dev/null
+++ b/tests/template/thygienictempl.nim
@@ -0,0 +1,18 @@
+    
+var
+  e = "abc"
+    
+raise newException(EIO, e & "ha!")
+
+template t() = echo(foo)
+
+var foo = 12
+t()
+
+
+template test_in(a, b, c: expr): bool {.immediate, dirty.} =
+  var result {.gensym.}: bool = false
+  false
+
+when isMainModule:
+  assert test_in(ret2, "test", str_val)
diff --git a/tests/template/tmodulealias.nim b/tests/template/tmodulealias.nim
new file mode 100644
index 000000000..a7d155e51
--- /dev/null
+++ b/tests/template/tmodulealias.nim
@@ -0,0 +1,19 @@
+discard """
+  disabled: true
+"""
+
+when defined(windows):
+  import winlean
+else:
+  import posix
+
+when defined(Windows):
+  template orig: expr = 
+    winlean
+else:
+  template orig: expr = 
+    posix
+
+proc socket(domain, typ, protocol: int): int =
+  result = orig.socket(ord(domain), ord(typ), ord(protocol)))
+
diff --git a/tests/template/tstempl.nim b/tests/template/tstempl.nim
new file mode 100644
index 000000000..2b4a8baa0
--- /dev/null
+++ b/tests/template/tstempl.nim
@@ -0,0 +1,24 @@
+discard """
+  output: '''global = levB, arg = levA, test = false
+levB'''
+"""
+
+# tstempl.nim
+import strutils
+
+type 
+  TLev = enum
+    levA,
+    levB
+
+var abclev = levB
+
+template tstLev(abclev: TLev) =
+  bind tstempl.abclev, `%`
+  writeln(stdout, "global = $1, arg = $2, test = $3" % [
+    $tstempl.abclev, $abclev, $(tstempl.abclev == abclev)])
+  # evaluates to true, but must be false
+
+
+tstLev(levA)
+writeln(stdout, $abclev)
diff --git a/tests/template/ttempl.nim b/tests/template/ttempl.nim
new file mode 100644
index 000000000..2c4785325
--- /dev/null
+++ b/tests/template/ttempl.nim
@@ -0,0 +1,27 @@
+# Test the new template file mechanism
+
+import
+  os, times
+
+include "sunset.tmpl"
+
+const
+  tabs = [["home", "index"],
+          ["news", "news"],
+          ["documentation", "documentation"],
+          ["download", "download"],
+          ["FAQ", "question"],
+          ["links", "links"]]
+
+
+var i = 0
+for item in items(tabs): 
+  var content = $i
+  var file: TFile
+  if open(file, changeFileExt(item[1], "html"), fmWrite): 
+    write(file, sunsetTemplate(current=item[1], ticker="", content=content,
+                               tabs=tabs))
+    close(file)
+  else:
+    write(stdout, "cannot open file for writing")
+  inc(i)
diff --git a/tests/template/ttempl2.nim b/tests/template/ttempl2.nim
new file mode 100644
index 000000000..142bbb8c7
--- /dev/null
+++ b/tests/template/ttempl2.nim
@@ -0,0 +1,19 @@
+discard """
+  file: "ttempl2.nim"
+  line: 18
+  errormsg: "undeclared identifier: \'b\'"
+"""
+template declareInScope(x: expr, t: typeDesc): stmt {.immediate.} =
+  var x: t
+  
+template declareInNewScope(x: expr, t: typeDesc): stmt {.immediate.} =
+  # open a new scope:
+  block: 
+    var x: t
+
+declareInScope(a, int)
+a = 42  # works, `a` is known here
+
+declareInNewScope(b, int)
+b = 42  #ERROR_MSG undeclared identifier: 'b'
+
diff --git a/tests/template/ttempl3.nim b/tests/template/ttempl3.nim
new file mode 100644
index 000000000..59be24624
--- /dev/null
+++ b/tests/template/ttempl3.nim
@@ -0,0 +1,58 @@
+
+template withOpenFile(f: expr, filename: string, mode: TFileMode,
+                      actions: stmt): stmt {.immediate.} =
+  block:
+    # test that 'f' is implicitly 'injecting':
+    var f: TFile
+    if open(f, filename, mode):
+      try:
+        actions
+      finally:
+        close(f)
+    else:
+      quit("cannot open for writing: " & filename)
+    
+withOpenFile(txt, "ttempl3.txt", fmWrite):
+  writeln(txt, "line 1")
+  txt.writeln("line 2")
+  
+var
+  myVar: array[0..1, int]
+
+# Test zero argument template: 
+template ha: expr = myVar[0]
+  
+ha = 1  
+echo(ha)
+
+
+# Test identifier generation:
+template prefix(name: expr): expr {.immediate.} = `"hu" name`
+
+var `hu "XYZ"` = "yay"
+
+echo prefix(XYZ)
+
+template typedef(name: expr, typ: typeDesc) {.immediate, dirty.} =
+  type
+    `T name`* = typ
+    `P name`* = ref `T name`
+    
+typedef(myint, int)
+var x: PMyInt
+
+
+# Test UFCS
+
+type
+  Foo = object
+    arg: int
+
+proc initFoo(arg: int): Foo =
+  result.arg = arg
+
+template create(typ: typeDesc, arg: expr): expr = `init typ`(arg)
+
+var ff = Foo.create(12)
+
+echo ff.arg
diff --git a/tests/template/ttempl4.nim b/tests/template/ttempl4.nim
new file mode 100644
index 000000000..26c82e471
--- /dev/null
+++ b/tests/template/ttempl4.nim
@@ -0,0 +1,8 @@
+
+template `:=`(name, val: expr): stmt {.immediate.} =
+  var name = val
+
+ha := 1 * 4
+hu := "ta-da" == "ta-da"
+echo ha, hu
+
diff --git a/tests/template/ttempl5.nim b/tests/template/ttempl5.nim
new file mode 100644
index 000000000..85692e97b
--- /dev/null
+++ b/tests/template/ttempl5.nim
@@ -0,0 +1,5 @@
+
+import mtempl5
+
+echo templ()
+
diff --git a/tests/template/ttemplreturntype.nim b/tests/template/ttemplreturntype.nim
new file mode 100644
index 000000000..642fa1b72
--- /dev/null
+++ b/tests/template/ttemplreturntype.nim
@@ -0,0 +1,4 @@
+
+template `=~` (a: int, b: int): bool = false
+var foo = 2 =~ 3
+
diff --git a/tests/template/utemplates.nim b/tests/template/utemplates.nim
new file mode 100644
index 000000000..68fbb23a6
--- /dev/null
+++ b/tests/template/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) {.immediate.} =
+    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"
+