new tester; all tests categorized
This commit is contained in:
parent
51ee524109
commit
20b5f31c03
481 changed files with 794 additions and 2506 deletions
9
tests/template/mcan_access_hidden_field.nim
Normal file
9
tests/template/mcan_access_hidden_field.nim
Normal file
|
|
@ -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
|
||||
|
||||
10
tests/template/mtempl5.nim
Normal file
10
tests/template/mtempl5.nim
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
|
||||
var
|
||||
gx = 88
|
||||
gy = 44
|
||||
|
||||
template templ*(): int =
|
||||
bind gx, gy
|
||||
gx + gy
|
||||
|
||||
|
||||
9
tests/template/tcan_access_hidden_field.nim
Normal file
9
tests/template/tcan_access_hidden_field.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
discard """
|
||||
output: 33
|
||||
"""
|
||||
|
||||
import mcan_access_hidden_field
|
||||
|
||||
var myfoo = createFoo(33, 44)
|
||||
|
||||
echo myfoo.geta
|
||||
18
tests/template/thygienictempl.nim
Normal file
18
tests/template/thygienictempl.nim
Normal file
|
|
@ -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)
|
||||
19
tests/template/tmodulealias.nim
Normal file
19
tests/template/tmodulealias.nim
Normal file
|
|
@ -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)))
|
||||
|
||||
24
tests/template/tstempl.nim
Normal file
24
tests/template/tstempl.nim
Normal file
|
|
@ -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)
|
||||
27
tests/template/ttempl.nim
Normal file
27
tests/template/ttempl.nim
Normal file
|
|
@ -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)
|
||||
19
tests/template/ttempl2.nim
Normal file
19
tests/template/ttempl2.nim
Normal file
|
|
@ -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'
|
||||
|
||||
58
tests/template/ttempl3.nim
Normal file
58
tests/template/ttempl3.nim
Normal file
|
|
@ -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
|
||||
8
tests/template/ttempl4.nim
Normal file
8
tests/template/ttempl4.nim
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
template `:=`(name, val: expr): stmt {.immediate.} =
|
||||
var name = val
|
||||
|
||||
ha := 1 * 4
|
||||
hu := "ta-da" == "ta-da"
|
||||
echo ha, hu
|
||||
|
||||
5
tests/template/ttempl5.nim
Normal file
5
tests/template/ttempl5.nim
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
import mtempl5
|
||||
|
||||
echo templ()
|
||||
|
||||
4
tests/template/ttemplreturntype.nim
Normal file
4
tests/template/ttemplreturntype.nim
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
template `=~` (a: int, b: int): bool = false
|
||||
var foo = 2 =~ 3
|
||||
|
||||
32
tests/template/utemplates.nim
Normal file
32
tests/template/utemplates.nim
Normal file
|
|
@ -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"
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue