aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2021-09-15 21:22:02 +0200
committerJulien Dessaux2021-09-15 21:22:02 +0200
commit84ba078a40cd2ed127b6df5137b1e0bd2389b2f3 (patch)
treeba39a5798e70f8ad214a8b594b0fb8a0cbef0d26
parentFinished rewriting database package tests for consistency (diff)
downloadtrains-84ba078a40cd2ed127b6df5137b1e0bd2389b2f3.tar.gz
trains-84ba078a40cd2ed127b6df5137b1e0bd2389b2f3.tar.bz2
trains-84ba078a40cd2ed127b6df5137b1e0bd2389b2f3.zip
Rewrote config tests for consistency
-rw-r--r--pkg/config/config_test.go20
-rw-r--r--pkg/config/error.go30
-rw-r--r--pkg/config/error_test.go11
3 files changed, 33 insertions, 28 deletions
diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go
index 43d4a48..ee130f3 100644
--- a/pkg/config/config_test.go
+++ b/pkg/config/config_test.go
@@ -1,10 +1,8 @@
package config
import (
- "reflect"
"testing"
- "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -35,14 +33,14 @@ func TestLoadFile(t *testing.T) {
name string
input string
expected *Config
- expectedError interface{}
+ expectedError error
}{
- {"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{}},
+ {"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},
{"Complete config", "test_data/complete.yaml", &completeConfig, nil},
@@ -52,12 +50,12 @@ func TestLoadFile(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))
+ requireErrorTypeMatch(t, err, tc.expectedError)
require.Nil(t, valid)
} else {
require.NoError(t, err)
}
- assert.Equal(t, tc.expected, valid, "Invalid value")
+ require.Equal(t, tc.expected, valid, "Invalid value")
})
}
}
diff --git a/pkg/config/error.go b/pkg/config/error.go
index c49b6a9..1fc1665 100644
--- a/pkg/config/error.go
+++ b/pkg/config/error.go
@@ -2,21 +2,19 @@ package config
import "fmt"
-type ErrorType int
-
// file open configuration file error
type OpenError struct {
path string
err error
}
-func (e *OpenError) Error() string {
+func (e OpenError) Error() string {
return fmt.Sprintf("Failed to open configuration file : %s", e.path)
}
-func (e *OpenError) Unwrap() error { return e.err }
+func (e OpenError) Unwrap() error { return e.err }
func newOpenError(path string, err error) error {
- return &OpenError{
+ return OpenError{
path: path,
err: err,
}
@@ -28,13 +26,13 @@ type DecodeError struct {
err error
}
-func (e *DecodeError) Error() string {
+func (e DecodeError) Error() string {
return fmt.Sprintf("Failed to decode configuration file : %s", e.path)
}
-func (e *DecodeError) Unwrap() error { return e.err }
+func (e DecodeError) Unwrap() error { return e.err }
func newDecodeError(path string, err error) error {
- return &DecodeError{
+ return DecodeError{
path: path,
err: err,
}
@@ -46,13 +44,13 @@ type InvalidAddressError struct {
err error
}
-func (e *InvalidAddressError) Error() string {
+func (e InvalidAddressError) Error() string {
return fmt.Sprintf("Invalid address %s : it must be a valid ipv4 address, ipv6 address, or resolvable name", e.address)
}
-func (e *InvalidAddressError) Unwrap() error { return e.err }
+func (e InvalidAddressError) Unwrap() error { return e.err }
func newInvalidAddressError(address string, err error) error {
- return &InvalidAddressError{
+ return InvalidAddressError{
address: address,
err: err,
}
@@ -64,13 +62,13 @@ type InvalidPortError struct {
err error
}
-func (e *InvalidPortError) Error() string {
+func (e InvalidPortError) Error() string {
return fmt.Sprintf("Invalid port %s : it must be a valid port number or tcp service name", e.port)
}
-func (e *InvalidPortError) Unwrap() error { return e.err }
+func (e InvalidPortError) Unwrap() error { return e.err }
func newInvalidPortError(port string, err error) error {
- return &InvalidPortError{
+ return InvalidPortError{
port: port,
err: err,
}
@@ -81,12 +79,12 @@ type InvalidTokenError struct {
token string
}
-func (e *InvalidTokenError) Error() string {
+func (e InvalidTokenError) Error() string {
return fmt.Sprintf("Invalid token %s : it must be an hexadecimal string that lookslike 12345678-9abc-def0-1234-56789abcdef0", e.token)
}
func newInvalidTokenError(token string) error {
- return &InvalidTokenError{
+ return InvalidTokenError{
token: token,
}
}
diff --git a/pkg/config/error_test.go b/pkg/config/error_test.go
index f9807c1..e3be1a8 100644
--- a/pkg/config/error_test.go
+++ b/pkg/config/error_test.go
@@ -1,6 +1,15 @@
package config
-import "testing"
+import (
+ "reflect"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func requireErrorTypeMatch(t *testing.T, err error, expected error) {
+ require.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(expected), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(expected))
+}
func TestErrorsCoverage(t *testing.T) {
openErr := OpenError{}