github.com/tomwright/dasel@v1.27.3/selector_test.go (about) 1 package dasel_test 2 3 import ( 4 "github.com/tomwright/dasel" 5 "reflect" 6 "testing" 7 ) 8 9 func testExtractNextSelector(in string, exp string, expRead int) func(t *testing.T) { 10 return func(t *testing.T) { 11 got, read := dasel.ExtractNextSelector(in) 12 if exp != got { 13 t.Errorf("expected %v, got %v", exp, got) 14 } 15 if read != expRead { 16 t.Errorf("expected read of %d, got %d", expRead, read) 17 } 18 } 19 } 20 21 func testDynamicSelectorToGroups(in string, exp []string) func(t *testing.T) { 22 return func(t *testing.T) { 23 got, err := dasel.DynamicSelectorToGroups(in) 24 if err != nil { 25 t.Errorf("unexpected error: %v", err) 26 return 27 } 28 if !reflect.DeepEqual(exp, got) { 29 t.Errorf("expected %v, got %v", exp, got) 30 } 31 } 32 } 33 34 func testFindDynamicSelectorParts(in string, exp dasel.DynamicSelectorParts) func(t *testing.T) { 35 return func(t *testing.T) { 36 got := dasel.FindDynamicSelectorParts(in) 37 if !reflect.DeepEqual(exp, got) { 38 t.Errorf("expected %v, got %v", exp, got) 39 } 40 } 41 } 42 43 func TestExtractNextSelector(t *testing.T) { 44 t.Run("Simple", testExtractNextSelector(`.metadata.name`, `.metadata`, 9)) 45 t.Run("EscapedDot", testExtractNextSelector(`.before\.after.name`, `.before.after`, 14)) 46 t.Run("EscapedSpace", testExtractNextSelector(`.before\ after.name`, `.before after`, 14)) 47 t.Run("DynamicWithPath", testExtractNextSelector(`.(.before.a=b).after.name`, `.(.before.a=b)`, 14)) 48 t.Run("EscapedFirstDot", testExtractNextSelector(`\.name`, `.name`, 6)) 49 t.Run("SimpleProp", testExtractNextSelector(`.name`, `.name`, 5)) 50 t.Run("SimpleIndex", testExtractNextSelector(`.[123]`, `.[123]`, 6)) 51 t.Run("SimpleLength", testExtractNextSelector(`.[#]`, `.[#]`, 4)) 52 t.Run("UnicodeCharacter", testExtractNextSelector(`.(name=Ägir).asd`, `.(name=Ägir)`, 13)) 53 } 54 55 func TestDynamicSelectorToGroups(t *testing.T) { 56 t.Run("Blank", testDynamicSelectorToGroups("", []string{})) 57 t.Run("Single", testDynamicSelectorToGroups("(a=1)", []string{ 58 "a=1", 59 })) 60 t.Run("Double", testDynamicSelectorToGroups("(a=1)(b=2)", []string{ 61 "a=1", 62 "b=2", 63 })) 64 t.Run("Many", testDynamicSelectorToGroups("(a=1)(b=2)(c=3)(d=4)", []string{ 65 "a=1", 66 "b=2", 67 "c=3", 68 "d=4", 69 })) 70 t.Run("Nested", testDynamicSelectorToGroups("(a=1.(x=3)(y=4))(b=2)", []string{ 71 "a=1.(x=3)(y=4)", 72 "b=2", 73 })) 74 t.Run("Dot", testDynamicSelectorToGroups("(a=.)(b=2)", []string{ 75 "a=.", 76 "b=2", 77 })) 78 t.Run("Unicode", testDynamicSelectorToGroups("(a=.)(b=Ägir)", []string{ 79 "a=.", 80 "b=Ägir", 81 })) 82 } 83 84 func TestFindDynamicSelectorParts(t *testing.T) { 85 t.Run("Blank", testFindDynamicSelectorParts("", dasel.DynamicSelectorParts{ 86 Key: "", 87 Comparison: "", 88 Value: "", 89 })) 90 t.Run("Equal", testFindDynamicSelectorParts("a=b", dasel.DynamicSelectorParts{ 91 Key: "a", 92 Comparison: "=", 93 Value: "b", 94 })) 95 t.Run("EqualWithEqualsInValue", testFindDynamicSelectorParts("a=b=c", dasel.DynamicSelectorParts{ 96 Key: "a", 97 Comparison: "=", 98 Value: "b=c", 99 })) 100 t.Run("NotEqual", testFindDynamicSelectorParts("a!=b", dasel.DynamicSelectorParts{ 101 Key: "a", 102 Comparison: "!=", 103 Value: "b", 104 })) 105 t.Run("MoreThanEqual", testFindDynamicSelectorParts("a>=b", dasel.DynamicSelectorParts{ 106 Key: "a", 107 Comparison: ">=", 108 Value: "b", 109 })) 110 t.Run("LessThanEqual", testFindDynamicSelectorParts("a<=b", dasel.DynamicSelectorParts{ 111 Key: "a", 112 Comparison: "<=", 113 Value: "b", 114 })) 115 t.Run("UnicodeEqual", testFindDynamicSelectorParts("name=Ägir", dasel.DynamicSelectorParts{ 116 Key: "name", 117 Comparison: "=", 118 Value: "Ägir", 119 })) 120 t.Run("NestedGroupIgnored", testFindDynamicSelectorParts("(.(x=y)).x=1", dasel.DynamicSelectorParts{ 121 Key: "(.(x=y)).x", 122 Comparison: "=", 123 Value: "1", 124 })) 125 t.Run("NestedGroupIgnored", testFindDynamicSelectorParts("a=(.(x=y))", dasel.DynamicSelectorParts{ 126 Key: "a", 127 Comparison: "=", 128 Value: "(.(x=y))", 129 })) 130 t.Run("SelectorOnBothSides", testFindDynamicSelectorParts("(.a.b.c)=(.x.y.z)", dasel.DynamicSelectorParts{ 131 Key: "(.a.b.c)", 132 Comparison: "=", 133 Value: "(.x.y.z)", 134 })) 135 t.Run("NestedWithSelectorOnBothSides", testFindDynamicSelectorParts("(.a.b.(1=2).c)=(.x.(3=4).y.z)", dasel.DynamicSelectorParts{ 136 Key: "(.a.b.(1=2).c)", 137 Comparison: "=", 138 Value: "(.x.(3=4).y.z)", 139 })) 140 }