(t *testing.T)
| 436 | } |
| 437 | |
| 438 | func TestVM_GroupAndSortOperations(t *testing.T) { |
| 439 | tests := []struct { |
| 440 | name string |
| 441 | expr string |
| 442 | env map[string]any |
| 443 | want any |
| 444 | expectError string |
| 445 | }{ |
| 446 | { |
| 447 | name: "group by single field", |
| 448 | expr: `groupBy([{"id": 1, "type": "a"}, {"id": 2, "type": "b"}, {"id": 3, "type": "a"}], #.type)`, |
| 449 | want: map[any][]any{ |
| 450 | "a": { |
| 451 | map[string]any{"id": 1, "type": "a"}, |
| 452 | map[string]any{"id": 3, "type": "a"}, |
| 453 | }, |
| 454 | "b": { |
| 455 | map[string]any{"id": 2, "type": "b"}, |
| 456 | }, |
| 457 | }, |
| 458 | }, |
| 459 | { |
| 460 | name: "sort by field ascending", |
| 461 | expr: `sortBy([{"id": 3}, {"id": 1}, {"id": 2}], #.id)`, |
| 462 | want: []any{ |
| 463 | map[string]any{"id": 1}, |
| 464 | map[string]any{"id": 2}, |
| 465 | map[string]any{"id": 3}, |
| 466 | }, |
| 467 | }, |
| 468 | { |
| 469 | name: "sort by field descending", |
| 470 | expr: `sortBy([{"id": 3}, {"id": 1}, {"id": 2}], #.id, "desc")`, |
| 471 | want: []any{ |
| 472 | map[string]any{"id": 3}, |
| 473 | map[string]any{"id": 2}, |
| 474 | map[string]any{"id": 1}, |
| 475 | }, |
| 476 | }, |
| 477 | { |
| 478 | name: "sort by computed value", |
| 479 | expr: `sortBy([1, 2, 3, 4], # % 2)`, |
| 480 | want: []any{2, 4, 1, 3}, |
| 481 | }, |
| 482 | { |
| 483 | name: "group by with complex key", |
| 484 | expr: `groupBy([1, 2, 3, 4, 5, 6], # % 2 == 0 ? "even" : "odd")`, |
| 485 | want: map[any][]any{ |
| 486 | "even": {2, 4, 6}, |
| 487 | "odd": {1, 3, 5}, |
| 488 | }, |
| 489 | }, |
| 490 | { |
| 491 | name: "invalid sort order", |
| 492 | expr: `sortBy([1, 2, 3], #, "invalid")`, |
| 493 | expectError: "unknown order", |
| 494 | }, |
| 495 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…