""" The `dynamic` type crossed with statements. """ class A get foo as String return 'foo' class E inherits Exception pass class Test def main .testOthers .testUnpack def testOthers # to-do: break this up into methods x as dynamic = 1 xq as dynamic? y as dynamic = 2 yq as dynamic? last as dynamic? # `for` loop statement for i in [1, 2, 3] assert i for x in [1, 2, 3] assert x x = [1, 2, 3] for i in x assert i for y in x assert y for xq in [1, 2, 3] assert xq xq = [1, 2, 3] for i in xq assert i for yq in xq assert yq for x in 10 assert x inherits int assert x >= 0 and x < 10 last = x assert last == 9 and x == 10 for xq in 10 assert xq inherits int assert xq >= 0 and xq < 10 last = xq assert last == 9 and xq == 10 for x in 2 : 10 assert x >= 2 and x <= 10 for xq in 2 : 10 assert xq >= 2 and x <= 10 da as dynamic = 1 for di in da : 10 assert di > 0 and di < 10 db as dynamic = 10 for dj in 1 : db assert dj > 0 and dj < db for dk in da : db assert dk > 0 and dk >= da and dk < db for x = 2 .. 10 assert x >= 2 and x <= 10 for xq = 2 .. 10 assert xq >= 2 and x <= 10 da as dynamic = 1 for di = da .. 10 assert di > 0 and di < 10 db as dynamic = 10 for dj = 1 .. db assert dj > 0 and dj < db for dk = da .. db assert dk > 0 and dk >= da and dk < db # if-inherits x = 5 visited = 0 if x inherits A visited += 1 assert x.foo == 'foo' assert visited == 0 x = A() if x inherits A visited += 1 assert x.foo == 'foo' assert visited == 1 xq = 5 if xq inherits A visited += 1 assert x.foo == 'foo' assert visited == 1 xq = A() if xq inherits A visited += 1 assert x.foo == 'foo' assert visited == 2 # if-not-nil xq = nil visited = 0 if xq visited += 1 assert visited == 0 xq = 'aoeu' if xq visited += 1 assert visited == 1 # print x = 5 sw = StringWriter() print x print to sw, x assert sw.toString.trim == '5' x = 'aoeu' sw = StringWriter() print x print to sw, x assert sw.toString.trim == 'aoeu' # trace sw = StringWriter() CobraCore.tracer.destination = sw x = 'aoeu' xq = 5 trace x, xq prefix = CobraCore.tracer.prefix sep = CobraCore.tracer.separator assert sw.toString.startsWith("[prefix]x='aoeu'[sep]xq=5[sep]") # throw visited = 0 try e as dynamic = E() throw e catch E visited = 1 assert visited == 1 # using using xq = StringWriter() xq.write('hi') assert xq is nil # while x = 0 while x < 10 x += 1 assert x == 10 # branch assert .nameOf(1) == 'one' assert .nameOf(2) == 'two' assert .nameOf(3) == 'a few' assert .nameOf(4) == 'a few' assert .nameOf(5) == 'many' assert .nameOf(1.0d) == 'one' assert .nameOf(2.0d) == 'two' assert .nameOf(3.0d) == 'a few' assert .nameOf(4.0d) == 'a few' assert .nameOf(5.0d) == 'many' def testUnpack t = [1, 2] to dynamic a, b = t assert a == 1 and b == 2 p1 = Pair('foo', 'bar') to dynamic c, d = p1 assert c == 'foo' and d == 'bar' p2 = Pair(2, 'two') to dynamic e, f = p2 assert e == 2 and f == 'two' def nameOf(i as dynamic) as String branch i on 1, return 'one' on 2, return 'two' on 3 or 4, return 'a few' else, return 'many' return ''