mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
test struct that doesn't implement from_toml
and to_toml
This commit is contained in:
parent
e497872d06
commit
15987d48a8
@ -1,18 +1,40 @@
|
||||
import toml
|
||||
|
||||
enum JobTitle {
|
||||
manager
|
||||
executive
|
||||
worker
|
||||
enum Rank {
|
||||
low
|
||||
medium
|
||||
high
|
||||
}
|
||||
|
||||
struct Planet {
|
||||
name string
|
||||
population u64
|
||||
size f64
|
||||
avg_temp int
|
||||
has_water bool
|
||||
rank Rank
|
||||
}
|
||||
|
||||
struct Employee {
|
||||
pub mut:
|
||||
mut:
|
||||
name string
|
||||
age int
|
||||
salary f32
|
||||
is_human bool
|
||||
title JobTitle
|
||||
rank Rank
|
||||
}
|
||||
|
||||
fn test_encode_and_decode() {
|
||||
p := Planet{'Mars', 0, 144.8, -81, true, .high}
|
||||
s := 'name = "Mars"
|
||||
population = 0
|
||||
size = 144.8
|
||||
avg_temp = -81
|
||||
has_water = true
|
||||
rank = 2'
|
||||
|
||||
assert toml.encode[Planet](p) == s
|
||||
assert toml.decode[Planet](s)! == p
|
||||
}
|
||||
|
||||
pub fn (e Employee) to_toml() string {
|
||||
@ -21,7 +43,7 @@ pub fn (e Employee) to_toml() string {
|
||||
mp['age'] = toml.Any(e.age)
|
||||
mp['salary'] = toml.Any(e.salary)
|
||||
mp['is_human'] = toml.Any(e.is_human)
|
||||
mp['title'] = toml.Any(int(e.title))
|
||||
mp['rank'] = toml.Any(int(e.rank))
|
||||
return mp.to_toml()
|
||||
}
|
||||
|
||||
@ -31,18 +53,18 @@ pub fn (mut e Employee) from_toml(any toml.Any) {
|
||||
e.age = mp['age'] or { toml.Any(0) }.int()
|
||||
e.salary = mp['salary'] or { toml.Any(0) }.f32()
|
||||
e.is_human = mp['is_human'] or { toml.Any(false) }.bool()
|
||||
e.title = unsafe { JobTitle(mp['title'] or { toml.Any(0) }.int()) }
|
||||
e.rank = unsafe { Rank(mp['rank'] or { toml.Any(0) }.int()) }
|
||||
}
|
||||
|
||||
fn test_encode_and_decode() {
|
||||
x := Employee{'Peter', 28, 95000.5, true, .worker}
|
||||
fn test_custom_encode_and_decode() {
|
||||
x := Employee{'Peter', 28, 95000.5, true, .medium}
|
||||
s := toml.encode[Employee](x)
|
||||
eprintln('Employee x: ${s}')
|
||||
assert s == r'name = "Peter"
|
||||
age = 28
|
||||
salary = 95000.5
|
||||
is_human = true
|
||||
title = 2'
|
||||
rank = 1'
|
||||
|
||||
y := toml.decode[Employee](s) or {
|
||||
println(err)
|
||||
@ -54,5 +76,5 @@ title = 2'
|
||||
assert y.age == 28
|
||||
assert y.salary == 95000.5
|
||||
assert y.is_human == true
|
||||
assert y.title == .worker
|
||||
assert y.rank == .medium
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user