aboutsummaryrefslogtreecommitdiff
path: root/config/config_test.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--config/config_test.go80
1 files changed, 33 insertions, 47 deletions
diff --git a/config/config_test.go b/config/config_test.go
index 0b78260..3904b5d 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -3,66 +3,52 @@ package config
import (
"reflect"
"testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func TestLoadFile(t *testing.T) {
- // Non existant file
- _, err := LoadFile("test_data/non-existant")
- if err == nil {
- t.Fatal("non-existant config file failed without error")
- }
-
- // Invalid yaml file
- _, err = LoadFile("test_data/invalid_yaml")
- if err == nil {
- t.Fatal("invalid_yaml config file failed without error")
- }
-
- // Invalid address
- if _, err = LoadFile("test_data/invalid_address.yaml"); err == nil {
- t.Fatal("Invalid address should fail to load")
- }
-
- // Invalid address unreasolvable
- if _, err = LoadFile("test_data/invalid_address_unresolvable.yaml"); err == nil {
- t.Fatal("Unresolvable address should fail to load")
- }
-
- // Invalid port
- if _, err = LoadFile("test_data/invalid_port.yaml"); err == nil {
- t.Fatal("Invalid port should fail to load")
- }
-
- // Invalid token
- if _, err = LoadFile("test_data/invalid_token.yaml"); err == nil {
- t.Fatal("Invalid token should fail to load")
- }
-
// Minimal yaml file
- want := Config{
+ minimalConfig := Config{
Address: "127.0.0.2",
Port: "8082",
Token: "12345678-9abc-def0-1234-56789abcdef0",
}
- config, err := LoadFile("test_data/minimal.yaml")
- if err != nil {
- t.Fatalf("minimal example failed with error: %v", err)
- }
- if config != nil && !reflect.DeepEqual(want, *config) {
- t.Fatalf("minimal example failed:\nwant:%+v\ngot: %+v", want, *config)
- }
// Minimal yaml file with hostname resolving
- want = Config{
+ minimalConfigWithResolving := Config{
Address: "localhost",
Port: "8082",
Token: "12345678-9abc-def0-1234-56789abcdef0",
}
- config, err = LoadFile("test_data/minimal_with_hostname.yaml")
- if err != nil {
- t.Fatalf("minimal example failed with error: %v", err)
- }
- if config != nil && !reflect.DeepEqual(want, *config) {
- t.Fatalf("minimal example failed:\nwant:%+v\ngot: %+v", want, *config)
+
+ // Test cases
+ testCases := []struct {
+ name string
+ input string
+ expected *Config
+ expectedError interface{}
+ }{
+ {"Non existant file", "test_data/non-existant", nil, &OpenError{}},
+ {"Invalid file content", "test_data/invalid.yaml", nil, &DecodeError{}},
+ {"Invalid address should fail to load", "test_data/invalid_address.yaml", nil, &InvalidAddressError{}},
+ {"Unresolvable address should fail to load", "test_data/invalid_address_unresolvable.yaml", nil, &InvalidAddressError{}},
+ {"Invalid port should fail to load", "test_data/invalid_port.yaml", nil, &InvalidPortError{}},
+ {"Invalid token should fail to load", "test_data/invalid_token.yaml", nil, &InvalidTokenError{}},
+ {"Minimal config", "test_data/minimal.yaml", &minimalConfig, nil},
+ {"Minimal config with resolving", "test_data/minimal_with_hostname.yaml", &minimalConfigWithResolving, nil},
+ }
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ valid, err := LoadFile(tc.input)
+ if tc.expectedError != nil {
+ require.Error(t, err)
+ assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError))
+ } else {
+ require.NoError(t, err)
+ }
+ assert.Equal(t, tc.expected, valid, "Invalid value")
+ })
}
}