cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/utils/absent_test.go
71lines · modecode
| 1 | package utils_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/cloudflare/pint/internal/parser" |
| 7 | "github.com/cloudflare/pint/internal/parser/utils" |
| 8 | "github.com/google/go-cmp/cmp" |
| 9 | ) |
| 10 | |
| 11 | func TestHasOuterAbsent(t *testing.T) { |
| 12 | type testCaseT struct { |
| 13 | expr string |
| 14 | output []string |
| 15 | } |
| 16 | |
| 17 | testCases := []testCaseT{ |
| 18 | { |
| 19 | expr: "foo", |
| 20 | }, |
| 21 | { |
| 22 | expr: "absent(foo)", |
| 23 | output: []string{"absent(foo)"}, |
| 24 | }, |
| 25 | { |
| 26 | expr: `absent(foo{job="bar"})`, |
| 27 | output: []string{`absent(foo{job="bar"})`}, |
| 28 | }, |
| 29 | { |
| 30 | expr: `absent(foo{job="bar"}) AND on(job) bar`, |
| 31 | output: []string{`absent(foo{job="bar"})`}, |
| 32 | }, |
| 33 | { |
| 34 | expr: `vector(1) or absent(foo{job="bar"}) AND on(job) bar`, |
| 35 | output: []string{`absent(foo{job="bar"})`}, |
| 36 | }, |
| 37 | { |
| 38 | expr: `up == 0 or absent(foo{job="bar"}) AND on(job) bar`, |
| 39 | output: []string{`absent(foo{job="bar"})`}, |
| 40 | }, |
| 41 | { |
| 42 | expr: `up == 0 or absent(foo{job="bar"}) or absent(bar)`, |
| 43 | output: []string{`absent(foo{job="bar"})`, `absent(bar)`}, |
| 44 | }, |
| 45 | } |
| 46 | |
| 47 | for _, tc := range testCases { |
| 48 | t.Run(tc.expr, func(t *testing.T) { |
| 49 | n, err := parser.DecodeExpr(tc.expr) |
| 50 | if err != nil { |
| 51 | t.Error(err) |
| 52 | t.FailNow() |
| 53 | } |
| 54 | calls := utils.HasOuterAbsent(n) |
| 55 | if len(calls) == 0 { |
| 56 | if len(tc.output) > 0 { |
| 57 | t.Errorf("HasOuterAbsent() returned nil, expected %s", tc.output) |
| 58 | } |
| 59 | } else { |
| 60 | var output = []string{} |
| 61 | for _, a := range calls { |
| 62 | output = append(output, a.Node.String()) |
| 63 | } |
| 64 | if diff := cmp.Diff(tc.output, output); diff != "" { |
| 65 | t.Errorf("HasOuterAbsent() returned wrong result (-want +got):\n%s", diff) |
| 66 | return |
| 67 | } |
| 68 | } |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 | |