new tester; all tests categorized

This commit is contained in:
Araq 2014-01-13 02:10:03 +01:00
commit 20b5f31c03
481 changed files with 794 additions and 2506 deletions

View 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

View file

@ -0,0 +1,10 @@
var
gx = 88
gy = 44
template templ*(): int =
bind gx, gy
gx + gy

View file

@ -0,0 +1,9 @@
discard """
output: 33
"""
import mcan_access_hidden_field
var myfoo = createFoo(33, 44)
echo myfoo.geta

View 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)

View 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)))

View 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
View 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)

View 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'

View 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

View 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

View file

@ -0,0 +1,5 @@
import mtempl5
echo templ()

View file

@ -0,0 +1,4 @@
template `=~` (a: int, b: int): bool = false
var foo = 2 =~ 3

View 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"