diff options
author | Miran <narimiran@users.noreply.github.com> | 2018-10-13 14:58:31 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-10-13 14:58:31 +0200 |
commit | 3c9fcc4c30dd76becacaab67f2587d88490806b9 (patch) | |
tree | 6b79838f8699f0993ac36e89549df94c0bbf7cd6 /tests/fields | |
parent | ef820769a47722cd33935dd94642aca9ecc09a8b (diff) | |
download | Nim-3c9fcc4c30dd76becacaab67f2587d88490806b9.tar.gz |
Merge tests into a larger file (part 2 of ∞) (#9335)
* merge controlflow tests * merge distinct tests * merge enum tests * merge fields tests * merge implicit tests * merge iter issues tests
Diffstat (limited to 'tests/fields')
-rw-r--r-- | tests/fields/tfieldindex.nim | 21 | ||||
-rw-r--r-- | tests/fields/tfielditerator.nim | 106 | ||||
-rw-r--r-- | tests/fields/tfielditerator2.nim | 70 | ||||
-rw-r--r-- | tests/fields/tfields.nim | 108 | ||||
-rw-r--r-- | tests/fields/tfields_in_template.nim | 15 | ||||
-rw-r--r-- | tests/fields/tfields_with_break.nim | 33 | ||||
-rw-r--r-- | tests/fields/timplicitfieldswithpartial.nim | 35 |
7 files changed, 195 insertions, 193 deletions
diff --git a/tests/fields/tfieldindex.nim b/tests/fields/tfieldindex.nim deleted file mode 100644 index 6de6d54bd..000000000 --- a/tests/fields/tfieldindex.nim +++ /dev/null @@ -1,21 +0,0 @@ -discard """ - output: "1" -""" - -type - TMyTuple = tuple[a, b: int] - -proc indexOf*(t: typedesc, name: string): int = - ## takes a tuple and looks for the field by name. - ## returs index of that field. - var - d: t - i = 0 - for n, x in fieldPairs(d): - if n == name: return i - i.inc - raise newException(ValueError, "No field " & name & " in type " & - astToStr(t)) - -echo TMyTuple.indexOf("b") - diff --git a/tests/fields/tfielditerator.nim b/tests/fields/tfielditerator.nim index 6d15ea05d..b1c357997 100644 --- a/tests/fields/tfielditerator.nim +++ b/tests/fields/tfielditerator.nim @@ -15,32 +15,100 @@ b: b x: 5 y: 6 z: abc +a char: true +a char: false +an int: 5 +an int: 6 +a string: abc +a string: I'm root! +CMP false +CMP true +CMP true +CMP false +CMP true +CMP true +a: a +b: b +x: 5 +y: 6 +z: abc +thaRootMan: I'm root! +myDisc: enC +c: Z +enC +Z ''' """ -type - TMyTuple = tuple[a, b: char, x, y: int, z: string] +block titerator1: + type + TMyTuple = tuple[a, b: char, x, y: int, z: string] + + proc p(x: char) = echo "a char: ", x <= 'a' + proc p(x: int) = echo "an int: ", x + proc p(x: string) = echo "a string: ", x + + var x: TMyTuple = ('a', 'b', 5, 6, "abc") + var y: TMyTuple = ('A', 'b', 5, 9, "abc") + + for f in fields(x): + p f + + for a, b in fields(x, y): + echo a == b + + for key, val in fieldPairs(x): + echo key, ": ", val + + assert x != y + assert x == x + assert(not (x < x)) + assert x <= x + assert y < x + assert y <= x + + +block titerator2: + type + SomeRootObj = object of RootObj + thaRootMan: string + TMyObj = object of SomeRootObj + a, b: char + x, y: int + z: string + + TEnum = enum enA, enB, enC + TMyCaseObj = object + case myDisc: TEnum + of enA: a: int + of enB: b: string + of enC: c: char + + proc p(x: char) = echo "a char: ", x <= 'a' + proc p(x: int) = echo "an int: ", x + proc p(x: string) = echo "a string: ", x -proc p(x: char) = echo "a char: ", x <= 'a' -proc p(x: int) = echo "an int: ", x -proc p(x: string) = echo "a string: ", x + proc myobj(a, b: char, x, y: int, z: string): TMyObj = + result.a = a; result.b = b; result.x = x; result.y = y; result.z = z + result.thaRootMan = "I'm root!" -var x: TMyTuple = ('a', 'b', 5, 6, "abc") -var y: TMyTuple = ('A', 'b', 5, 9, "abc") + var x = myobj('a', 'b', 5, 6, "abc") + var y = myobj('A', 'b', 5, 9, "abc") -for f in fields(x): - p f + for f in fields(x): + p f -for a, b in fields(x, y): - echo a == b + for a, b in fields(x, y): + echo "CMP ", a == b -for key, val in fieldPairs(x): - echo key, ": ", val + for key, val in fieldPairs(x): + echo key, ": ", val -assert x != y -assert x == x -assert(not (x < x)) -assert x <= x -assert y < x -assert y <= x + var co: TMyCaseObj + co.myDisc = enC + co.c = 'Z' + for key, val in fieldPairs(co): + echo key, ": ", val + for val in fields(co): + echo val \ No newline at end of file diff --git a/tests/fields/tfielditerator2.nim b/tests/fields/tfielditerator2.nim deleted file mode 100644 index c8e230cf5..000000000 --- a/tests/fields/tfielditerator2.nim +++ /dev/null @@ -1,70 +0,0 @@ -discard """ - output: ''' -a char: true -a char: false -an int: 5 -an int: 6 -a string: abc -a string: I'm root! -CMP false -CMP true -CMP true -CMP false -CMP true -CMP true -a: a -b: b -x: 5 -y: 6 -z: abc -thaRootMan: I'm root! -myDisc: enC -c: Z -enC -Z -''' -""" - -type - SomeRootObj = object of RootObj - thaRootMan: string - TMyObj = object of SomeRootObj - a, b: char - x, y: int - z: string - - TEnum = enum enA, enB, enC - TMyCaseObj = object - case myDisc: TEnum - of enA: a: int - of enB: b: string - of enC: c: char - -proc p(x: char) = echo "a char: ", x <= 'a' -proc p(x: int) = echo "an int: ", x -proc p(x: string) = echo "a string: ", x - -proc myobj(a, b: char, x, y: int, z: string): TMyObj = - result.a = a; result.b = b; result.x = x; result.y = y; result.z = z - result.thaRootMan = "I'm root!" - -var x = myobj('a', 'b', 5, 6, "abc") -var y = myobj('A', 'b', 5, 9, "abc") - -for f in fields(x): - p f - -for a, b in fields(x, y): - echo "CMP ", a == b - -for key, val in fieldPairs(x): - echo key, ": ", val - -var co: TMyCaseObj -co.myDisc = enC -co.c = 'Z' -for key, val in fieldPairs(co): - echo key, ": ", val - -for val in fields(co): - echo val diff --git a/tests/fields/tfields.nim b/tests/fields/tfields.nim new file mode 100644 index 000000000..d52b5e8d2 --- /dev/null +++ b/tests/fields/tfields.nim @@ -0,0 +1,108 @@ +discard """ + output: ''' +n +n +(one: 1, two: 2, three: 3) +1 +2 +3 +(one: 4, two: 5, three: 6) +4 +(one: 7, two: 8, three: 9) +7 +8 +9 +(foo: 38, other: "string here") +43 +100 +90 +''' +""" + + +block tindex: + type + TMyTuple = tuple[a, b: int] + + proc indexOf(t: typedesc, name: string): int = + ## takes a tuple and looks for the field by name. + ## returs index of that field. + var + d: t + i = 0 + for n, x in fieldPairs(d): + if n == name: return i + i.inc + raise newException(ValueError, "No field " & name & " in type " & + astToStr(t)) + + doAssert TMyTuple.indexOf("b") == 1 + + + +block ttemplate: + # bug #1902 + # This works. + for name, value in (n: "v").fieldPairs: + echo name + + template wrapper: typed = + for name, value in (n: "v").fieldPairs: + echo name + wrapper() + + + +block tbreak: + # bug #2134 + type + TestType = object + one: int + two: int + three: int + + var + ab = TestType(one:1, two:2, three:3) + ac = TestType(one:4, two:5, three:6) + ad = TestType(one:7, two:8, three:9) + tstSeq = [ab, ac, ad] + + for tstElement in mitems(tstSeq): + echo tstElement + for tstField in fields(tstElement): + #for tstField in [1,2,4,6]: + echo tstField + if tstField == 4: + break + + + +block timplicit_with_partial: + type + Base = ref object of RootObj + Foo {.partial.} = ref object of Base + + proc my(f: Foo) = + #var f.next = f + let f.foo = 38 + let f.other = "string here" + echo f[] + echo f.foo + 5 + + var g: Foo + new(g) + my(g) + + type + FooTask {.partial.} = ref object of RootObj + + proc foo(t: FooTask) {.liftLocals: t.} = + var x = 90 + if true: + var x = 10 + while x < 100: + inc x + echo x + echo x + + foo(FooTask()) \ No newline at end of file diff --git a/tests/fields/tfields_in_template.nim b/tests/fields/tfields_in_template.nim deleted file mode 100644 index b7d5d2343..000000000 --- a/tests/fields/tfields_in_template.nim +++ /dev/null @@ -1,15 +0,0 @@ -discard """ - output: '''n -n''' -""" - -# bug #1902 -# This works. -for name, value in (n: "v").fieldPairs: - echo name - -# This doesn't compile - "expression 'name' has no type (or is ambiguous)". -template wrapper: typed = - for name, value in (n: "v").fieldPairs: - echo name -wrapper() diff --git a/tests/fields/tfields_with_break.nim b/tests/fields/tfields_with_break.nim deleted file mode 100644 index 1f2632692..000000000 --- a/tests/fields/tfields_with_break.nim +++ /dev/null @@ -1,33 +0,0 @@ -discard """ - output: '''(one: 1, two: 2, three: 3) -1 -2 -3 -(one: 4, two: 5, three: 6) -4 -(one: 7, two: 8, three: 9) -7 -8 -9''' -""" - -# bug #2134 -type - TestType = object - one: int - two: int - three: int - -var - ab = TestType(one:1, two:2, three:3) - ac = TestType(one:4, two:5, three:6) - ad = TestType(one:7, two:8, three:9) - tstSeq = [ab, ac, ad] - -for tstElement in mitems(tstSeq): - echo tstElement - for tstField in fields(tstElement): - #for tstField in [1,2,4,6]: - echo tstField - if tstField == 4: - break diff --git a/tests/fields/timplicitfieldswithpartial.nim b/tests/fields/timplicitfieldswithpartial.nim deleted file mode 100644 index 937833257..000000000 --- a/tests/fields/timplicitfieldswithpartial.nim +++ /dev/null @@ -1,35 +0,0 @@ -discard """ - output: '''(foo: 38, other: "string here") -43 -100 -90''' -""" - -type - Base = ref object of RootObj - Foo {.partial.} = ref object of Base - -proc my(f: Foo) = - #var f.next = f - let f.foo = 38 - let f.other = "string here" - echo f[] - echo f.foo + 5 - -var g: Foo -new(g) -my(g) - -type - FooTask {.partial.} = ref object of RootObj - -proc foo(t: FooTask) {.liftLocals: t.} = - var x = 90 - if true: - var x = 10 - while x < 100: - inc x - echo x - echo x - -foo(FooTask()) |