From 4aae29e4bf2648e97dd38d1d732d5a02e9b03264 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Tue, 3 Jul 2018 19:46:54 +0200 Subject: [PATCH] Use file contents for config test --- config/config_test.go | 95 +++++++++++++++++++++++++++++++------------ config/example.toml | 8 ---- 2 files changed, 68 insertions(+), 35 deletions(-) delete mode 100644 config/example.toml diff --git a/config/config_test.go b/config/config_test.go index 6637632..44efb06 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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` diff --git a/config/example.toml b/config/example.toml deleted file mode 100644 index 0fcc062..0000000 --- a/config/example.toml +++ /dev/null @@ -1,8 +0,0 @@ -[numbers] -ary = [1, 2, 3] -i = 123 - -[words] -foo = "bar" -[words.list] -foo = ["buzz", "fizz"] \ No newline at end of file