tests: cleanup if_smartcast_test.v
parent
40ed2e1b3d
commit
96c22a26b9
|
@ -2,9 +2,11 @@ struct Abc {
|
||||||
mut:
|
mut:
|
||||||
val string
|
val string
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Xyz {
|
struct Xyz {
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Alphabet = Abc | Xyz
|
type Alphabet = Abc | Xyz
|
||||||
|
|
||||||
fn test_if_smartcast() {
|
fn test_if_smartcast() {
|
||||||
|
@ -15,14 +17,14 @@ fn test_if_smartcast() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_mutable() {
|
fn test_mutable() {
|
||||||
mut x := Alphabet(Abc{'test'})
|
mut x := Alphabet(Abc{'original'})
|
||||||
if x is Abc {
|
if x is Abc {
|
||||||
y := Abc{}
|
assert x.val == 'original'
|
||||||
mut mx := x
|
x.val = 'changed'
|
||||||
mx = &y
|
assert x.val == 'changed'
|
||||||
assert mx == &y
|
}
|
||||||
assert u64(mx) == u64(&y)
|
if x is Abc {
|
||||||
assert u64(x) != u64(&y)
|
assert x.val == 'changed'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,39 +45,72 @@ fn test_as_cast() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Test {
|
struct Container {
|
||||||
abc Alphabet
|
abc Alphabet
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_mutable_with_struct() {
|
fn test_mutable_with_struct() {
|
||||||
mut x := Test{Abc{'test'}}
|
mut c := Container{Abc{'original'}}
|
||||||
if x.abc is Abc as test {
|
if c.abc is Abc as abc {
|
||||||
mut ttt := test
|
assert abc.val == 'original'
|
||||||
assert u64(ttt) == u64(ttt)
|
mut mabc := abc
|
||||||
ttt.val = 'test'
|
// NB: since `abc` is a pointer,
|
||||||
assert ttt.val == 'test'
|
// `mabc` points to the same data:
|
||||||
|
assert mabc.val == 'original'
|
||||||
|
// Modifying `mabc`, modifies the data of abc too.
|
||||||
|
mabc.val = 'xyz'
|
||||||
|
assert abc.val == 'xyz'
|
||||||
|
}
|
||||||
|
if c.abc is Abc as another {
|
||||||
|
// NB: in this second smart cast, `another` is
|
||||||
|
// the same wrapped value, that was changed in
|
||||||
|
// the first smart cast:
|
||||||
|
assert another.val == 'xyz'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_as_cast_with_struct() {
|
fn test_as_cast_with_struct() {
|
||||||
x := Test{Abc{'test'}}
|
x := Container{Abc{'test'}}
|
||||||
if x.abc is Abc as test {
|
if x.abc is Abc as test {
|
||||||
assert test.val == 'test'
|
assert test.val == 'test'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CellStr { str string }
|
struct CellStr {
|
||||||
struct CellInt { itg i64 }
|
str string
|
||||||
struct CellFloat { flt f64 }
|
}
|
||||||
type Cell = CellStr | CellInt | CellFloat
|
|
||||||
|
struct CellInt {
|
||||||
|
itg i64
|
||||||
|
}
|
||||||
|
|
||||||
|
struct CellFloat {
|
||||||
|
flt f64
|
||||||
|
}
|
||||||
|
|
||||||
|
struct CellU32 {
|
||||||
|
u u32
|
||||||
|
}
|
||||||
|
|
||||||
|
type Cell = CellFloat | CellInt | CellStr | CellU32
|
||||||
|
|
||||||
fn test_mutability() {
|
fn test_mutability() {
|
||||||
my_str := 'the quick brown fox jumps over the lazy dog.'
|
my_str := 'the quick brown fox jumps over the lazy dog.'
|
||||||
my_itg := -1234567890
|
my_itg := -1234567890
|
||||||
my_flt := 3.14159265358979323846
|
my_flt := 3.14159265358979323846
|
||||||
my_u32 := u32(4294967296)
|
my_u32 := u32(4294967295)
|
||||||
cell_str := CellStr{ str: my_str }
|
cell_str := CellStr{
|
||||||
cell_itg := CellInt{ itg: my_itg }
|
str: my_str
|
||||||
cell_flt := CellFloat{ flt: my_flt }
|
}
|
||||||
|
cell_itg := CellInt{
|
||||||
|
itg: my_itg
|
||||||
|
}
|
||||||
|
cell_flt := CellFloat{
|
||||||
|
flt: my_flt
|
||||||
|
}
|
||||||
|
cell_u32 := CellU32{
|
||||||
|
u: my_u32
|
||||||
|
}
|
||||||
mut cell := Cell{}
|
mut cell := Cell{}
|
||||||
cell = cell_str
|
cell = cell_str
|
||||||
if cell is CellStr {
|
if cell is CellStr {
|
||||||
|
@ -85,4 +120,12 @@ fn test_mutability() {
|
||||||
if cell is CellInt {
|
if cell is CellInt {
|
||||||
println('$cell.itg')
|
println('$cell.itg')
|
||||||
}
|
}
|
||||||
|
cell = cell_flt
|
||||||
|
if cell is CellFloat {
|
||||||
|
println('$cell.flt')
|
||||||
|
}
|
||||||
|
cell = cell_u32
|
||||||
|
if cell is CellU32 {
|
||||||
|
println('$cell.u')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue