added missing test files

This commit is contained in:
Andreas Rumpf
2016-08-26 14:49:57 +02:00
parent 98859a7248
commit 34dd08e9ae
2 changed files with 110 additions and 0 deletions

View File

@@ -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

View File

@@ -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