summary refs log tree commit diff stats
path: root/tests/fields
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-01-13 02:10:03 +0100
committerAraq <rumpf_a@web.de>2014-01-13 02:10:03 +0100
commit20b5f31c03fb556ec0aa2428a40adbac004d8987 (patch)
tree58086941e7d6bb8f480ca1173a95722ada9435b2 /tests/fields
parent51ee524109cf7e3e86c676bc1676063a01bfd979 (diff)
downloadNim-20b5f31c03fb556ec0aa2428a40adbac004d8987.tar.gz
new tester; all tests categorized
Diffstat (limited to 'tests/fields')
-rw-r--r--tests/fields/tfieldindex.nim21
-rw-r--r--tests/fields/tfielditerator.nim46
-rw-r--r--tests/fields/tfielditerator2.nim64
3 files changed, 131 insertions, 0 deletions
diff --git a/tests/fields/tfieldindex.nim b/tests/fields/tfieldindex.nim
new file mode 100644
index 000000000..f0674af54
--- /dev/null
+++ b/tests/fields/tfieldindex.nim
@@ -0,0 +1,21 @@
+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(EInvalidValue, "No field " & name & " in type " & 
+    astToStr(t))
+
+echo TMyTuple.indexOf("b")
+
diff --git a/tests/fields/tfielditerator.nim b/tests/fields/tfielditerator.nim
new file mode 100644
index 000000000..2919aab41
--- /dev/null
+++ b/tests/fields/tfielditerator.nim
@@ -0,0 +1,46 @@
+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
+'''
+"""
+
+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
+
diff --git a/tests/fields/tfielditerator2.nim b/tests/fields/tfielditerator2.nim
new file mode 100644
index 000000000..76fa568f2
--- /dev/null
+++ b/tests/fields/tfielditerator2.nim
@@ -0,0 +1,64 @@
+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
+myDisc: enC
+c: Z
+enC
+Z
+'''
+"""
+
+type
+  TMyObj = object
+    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
+
+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 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