Skip to content

Commit 6478c6a

Browse files
committed
Use JSONTestSuite
1 parent 1b09f40 commit 6478c6a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

test/test_JSONTestSuite.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
# Test suite from "Parsing JSON is a Minefield"
6+
# https://seriot.ch/projects/parsing_json.html
7+
# https://github.com/nst/JSONTestSuite
8+
class TestJsonchecker < Minitest::Test
9+
Dir["#{DATA_DIR}/JSONTestSuite/test_parsing/*.json"].each do |filename|
10+
name = File.basename(filename, ".json")
11+
12+
define_method(:"test_#{name}") do
13+
original_json = File.read(filename)
14+
expectation = name[0]
15+
16+
if original_json.include?(0.chr)
17+
return skip("FIXME: better null handling")
18+
end
19+
20+
if expectation == "n"
21+
assert_invalid_json(original_json)
22+
elsif expectation == "y"
23+
assert_valid_json(original_json)
24+
elsif expectation == "i"
25+
assert_optional_support(original_json)
26+
else
27+
raise "unrecognized filename: #{filenme}"
28+
end
29+
end
30+
end
31+
32+
def assert_optional_support(original_json)
33+
# Whatever it is, just be consistent
34+
if RapidJSON.valid_json?(original_json)
35+
assert_valid_json(original_json)
36+
else
37+
assert_invalid_json(original_json)
38+
end
39+
end
40+
41+
def assert_valid_json(original_json)
42+
assert RapidJSON.valid_json?(original_json)
43+
RapidJSON.parse(original_json)
44+
end
45+
46+
def assert_invalid_json(original_json)
47+
refute RapidJSON.valid_json?(original_json)
48+
ex = assert_raises RapidJSON::ParseError do
49+
RapidJSON.parse(original_json)
50+
end
51+
#re = /JSON parse error: .* \(\d+\)\z/
52+
re = /JSON parse error: .*/
53+
assert_match re, ex.message
54+
end
55+
end

0 commit comments

Comments
 (0)