Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for TOML parser #155

Merged
merged 3 commits into from Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion parsers/hcl/hcl_test.go
Expand Up @@ -108,7 +108,6 @@ func TestHCL_Unmarshal(t *testing.T) {
func TestHCL_Marshal(t *testing.T) {

hclParserWithFlatten := Parser(true)
// hclParserWithoutFlatten := Parser(false)

testCases := []struct {
name string
Expand Down
139 changes: 139 additions & 0 deletions parsers/toml/toml_test.go
@@ -0,0 +1,139 @@
package toml

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestTOML_Unmarshal(t *testing.T) {
testCases := []struct {
name string
input []byte
output map[string]interface{}
isErr bool
}{
{
name: "Empty TOML",
input: []byte(``),
output: map[string]interface{}{},
},
{
name: "Valid TOML",
input: []byte(`key = "val"
name = "test"
number = 2
`),
output: map[string]interface{}{
"key": "val",
"name": "test",
"number": int64(2),
},
},
{
name: "Invalid TOML - missing end quotes",
input: []byte(`key = "val`),
isErr: true,
},
{
name: "Complex TOML - All types",
input: []byte(`array = [ 1, 2, 3 ]
boolean = true
color = "gold"
number = 123
string = "Hello World"
[object]
a = "b"
c = "d"`),
output: map[string]interface{}{
"array": []interface{}{int64(1), int64(2), int64(3)},
"boolean": true,
"color": "gold",
"number": int64(123),
"object": map[string]interface{}{"a": "b", "c": "d"},
"string": "Hello World",
},
},
{
name: "Invalid TOML - missing equal",
input: []byte(`key "val"`),
isErr: true,
},
}

tp := Parser()

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out, err := tp.Unmarshal(tc.input)
if tc.isErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tc.output, out)
}
})
}
}

func TestTOML_Marshal(t *testing.T) {
testCases := []struct {
name string
input map[string]interface{}
output []byte
isErr bool
}{
{
name: "Empty TOML",
input: map[string]interface{}{},
output: []byte(nil),
},
{
name: "Valid TOML",
input: map[string]interface{}{
"key": "val",
"name": "test",
"number": 2.0,
},
output: []byte(`key = "val"
name = "test"
number = 2.0
`),
},
{
name: "Complex TOML - All types",
input: map[string]interface{}{
"array": []interface{}{1, 2, 3, 4, 5},
"boolean": true,
"color": "gold",
"number": 123,
"object": map[string]interface{}{"a": "b", "c": "d"},
"string": "Hello World",
},
output: []byte(`array = [1,2,3,4,5]
boolean = true
color = "gold"
number = 123
string = "Hello World"

[object]
a = "b"
c = "d"
`),
},
}

tp := Parser()

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out, err := tp.Marshal(tc.input)
if tc.isErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tc.output, out)
}
})
}
}
2 changes: 0 additions & 2 deletions parsers/yaml/yaml_test.go
@@ -1,7 +1,6 @@
package yaml

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -83,7 +82,6 @@ key: #Here is a single-line comment
assert.Nil(t, err)
for i, k := range tc.keys {
v := out[k]
fmt.Println(out[k])
assert.Equal(t, tc.values[i], v)
}
}
Expand Down