Skip to content

Commit f888e93

Browse files
committed
Add tests
1 parent 0b4080f commit f888e93

26 files changed

+301
-55
lines changed

context.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ func newContextWithFunctions(value interface{}, selector string, functions *Func
3030
}
3131

3232
v = Value{
33-
Value: reflectVal,
34-
metadata: map[string]interface{}{},
33+
Value: reflectVal,
3534
}
3635
}
3736

@@ -46,10 +45,6 @@ func newContextWithFunctions(value interface{}, selector string, functions *Func
4645
v.Unpack().Set(value.Value)
4746
}
4847

49-
if v.metadata == nil {
50-
v.metadata = map[string]interface{}{}
51-
}
52-
5348
if v.Metadata("key") == nil {
5449
v.WithMetadata("key", "root")
5550
}

context_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dasel
22

33
import (
4+
"errors"
45
"reflect"
56
"testing"
67
)
@@ -49,6 +50,19 @@ func selectTest(selector string, original interface{}, exp []interface{}) func(t
4950
}
5051
}
5152

53+
func selectTestErr(selector string, original interface{}, expErr error) func(t *testing.T) {
54+
return func(t *testing.T) {
55+
c := newSelectContext(original, selector)
56+
57+
_, err := c.Run()
58+
59+
if !errors.Is(err, expErr) {
60+
t.Errorf("expected error: %v, got %v", expErr, err)
61+
return
62+
}
63+
}
64+
}
65+
5266
func TestNewContext(t *testing.T) {
5367
t.Run("map propagation", func(t *testing.T) {
5468
orig := map[string]interface{}{

func.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dasel
22

33
import (
44
"fmt"
5+
"reflect"
56
"strings"
67
)
78

@@ -14,7 +15,7 @@ func (e ErrUnknownFunction) Error() string {
1415
}
1516

1617
func (e ErrUnknownFunction) Is(other error) bool {
17-
_, ok := other.(ErrUnknownFunction)
18+
_, ok := other.(*ErrUnknownFunction)
1819
return ok
1920
}
2021

@@ -29,8 +30,20 @@ func (e ErrUnexpectedFunctionArgs) Error() string {
2930
}
3031

3132
func (e ErrUnexpectedFunctionArgs) Is(other error) bool {
32-
_, ok := other.(ErrUnexpectedFunctionArgs)
33-
return ok
33+
o, ok := other.(*ErrUnexpectedFunctionArgs)
34+
if !ok {
35+
return false
36+
}
37+
if o.Function != "" && o.Function != e.Function {
38+
return false
39+
}
40+
if o.Message != "" && o.Message != e.Message {
41+
return false
42+
}
43+
if o.Args != nil && !reflect.DeepEqual(o.Args, e.Args) {
44+
return false
45+
}
46+
return true
3447
}
3548

3649
func standardFunctions() *FunctionCollection {

func_all_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ package dasel
33
import "testing"
44

55
func TestAllFunc(t *testing.T) {
6+
t.Run("Args", selectTestErr(
7+
"all(x)",
8+
map[string]interface{}{},
9+
&ErrUnexpectedFunctionArgs{
10+
Function: "all",
11+
Args: []string{"x"},
12+
}),
13+
)
14+
615
t.Run(
716
"RootAll",
817
selectTest(

func_and.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
var AndFunc = BasicFunction{
99
name: "and",
1010
runFn: func(c *Context, s *Step, args []string) (Values, error) {
11-
if err := requireXOrMoreArgs("filter", args, 1); err != nil {
11+
if err := requireXOrMoreArgs("and", args, 1); err != nil {
1212
return nil, err
1313
}
1414

func_and_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import (
55
)
66

77
func TestAndFunc(t *testing.T) {
8+
t.Run("Args", selectTestErr(
9+
"and()",
10+
map[string]interface{}{},
11+
&ErrUnexpectedFunctionArgs{
12+
Function: "and",
13+
Args: []string{},
14+
}),
15+
)
16+
817
t.Run(
918
"NoneEqualMoreThan",
1019
selectTest(

func_equal_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import (
66

77
func TestEqualFunc(t *testing.T) {
88

9+
t.Run("Args", selectTestErr(
10+
"equal()",
11+
map[string]interface{}{},
12+
&ErrUnexpectedFunctionArgs{
13+
Function: "equal",
14+
Args: []string{},
15+
}),
16+
)
17+
918
t.Run(
1019
"Single Equal",
1120
selectTest(

func_filter_or_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import (
66

77
func TestFilterOrFunc(t *testing.T) {
88

9+
t.Run("Args", selectTestErr(
10+
"filterOr()",
11+
map[string]interface{}{},
12+
&ErrUnexpectedFunctionArgs{
13+
Function: "filterOr",
14+
Args: []string{},
15+
}),
16+
)
17+
918
t.Run(
1019
"Filter Equal Key",
1120
selectTest(

func_filter_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import (
66

77
func TestFilterFunc(t *testing.T) {
88

9+
t.Run("Args", selectTestErr(
10+
"filter()",
11+
map[string]interface{}{},
12+
&ErrUnexpectedFunctionArgs{
13+
Function: "filter",
14+
Args: []string{},
15+
}),
16+
)
17+
918
t.Run(
1019
"Filter Equal Key",
1120
selectTest(

func_first_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import (
55
)
66

77
func TestFirstFunc(t *testing.T) {
8+
t.Run("Args", selectTestErr(
9+
"first(x)",
10+
map[string]interface{}{},
11+
&ErrUnexpectedFunctionArgs{
12+
Function: "first",
13+
Args: []string{"x"},
14+
}),
15+
)
16+
817
original := map[string]interface{}{
918
"name": map[string]interface{}{
1019
"first": "Tom",

func_index_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import (
55
)
66

77
func TestIndexFunc(t *testing.T) {
8+
t.Run("Args", selectTestErr(
9+
"index()",
10+
map[string]interface{}{},
11+
&ErrUnexpectedFunctionArgs{
12+
Function: "index",
13+
Args: []string{},
14+
}),
15+
)
16+
817
original := map[string]interface{}{
918
"name": map[string]interface{}{
1019
"first": "Tom",

func_last_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import (
55
)
66

77
func TestLastFunc(t *testing.T) {
8+
t.Run("Args", selectTestErr(
9+
"last(x)",
10+
map[string]interface{}{},
11+
&ErrUnexpectedFunctionArgs{
12+
Function: "last",
13+
Args: []string{"x"},
14+
}),
15+
)
16+
817
original := map[string]interface{}{
918
"name": map[string]interface{}{
1019
"first": "Tom",

func_len_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import (
55
)
66

77
func TestLenFunc(t *testing.T) {
8+
t.Run("Args", selectTestErr(
9+
"len(x)",
10+
map[string]interface{}{},
11+
&ErrUnexpectedFunctionArgs{
12+
Function: "len",
13+
Args: []string{"x"},
14+
}),
15+
)
16+
817
data := map[string]interface{}{
918
"string": "hello",
1019
"slice": []interface{}{

func_less_than_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import (
66

77
func TestLessThanFunc(t *testing.T) {
88

9+
t.Run("Args", selectTestErr(
10+
"lessThan()",
11+
map[string]interface{}{},
12+
&ErrUnexpectedFunctionArgs{
13+
Function: "lessThan",
14+
Args: []string{},
15+
}),
16+
)
17+
918
t.Run(
1019
"Less Than",
1120
selectTest(

func_metadata_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import (
66
)
77

88
func TestMetadataFunc(t *testing.T) {
9+
t.Run("Args", selectTestErr(
10+
"metadata()",
11+
map[string]interface{}{},
12+
&ErrUnexpectedFunctionArgs{
13+
Function: "metadata",
14+
Args: []string{},
15+
}),
16+
)
17+
918
t.Run("Type", func(t *testing.T) {
1019
orig := []interface{}{
1120
"abc", true, false, 1, 1.1, []interface{}{1},

func_more_than_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import (
66

77
func TestMoreThanFunc(t *testing.T) {
88

9+
t.Run("Args", selectTestErr(
10+
"moreThan()",
11+
map[string]interface{}{},
12+
&ErrUnexpectedFunctionArgs{
13+
Function: "moreThan",
14+
Args: []string{},
15+
}),
16+
)
17+
918
t.Run(
1019
"More Than",
1120
selectTest(

func_not.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
var NotFunc = BasicFunction{
99
name: "not",
1010
runFn: func(c *Context, s *Step, args []string) (Values, error) {
11-
if err := requireXOrMoreArgs("filter", args, 1); err != nil {
11+
if err := requireXOrMoreArgs("not", args, 1); err != nil {
1212
return nil, err
1313
}
1414

func_not_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import (
55
)
66

77
func TestNotFunc(t *testing.T) {
8+
t.Run("Args", selectTestErr(
9+
"not()",
10+
map[string]interface{}{},
11+
&ErrUnexpectedFunctionArgs{
12+
Function: "not",
13+
Args: []string{},
14+
}),
15+
)
816

917
t.Run(
1018
"Single Equal",

func_or_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import (
55
)
66

77
func TestOrFunc(t *testing.T) {
8+
t.Run("Args", selectTestErr(
9+
"or()",
10+
map[string]interface{}{},
11+
&ErrUnexpectedFunctionArgs{
12+
Function: "or",
13+
Args: []string{},
14+
}),
15+
)
16+
817
t.Run(
918
"NoneEqualMoreThan",
1019
selectTest(

func_parent_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import (
55
)
66

77
func TestParentFunc(t *testing.T) {
8+
t.Run("Args", selectTestErr(
9+
"parent(x)",
10+
map[string]interface{}{},
11+
&ErrUnexpectedFunctionArgs{
12+
Function: "parent",
13+
Args: []string{"x"},
14+
}),
15+
)
16+
817
t.Run(
918
"SimpleParent",
1019
selectTest(

func_property_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import (
55
)
66

77
func TestPropertyFunc(t *testing.T) {
8+
t.Run("Args", selectTestErr(
9+
"property()",
10+
map[string]interface{}{},
11+
&ErrUnexpectedFunctionArgs{
12+
Function: "property",
13+
Args: []string{},
14+
}),
15+
)
16+
817
original := map[string]interface{}{
918
"name": map[string]interface{}{
1019
"first": "Tom",

func_this_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import (
55
)
66

77
func TestThisFunc(t *testing.T) {
8+
t.Run("Args", selectTestErr(
9+
"this(x)",
10+
map[string]interface{}{},
11+
&ErrUnexpectedFunctionArgs{
12+
Function: "this",
13+
Args: []string{"x"},
14+
}),
15+
)
816
t.Run(
917
"SimpleThis",
1018
selectTest(

0 commit comments

Comments
 (0)