-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted yaml_parser into utils; Added tests for yaml_parser
- Loading branch information
Showing
3 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type ConfigTest struct { | ||
// Define the fields of your ConfigTest here | ||
Field1 string `yaml:"field1"` | ||
Field2 int `yaml:"field2"` | ||
} | ||
|
||
func TestParseYamlData(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input []byte | ||
expected *ConfigTest | ||
expectError bool | ||
}{ | ||
{ | ||
name: "Valid YAML", | ||
input: []byte(` | ||
field1: "value1" | ||
field2: 2 | ||
`), | ||
expected: &ConfigTest{ | ||
Field1: "value1", | ||
Field2: 2, | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Empty YAML", | ||
input: []byte(``), | ||
expected: &ConfigTest{}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Invalid YAML", | ||
input: []byte(` | ||
field1: "value1" | ||
field2: "invalid_int" | ||
`), | ||
expected: nil, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "Unknown Field", | ||
input: []byte(` | ||
field1: "value1" | ||
field2: 2 | ||
unknown_field: "value" | ||
`), | ||
expected: nil, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
config, err := ParseYamlData[ConfigTest](tt.input) | ||
if tt.expectError { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expected, config) | ||
} | ||
}) | ||
} | ||
} |