-
Notifications
You must be signed in to change notification settings - Fork 2
/
expressions_test.go
128 lines (113 loc) · 3.67 KB
/
expressions_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package worksheets
import (
"github.com/stretchr/testify/require"
)
var defsForSelectors = `
type child worksheet {
1:name text
}
type parent worksheet {
2:ref_to_child child
3:refs_to_children []child
}`
func (s *Zuite) TestSelectors() {
// single field
child := s.defsForSelectors.MustNewWorksheet("child")
child.MustSet("name", alice)
{
actual, err := tSelector([]string{"name"}).compute(child)
require.NoError(s.T(), err)
require.Equal(s.T(), alice, actual)
}
// path to field
parent := s.defsForSelectors.MustNewWorksheet("parent")
parent.MustSet("ref_to_child", child)
{
actual, err := tSelector([]string{"ref_to_child", "name"}).compute(parent)
require.NoError(s.T(), err)
require.Equal(s.T(), alice, actual)
}
// slice expression
parent.MustAppend("refs_to_children", child)
{
actual, err := tSelector([]string{"refs_to_children", "name"}).compute(parent)
require.NoError(s.T(), err)
slice, ok := actual.(*Slice)
require.True(s.T(), ok)
require.Equal(s.T(), []Value{alice}, slice.Elements())
require.Equal(s.T(), &SliceType{&TextType{}}, slice.Type())
}
}
func (s *Zuite) TestSelectorSliceTypes() {
child := s.defsForSelectors.MustNewWorksheet("child")
parent := s.defsForSelectors.MustNewWorksheet("parent")
parent.MustAppend("refs_to_children", child)
// even with an undefined value, slice type should match field def type
{
actual, err := tSelector([]string{"refs_to_children", "name"}).compute(parent)
require.NoError(s.T(), err)
slice, ok := actual.(*Slice)
require.True(s.T(), ok)
require.Equal(s.T(), []Value{NewUndefined()}, slice.Elements())
require.Equal(s.T(), &SliceType{&TextType{}}, slice.Type())
}
// a selected empty slice should still be of the correct type
parent.MustDel("refs_to_children", 0)
{
actual, err := tSelector([]string{"refs_to_children", "name"}).compute(parent)
require.NoError(s.T(), err)
slice, ok := actual.(*Slice)
require.True(s.T(), ok)
require.Empty(s.T(), slice.Elements())
require.Equal(s.T(), &SliceType{&TextType{}}, slice.Type())
}
}
func (s *Zuite) TestFnArgs_newFromValues() {
args := newFnArgs(nil, nil, []Value{vZero})
require.Equal(s.T(), 1, args.num())
require.True(s.T(), args.has(0))
require.False(s.T(), args.has(1))
arg0, err := args.get(0)
require.NoError(s.T(), err)
require.Equal(s.T(), vZero, arg0)
}
func (s *Zuite) TestFnArgs_checkArgsNum() {
args := newLazyFnArgs(nil, nil, []expression{vZero})
// before lazy eval
err := args.checkArgsNum(4)
require.EqualError(s.T(), err, "4 argument(s) expected but 1 found")
// after lazy eval
_, err = args.get(0)
require.NoError(s.T(), err)
err = args.checkArgsNum(4)
require.EqualError(s.T(), err, "4 argument(s) expected but 1 found")
}
func (s *Zuite) TestFnArgs_get() {
args := newLazyFnArgs(nil, nil, []expression{vZero})
s.Len(args.exprs, 1)
s.NotNil(args.exprs[0])
s.Len(args.values, 1)
s.Nil(args.values[0])
s.Len(args.errs, 1)
s.Nil(args.errs[0])
v, err := args.get(0)
require.NoError(s.T(), err)
require.Equal(s.T(), vZero, v)
s.Len(args.exprs, 1)
s.Nil(args.exprs[0])
s.Len(args.values, 1)
s.Equal(vZero, args.values[0])
s.Len(args.errs, 1)
s.Nil(args.errs[0])
}