summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-08-26 14:49:57 +0200
committerAndreas Rumpf <rumpf_a@web.de>2016-08-26 14:49:57 +0200
commit34dd08e9ae56b0003fc4ea32262b5284cfd21c41 (patch)
treee2bfbee834c75de03ddcbd7defc9d6e390ec0358
parent98859a72486038995f66d499f76172bb6e670a22 (diff)
downloadNim-34dd08e9ae56b0003fc4ea32262b5284cfd21c41.tar.gz
added missing test files
-rw-r--r--tests/macros/tgettype2.nim67
-rw-r--r--tests/metatype/twildtypedesc.nim43
2 files changed, 110 insertions, 0 deletions
diff --git a/tests/macros/tgettype2.nim b/tests/macros/tgettype2.nim
new file mode 100644
index 000000000..f129e6e1b
--- /dev/null
+++ b/tests/macros/tgettype2.nim
@@ -0,0 +1,67 @@
+
+import macros, typetraits
+
+type Foo = distinct int
+type Bar = distinct int
+type Baz = int
+
+let foo = 0.Foo
+let bar = 1.Bar
+let baz = 2.Baz
+
+type MyType[T] = distinct tuple[a,b:T]
+type MySimpleType = distinct tuple[a,b: int]
+
+var v: seq[int]
+var vv: seq[float]
+var t: MyType[int]
+var tt: MyType[float]
+var s: MySimpleType
+
+echo "############"
+echo "#### gt ####"
+echo "############"
+
+macro gt(a: typed): string =
+  let b = a.getType
+  var str = "gt(" & $a & "):\t" & b.repr
+  if b.kind == nnkSym: # bad predicat to check weather the type has an implementation
+    str = str & ", " & b.getType.repr  # append the implementation to the result
+  result = newLit(str)
+
+echo gt(Foo) # typeDesc[Foo]
+echo gt(Bar) # typeDesc[Bar]
+echo gt(Baz) # typeDesc[int]     shouldn't it be typeDesc[Baz]?
+echo gt(foo) # distinct[int]     I would prefer Foo, distinct[int]
+echo gt(bar) # distinct[int]     I would prefer Bar, distinct[int]
+echo gt(baz) # int, int          I would prefer Baz, int
+
+echo gt(v)   # seq[int], ok
+echo gt(vv)  # seq[float], ok
+echo gt(t)   # MyType, distinct[tuple[int, int]]      I would prefer MyType[int],   distinct[tuple[int, int]]
+echo gt(tt)  # MyType, distinct[tuple[float, float]]  I would prefer MyType[float], distinct[tuple[int, int]]
+echo gt(s)   # distinct[tuple[int, int]]              I would prefer MySimpleType, distinct[tuple[int,int]]
+
+echo "#############"
+echo "#### gt2 ####"
+echo "#############"
+
+# get type name via typetraits
+
+macro gt2(a: typed): string =
+  let prefix = "gt2(" & $a & "): \t"
+  result = quote do:
+    `prefix` & `a`.type.name
+
+echo gt2(Foo) # Foo  shouldn't this be typeDesc[Foo] ?
+echo gt2(Bar) # Bar  shouldn't this be typeDesc[Bar] ?
+echo gt2(Baz) # Baz  shouldn't this be typeDesc[Baz] ?
+echo gt2(foo) # Foo
+echo gt2(bar) # Bar
+echo gt2(baz) # Baz
+
+echo gt2(v)   # seq[int]
+echo gt2(vv)  # seq[float]
+echo gt2(t)   # MyType[system.int]      why is it system.int and not just int like in seq?
+echo gt2(tt)  # MyType[system.float]    why is it system.float and not just float like in seq?
+echo gt2(s)   # MySimpleType
diff --git a/tests/metatype/twildtypedesc.nim b/tests/metatype/twildtypedesc.nim
new file mode 100644
index 000000000..268bff0d8
--- /dev/null
+++ b/tests/metatype/twildtypedesc.nim
@@ -0,0 +1,43 @@
+discard """
+  output: '''123
+123
+123
+123
+123
+123'''
+"""
+
+import strutils
+
+proc unpack(t: typedesc[string], v: string): string = $v
+proc unpack(t: typedesc[int], v: string): int = parseInt(v)
+
+proc unpack[T](v: string): T =
+  unpack T, v
+
+var s = "123"
+
+assert(unpack[string](s) is string)
+assert(unpack[int](s) is int)
+
+echo unpack[int](s)
+echo unpack[string](s)
+
+echo unpack(int,s)
+echo unpack(string,s)
+
+template `as`*(x: untyped, t: typedesc): untyped = unpack(t,x)
+
+echo s as int
+echo s as string
+
+# bug #4534
+
+proc unit(t: typedesc[int]): t = 0
+proc unit(t: typedesc[string]): t = ""
+proc unit(t: typedesc[float]): t = 0.0
+
+assert unit(int) == 0
+assert unit(string) == ""
+assert unit(float) == 0.0
+