1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/v/tests/for_t_fields_with_comptime_if_test.v
2022-04-15 18:25:45 +03:00

55 lines
796 B
V

struct Abc {
a u8
b u8
c int
}
fn decode<T>() T {
mut x := T{}
$for field in T.fields {
$if field.typ is u8 {
x.$(field.name) = 1
} $else {
x.$(field.name) = 3
}
if x.$(field.name) == 1 {
x.$(field.name) = 5
}
}
return x
}
fn test_decode() {
abc := decode<Abc>()
assert abc.a == 5
assert abc.b == 5
assert abc.c == 3
}
struct Abc2 {
an_int int
a_byte u8
a_string string
}
fn decode2<T>() T {
mut x := T{}
$for field in T.fields {
$if field.typ is u8 {
x.$(field.name) = u8(-1)
} $else $if field.typ is int {
x.$(field.name) = int(-1)
} $else $if field.typ is string {
x.$(field.name) = 'hi'
}
}
return x
}
fn test_decode2() {
abc := decode2<Abc2>()
assert abc.an_int == -1
assert abc.a_byte == 0xff
assert abc.a_string == 'hi'
}