diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/destructor/tdestructor.nim | 44 | ||||
-rw-r--r-- | tests/metatype/tusertypeclasses.nim | 13 | ||||
-rw-r--r-- | tests/specialops/tdotops.nim | 66 |
3 files changed, 123 insertions, 0 deletions
diff --git a/tests/destructor/tdestructor.nim b/tests/destructor/tdestructor.nim index bb1410d92..f10c0cc9c 100644 --- a/tests/destructor/tdestructor.nim +++ b/tests/destructor/tdestructor.nim @@ -12,6 +12,13 @@ myobj destroyed ---- mygeneric3 constructed mygeneric1 destroyed +---- +mygeneric1 destroyed +---- +myobj destroyed +---- +---- +myobj destroyed ''' """ @@ -31,6 +38,22 @@ type x: A y: B z: C + + TObjKind = enum A, B, C, D + + TCaseObj = object + case kind: TObjKind + of A: + x: TMyGeneric1[int] + of B, C: + y: TMyObj + else: + case innerKind: TObjKind + of A, B, C: + p: TMyGeneric3[int, float, string] + of D: + q: TMyGeneric3[TMyObj, int, int] + r: string proc destruct(o: var TMyObj) {.destructor.} = if o.p != nil: dealloc o.p @@ -82,3 +105,24 @@ mygeneric2[int](10) echo "----" mygeneric3() +proc caseobj = + block: + echo "----" + var o1 = TCaseObj(kind: A, x: TMyGeneric1[int](x: 10)) + + block: + echo "----" + var o2 = TCaseObj(kind: B, y: open()) + + block: + echo "----" + var o3 = TCaseObj(kind: D, innerKind: B, r: "test", + p: TMyGeneric3[int, float, string](x: 10, y: 1.0, z: "test")) + + block: + echo "----" + var o4 = TCaseObj(kind: D, innerKind: D, r: "test", + q: TMyGeneric3[TMyObj, int, int](x: open(), y: 1, z: 0)) + +caseobj() + diff --git a/tests/metatype/tusertypeclasses.nim b/tests/metatype/tusertypeclasses.nim index 4c2f07b85..4c8c0fc56 100644 --- a/tests/metatype/tusertypeclasses.nim +++ b/tests/metatype/tusertypeclasses.nim @@ -26,3 +26,16 @@ foo 10 foo "test" foo(@[TObj(x: 10), TObj(x: 20)]) +proc intval(x: int) = discard + +# check real and virtual fields +type + TFoo = generic T + intval T.x + intval T.y + +proc y(x: TObj): int = 10 + +proc testFoo(x: TFoo) = discard +testFoo(TObj(x: 10)) + diff --git a/tests/specialops/tdotops.nim b/tests/specialops/tdotops.nim new file mode 100644 index 000000000..ce5b3942d --- /dev/null +++ b/tests/specialops/tdotops.nim @@ -0,0 +1,66 @@ +discard """ + output: ''' +10 +assigning z = 20 +reading field y +20 +call to y +dot call +no params call to a +100 +no params call to b +100 +one param call to c with 10 +100''' +""" + +type + T1 = object + x*: int + + TD = distinct T1 + + T2 = object + x: int + +proc `.`*(v: T1, f: string): int = + echo "reading field ", f + return v.x + +proc `.=`(x: var T1, f: string{lit}, v: int) = + echo "assigning ", f, " = ", v + x.x = v + +template `.()`(x: T1, f: string, args: varargs[expr]): string = + echo "call to ", f + "dot call" + +echo "" + +var t = T1(x: 10) + +echo t.x +t.z = 20 +echo t.y +echo t.y() + +var d = TD(t) +assert(not compiles(d.y)) + +proc `.`(v: T2, f: string): int = + echo "no params call to ", f + return v.x + +proc `.`*(v: T2, f: string, a: int): int = + echo "one param call to ", f, " with ", a + return v.x + +var tt = T2(x: 100) + +echo tt.a +echo tt.b() +echo tt.c(10) + +assert(not compiles(tt.d("x"))) +assert(not compiles(tt.d(1, 2))) + |