cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.4.3

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/parser/utils/absent_test.go

75lines · modecode

1package utils_test
2
3import (
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
11func 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 expr: `absent(sum(nonexistent{job="myjob"}))`,
47 output: []string{`absent(sum(nonexistent{job="myjob"}))`},
48 },
49 }
50
51 for _, tc := range testCases {
52 t.Run(tc.expr, func(t *testing.T) {
53 n, err := parser.DecodeExpr(tc.expr)
54 if err != nil {
55 t.Error(err)
56 t.FailNow()
57 }
58 calls := utils.HasOuterAbsent(n)
59 if len(calls) == 0 {
60 if len(tc.output) > 0 {
61 t.Errorf("HasOuterAbsent() returned nil, expected %s", tc.output)
62 }
63 } else {
64 var output = []string{}
65 for _, a := range calls {
66 output = append(output, a.Node.String())
67 }
68 if diff := cmp.Diff(tc.output, output); diff != "" {
69 t.Errorf("HasOuterAbsent() returned wrong result (-want +got):\n%s", diff)
70 return
71 }
72 }
73 })
74 }
75}
76