github.com/chainguard-dev/yam@v0.0.7/pkg/yam/formatted/path/path_test.go (about) 1 package path 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 var assertErrExpressionNotSupported assert.ErrorAssertionFunc = func(t assert.TestingT, err error, i ...interface{}) bool { 11 return assert.ErrorIs(t, err, ErrExpressionNotSupported) 12 } 13 14 func TestParse(t *testing.T) { 15 cases := []struct { 16 expression string 17 expectedPath Path 18 assertErr assert.ErrorAssertionFunc 19 }{ 20 { 21 expression: rootExpression, 22 expectedPath: Root(), 23 assertErr: assert.NoError, 24 }, 25 { 26 expression: ".some-key", 27 expectedPath: Path{ 28 parts: []Part{ 29 rootPart{}, 30 mapPart{key: "some-key"}, 31 }, 32 }, 33 assertErr: assert.NoError, 34 }, 35 { 36 expression: ".*", 37 expectedPath: Path{ 38 parts: []Part{ 39 rootPart{}, 40 mapPart{key: anyKey}, 41 }, 42 }, 43 assertErr: assert.NoError, 44 }, 45 { 46 expression: ".[5]", 47 expectedPath: Path{ 48 parts: []Part{ 49 rootPart{}, 50 seqPart{index: 5}, 51 }, 52 }, 53 assertErr: assert.NoError, 54 }, 55 { 56 expression: ".[]", 57 expectedPath: Path{ 58 parts: []Part{ 59 rootPart{}, 60 seqPart{index: anyIndex}, 61 }, 62 }, 63 assertErr: assert.NoError, 64 }, 65 { 66 expression: ".A_KEY[].other-key.thing[0]", 67 expectedPath: Path{ 68 parts: []Part{ 69 rootPart{}, 70 mapPart{key: "A_KEY"}, 71 seqPart{index: anyIndex}, 72 mapPart{key: "other-key"}, 73 mapPart{key: "thing"}, 74 seqPart{index: 0}, 75 }, 76 }, 77 assertErr: assert.NoError, 78 }, 79 { 80 expression: "no-leading-dot", 81 expectedPath: Path{}, 82 assertErr: assertErrExpressionNotSupported, 83 }, 84 { 85 expression: ".someB@Dcharacter$", 86 expectedPath: Path{}, 87 assertErr: assertErrExpressionNotSupported, 88 }, 89 { 90 expression: ".unmatched-bracket[", 91 expectedPath: Path{}, 92 assertErr: assertErrExpressionNotSupported, 93 }, 94 } 95 96 for _, tt := range cases { 97 t.Run(tt.expression, func(t *testing.T) { 98 p, err := Parse(tt.expression) 99 tt.assertErr(t, err) 100 101 if diff := cmp.Diff(tt.expectedPath, p, cmp.AllowUnexported(Path{}, rootPart{}, mapPart{}, seqPart{})); diff != "" { 102 t.Errorf("got unexpected value from Parse (-want, +got):\n%s", diff) 103 } 104 }) 105 } 106 }