-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions_test.go
95 lines (77 loc) · 2.35 KB
/
options_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
package jsonpath
import (
"fmt"
"testing"
"github.com/evilmonkeyinc/jsonpath/option"
"github.com/stretchr/testify/assert"
)
// Test that OptionFunction func conforms to Option interface
var _ Option = OptionFunction(func(selector *Selector) error { return nil })
func Test_OptionFunction(t *testing.T) {
t.Run("nil", func(t *testing.T) {
called := false
option := OptionFunction(func(selector *Selector) error {
called = true
return nil
})
err := option.Apply(nil)
assert.Nil(t, err)
assert.True(t, called)
})
t.Run("error", func(t *testing.T) {
called := false
option := OptionFunction(func(selector *Selector) error {
called = true
return fmt.Errorf("fail")
})
err := option.Apply(nil)
assert.EqualError(t, err, "fail")
assert.True(t, called)
})
}
func Test_ScriptEngine(t *testing.T) {
t.Run("first", func(t *testing.T) {
engine := &testScriptEngine{value: 1}
option := ScriptEngine(engine)
selector := &Selector{}
err := option.Apply(selector)
assert.Nil(t, err)
assert.Equal(t, engine, selector.engine)
})
t.Run("second", func(t *testing.T) {
engine1 := &testScriptEngine{value: 1}
engine2 := &testScriptEngine{value: 2}
option1 := ScriptEngine(engine1)
option2 := ScriptEngine(engine2)
selector := &Selector{}
err := option1.Apply(selector)
assert.Nil(t, err)
err = option2.Apply(selector)
assert.Nil(t, err)
assert.Equal(t, engine1, selector.engine)
assert.NotEqual(t, engine2, selector.engine)
})
}
func Test_QueryOptions(t *testing.T) {
t.Run("first", func(t *testing.T) {
input := &option.QueryOptions{AllowMapReferenceByIndex: true, AllowStringReferenceByIndex: true}
option := QueryOptions(input)
selector := &Selector{}
err := option.Apply(selector)
assert.Nil(t, err)
assert.Equal(t, input, selector.Options)
})
t.Run("second", func(t *testing.T) {
input1 := &option.QueryOptions{AllowMapReferenceByIndex: true, AllowStringReferenceByIndex: true}
input2 := &option.QueryOptions{AllowMapReferenceByIndex: false, AllowStringReferenceByIndex: false}
option1 := QueryOptions(input1)
option2 := QueryOptions(input2)
selector := &Selector{}
err := option1.Apply(selector)
assert.Nil(t, err)
err = option2.Apply(selector)
assert.EqualError(t, err, "option already set")
assert.Equal(t, input1, selector.Options)
assert.NotEqual(t, input2, selector.Options)
})
}