nim-stew/tests/test_objects.nim

68 lines
1.2 KiB
Nim
Raw Normal View History

2020-04-22 12:41:42 +00:00
import
unittest, typetraits,
2020-03-29 15:34:45 +00:00
../stew/objects
when defined(nimHasUsed):
{.used.}
{.experimental: "notnil".}
2020-03-29 15:34:45 +00:00
suite "Objects":
test "baseType":
type
Foo = ref object of RootObj
Bar = ref object of Foo
Baz = object of RootObj
Bob = object of Baz
Bill = ref object of Bob
var
foo = Foo()
bar = Bar()
baz = Baz()
bob = Bob()
bill = Bill()
when defined(nimTypeNames):
check:
foo.baseType == "Foo:ObjectType"
bar.baseType == "Bar:ObjectType"
baz.baseType == "Baz"
bob.baseType == "Bob"
bill.baseType == "Bill:ObjectType"
proc f(o: Foo) =
check $o.type == "Foo"
check o.baseType == "Bar:ObjectType"
f(bar)
2020-04-22 12:41:42 +00:00
test "declval":
type
Bar = object
x: RootRef not nil
2020-04-22 12:41:42 +00:00
proc foo(x: int): string =
discard
proc foo(x: var int): float =
discard
proc foo(x: Bar): int =
discard
2020-04-22 12:41:42 +00:00
type
T1 = typeof foo(declval(int))
T2 = typeof foo(declval(var int))
T3 = typeof foo(declval(lent int))
T4 = typeof foo(declval(Bar))
T5 = typeof foo(declval(var Bar))
2020-04-22 12:41:42 +00:00
check:
T1 is string
T2 is float
T3 is string
T4 is int
T5 is int
2020-04-22 12:41:42 +00:00