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) {
type numbers struct {
Ary []int `toml:"ary"`
I int `toml:"i"`
type superEmbed struct {
B int `toml:"b"`
}
var nums numbers
Require("numbers", &nums)
numsExp := numbers{
Ary: []int{1, 2, 3},
I: 123,
type embed struct {
A int `toml:"a"`
Super superEmbed `toml:"super"`
}
type words struct {
Foo string `toml:"foo"`
List struct {
Foo []string `toml:"foo"`
} `toml:"list"`
type everything struct {
Int int `toml:"int"`
Int64 int64 `toml:"int64"`
Uint uint `toml:"uint"`
Uint16 uint16 `toml:"uint16"`
Str string `toml:"str"`
Slice []string `toml:"slice"`
Map map[string]uint `toml:"map"`
Embed embed `toml:"embed"`
}
var w words
Require("words", &w)
wordsExp := words{
Foo: "bar",
List: struct {
Foo []string `toml:"foo"`
}{
Foo: []string{"buzz", "fizz"},
var e everything
Require("everything", &e)
exp := everything{
Int: 1,
Int64: 2,
Uint: 3,
Uint16: 4,
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 {
t.Fatalf("Failed to load config: %v", err)
}
if !cmp.Equal(numsExp, nums) {
t.Errorf("Numbers mismatch: %s", cmp.Diff(numsExp, nums))
if !cmp.Equal(exp, e) {
t.Errorf("Numbers mismatch: %s", cmp.Diff(exp, e))
}
if !cmp.Equal(wordsExp, w) {
t.Errorf("Words mismatch: %s", cmp.Diff(wordsExp, w))
if !cmp.Equal(eexp, x) {
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"]