-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsontagconverter_test.go
57 lines (50 loc) · 1.24 KB
/
jsontagconverter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package dynamictags
import (
"encoding/json"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
const (
JSON_DATA = `{
"root" : {
"testcfg1" : {
"val1" : 123,
"val2" : true,
"val3" : "testdata"
}
}
}
`
TEST_PATH = "$.root.testcfg1"
INCORRECT_PATH = "$.data"
EXPECTED_JSON_TAG = "json"
)
func TestJsonTagConverter(t *testing.T) {
var res any
err := json.Unmarshal([]byte(JSON_DATA), &res)
assert.NoError(t, err)
// Case 1 correct create tag converter
conv, err := NewJsonTagConverter(res, TEST_PATH)
assert.NoError(t, err)
assert.NotNil(t, conv)
assert.Equal(t, EXPECTED_JSON_TAG, conv.GetTag())
// Get existed value
val, ok, err := conv.GetSimpleValue("val1", reflect.StructField{}, reflect.Value{}, "$")
assert.NotNil(t, val)
assert.NoError(t, err)
assert.True(t, ok)
// Get non existed value
val, ok, err = conv.GetSimpleValue("val1567", reflect.StructField{}, reflect.Value{}, "$")
assert.Nil(t, val)
assert.NoError(t, err)
assert.False(t, ok)
// Case 2 json data is nil
conv, err = NewJsonTagConverter(nil, TEST_PATH)
assert.Error(t, err)
assert.Nil(t, conv)
// Case 3 incorrect path
conv, err = NewJsonTagConverter(nil, INCORRECT_PATH)
assert.Error(t, err)
assert.Nil(t, conv)
}