Skip to content

Commit

Permalink
add parse tests
Browse files Browse the repository at this point in the history
Signed-off-by: mario <[email protected]>
  • Loading branch information
MarioBassem committed Nov 11, 2023
1 parent 78277d6 commit 36a32c2
Showing 1 changed file with 67 additions and 2 deletions.
69 changes: 67 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,79 @@
package parser

import (
"encoding/json"
"testing"

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

// func TestParse(t *testing.T) {
func TestParse(t *testing.T) {
tests := map[string]struct {
input string
expected bool
value map[string]interface{}
}{
"empty": {
input: "",
expected: false,
value: nil,
},
"not_ended": {
input: `{"key1":"val1"`,
expected: false,
value: nil,
},
"not_started": {
input: `"key1":"val1"}`,
expected: false,
value: nil,
},
"missing_colon": {
input: `{"key1" "val1"}`,
expected: false,
value: nil,
},
"valid_string_val": {
input: `{"key1": "v\tal1"}`,
expected: true,
value: map[string]interface{}{"key1": "val1"},
},
"non_string_key": {
input: `{ab123: "val1"}`,
expected: false,
value: nil,
},
"valid_number_val": {
input: `{"key2":234.512}`,
expected: true,
value: map[string]interface{}{"key2": float64(234.512)},
},
"valid_array_val": {
input: `{ "key\t1":[1,2,3]}`,
expected: true,
value: map[string]interface{}{"key1": []interface{}{float64(1), float64(2), float64(3)}},
},
"text_after_top_level_struct": {
input: `{"key":"val"} hi`,
expected: false,
value: nil,
},
}

// }
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
expectedVal := map[string]interface{}{}
expectedErr := json.Unmarshal([]byte(tc.input), &expectedVal)
gotVal, gotErr := Parse([]byte(tc.input))
if expectedErr == nil {
assert.NoError(t, gotErr)
assert.Equal(t, expectedVal, gotVal)
} else {
assert.Error(t, gotErr)
}
})
}
}

func TestGetNumber(t *testing.T) {
tests := map[string]struct {
Expand Down

0 comments on commit 36a32c2

Please sign in to comment.