github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/json/path_test.gno (about)

     1  package json
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestParseJSONPath(t *testing.T) {
     8  	tests := []struct {
     9  		name     string
    10  		path     string
    11  		expected []string
    12  	}{
    13  		{name: "Empty string path", path: "", expected: []string{}},
    14  		{name: "Root only path", path: "$", expected: []string{"$"}},
    15  		{name: "Root with dot path", path: "$.", expected: []string{"$"}},
    16  		{name: "All objects in path", path: "$..", expected: []string{"$", ".."}},
    17  		{name: "Only children in path", path: "$.*", expected: []string{"$", "*"}},
    18  		{name: "All objects' children in path", path: "$..*", expected: []string{"$", "..", "*"}},
    19  		{name: "Simple dot notation path", path: "$.root.element", expected: []string{"$", "root", "element"}},
    20  		{name: "Complex dot notation path with wildcard", path: "$.root.*.element", expected: []string{"$", "root", "*", "element"}},
    21  		{name: "Path with array wildcard", path: "$.phoneNumbers[*].type", expected: []string{"$", "phoneNumbers", "*", "type"}},
    22  		{name: "Path with filter expression", path: "$.store.book[?(@.price < 10)].title", expected: []string{"$", "store", "book", "?(@.price < 10)", "title"}},
    23  		{name: "Path with formula", path: "$..phoneNumbers..('ty' + 'pe')", expected: []string{"$", "..", "phoneNumbers", "..", "('ty' + 'pe')"}},
    24  		{name: "Simple bracket notation path", path: "$['root']['element']", expected: []string{"$", "'root'", "'element'"}},
    25  		{name: "Complex bracket notation path with wildcard", path: "$['root'][*]['element']", expected: []string{"$", "'root'", "*", "'element'"}},
    26  		{name: "Bracket notation path with integer index", path: "$['store']['book'][0]['title']", expected: []string{"$", "'store'", "'book'", "0", "'title'"}},
    27  		{name: "Complex path with wildcard in bracket notation", path: "$['root'].*['element']", expected: []string{"$", "'root'", "*", "'element'"}},
    28  		{name: "Mixed notation path with dot after bracket", path: "$.['root'].*.['element']", expected: []string{"$", "'root'", "*", "'element'"}},
    29  		{name: "Mixed notation path with dot before bracket", path: "$['root'].*.['element']", expected: []string{"$", "'root'", "*", "'element'"}},
    30  		{name: "Single character path with root", path: "$.a", expected: []string{"$", "a"}},
    31  		{name: "Multiple characters path with root", path: "$.abc", expected: []string{"$", "abc"}},
    32  		{name: "Multiple segments path with root", path: "$.a.b.c", expected: []string{"$", "a", "b", "c"}},
    33  		{name: "Multiple segments path with wildcard and root", path: "$.a.*.c", expected: []string{"$", "a", "*", "c"}},
    34  		{name: "Multiple segments path with filter and root", path: "$.a[?(@.b == 'c')].d", expected: []string{"$", "a", "?(@.b == 'c')", "d"}},
    35  		{name: "Complex path with multiple filters", path: "$.a[?(@.b == 'c')].d[?(@.e == 'f')].g", expected: []string{"$", "a", "?(@.b == 'c')", "d", "?(@.e == 'f')", "g"}},
    36  		{name: "Complex path with multiple filters and wildcards", path: "$.a[?(@.b == 'c')].*.d[?(@.e == 'f')].g", expected: []string{"$", "a", "?(@.b == 'c')", "*", "d", "?(@.e == 'f')", "g"}},
    37  		{name: "Path with array index and root", path: "$.a[0].b", expected: []string{"$", "a", "0", "b"}},
    38  		{name: "Path with multiple array indices and root", path: "$.a[0].b[1].c", expected: []string{"$", "a", "0", "b", "1", "c"}},
    39  		{name: "Path with array index, wildcard and root", path: "$.a[0].*.c", expected: []string{"$", "a", "0", "*", "c"}},
    40  	}
    41  
    42  	for _, tt := range tests {
    43  		t.Run(tt.name, func(t *testing.T) {
    44  			reult, _ := ParsePath(tt.path)
    45  			if !isEqualSlice(reult, tt.expected) {
    46  				t.Errorf("ParsePath(%s) expected: %v, got: %v", tt.path, tt.expected, reult)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func isEqualSlice(a, b []string) bool {
    53  	if len(a) != len(b) {
    54  		return false
    55  	}
    56  
    57  	for i, v := range a {
    58  		if v != b[i] {
    59  			return false
    60  		}
    61  	}
    62  
    63  	return true
    64  }