| 1 | package dasel_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/google/go-cmp/cmp" |
| 7 | "github.com/tomwright/dasel/v3" |
| 8 | ) |
| 9 | |
| 10 | type modifyTestCase struct { |
| 11 | selector string |
| 12 | in any |
| 13 | value any |
| 14 | exp any |
| 15 | count int |
| 16 | } |
| 17 | |
| 18 | func (tc modifyTestCase) run(t *testing.T) { |
| 19 | count, err := dasel.Modify(t.Context(), &tc.in, tc.selector, tc.value) |
| 20 | if err != nil { |
| 21 | t.Fatalf("unexpected error: %v", err) |
| 22 | } |
| 23 | if count != tc.count { |
| 24 | t.Errorf("unexpected count: %d", count) |
| 25 | } |
| 26 | if !cmp.Equal(tc.exp, tc.in) { |
| 27 | t.Errorf("unexpected result: %s", cmp.Diff(tc.exp, tc.in)) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestQuery(t *testing.T) { |
| 32 | t.Run("basic query", func(t *testing.T) { |
| 33 | inputData := map[string]any{ |
| 34 | "users": []map[string]any{ |
| 35 | {"name": "Alice", "age": 30}, |
| 36 | {"name": "Bob", "age": 25}, |
| 37 | }, |
| 38 | } |
| 39 | results, count, err := dasel.Query(t.Context(), inputData, "users.map(name)...") |
| 40 | if err != nil { |
| 41 | t.Fatalf("unexpected error: %v", err) |
| 42 | } |
| 43 | if count != 2 { |
| 44 | t.Errorf("unexpected count: %d", count) |
| 45 | } |
| 46 | exp := []string{"Alice", "Bob"} |
| 47 | for i, r := range results { |
| 48 | strVal, err := r.StringValue() |
| 49 | if err != nil { |
| 50 | t.Fatalf("unexpected error: %v", err) |
| 51 | } |
| 52 | if strVal != exp[i] { |
| 53 | t.Errorf("unexpected result at index %d: %s", i, strVal) |
| 54 | } |
| 55 | } |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | func TestSelect(t *testing.T) { |
| 60 | t.Run("basic select", func(t *testing.T) { |
| 61 | inputData := map[string]any{ |
| 62 | "users": []map[string]any{ |
| 63 | {"name": "Alice", "age": 30}, |
| 64 | {"name": "Bob", "age": 25}, |
| 65 | }, |
| 66 | } |
| 67 | result, count, err := dasel.Select(t.Context(), inputData, "users.map(name)...") |
| 68 | if err != nil { |
| 69 | t.Fatalf("unexpected error: %v", err) |
| 70 | } |
| 71 | if count != 2 { |
| 72 | t.Errorf("unexpected count: %d", count) |
| 73 | } |
| 74 | exp := []any{"Alice", "Bob"} |
| 75 | if !cmp.Equal(exp, result) { |
| 76 | t.Errorf("unexpected result: %s", cmp.Diff(exp, result)) |
| 77 | } |
| 78 | }) |
| 79 | } |
| 80 | |
| 81 | func TestTernary(t *testing.T) { |
| 82 | t.Run("true literal", func(t *testing.T) { |
| 83 | result, count, err := dasel.Select(t.Context(), nil, `true ? "yes" : "no"`) |
| 84 | if err != nil { |
| 85 | t.Fatalf("unexpected error: %v", err) |
| 86 | } |
| 87 | if count != 1 { |
| 88 | t.Errorf("unexpected count: %d", count) |
| 89 | } |
| 90 | exp := []any{"yes"} |
| 91 | if !cmp.Equal(exp, result) { |
| 92 | t.Errorf("unexpected result: %s", cmp.Diff(exp, result)) |
| 93 | } |
| 94 | }) |
| 95 | t.Run("false literal", func(t *testing.T) { |
| 96 | result, count, err := dasel.Select(t.Context(), nil, `false ? "yes" : "no"`) |
| 97 | if err != nil { |
| 98 | t.Fatalf("unexpected error: %v", err) |
| 99 | } |
| 100 | if count != 1 { |
| 101 | t.Errorf("unexpected count: %d", count) |
| 102 | } |
| 103 | exp := []any{"no"} |
| 104 | if !cmp.Equal(exp, result) { |
| 105 | t.Errorf("unexpected result: %s", cmp.Diff(exp, result)) |
| 106 | } |
| 107 | }) |
| 108 | t.Run("condition with comparison", func(t *testing.T) { |
| 109 | inputData := map[string]any{"score": 85} |
| 110 | result, count, err := dasel.Select(t.Context(), inputData, `score >= 70 ? "pass" : "fail"`) |
| 111 | if err != nil { |
| 112 | t.Fatalf("unexpected error: %v", err) |
| 113 | } |
| 114 | if count != 1 { |
| 115 | t.Errorf("unexpected count: %d", count) |
| 116 | } |
| 117 | exp := []any{"pass"} |
| 118 | if !cmp.Equal(exp, result) { |
| 119 | t.Errorf("unexpected result: %s", cmp.Diff(exp, result)) |
| 120 | } |
| 121 | }) |
| 122 | t.Run("condition with comparison false", func(t *testing.T) { |
| 123 | inputData := map[string]any{"score": 50} |
| 124 | result, count, err := dasel.Select(t.Context(), inputData, `score >= 70 ? "pass" : "fail"`) |
| 125 | if err != nil { |
| 126 | t.Fatalf("unexpected error: %v", err) |
| 127 | } |
| 128 | if count != 1 { |
| 129 | t.Errorf("unexpected count: %d", count) |
| 130 | } |
| 131 | exp := []any{"fail"} |
| 132 | if !cmp.Equal(exp, result) { |
| 133 | t.Errorf("unexpected result: %s", cmp.Diff(exp, result)) |
| 134 | } |
| 135 | }) |
| 136 | t.Run("nested ternary", func(t *testing.T) { |
| 137 | result, count, err := dasel.Select(t.Context(), nil, `true ? (false ? "a" : "b") : "c"`) |
| 138 | if err != nil { |
| 139 | t.Fatalf("unexpected error: %v", err) |
| 140 | } |
| 141 | if count != 1 { |
| 142 | t.Errorf("unexpected count: %d", count) |
| 143 | } |
| 144 | exp := []any{"b"} |
| 145 | if !cmp.Equal(exp, result) { |
| 146 | t.Errorf("unexpected result: %s", cmp.Diff(exp, result)) |
| 147 | } |
| 148 | }) |
| 149 | t.Run("ternary returns number", func(t *testing.T) { |
| 150 | result, count, err := dasel.Select(t.Context(), nil, `true ? 42 : 0`) |
| 151 | if err != nil { |
| 152 | t.Fatalf("unexpected error: %v", err) |
| 153 | } |
| 154 | if count != 1 { |
| 155 | t.Errorf("unexpected count: %d", count) |
| 156 | } |
| 157 | exp := []any{int64(42)} |
| 158 | if !cmp.Equal(exp, result) { |
| 159 | t.Errorf("unexpected result: %s", cmp.Diff(exp, result)) |
| 160 | } |
| 161 | }) |
| 162 | t.Run("ternary with arithmetic", func(t *testing.T) { |
| 163 | result, count, err := dasel.Select(t.Context(), nil, `true ? 1 + 2 : 3 + 4`) |
| 164 | if err != nil { |
| 165 | t.Fatalf("unexpected error: %v", err) |
| 166 | } |
| 167 | if count != 1 { |
| 168 | t.Errorf("unexpected count: %d", count) |
| 169 | } |
| 170 | exp := []any{int64(3)} |
| 171 | if !cmp.Equal(exp, result) { |
| 172 | t.Errorf("unexpected result: %s", cmp.Diff(exp, result)) |
| 173 | } |
| 174 | }) |
| 175 | t.Run("ternary with property lookup", func(t *testing.T) { |
| 176 | inputData := map[string]any{"active": true, "name": "Alice"} |
| 177 | result, count, err := dasel.Select(t.Context(), inputData, `active ? name : "unknown"`) |
| 178 | if err != nil { |
| 179 | t.Fatalf("unexpected error: %v", err) |
| 180 | } |
| 181 | if count != 1 { |
| 182 | t.Errorf("unexpected count: %d", count) |
| 183 | } |
| 184 | exp := []any{"Alice"} |
| 185 | if !cmp.Equal(exp, result) { |
| 186 | t.Errorf("unexpected result: %s", cmp.Diff(exp, result)) |
| 187 | } |
| 188 | }) |
| 189 | t.Run("ternary in map", func(t *testing.T) { |
| 190 | inputData := map[string]any{ |
| 191 | "items": []any{ |
| 192 | map[string]any{"val": 10}, |
| 193 | map[string]any{"val": 3}, |
| 194 | map[string]any{"val": 7}, |
| 195 | }, |
| 196 | } |
| 197 | result, count, err := dasel.Select(t.Context(), inputData, `items.map(val >= 5 ? "big" : "small")...`) |
| 198 | if err != nil { |
| 199 | t.Fatalf("unexpected error: %v", err) |
| 200 | } |
| 201 | if count != 3 { |
| 202 | t.Errorf("unexpected count: %d", count) |
| 203 | } |
| 204 | exp := []any{"big", "small", "big"} |
| 205 | if !cmp.Equal(exp, result) { |
| 206 | t.Errorf("unexpected result: %s", cmp.Diff(exp, result)) |
| 207 | } |
| 208 | }) |
| 209 | t.Run("ternary with logical operators", func(t *testing.T) { |
| 210 | inputData := map[string]any{"a": 5, "b": 10} |
| 211 | result, count, err := dasel.Select(t.Context(), inputData, `a > 1 && b > 5 ? "both" : "nope"`) |
| 212 | if err != nil { |
| 213 | t.Fatalf("unexpected error: %v", err) |
| 214 | } |
| 215 | if count != 1 { |
| 216 | t.Errorf("unexpected count: %d", count) |
| 217 | } |
| 218 | exp := []any{"both"} |
| 219 | if !cmp.Equal(exp, result) { |
| 220 | t.Errorf("unexpected result: %s", cmp.Diff(exp, result)) |
| 221 | } |
| 222 | }) |
| 223 | } |
| 224 | |
| 225 | func TestModify(t *testing.T) { |
| 226 | t.Run("index", func(t *testing.T) { |
| 227 | t.Run("int over int", modifyTestCase{ |
| 228 | selector: "$this[1]", |
| 229 | in: []int{1, 2, 3}, |
| 230 | value: 4, |
| 231 | exp: []int{1, 4, 3}, |
| 232 | count: 1, |
| 233 | }.run) |
| 234 | t.Run("string over int", modifyTestCase{ |
| 235 | selector: "$this[1]", |
| 236 | in: []any{1, 2, 3}, |
| 237 | value: "4", |
| 238 | exp: []any{1, "4", 3}, |
| 239 | count: 1, |
| 240 | }.run) |
| 241 | }) |
| 242 | } |