-
Notifications
You must be signed in to change notification settings - Fork 4
/
field_test.go
85 lines (65 loc) · 1.85 KB
/
field_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package neatly_test
import (
"github.com/stretchr/testify/assert"
"github.com/viant/neatly"
"github.com/viant/toolbox/data"
"testing"
)
func TestFieldExpression_Set(t *testing.T) {
{
var object = data.NewMap()
field1 := neatly.NewField("Field1")
field1.Set(123, object)
assert.Equal(t, 123, object.GetInt("Field1"))
}
{
var object = data.NewMap()
field1 := neatly.NewField("Req.[]Array.H")
assert.True(t, field1.HasArrayComponent)
assert.False(t, field1.IsArray)
assert.True(t, field1.Child.IsArray)
assert.Equal(t, "H", field1.Child.Child.Field)
field1.Set("v1H", object)
field1.Set("v2H", object, 1)
field2 := neatly.NewField("Req.[]Array.A")
field2.Set("v1A", object)
field2.Set("v2A", object, 1)
field3 := neatly.NewField("Req.Field")
field3.Set("v", object)
assert.True(t, object.Has("Req"))
var reqObject = object.GetMap("Req")
assert.NotNil(t, reqObject)
assert.Equal(t, "v", reqObject.GetString("Field"))
assert.True(t, reqObject.Has("Array"))
assert.True(t, reqObject.Has("Field"))
array := reqObject.GetCollection("Array")
assert.NotNil(t, array)
assert.Equal(t, 2, len(*array))
err := array.RangeMap(func(item data.Map, index int) (bool, error) {
switch index {
case 0:
assert.Equal(t, "v1H", item.GetString("H"))
assert.Equal(t, "v1A", item.GetString("A"))
case 1:
assert.Equal(t, "v2H", item.GetString("H"))
assert.Equal(t, "v2A", item.GetString("A"))
}
return true, nil
})
assert.Nil(t, err)
}
{
var object = data.NewMap()
field1 := neatly.NewField("/Field1")
field1.Set(123, object)
assert.Equal(t, 123, object.GetInt("Field1"))
assert.True(t, field1.IsRoot)
}
{
var object = data.NewMap()
field1 := neatly.NewField(":Field1")
field1.Set(123, object)
assert.Equal(t, 123, object.GetInt("Field1"))
assert.True(t, field1.IsVirtual)
}
}