|
| 1 | +package criterion |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/speakeasy-api/openapi/arazzo/expression" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNewCondition(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + testCases := map[string]struct { |
| 15 | + raw string |
| 16 | + expected *Condition |
| 17 | + expectedError error |
| 18 | + }{ |
| 19 | + "empty string": { |
| 20 | + raw: "", |
| 21 | + expected: nil, |
| 22 | + }, |
| 23 | + "expression only": { |
| 24 | + raw: "$statusCode", |
| 25 | + expected: nil, |
| 26 | + expectedError: fmt.Errorf("condition must at least be in the format [expression] [operator] [value]"), |
| 27 | + }, |
| 28 | + "expression and operator only": { |
| 29 | + raw: "$statusCode ==", |
| 30 | + expected: nil, |
| 31 | + expectedError: fmt.Errorf("condition must at least be in the format [expression] [operator] [value]"), |
| 32 | + }, |
| 33 | + "$statusCode == 200": { |
| 34 | + raw: "$statusCode == 200", |
| 35 | + expected: &Condition{ |
| 36 | + Expression: expression.Expression("$statusCode"), |
| 37 | + Operator: OperatorEQ, |
| 38 | + Value: "200", |
| 39 | + }, |
| 40 | + }, |
| 41 | + "$response.body#/test == 'string literal with spaces'": { |
| 42 | + raw: "$response.body#/test == 'string literal with spaces'", |
| 43 | + expected: &Condition{ |
| 44 | + Expression: expression.Expression("$response.body#/test"), |
| 45 | + Operator: OperatorEQ, |
| 46 | + Value: "'string literal with spaces'", |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + for testName, testCase := range testCases { |
| 52 | + t.Run(testName, func(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + |
| 55 | + actual, actualError := newCondition(testCase.raw) |
| 56 | + |
| 57 | + if testCase.expectedError != nil { |
| 58 | + assert.EqualError(t, actualError, testCase.expectedError.Error()) |
| 59 | + } |
| 60 | + |
| 61 | + assert.EqualExportedValues(t, testCase.expected, actual) |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
0 commit comments