1
0
Fork 0

Use file contents for config test

This commit is contained in:
Gregory Eremin 2018-07-03 19:46:54 +02:00
parent 32b5607235
commit 4aae29e4bf
2 changed files with 68 additions and 35 deletions

View File

@ -7,42 +7,83 @@ import (
) )
func TestConfig(t *testing.T) { func TestConfig(t *testing.T) {
type numbers struct { type superEmbed struct {
Ary []int `toml:"ary"` B int `toml:"b"`
I int `toml:"i"`
} }
var nums numbers type embed struct {
Require("numbers", &nums) A int `toml:"a"`
numsExp := numbers{ Super superEmbed `toml:"super"`
Ary: []int{1, 2, 3},
I: 123,
} }
type everything struct {
type words struct { Int int `toml:"int"`
Foo string `toml:"foo"` Int64 int64 `toml:"int64"`
List struct { Uint uint `toml:"uint"`
Foo []string `toml:"foo"` Uint16 uint16 `toml:"uint16"`
} `toml:"list"` Str string `toml:"str"`
Slice []string `toml:"slice"`
Map map[string]uint `toml:"map"`
Embed embed `toml:"embed"`
} }
var w words var e everything
Require("words", &w) Require("everything", &e)
wordsExp := words{ exp := everything{
Foo: "bar", Int: 1,
List: struct { Int64: 2,
Foo []string `toml:"foo"` Uint: 3,
}{ Uint16: 4,
Foo: []string{"buzz", "fizz"}, Str: "cranky",
Slice: []string{"alpha", "beta"},
Map: map[string]uint{
"a": 10,
"b": 20,
"c": 30,
},
Embed: embed{
A: 100,
Super: superEmbed{
B: 200,
},
}, },
} }
err := Load("example.toml") type extra struct {
Foo string `toml:"foo"`
}
var x extra
Require("extra", &x)
eexp := extra{Foo: "bar"}
err := Load(testConfig)
if err != nil { if err != nil {
t.Fatalf("Failed to load config: %v", err) t.Fatalf("Failed to load config: %v", err)
} }
if !cmp.Equal(numsExp, nums) { if !cmp.Equal(exp, e) {
t.Errorf("Numbers mismatch: %s", cmp.Diff(numsExp, nums)) t.Errorf("Numbers mismatch: %s", cmp.Diff(exp, e))
} }
if !cmp.Equal(wordsExp, w) { if !cmp.Equal(eexp, x) {
t.Errorf("Words mismatch: %s", cmp.Diff(wordsExp, w)) t.Errorf("Words mismatch: %s", cmp.Diff(eexp, x))
} }
} }
const testConfig = `
[everything]
int = 1
int64 = 2
uint = 3
uint16 = 4
str = "cranky"
slice = ["alpha", "beta"]
[everything.map]
a = 10
b = 20
c = 30
[everything.embed]
a = 100
[everything.embed.super]
b = 200
[extra]
foo = "bar"
[too.deep]
answer = 42`

View File

@ -1,8 +0,0 @@
[numbers]
ary = [1, 2, 3]
i = 123
[words]
foo = "bar"
[words.list]
foo = ["buzz", "fizz"]