diff options
author | Araq <rumpf_a@web.de> | 2014-01-13 02:10:03 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2014-01-13 02:10:03 +0100 |
commit | 20b5f31c03fb556ec0aa2428a40adbac004d8987 (patch) | |
tree | 58086941e7d6bb8f480ca1173a95722ada9435b2 /tests/bind | |
parent | 51ee524109cf7e3e86c676bc1676063a01bfd979 (diff) | |
download | Nim-20b5f31c03fb556ec0aa2428a40adbac004d8987.tar.gz |
new tester; all tests categorized
Diffstat (limited to 'tests/bind')
-rw-r--r-- | tests/bind/mbind3.nim | 10 | ||||
-rw-r--r-- | tests/bind/tbind1.nim | 21 | ||||
-rw-r--r-- | tests/bind/tbind2.nim | 17 | ||||
-rw-r--r-- | tests/bind/tbind3.nim | 11 | ||||
-rw-r--r-- | tests/bind/tbindoverload.nim | 12 | ||||
-rw-r--r-- | tests/bind/tbindtypedesc.nim | 12 | ||||
-rw-r--r-- | tests/bind/tdatabind.nim | 95 | ||||
-rw-r--r-- | tests/bind/tmixin.nim | 27 | ||||
-rw-r--r-- | tests/bind/tnicerrorforsymchoice.nim | 18 |
9 files changed, 223 insertions, 0 deletions
diff --git a/tests/bind/mbind3.nim b/tests/bind/mbind3.nim new file mode 100644 index 000000000..d02bc79d0 --- /dev/null +++ b/tests/bind/mbind3.nim @@ -0,0 +1,10 @@ +# Module A +var + lastId = 0 + +template genId*: expr = + bind lastId + inc(lastId) + lastId + + diff --git a/tests/bind/tbind1.nim b/tests/bind/tbind1.nim new file mode 100644 index 000000000..6593b2307 --- /dev/null +++ b/tests/bind/tbind1.nim @@ -0,0 +1,21 @@ +discard """ + file: "tbind1.nim" + output: "3" +""" +# Test the new ``bind`` keyword for templates + +proc p1(x: int8, y: int): int = return x + y + +template tempBind(x, y: expr): expr = + bind p1 + p1(x, y) + +proc p1(x: int, y: int8): int = return x - y + +# This is tricky: the call to ``p1(1'i8, 2'i8)`` should not fail in line 6, +# because it is not ambiguous there. But it is ambiguous after line 8. + +echo tempBind(1'i8, 2'i8) #OUT 3 + + + diff --git a/tests/bind/tbind2.nim b/tests/bind/tbind2.nim new file mode 100644 index 000000000..e8e21ad02 --- /dev/null +++ b/tests/bind/tbind2.nim @@ -0,0 +1,17 @@ +discard """ + file: "tbind2.nim" + line: 14 + errormsg: "ambiguous call" +""" +# Test the new ``bind`` keyword for templates + +proc p1(x: int8, y: int): int = return x + y +proc p1(x: int, y: int8): int = return x - y + +template tempBind(x, y: expr): expr = + (bind p1(x, y)) #ERROR_MSG ambiguous call + +echo tempBind(1'i8, 2'i8) + + + diff --git a/tests/bind/tbind3.nim b/tests/bind/tbind3.nim new file mode 100644 index 000000000..551acc10f --- /dev/null +++ b/tests/bind/tbind3.nim @@ -0,0 +1,11 @@ +discard """ + file: "tbind3.nim" + output: "1" +""" +# Module B +import mbind3 + +echo genId() #OUT 1 + + + diff --git a/tests/bind/tbindoverload.nim b/tests/bind/tbindoverload.nim new file mode 100644 index 000000000..6f5bb339e --- /dev/null +++ b/tests/bind/tbindoverload.nim @@ -0,0 +1,12 @@ +import strtabs + +template t*() = + block: + bind newStringTable + discard {"Content-Type": "text/html"}.newStringTable() + + discard {:}.newStringTable + +#discard {"Content-Type": "text/html"}.newStringTable() + +t() diff --git a/tests/bind/tbindtypedesc.nim b/tests/bind/tbindtypedesc.nim new file mode 100644 index 000000000..d6fbae537 --- /dev/null +++ b/tests/bind/tbindtypedesc.nim @@ -0,0 +1,12 @@ +discard """ + line: 11 + file: "tbindtypedesc.nim" + errormsg: "type mismatch: got (typedesc[float], string)" +""" + +proc foo(T: typedesc; some: T) = + echo($some) + +foo int, 4 +foo float, "bad" + diff --git a/tests/bind/tdatabind.nim b/tests/bind/tdatabind.nim new file mode 100644 index 000000000..afa8aa47b --- /dev/null +++ b/tests/bind/tdatabind.nim @@ -0,0 +1,95 @@ +discard """ + disabled: true +""" + +import events +type + TProperty*[T] = object of TObject + getProc: proc(property: TProperty[T]): T {.nimcall.} + setProc: proc(property: var TProperty[T], value: T) {.nimcall.} + value: T + ValueChanged*: TEventHandler + Binders: seq[TProperty[T]] + EEmitter: TEventEmitter + # Not a descriptive name but it was that or TPropertyValueChangeEventArgs. + TValueEventArgs[T] = object of TEventArgs + Property*: TProperty[T] + + +proc newProperty*[T](value: T): TProperty[T] = + var prop: TProperty[T] + + prop.EEmitter = initEventEmitter() + prop.Binders = @[] + prop.ValueChanged = initEventHandler("ValueChanged") + prop.value = value + + proc getter(property: TProperty[T]): T = + return property.value + + prop.getProc = getter + + proc setter(property: var TProperty[T], v: T) = + property.value = v + + # fire event here + var args: TValueEventArgs[T] + args.Property = property + property.EEmitter.emit(property.ValueChanged, args) + + prop.setProc = setter + + return prop + +proc `prop`[T] (p: TProperty[T]): T = + # I'm assuming this is trying to get a value from the property. + # i.e. myVar = myProperty + return p.getProc(p) + +proc `~=`[T] (p: var TProperty[T], v: T) = + # Assuming this is setting the value. + p.setProc(p, v) + +proc `$`[T] (p: TProperty[T]): string = + var value = p.getProc(p) + return $value + +proc propertyBind*[T](p1: var TProperty[T], p2: var TProperty[T]) = + p1.Binders.add(p2) + + # make handler -> handler[T] so trigger even more generics bugs ... + proc handler(e: TEventArgs) = + type TEA = TValueEventArgs[T] + var args = TEA(e) + var val = args.Property.getProc(p1) + for i in countup(0, len(e.Property.ValueChanged.Binders) -1): + var binded = e.Property.ValueChanged.Binders[i] + binded.setProc(binded, val) + + echo("Property 1 has changed to " & $val) + + if p1.ValueChanged.containsHandler(handler) == false: + addHandler(p1.ValueChanged, handler) + +proc `->`[T](p1: var TProperty[T], p2: var TProperty[T]) = + propertyBind(p2,p1) + +when isMainModule: + # Initial value testing + var myProp = newProperty(5) + + echo(myProp) + + myProp ~= 7 # Temp operator until overloading of '=' is implemented. + echo(myProp) + + # Binding testing + + var prop1 = newProperty(9) + var prop2: TProperty[int] + + prop2 -> prop1 # Binds prop2 to prop1 + + prop1 ~= 7 + echo(prop2) # Output: 7 + diff --git a/tests/bind/tmixin.nim b/tests/bind/tmixin.nim new file mode 100644 index 000000000..d841326a5 --- /dev/null +++ b/tests/bind/tmixin.nim @@ -0,0 +1,27 @@ +discard """ + output: "1\n2" +""" + +type + TFoo1 = object of TObject + v: int + TFoo2 = object of TFoo1 + v2: int + +proc test(f: TFoo1) = + echo "1" + +proc Foo[T](f: T) = + mixin test + test(f) + +var + a: TFoo1 + b: TFoo2 + + +proc test(f: TFoo2) = + echo "2" + +Foo(a) +Foo(b) diff --git a/tests/bind/tnicerrorforsymchoice.nim b/tests/bind/tnicerrorforsymchoice.nim new file mode 100644 index 000000000..bc271dcab --- /dev/null +++ b/tests/bind/tnicerrorforsymchoice.nim @@ -0,0 +1,18 @@ +discard """ + line: 18 + errormsg: "type mismatch: got (proc (TScgi) | proc (PAsyncSocket, PStringTable, string))" +""" + +#bug #442 +import scgi, sockets, asyncio, strtabs +proc handleSCGIRequest[TScgi: TScgiState | PAsyncScgiState](s: TScgi) = + nil +proc handleSCGIRequest(client: PAsyncSocket, headers: PStringTable, + input: string) = + nil + +proc test(handle: proc (client: PAsyncSocket, headers: PStringTable, + input: string), b: int) = + nil + +test(handleSCGIRequest) |