Skip to content

Commit

Permalink
Merge pull request #45 from eliastor/feature/toml
Browse files Browse the repository at this point in the history
some more toml cases in tests
  • Loading branch information
Geoffrey J. Teale authored Sep 19, 2019
2 parents 63a14d4 + 2666580 commit fd18d35
Showing 1 changed file with 64 additions and 13 deletions.
77 changes: 64 additions & 13 deletions backend/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,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) {
Expand All @@ -59,7 +62,7 @@ func TestFileBackend(t *testing.T) {
}`)
defer cleanup()

testLoad(t, path)
testLoad(t, path, &config{}, &ekv)
})

t.Run("YAML", func(t *testing.T) {
Expand All @@ -70,18 +73,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) {
Expand Down

0 comments on commit fd18d35

Please sign in to comment.