11package parser
22
33import (
4+ "fmt"
5+ "reflect"
6+ "sort"
47 "testing"
58)
69
@@ -16,7 +19,7 @@ func TestFilter(t *testing.T) {
1619 },
1720 }
1821
19- cases := map [string ]string {
22+ cases := map [string ]interface {} {
2023 "top" : "I am the top" ,
2124 "bottom.16" : "left" ,
2225 "bottom.13.1" : "right 2" ,
@@ -35,6 +38,94 @@ func TestFilter(t *testing.T) {
3538 }
3639}
3740
41+ func TestStarFilterSlice (t * testing.T ) {
42+ input := map [string ]interface {}{
43+ "top" : []string {
44+ "foo" ,
45+ "bar" ,
46+ "baz" ,
47+ },
48+ "bottom" : []interface {}{
49+ map [string ]int {
50+ "id" : 1 ,
51+ },
52+ map [string ]int {
53+ "id" : 2 ,
54+ },
55+ map [string ]int {
56+ "id" : 3 ,
57+ },
58+ },
59+ }
60+
61+ cases := map [string ]interface {}{
62+ "top.*" : []interface {}{"foo" , "bar" , "baz" },
63+ "bottom.*.id" : []interface {}{1 , 2 , 3 },
64+ }
65+
66+ for path , expected := range cases {
67+ actual , err := Filter (input , path )
68+
69+ if err != nil {
70+ t .Fail ()
71+ }
72+
73+ if ! reflect .DeepEqual (expected , actual ) {
74+ t .Errorf ("case failed: %v vs %v" , expected , actual )
75+ }
76+ }
77+ }
78+
79+ func TestStarFilterMap (t * testing.T ) {
80+ input := map [string ]interface {}{
81+ "top" : map [string ]int {
82+ "one" : 1 ,
83+ "two" : 2 ,
84+ "three" : 3 ,
85+ },
86+ "bottom" : map [string ]interface {}{
87+ "a" : map [string ]int {
88+ "id" : 4 ,
89+ },
90+ "b" : map [string ]int {
91+ "id" : 5 ,
92+ },
93+ "c" : map [string ]int {
94+ "id" : 6 ,
95+ },
96+ },
97+ }
98+
99+ cases := map [string ]interface {}{
100+ "top.*" : []int {1 , 2 , 3 },
101+ "bottom.*.id" : []int {4 , 5 , 6 },
102+ }
103+
104+ for path , expected := range cases {
105+ actual , err := Filter (input , path )
106+
107+ val := reflect .ValueOf (actual )
108+
109+ // Some "fun" type conversion here to make sure things work ok
110+ intActual := make ([]int , val .Len ())
111+
112+ for i := 0 ; i < val .Len (); i ++ {
113+ fmt .Println (val .Index (i ))
114+ intActual [i ] = val .Index (i ).Interface ().(int )
115+ }
116+
117+ sort .Ints (intActual )
118+
119+ if err != nil {
120+ t .Fail ()
121+ }
122+
123+ if ! reflect .DeepEqual (expected , intActual ) {
124+ t .Errorf ("case failed: %v vs %v" , expected , actual )
125+ }
126+ }
127+ }
128+
38129func TestFilterBadKey (t * testing.T ) {
39130 input := map [string ]interface {}{
40131 "foo" : "bar" ,
@@ -46,3 +137,23 @@ func TestFilterBadKey(t *testing.T) {
46137 t .Errorf ("unexpected return values: %v, %v" , val , err )
47138 }
48139}
140+
141+ func TestStarFilterBadKey (t * testing.T ) {
142+ input := []interface {}{
143+ map [string ]int {
144+ "id" : 1 ,
145+ },
146+ map [string ]int {
147+ "id" : 2 ,
148+ },
149+ map [string ]int {
150+ "notid" : 3 ,
151+ },
152+ }
153+
154+ val , err := Filter (input , "*.id" )
155+
156+ if err == nil || err .Error () != "key does not exist: id" {
157+ t .Errorf ("unexpected return values: %v, %v" , val , err )
158+ }
159+ }
0 commit comments