|
| 1 | +//go:build compare |
| 2 | + |
| 3 | +// Package compare tests theory/jsonpath against the [json-path-comparison] |
| 4 | +// project's regression suite. It requires the file regression_suite.yaml to |
| 5 | +// be in this directory. The test only runs with the "compare" tag. Use make |
| 6 | +// for the easiest way to download regression_suite.yaml and run the tests: |
| 7 | +// |
| 8 | +// make test-compare |
| 9 | +// |
| 10 | +// [json-path-comparison]: https://github.com/cburgmer/json-path-comparison |
| 11 | +package compare |
| 12 | + |
| 13 | +import ( |
| 14 | + "os" |
| 15 | + "path/filepath" |
| 16 | + "runtime" |
| 17 | + "testing" |
| 18 | + |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | + "github.com/theory/jsonpath" |
| 22 | + "gopkg.in/yaml.v3" |
| 23 | +) |
| 24 | + |
| 25 | +type Query struct { |
| 26 | + ID string `yaml:"id"` |
| 27 | + Selector string `yaml:"selector"` |
| 28 | + Document any `yaml:"document"` |
| 29 | + Consensus any `yaml:"consensus"` |
| 30 | + Ordered bool `yaml:"ordered"` |
| 31 | +} |
| 32 | + |
| 33 | +func file(t *testing.T) string { |
| 34 | + t.Helper() |
| 35 | + _, fn, _, ok := runtime.Caller(0) |
| 36 | + assert.True(t, ok) |
| 37 | + return filepath.Clean(filepath.Join( |
| 38 | + filepath.Dir(fn), |
| 39 | + "regression_suite.yaml", |
| 40 | + )) |
| 41 | +} |
| 42 | + |
| 43 | +func queries(t *testing.T) []Query { |
| 44 | + t.Helper() |
| 45 | + data, err := os.ReadFile(file(t)) |
| 46 | + require.NoError(t, err) |
| 47 | + var q struct { |
| 48 | + Queries []Query `yaml:"queries"` |
| 49 | + } |
| 50 | + require.NoError(t, yaml.Unmarshal(data, &q)) |
| 51 | + return q.Queries |
| 52 | +} |
| 53 | + |
| 54 | +func TestConsensus(t *testing.T) { |
| 55 | + t.Parallel() |
| 56 | + |
| 57 | + skip := map[string]string{ |
| 58 | + "array_slice_with_step_and_leading_zeros": "leading zeros disallowed integers; see RFC 9535 sections 2.3.3.1, 2.3.4.1", |
| 59 | + "dot_notation_with_number_on_object": "leading digits disallowed in shorthand names; see RFC 9535 section 2.5.1.1", |
| 60 | + "dot_notation_with_dash": "dash disallowed in shorthand hames; see RFC 9535 section 2.5.1.1", |
| 61 | + } |
| 62 | + |
| 63 | + for _, q := range queries(t) { |
| 64 | + t.Run(q.ID, func(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + |
| 67 | + if q.Consensus == "NOT_SUPPORTED" { |
| 68 | + t.Skip(q.Consensus) |
| 69 | + } |
| 70 | + |
| 71 | + if r, ok := skip[q.ID]; ok { |
| 72 | + t.Skip(r) |
| 73 | + } |
| 74 | + |
| 75 | + path, err := jsonpath.Parse(q.Selector) |
| 76 | + // XXX Why is consensus empty? |
| 77 | + if q.Consensus != nil { |
| 78 | + require.NoError(t, err) |
| 79 | + } |
| 80 | + if err != nil { |
| 81 | + // XXX Why is there an error? TODOs? |
| 82 | + assert.Nil(t, path) |
| 83 | + return |
| 84 | + } |
| 85 | + result := []any(path.Select(q.Document)) |
| 86 | + |
| 87 | + switch { |
| 88 | + case q.Ordered: |
| 89 | + assert.Equal(t, q.Consensus, result) |
| 90 | + case q.Consensus == nil: |
| 91 | + // XXX What to do here? |
| 92 | + // assert.Empty(t, result) |
| 93 | + default: |
| 94 | + assert.ElementsMatch(t, q.Consensus, result) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments