From 266658077ac39d601f4feb313d6377f16b48c4aa Mon Sep 17 00:00:00 2001 From: b0000000000000t Date: Sat, 22 Sep 2018 15:58:11 +0600 Subject: [PATCH] some more toml cases in tests --- backend/file/file_test.go | 77 ++++++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 13 deletions(-) diff --git a/backend/file/file_test.go b/backend/file/file_test.go index dcced62..43c4450 100644 --- a/backend/file/file_test.go +++ b/backend/file/file_test.go @@ -39,15 +39,18 @@ func TestFileBackend(t *testing.T) { Timeout time.Duration } - testLoad := func(t *testing.T, path string) { - var c config + ekv := config{ + Name: "some name", + Age: 10, + Timeout: 10, + } + + testLoad := func(t *testing.T, path string, template interface{}, expected interface{}) { b := file.NewBackend(path) - err := b.Unmarshal(context.Background(), &c) + err := b.Unmarshal(context.Background(), template) require.NoError(t, err) - require.Equal(t, "some name", c.Name) - require.Equal(t, 10, c.Age) - require.Equal(t, 10*time.Nanosecond, c.Timeout) + require.EqualValues(t, expected, template) } t.Run("JSON", func(t *testing.T) { @@ -58,7 +61,7 @@ func TestFileBackend(t *testing.T) { }`) defer cleanup() - testLoad(t, path) + testLoad(t, path, &config{}, &ekv) }) t.Run("YAML", func(t *testing.T) { @@ -69,18 +72,66 @@ func TestFileBackend(t *testing.T) { `) defer cleanup() - testLoad(t, path) + testLoad(t, path, &config{}, &ekv) }) - t.Run("TOML", func(t *testing.T) { - path, cleanup := createTempFile(t, "config.toml", - `name = "some name" + + t.Run("Simple KV", func(t *testing.T) { + path, cleanup := createTempFile(t, "config.toml", + `name = "some name" age = 10 timeout = 10 `) - defer cleanup() + defer cleanup() + + testLoad(t, path, &config{}, &ekv) + }) + + t.Run("TOML Table", func(t *testing.T) { + path, cleanup := createTempFile(t, "config.toml", + `title = "title!" +[Config] +name = "some name" +age = 10 +timeout = 10 +`) + defer cleanup() + type Config struct { + Title string + Config config + } + e := Config{ + Title: "title!", + Config: config{ + Name: "some name", + Age: 10, + Timeout: 10, + }, + } + + testLoad(t, path, &Config{}, &e) + }) + + t.Run("TOML Array of Tables", func(t *testing.T) { + path, cleanup := createTempFile(t, "config.toml", + `[[Config]] +name = "Alice" +age = 10 +timeout = 10 +[[Config]] +name = "Bob" +age = 11 +timeout = 11 +`) + defer cleanup() + type Configs struct { + Config []config + } + e := Configs{Config: []config{{Name: "Alice", Age: 10, Timeout: 10}, {Name: "Bob", Age: 11, Timeout: 11}}} + + testLoad(t, path, &Configs{}, &e) + }) - testLoad(t, path) }) t.Run("Unsupported extension", func(t *testing.T) {