mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
add json sumtype test
This commit is contained in:
parent
bcf9bba539
commit
d5fe62b37d
1 changed files with 20 additions and 0 deletions
|
@ -105,3 +105,23 @@ fn test_sum_types() {
|
|||
j := json.encode(animals[0])
|
||||
assert j == '{"cat_name":"Whiskers","_type":"Cat"}'
|
||||
}
|
||||
|
||||
type Value = string | i32
|
||||
|
||||
struct Node {
|
||||
value Value
|
||||
}
|
||||
|
||||
fn test_sum_types_with_i32() {
|
||||
data1 := json.encode([Node{i32(128)}, Node{'mystring'}])
|
||||
assert data1 == '[{"value":128},{"value":"mystring"}]'
|
||||
|
||||
node := json.decode([]Node, data1) or {
|
||||
println(err)
|
||||
assert false
|
||||
return
|
||||
}
|
||||
assert node.len == 2
|
||||
assert node[0].value == Value(i32(128))
|
||||
assert node[1].value == Value('mystring')
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue