(t *testing.T)
| 222 | } |
| 223 | |
| 224 | func TestFlattenExprToOperations(t *testing.T) { |
| 225 | type testOperation struct { |
| 226 | opType Operator |
| 227 | cond []int |
| 228 | } |
| 229 | |
| 230 | testCases := []struct { |
| 231 | name, query string |
| 232 | expected []testOperation |
| 233 | }{ |
| 234 | { |
| 235 | name: "three single condition ORs", |
| 236 | query: `{ .attr = "123" || .service = "b" || .env = "staging" }`, |
| 237 | expected: []testOperation{{opType: OpOr, cond: []int{1, 1, 1}}}, |
| 238 | }, |
| 239 | { |
| 240 | name: "one double condition ORs with two single condition ORs", |
| 241 | query: `{ (.attr = "123" && .foo = "bar") || .service = "b" || .env = "staging" }`, |
| 242 | expected: []testOperation{{opType: OpOr, cond: []int{2, 1, 1}}}, |
| 243 | }, |
| 244 | { |
| 245 | name: "two duplicated OR conditions", |
| 246 | query: `{ (.attr = "123" && .foo = "bar") || .service = "b" || .env = "staging" || (.attr = "123" && .foo = "bar") || .service = "b" }`, |
| 247 | expected: []testOperation{{opType: OpOr, cond: []int{2, 1, 1}}}, |
| 248 | }, |
| 249 | { |
| 250 | name: "one single condition AND three single condition ORs", |
| 251 | query: `{ name = "abc" && (.attr = "123" || .service = "b" || .env = "staging") }`, |
| 252 | expected: []testOperation{{opType: OpAnd, cond: []int{1}}, {opType: OpOr, cond: []int{1, 1, 1}}}, |
| 253 | }, |
| 254 | { |
| 255 | name: "one AND one double condition OR with two single condition ORs", |
| 256 | query: `{ name = "abc" && ( (.attr = "123" && .foo = "bar") || .service = "b" || .env = "staging") }`, |
| 257 | expected: []testOperation{{opType: OpAnd, cond: []int{1}}, {opType: OpOr, cond: []int{2, 1, 1}}}, |
| 258 | }, |
| 259 | { |
| 260 | name: "two ANDs one double condition OR with two single condition ORs", |
| 261 | query: `{ name = "abc" && .attr = "abc" && ( (.attr = "123" && .foo = "bar") || .service = "b" || .env = "staging") }`, |
| 262 | expected: []testOperation{{opType: OpAnd, cond: []int{1}}, {opType: OpAnd, cond: []int{1}}, {opType: OpOr, cond: []int{2, 1, 1}}}, |
| 263 | }, |
| 264 | { |
| 265 | name: "two AND with single condition OR in between", |
| 266 | query: `{ .attr = "123" && (.service = "b" || .service = "a" ) && .env = "staging" }`, |
| 267 | expected: []testOperation{{opType: OpAnd, cond: []int{1}}, {opType: OpOr, cond: []int{1, 1}}, {opType: OpAnd, cond: []int{1}}}, |
| 268 | }, |
| 269 | { |
| 270 | name: "two ANDs of single condition ORs", |
| 271 | query: `{ ( .attr = "123" || .service = "b" ) && ( .service = "a" || .env = "staging" ) }`, |
| 272 | expected: []testOperation{{opType: OpOr, cond: []int{1, 1}}, {opType: OpOr, cond: []int{1, 1}}}, |
| 273 | }, |
| 274 | { |
| 275 | name: "two ANDs of multiple conditions ORs", |
| 276 | query: `{ ( .attr = "123" || .service = "b" ) && ( .service = "a" || ( .env = "staging" && .foo = "bar" ) ) }`, |
| 277 | expected: []testOperation{{opType: OpOr, cond: []int{1, 1}}, {opType: OpOr, cond: []int{1, 2}}}, |
| 278 | }, |
| 279 | } |
| 280 | for _, tc := range testCases { |
| 281 | t.Run(tc.name, func(t *testing.T) { |
nothing calls this directly
no test coverage detected