diff options
Diffstat (limited to 'tests/stdlib/twith.nim')
-rw-r--r-- | tests/stdlib/twith.nim | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/stdlib/twith.nim b/tests/stdlib/twith.nim new file mode 100644 index 000000000..ceadbe7bf --- /dev/null +++ b/tests/stdlib/twith.nim @@ -0,0 +1,44 @@ +discard """ + matrix: "--mm:refc; --mm:orc" +""" + +import std/with +import std/[assertions, formatfloat] + +type + Foo = object + col, pos: string + name: string + +proc setColor(f: var Foo; r, g, b: int) = f.col = $(r, g, b) +proc setPosition(f: var Foo; x, y: float) = f.pos = $(x, y) + +var f: Foo +with(f, setColor(2, 3, 4), setPosition(0.0, 1.0)) +doAssert f.col == "(2, 3, 4)" +doAssert f.pos == "(0.0, 1.0)" + +f = Foo() +with f: + col = $(2, 3, 4) + pos = $(0.0, 1.0) + _.name = "bar" +doAssert f.col == "(2, 3, 4)" +doAssert f.pos == "(0.0, 1.0)" +doAssert f.name == "bar" + +type + Baz* = object + a*, b*: int + Bar* = object + x*: int + baz*: Baz + +var bar: Bar +with bar: + x = 1 + with baz: + a = 2 + +doAssert bar.x == 1 +doAssert bar.baz.a == 2 |