cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/utils/binary_expr_test.go
61lines · modecode
| 1 | package utils_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/require" |
| 7 | |
| 8 | "github.com/cloudflare/pint/internal/parser" |
| 9 | "github.com/cloudflare/pint/internal/parser/utils" |
| 10 | ) |
| 11 | |
| 12 | func TestHasOuterBinaryExpr(t *testing.T) { |
| 13 | type testCaseT struct { |
| 14 | expr string |
| 15 | output string |
| 16 | } |
| 17 | |
| 18 | testCases := []testCaseT{ |
| 19 | { |
| 20 | expr: "foo", |
| 21 | }, |
| 22 | { |
| 23 | expr: "foo / bar", |
| 24 | output: "foo / bar", |
| 25 | }, |
| 26 | { |
| 27 | expr: "(foo / bar)", |
| 28 | output: "foo / bar", |
| 29 | }, |
| 30 | { |
| 31 | expr: "(foo / bar) / bob", |
| 32 | output: "(foo / bar) / bob", |
| 33 | }, |
| 34 | { |
| 35 | expr: "foo / bar / bob", |
| 36 | output: "foo / bar / bob", |
| 37 | }, |
| 38 | { |
| 39 | expr: "foo / bar > 0", |
| 40 | output: "foo / bar > 0", |
| 41 | }, |
| 42 | } |
| 43 | |
| 44 | for _, tc := range testCases { |
| 45 | t.Run(tc.expr, func(t *testing.T) { |
| 46 | n, err := parser.DecodeExpr(tc.expr) |
| 47 | if err != nil { |
| 48 | t.Error(err) |
| 49 | t.FailNow() |
| 50 | } |
| 51 | bin := utils.HasOuterBinaryExpr(n) |
| 52 | if bin == nil { |
| 53 | if tc.output != "" { |
| 54 | t.Errorf("HasOuterBinaryExpr() returned nil, expected %s", tc.output) |
| 55 | } |
| 56 | } else { |
| 57 | require.Equal(t, tc.output, bin.String(), "HasOuterBinaryExpr() returned wrong output") |
| 58 | } |
| 59 | }) |
| 60 | } |
| 61 | } |
| 62 | |