add some tests

This commit is contained in:
ringabout
2023-03-23 21:47:17 +08:00
parent 0175be50a9
commit 251fec94ce

View File

@@ -0,0 +1,78 @@
type
Vector = object
a: int = 999
b, c: int
block: # positional construction
## It specifies all the unnamed parameters
var x = Vector(1, 2, 3)
doAssert x.b == 2
block:
## unnamed parameters can be mixed with named parameters
block:
var x = Vector(a: 1, 2, 3)
doAssert x.c == 3
block:
var x = Vector(1, b: 2, 3)
doAssert x.c == 3
block:
var x = Vector(1, 2, c: 3)
doAssert x.c == 3
block:
## Parameters can be omitted before a named parameter
var x = Vector(b: 2, 3)
doAssert x.c == 3
block:
## Object variants support unnamed parameters for tags, which should be known at the compile time.
type
Color = enum
Red, Blue, Yellow
Factor = object
id: int
case flag: Color
of Red:
num: int
of Blue, Yellow:
done: bool
name: string
block:
var x = Factor(1, Red, 2, "1314")
doAssert x.num == 2
block:
var x = Factor(1, Blue, true, "1314")
doAssert x.done == true
block:
var x = Factor(1, Yellow, false, "1314")
doAssert x.done == false
type
Ciao = object
id: int
case flag: bool = false
of true:
num: int
of false:
done: bool
name: string
block:
var x = Ciao(12, false, false, "123")
doAssert x.num == false
block:
var x = Ciao(12, flag: true, 1, "123")
doAssert x.num == 1
block:
var x = Ciao(flag: true, 1, "123")
doAssert x.num == 1