diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..3b9355f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "test/jsonpath-compliance-test-suite"] + path = test/jsonpath-compliance-test-suite + url = https://github.com/jsonpath-standard/jsonpath-compliance-test-suite.git diff --git a/test/jsonpath-compliance-test-suite b/test/jsonpath-compliance-test-suite new file mode 160000 index 0000000..9cf4a75 --- /dev/null +++ b/test/jsonpath-compliance-test-suite @@ -0,0 +1 @@ +Subproject commit 9cf4a7517828d4f18557959682a4767de4735f94 diff --git a/test/test_jsonpath_compliance.rb b/test/test_jsonpath_compliance.rb new file mode 100644 index 0000000..44700e2 --- /dev/null +++ b/test/test_jsonpath_compliance.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'minitest/autorun' +require 'jsonpath' +require 'json' + +class TestJsonpathCompliance < MiniTest::Unit::TestCase + skipped = [ + 'whitespace, filter, space between parenthesized expression and bracket', + 'whitespace, filter, return between parenthesized expression and bracket', + 'filter, group terms, left', + 'whitespace, filter, tab between parenthesized expression and bracket' + ] + JSON.parse(File.read('test/jsonpath-compliance-test-suite/cts.json')).fetch('tests').each_with_index do |test, index| + name = test['name'] + method_name = name.gsub(/[^a-z0-9]+/, '_').sub(/_$/, '') + define_method("test_jsonpath_compliance_#{method_name}") do + skip "#{name} – skipping" if skipped.include?(name) + if test.key?('result') + result = Timeout.timeout(1) do + JsonPath.new(test.fetch('selector')).on(test['document']) + end + assert_equal(test['result'], result, "Test: #{name.inspect}") + elsif test['invalid_selector'] + skip "skipping #{name} – invalid_selector #{test.fetch('selector').inspect}" + else + raise "don't know how to test #{name} – #{test.inspect}" + end + end + end +end