Skip to content

Commit

Permalink
Merge pull request #6 from luizvnasc/enhancement/tests2
Browse files Browse the repository at this point in the history
Enhancement/tests2
  • Loading branch information
luizvnasc authored Dec 16, 2019
2 parents 7c87f1a + b1a7ce8 commit b318aa4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 55 deletions.
4 changes: 2 additions & 2 deletions gonfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Load(path string, config interface{}) error {
if err != nil {
return LoadError
}
unmarshaler, err := getUnmarchaler(path)
unmarshaler, err := getUnmarshaler(path)
if err != nil {
return err
}
Expand All @@ -28,7 +28,7 @@ func Load(path string, config interface{}) error {
return nil
}

func getUnmarchaler(path string) (unmarshalerFunc, error) {
func getUnmarshaler(path string) (unmarshalerFunc, error) {
ext := filepath.Ext(path)

switch ext {
Expand Down
115 changes: 62 additions & 53 deletions gonfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
validXMLFile = "./test/config.xml"
invalidXMLFile = "./test/invalid.xml"
invalidXMLBodyFile = "./test/invalid_config.xml"
unsupportedFile = "./test/config.xyz"
)

type SomeConfiguration struct {
Expand All @@ -37,69 +38,77 @@ func init() {
}
json.Unmarshal(b, &configValid)
}
func TestLoadJSON(t *testing.T) {
func TestGonfig(t *testing.T) {
config := SomeConfiguration{}
t.Run("JSON tests", func(t *testing.T) {
t.Run("Load a configuration from a valid json file", func(t *testing.T) {
err := gonfig.Load(validJsonFile, &config)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
if !reflect.DeepEqual(config, configValid) {
t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config)
}
})

t.Run("Load a configuration from a valid json file", func(t *testing.T) {
err := gonfig.Load(validJsonFile, &config)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
if !reflect.DeepEqual(config, configValid) {
t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config)
}
})
t.Run("Load a configuration from an invalid json file", func(t *testing.T) {
err := gonfig.Load(invalidJsonFile, &config)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
})

t.Run("Load a configuration from an invalid json file", func(t *testing.T) {
err := gonfig.Load(invalidJsonFile, &config)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
t.Run("Load a configuration from an invalid json body", func(t *testing.T) {
err := gonfig.Load(invalidJsonBodyFile, &config)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
})
})
t.Run("XML tests", func(t *testing.T) {
t.Run("Load a configuration from a valid xml file", func(t *testing.T) {
err := gonfig.Load(validXMLFile, &config)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
if !reflect.DeepEqual(config, configValid) {
t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config)
}
})
t.Run("Load a configuration from an invalid json file", func(t *testing.T) {
err := gonfig.Load(invalidXMLFile, &config)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
})

t.Run("Load a configuration from an invalid json body", func(t *testing.T) {
err := gonfig.Load(invalidJsonBodyFile, &config)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
t.Run("Load a configuration from an invalid json body", func(t *testing.T) {
err := gonfig.Load(invalidXMLBodyFile, &config)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
})
})
}

func TestLoadXML(t *testing.T) {
config := SomeConfiguration{}

t.Run("Load a configuration from a valid xml file", func(t *testing.T) {
err := gonfig.Load(validXMLFile, &config)
if err != nil {
t.Errorf("Error loading the configuration: %v", err)
}
if !reflect.DeepEqual(config, configValid) {
t.Errorf("Error loading the configuration: expected %v, got %v", configValid, config)
}
})
t.Run("Load a configuration from an invalid json file", func(t *testing.T) {
err := gonfig.Load(invalidXMLFile, &config)
t.Run("Unsupported file", func(t *testing.T) {
err := gonfig.Load(unsupportedFile, &config)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
if err != gonfig.UnsupportedFileError {
t.Errorf("Expected the error %v, got %v", gonfig.UnsupportedFileError, err)
}
})

t.Run("Load a configuration from an invalid json body", func(t *testing.T) {
err := gonfig.Load(invalidXMLBodyFile, &config)
if err == nil {
t.Errorf("It was expected to get an error. Got nil")
}
if err != gonfig.LoadError {
t.Errorf("Expected the error %v, got %v", gonfig.LoadError, err)
}
})
}
1 change: 1 addition & 0 deletions test/config.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unsupported file

0 comments on commit b318aa4

Please sign in to comment.