summary refs log tree commit diff stats
path: root/tests/fields/tfielditerator.nim
blob: d1fbf02f95f6bdfb005bb41576c078111227edc0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
discard """
  output: '''
a char: true
a char: false
an int: 5
an int: 6
a string: abc
false
true
true
false
true
a: a
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
'''
"""

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

  doAssert x != y
  doAssert x == x
  doAssert(not (x < x))
  doAssert x <= x
  doAssert y < x
  doAssert 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 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(myDisc: enC, c: 'Z')
  for key, val in fieldPairs(co):
    echo key, ": ", val

  for val in fields(co):
    echo val