Skip to content

Commit 613300d

Browse files
committed
Add some tests
1 parent 4f9b948 commit 613300d

8 files changed

+84
-8
lines changed

func_first.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var FirstFunc = BasicFunction{
2525
value := val.Index(0)
2626
res = append(res, value)
2727
default:
28-
return nil, fmt.Errorf("cannot use first selector on non slice/array types")
28+
return nil, fmt.Errorf("cannot use first selector on non slice/array types: %w", &ErrIndexNotFound{Index: 0})
2929
}
3030
}
3131

func_first_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ func TestFirstFunc(t *testing.T) {
1414
}),
1515
)
1616

17+
t.Run("NotFound", selectTestErr(
18+
"first()",
19+
[]interface{}{},
20+
&ErrIndexNotFound{
21+
Index: 0,
22+
}),
23+
)
24+
25+
t.Run("NotFoundOnInvalidType", selectTestErr(
26+
"x.first()",
27+
map[string]interface{}{"x": "y"},
28+
&ErrIndexNotFound{
29+
Index: 0,
30+
}),
31+
)
32+
1733
original := map[string]interface{}{
1834
"name": map[string]interface{}{
1935
"first": "Tom",

func_index.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ func (e ErrIndexNotFound) Error() string {
1616
}
1717

1818
func (e ErrIndexNotFound) Is(other error) bool {
19-
_, ok := other.(*ErrIndexNotFound)
20-
return ok
19+
o, ok := other.(*ErrIndexNotFound)
20+
if !ok {
21+
return false
22+
}
23+
if o.Index >= 0 && o.Index != e.Index {
24+
return false
25+
}
26+
return true
2127
}
2228

2329
var IndexFunc = BasicFunction{
@@ -57,7 +63,7 @@ var IndexFunc = BasicFunction{
5763
value := val.Index(index)
5864
res = append(res, value)
5965
default:
60-
return nil, fmt.Errorf("cannot use index selector on non slice/array types")
66+
return nil, fmt.Errorf("cannot use index selector on non slice/array types: %w", &ErrIndexNotFound{Index: index})
6167
}
6268
}
6369
}

func_index_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ func TestIndexFunc(t *testing.T) {
1414
}),
1515
)
1616

17+
t.Run("NotFound", selectTestErr(
18+
"[0]",
19+
[]interface{}{},
20+
&ErrIndexNotFound{
21+
Index: 0,
22+
}),
23+
)
24+
25+
t.Run("NotFoundOnInvalidType", selectTestErr(
26+
"[0]",
27+
map[string]interface{}{},
28+
&ErrIndexNotFound{
29+
Index: 0,
30+
}),
31+
)
32+
1733
original := map[string]interface{}{
1834
"name": map[string]interface{}{
1935
"first": "Tom",

func_last.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var LastFunc = BasicFunction{
2626
value := val.Index(index)
2727
res = append(res, value)
2828
default:
29-
return nil, fmt.Errorf("cannot use last selector on non slice/array types")
29+
return nil, fmt.Errorf("cannot use last selector on non slice/array types: %w", &ErrIndexNotFound{Index: 0})
3030
}
3131
}
3232

func_last_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ func TestLastFunc(t *testing.T) {
1414
}),
1515
)
1616

17+
t.Run("NotFound", selectTestErr(
18+
"last()",
19+
[]interface{}{},
20+
&ErrIndexNotFound{
21+
Index: -1,
22+
}),
23+
)
24+
25+
t.Run("NotFoundOnInvalidType", selectTestErr(
26+
"x.last()",
27+
map[string]interface{}{"x": "y"},
28+
&ErrIndexNotFound{
29+
Index: 0,
30+
}),
31+
)
32+
1733
original := map[string]interface{}{
1834
"name": map[string]interface{}{
1935
"first": "Tom",

func_property.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ func (e ErrPropertyNotFound) Error() string {
1515
}
1616

1717
func (e ErrPropertyNotFound) Is(other error) bool {
18-
_, ok := other.(*ErrPropertyNotFound)
19-
return ok
18+
o, ok := other.(*ErrPropertyNotFound)
19+
if !ok {
20+
return false
21+
}
22+
if o.Property != "" && o.Property != e.Property {
23+
return false
24+
}
25+
return true
2026
}
2127

2228
var PropertyFunc = BasicFunction{
@@ -64,7 +70,7 @@ var PropertyFunc = BasicFunction{
6470
}
6571
res = append(res, value)
6672
default:
67-
return nil, fmt.Errorf("cannot use property selector on non map/struct types: %s", val.Kind().String())
73+
return nil, fmt.Errorf("cannot use property selector on non map/struct types: %s: %w", val.Kind().String(), &ErrPropertyNotFound{Property: property})
6874
}
6975
}
7076
}

func_property_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ func TestPropertyFunc(t *testing.T) {
1414
}),
1515
)
1616

17+
t.Run("NotFound", selectTestErr(
18+
"asd",
19+
map[string]interface{}{"x": "y"},
20+
&ErrPropertyNotFound{
21+
Property: "asd",
22+
}),
23+
)
24+
25+
t.Run("NotFoundOnString", selectTestErr(
26+
"x.asd",
27+
map[string]interface{}{"x": "y"},
28+
&ErrPropertyNotFound{
29+
Property: "asd",
30+
}),
31+
)
32+
1733
original := map[string]interface{}{
1834
"name": map[string]interface{}{
1935
"first": "Tom",

0 commit comments

Comments
 (0)