File: ast_test.go

package info (click to toggle)
golang-github-hashicorp-go-bexpr 0.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 356 kB
  • sloc: makefile: 50
file content (92 lines) | stat: -rw-r--r-- 3,641 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package bexpr

import (
	"bytes"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestAST_Dump(t *testing.T) {
	t.Parallel()
	type testCase struct {
		expr     Expression
		expected string
	}

	tests := map[string]testCase{
		"MatchEqual": {
			expr:     &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchEqual, Value: &MatchValue{Raw: "baz"}},
			expected: "Equal {\n   Selector: foo.bar\n   Value: \"baz\"\n}\n",
		},
		"MatchNotEqual": {
			expr:     &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchNotEqual, Value: &MatchValue{Raw: "baz"}},
			expected: "Not Equal {\n   Selector: foo.bar\n   Value: \"baz\"\n}\n",
		},
		"MatchIn": {
			expr:     &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchIn, Value: &MatchValue{Raw: "baz"}},
			expected: "In {\n   Selector: foo.bar\n   Value: \"baz\"\n}\n",
		},
		"MatchNotIn": {
			expr:     &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchNotIn, Value: &MatchValue{Raw: "baz"}},
			expected: "Not In {\n   Selector: foo.bar\n   Value: \"baz\"\n}\n",
		},
		"MatchIsEmpty": {
			expr:     &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchIsEmpty, Value: nil},
			expected: "Is Empty {\n   Selector: foo.bar\n}\n",
		},
		"MatchIsNotEmpty": {
			expr:     &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchIsNotEmpty, Value: nil},
			expected: "Is Not Empty {\n   Selector: foo.bar\n}\n",
		},
		"MatchUnknown": {
			expr:     &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchOperator(42), Value: nil},
			expected: "UNKNOWN {\n   Selector: foo.bar\n}\n",
		},
		"UnaryOpNot": {
			expr:     &UnaryExpression{Operator: UnaryOpNot, Operand: &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchIsEmpty, Value: nil}},
			expected: "Not {\n   Is Empty {\n      Selector: foo.bar\n   }\n}\n",
		},
		"UnaryOpUnknown": {
			expr:     &UnaryExpression{Operator: UnaryOperator(42), Operand: &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchIsEmpty, Value: nil}},
			expected: "UNKNOWN {\n   Is Empty {\n      Selector: foo.bar\n   }\n}\n",
		},
		"BinaryOpAnd": {
			expr: &BinaryExpression{
				Operator: BinaryOpAnd,
				Left:     &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchIsEmpty, Value: nil},
				Right:    &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchIsEmpty, Value: nil},
			},
			expected: "And {\n   Is Empty {\n      Selector: foo.bar\n   }\n   Is Empty {\n      Selector: foo.bar\n   }\n}\n",
		},
		"BinaryOpOr": {
			expr: &BinaryExpression{
				Operator: BinaryOpOr,
				Left:     &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchIsEmpty, Value: nil},
				Right:    &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchIsEmpty, Value: nil},
			},
			expected: "Or {\n   Is Empty {\n      Selector: foo.bar\n   }\n   Is Empty {\n      Selector: foo.bar\n   }\n}\n",
		},
		"BinaryOpUnknown": {
			expr: &BinaryExpression{
				Operator: BinaryOperator(42),
				Left:     &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchIsEmpty, Value: nil},
				Right:    &MatchExpression{Selector: Selector{"foo", "bar"}, Operator: MatchIsEmpty, Value: nil},
			},
			expected: "UNKNOWN {\n   Is Empty {\n      Selector: foo.bar\n   }\n   Is Empty {\n      Selector: foo.bar\n   }\n}\n",
		},
	}

	for name, tcase := range tests {
		tcase := tcase
		t.Run(name, func(t *testing.T) {
			t.Parallel()

			buf := new(bytes.Buffer)
			tcase.expr.ExpressionDump(buf, "   ", 0)
			actual := buf.String()

			require.Equal(t, tcase.expected, actual)
		})
	}
}