fix: fixed compilation; added some tests

main
Jef Roosens 2022-12-28 17:01:51 +01:00
parent 10462919e8
commit 7e9af5c6c8
5 changed files with 54 additions and 4 deletions

10
conf.v
View File

@ -72,8 +72,9 @@ pub fn load<T>(conf LoadConfig) !T {
res.$(field.name) = s.string()
} $else $if field.typ is int {
res.$(field.name) = s.int()
} $else {
$compile_error('Unsupported config struct field type detected.')
// This seems to not work in V 0.3.2
//} $else {
// $compile_error('Unsupported config struct field type detected.')
}
has_value[field.name] = true
@ -91,8 +92,9 @@ pub fn load<T>(conf LoadConfig) !T {
res.$(field.name) = env_value
} $else $if field.typ is int {
res.$(field.name) = env_value.int()
} $else {
$compile_error('Unsupported config struct field type detected.')
// This seems to not work in V 0.3.2
//} $else {
// $compile_error('Unsupported config struct field type detected.')
}
has_value[field.name] = true

43
conf_test.v 100644
View File

@ -0,0 +1,43 @@
module conf
struct SimpleConf {
some_int int
some_string string
}
struct SimpleConfDefaults {
some_int int = 3
some_string string = 'hi'
}
fn test_simple() {
conf := load<SimpleConf>(default_path: 'test/test_simple.toml')!
assert conf == SimpleConf{
some_int: 2
some_string: 'hi'
}
}
fn test_zeroed() {
conf := load<SimpleConf>(default_path: 'test/test_zeroed.toml')!
assert conf == SimpleConf{
some_int: 0
some_string: ''
}
}
fn test_zeroed_defaults() {
conf := load<SimpleConfDefaults>(default_path: 'test/test_zeroed.toml')!
assert conf == SimpleConfDefaults{
some_int: 0
some_string: ''
}
}
fn test_defaults() {
conf := load<SimpleConfDefaults>(default_path: 'test/test_single_value.toml')!
assert conf == SimpleConfDefaults{
some_int: 3
some_string: 'hi'
}
}

View File

@ -0,0 +1,2 @@
some_int = 2
some_string = "hi"

View File

@ -0,0 +1 @@
some_string = "hi"

View File

@ -0,0 +1,2 @@
some_int = 0
some_string = ""