Skip to content

Commit 6f907a9

Browse files
committed
Refactor tests and make pure parser green
1 parent 0ece7a0 commit 6f907a9

22 files changed

Lines changed: 813 additions & 810 deletions

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.8.3
1+
2.0.0

json.gemspec

-475 Bytes
Binary file not shown.

json_pure.gemspec

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# -*- encoding: utf-8 -*-
2-
# stub: json_pure 1.8.3 ruby lib
2+
# stub: json_pure 2.0.0 ruby lib
33

44
Gem::Specification.new do |s|
55
s.name = "json_pure"
6-
s.version = "1.8.3"
6+
s.version = "2.0.0"
77

88
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
99
s.require_paths = ["lib"]
1010
s.authors = ["Florian Frank"]
11-
s.date = "2015-06-13"
11+
s.date = "2015-06-15"
1212
s.description = "This is a JSON implementation in pure Ruby."
1313
s.email = "flori@ping.de"
1414
s.extra_rdoc_files = ["README.rdoc"]
15-
s.files = ["./tests/test_helper.rb", "./tests/test_json.rb", "./tests/test_json_addition.rb", "./tests/test_json_encoding.rb", "./tests/test_json_fixtures.rb", "./tests/test_json_generate.rb", "./tests/test_json_generic_object.rb", "./tests/test_json_string_matching.rb", "./tests/test_json_unicode.rb", ".gitignore", ".travis.yml", "CHANGES", "COPYING", "COPYING-json-jruby", "GPL", "Gemfile", "README-json-jruby.markdown", "README.rdoc", "Rakefile", "TODO", "VERSION", "data/example.json", "data/index.html", "data/prototype.js", "diagrams/.keep", "ext/json/ext/fbuffer/fbuffer.h", "ext/json/ext/generator/depend", "ext/json/ext/generator/extconf.rb", "ext/json/ext/generator/generator.c", "ext/json/ext/generator/generator.h", "ext/json/ext/parser/depend", "ext/json/ext/parser/extconf.rb", "ext/json/ext/parser/parser.c", "ext/json/ext/parser/parser.h", "ext/json/ext/parser/parser.rl", "ext/json/extconf.rb", "install.rb", "java/src/json/ext/ByteListTranscoder.java", "java/src/json/ext/Generator.java", "java/src/json/ext/GeneratorMethods.java", "java/src/json/ext/GeneratorService.java", "java/src/json/ext/GeneratorState.java", "java/src/json/ext/OptionsReader.java", "java/src/json/ext/Parser.java", "java/src/json/ext/Parser.rl", "java/src/json/ext/ParserService.java", "java/src/json/ext/RuntimeInfo.java", "java/src/json/ext/StringDecoder.java", "java/src/json/ext/StringEncoder.java", "java/src/json/ext/Utils.java", "json-java.gemspec", "json.gemspec", "json_pure.gemspec", "lib/json.rb", "lib/json/add/bigdecimal.rb", "lib/json/add/complex.rb", "lib/json/add/core.rb", "lib/json/add/date.rb", "lib/json/add/date_time.rb", "lib/json/add/exception.rb", "lib/json/add/ostruct.rb", "lib/json/add/range.rb", "lib/json/add/rational.rb", "lib/json/add/regexp.rb", "lib/json/add/struct.rb", "lib/json/add/symbol.rb", "lib/json/add/time.rb", "lib/json/common.rb", "lib/json/ext.rb", "lib/json/ext/.keep", "lib/json/generic_object.rb", "lib/json/pure.rb", "lib/json/pure/generator.rb", "lib/json/pure/parser.rb", "lib/json/version.rb", "tests/fixtures/fail1.json", "tests/fixtures/fail10.json", "tests/fixtures/fail11.json", "tests/fixtures/fail12.json", "tests/fixtures/fail13.json", "tests/fixtures/fail14.json", "tests/fixtures/fail18.json", "tests/fixtures/fail19.json", "tests/fixtures/fail2.json", "tests/fixtures/fail20.json", "tests/fixtures/fail21.json", "tests/fixtures/fail22.json", "tests/fixtures/fail23.json", "tests/fixtures/fail24.json", "tests/fixtures/fail25.json", "tests/fixtures/fail27.json", "tests/fixtures/fail28.json", "tests/fixtures/fail3.json", "tests/fixtures/fail4.json", "tests/fixtures/fail5.json", "tests/fixtures/fail6.json", "tests/fixtures/fail7.json", "tests/fixtures/fail8.json", "tests/fixtures/fail9.json", "tests/fixtures/pass1.json", "tests/fixtures/pass15.json", "tests/fixtures/pass16.json", "tests/fixtures/pass17.json", "tests/fixtures/pass2.json", "tests/fixtures/pass26.json", "tests/fixtures/pass3.json", "tests/test_helper.rb", "tests/test_json.rb", "tests/test_json_addition.rb", "tests/test_json_encoding.rb", "tests/test_json_fixtures.rb", "tests/test_json_generate.rb", "tests/test_json_generic_object.rb", "tests/test_json_string_matching.rb", "tests/test_json_unicode.rb", "tools/fuzz.rb", "tools/server.rb"]
15+
s.files = ["./tests/test_helper.rb", ".gitignore", ".travis.yml", "CHANGES", "COPYING", "COPYING-json-jruby", "GPL", "Gemfile", "README-json-jruby.markdown", "README.rdoc", "Rakefile", "TODO", "VERSION", "data/example.json", "data/index.html", "data/prototype.js", "diagrams/.keep", "ext/json/ext/fbuffer/fbuffer.h", "ext/json/ext/generator/depend", "ext/json/ext/generator/extconf.rb", "ext/json/ext/generator/generator.c", "ext/json/ext/generator/generator.h", "ext/json/ext/parser/depend", "ext/json/ext/parser/extconf.rb", "ext/json/ext/parser/parser.c", "ext/json/ext/parser/parser.h", "ext/json/ext/parser/parser.rl", "ext/json/extconf.rb", "install.rb", "java/src/json/ext/ByteListTranscoder.java", "java/src/json/ext/Generator.java", "java/src/json/ext/GeneratorMethods.java", "java/src/json/ext/GeneratorService.java", "java/src/json/ext/GeneratorState.java", "java/src/json/ext/OptionsReader.java", "java/src/json/ext/Parser.java", "java/src/json/ext/Parser.rl", "java/src/json/ext/ParserService.java", "java/src/json/ext/RuntimeInfo.java", "java/src/json/ext/StringDecoder.java", "java/src/json/ext/StringEncoder.java", "java/src/json/ext/Utils.java", "json-java.gemspec", "json.gemspec", "json_pure.gemspec", "lib/json.rb", "lib/json/add/bigdecimal.rb", "lib/json/add/complex.rb", "lib/json/add/core.rb", "lib/json/add/date.rb", "lib/json/add/date_time.rb", "lib/json/add/exception.rb", "lib/json/add/ostruct.rb", "lib/json/add/range.rb", "lib/json/add/rational.rb", "lib/json/add/regexp.rb", "lib/json/add/struct.rb", "lib/json/add/symbol.rb", "lib/json/add/time.rb", "lib/json/common.rb", "lib/json/ext.rb", "lib/json/ext/.keep", "lib/json/generic_object.rb", "lib/json/pure.rb", "lib/json/pure/generator.rb", "lib/json/pure/parser.rb", "lib/json/version.rb", "tests/fixtures/fail10.json", "tests/fixtures/fail11.json", "tests/fixtures/fail12.json", "tests/fixtures/fail13.json", "tests/fixtures/fail14.json", "tests/fixtures/fail18.json", "tests/fixtures/fail19.json", "tests/fixtures/fail2.json", "tests/fixtures/fail20.json", "tests/fixtures/fail21.json", "tests/fixtures/fail22.json", "tests/fixtures/fail23.json", "tests/fixtures/fail24.json", "tests/fixtures/fail25.json", "tests/fixtures/fail27.json", "tests/fixtures/fail28.json", "tests/fixtures/fail3.json", "tests/fixtures/fail4.json", "tests/fixtures/fail5.json", "tests/fixtures/fail6.json", "tests/fixtures/fail7.json", "tests/fixtures/fail8.json", "tests/fixtures/fail9.json", "tests/fixtures/obsolete_fail1.json", "tests/fixtures/pass1.json", "tests/fixtures/pass15.json", "tests/fixtures/pass16.json", "tests/fixtures/pass17.json", "tests/fixtures/pass2.json", "tests/fixtures/pass26.json", "tests/fixtures/pass3.json", "tests/json_addition_test.rb", "tests/json_common_interface_test.rb", "tests/json_encoding_test.rb", "tests/json_ext_parser_test.rb", "tests/json_fixtures_test.rb", "tests/json_generator_test.rb", "tests/json_generic_object_test.rb", "tests/json_parser_test.rb", "tests/json_string_matching_test.rb", "tests/test_helper.rb", "tools/fuzz.rb", "tools/server.rb"]
1616
s.homepage = "http://flori.github.com/json"
1717
s.licenses = ["Ruby"]
1818
s.rdoc_options = ["--title", "JSON implemention for ruby", "--main", "README.rdoc"]
1919
s.rubygems_version = "2.4.6"
2020
s.summary = "JSON Implementation for Ruby"
21-
s.test_files = ["./tests/test_helper.rb", "./tests/test_json.rb", "./tests/test_json_addition.rb", "./tests/test_json_encoding.rb", "./tests/test_json_fixtures.rb", "./tests/test_json_generate.rb", "./tests/test_json_generic_object.rb", "./tests/test_json_string_matching.rb", "./tests/test_json_unicode.rb"]
21+
s.test_files = ["./tests/test_helper.rb"]
2222

2323
if s.respond_to? :specification_version then
2424
s.specification_version = 4

lib/json/common.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
module JSON
55
class << self
6-
# If _object_ is string-like, parse the string and return the parsed result
7-
# as a Ruby data structure. Otherwise generate a JSON text from the Ruby
8-
# data structure object and return it.
6+
# If _object_ is string-like, parse the string and return the parsed
7+
# result as a Ruby data structure. Otherwise generate a JSON text from the
8+
# Ruby data structure object and return it.
99
#
10-
# The _opts_ argument is passed through to generate/parse respectively. See
11-
# generate and parse for their documentation.
10+
# The _opts_ argument is passed through to generate/parse respectively.
11+
# See generate and parse for their documentation.
1212
def [](object, opts = {})
1313
if object.respond_to? :to_str
1414
JSON.parse(object.to_str, opts)
@@ -138,8 +138,8 @@ class MissingUnicodeSupport < JSONError; end
138138
# _opts_ can have the following
139139
# keys:
140140
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
141-
# structures. Disable depth checking with :max_nesting => false. It defaults
142-
# to 100.
141+
# structures. Disable depth checking with :max_nesting => false. It
142+
# defaults to 100.
143143
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
144144
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
145145
# to false.
@@ -161,9 +161,9 @@ def parse(source, opts = {})
161161
#
162162
# _opts_ can have the following keys:
163163
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
164-
# structures. Enable depth checking with :max_nesting => anInteger. The parse!
165-
# methods defaults to not doing max depth checking: This can be dangerous
166-
# if someone wants to fill up your stack.
164+
# structures. Enable depth checking with :max_nesting => anInteger. The
165+
# parse! methods defaults to not doing max depth checking: This can be
166+
# dangerous if someone wants to fill up your stack.
167167
# * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
168168
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
169169
# to true.

lib/json/pure/parser.rb

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Parser < StringScanner
4848
)+
4949
)mx
5050

51-
UNPARSED = Object.new
51+
UNPARSED = Object.new.freeze
5252

5353
# Creates a new JSON::Pure::Parser instance for the string _source_.
5454
#
@@ -107,39 +107,21 @@ def reset
107107
@current_nesting = 0
108108
end
109109

110-
# Parses the current JSON string _source_ and returns the complete data
111-
# structure as a result.
110+
# Parses the current JSON string _source_ and returns the
111+
# complete data structure as a result.
112112
def parse
113113
reset
114114
obj = nil
115-
if @quirks_mode
116-
while !eos? && skip(IGNORE)
117-
end
118-
if eos?
119-
raise ParserError, "source did not contain any JSON!"
120-
else
121-
obj = parse_value
122-
obj == UNPARSED and raise ParserError, "source did not contain any JSON!"
123-
end
115+
while !eos? && skip(IGNORE) do end
116+
if eos?
117+
raise ParserError, "source is not valid JSON!"
124118
else
125-
until eos?
126-
case
127-
when scan(OBJECT_OPEN)
128-
obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
129-
@current_nesting = 1
130-
obj = parse_object
131-
when scan(ARRAY_OPEN)
132-
obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
133-
@current_nesting = 1
134-
obj = parse_array
135-
when skip(IGNORE)
136-
;
137-
else
138-
raise ParserError, "source '#{peek(20)}' not in JSON!"
139-
end
140-
end
141-
obj or raise ParserError, "source did not contain any JSON!"
119+
obj = parse_value
120+
UNPARSED.equal?(obj) and raise ParserError,
121+
"source is not valid JSON!"
142122
end
123+
while !eos? && skip(IGNORE) do end
124+
eos? or raise ParserError, "source is not valid JSON!"
143125
obj
144126
end
145127

@@ -149,7 +131,8 @@ def convert_encoding(source)
149131
if source.respond_to?(:to_str)
150132
source = source.to_str
151133
else
152-
raise TypeError, "#{source.inspect} is not like a string"
134+
raise TypeError,
135+
"#{source.inspect} is not like a string"
153136
end
154137
if defined?(::Encoding)
155138
source = source.encode(::Encoding::UTF_8)
@@ -222,7 +205,7 @@ def parse_value
222205
false
223206
when scan(NULL)
224207
nil
225-
when (string = parse_string) != UNPARSED
208+
when !UNPARSED.equal?(string = parse_string)
226209
string
227210
when scan(ARRAY_OPEN)
228211
@current_nesting += 1
@@ -252,7 +235,7 @@ def parse_array
252235
delim = false
253236
until eos?
254237
case
255-
when (value = parse_value) != UNPARSED
238+
when !UNPARSED.equal?(value = parse_value)
256239
delim = false
257240
result << value
258241
skip(IGNORE)
@@ -284,13 +267,13 @@ def parse_object
284267
delim = false
285268
until eos?
286269
case
287-
when (string = parse_string) != UNPARSED
270+
when !UNPARSED.equal?(string = parse_string)
288271
skip(IGNORE)
289272
unless scan(PAIR_DELIMITER)
290273
raise ParserError, "expected ':' in object at '#{peek(20)}'!"
291274
end
292275
skip(IGNORE)
293-
unless (value = parse_value).equal? UNPARSED
276+
unless UNPARSED.equal?(value = parse_value)
294277
result[@symbolize_names ? string.to_sym : string] = value
295278
delim = false
296279
skip(IGNORE)

lib/json/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module JSON
22
# JSON version
3-
VERSION = '1.8.3'
3+
VERSION = '2.0.0'
44
VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
55
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
66
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:

tests/fixtures/fail1.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/fixtures/obsolete_fail1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"A JSON payload should be an object or array, not a string."
Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#!/usr/bin/env ruby
2-
# -*- coding:utf-8 -*-
3-
41
require 'test_helper'
52
require 'json/add/core'
63
require 'json/add/complex'
@@ -9,7 +6,7 @@
96
require 'json/add/ostruct'
107
require 'date'
118

12-
class TestJSONAddition < Test::Unit::TestCase
9+
class JSONAdditionTest < Test::Unit::TestCase
1310
include JSON
1411

1512
class A
@@ -63,7 +60,7 @@ def self.json_creatable?
6360

6461
def to_json(*args)
6562
{
66-
'json_class' => 'TestJSONAddition::Nix',
63+
'json_class' => 'JSONAdditionTest::Nix',
6764
}.to_json(*args)
6865
end
6966
end
@@ -72,7 +69,7 @@ def test_extended_json
7269
a = A.new(666)
7370
assert A.json_creatable?
7471
json = generate(a)
75-
a_again = JSON.parse(json, :create_additions => true)
72+
a_again = parse(json, :create_additions => true)
7673
assert_kind_of a.class, a_again
7774
assert_equal a, a_again
7875
end
@@ -81,21 +78,21 @@ def test_extended_json_default
8178
a = A.new(666)
8279
assert A.json_creatable?
8380
json = generate(a)
84-
a_hash = JSON.parse(json)
81+
a_hash = parse(json)
8582
assert_kind_of Hash, a_hash
8683
end
8784

8885
def test_extended_json_disabled
8986
a = A.new(666)
9087
assert A.json_creatable?
9188
json = generate(a)
92-
a_again = JSON.parse(json, :create_additions => true)
89+
a_again = parse(json, :create_additions => true)
9390
assert_kind_of a.class, a_again
9491
assert_equal a, a_again
95-
a_hash = JSON.parse(json, :create_additions => false)
92+
a_hash = parse(json, :create_additions => false)
9693
assert_kind_of Hash, a_hash
9794
assert_equal(
98-
{"args"=>[666], "json_class"=>"TestJSONAddition::A"}.sort_by { |k,| k },
95+
{"args"=>[666], "json_class"=>"JSONAdditionTest::A"}.sort_by { |k,| k },
9996
a_hash.sort_by { |k,| k }
10097
)
10198
end
@@ -104,14 +101,14 @@ def test_extended_json_fail1
104101
b = B.new
105102
assert !B.json_creatable?
106103
json = generate(b)
107-
assert_equal({ "json_class"=>"TestJSONAddition::B" }, JSON.parse(json))
104+
assert_equal({ "json_class"=>"JSONAdditionTest::B" }, parse(json))
108105
end
109106

110107
def test_extended_json_fail2
111108
c = C.new
112109
assert !C.json_creatable?
113110
json = generate(c)
114-
assert_raises(ArgumentError, NameError) { JSON.parse(json, :create_additions => true) }
111+
assert_raises(ArgumentError, NameError) { parse(json, :create_additions => true) }
115112
end
116113

117114
def test_raw_strings
@@ -129,7 +126,7 @@ def test_raw_strings
129126
assert_match(/\A\{.*\}\z/, json)
130127
assert_match(/"json_class":"String"/, json)
131128
assert_match(/"raw":\[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255\]/, json)
132-
raw_again = JSON.parse(json, :create_additions => true)
129+
raw_again = parse(json, :create_additions => true)
133130
assert_equal raw, raw_again
134131
end
135132

@@ -166,19 +163,19 @@ def test_core
166163

167164
def test_utc_datetime
168165
now = Time.now
169-
d = DateTime.parse(now.to_s, :create_additions => true) # usual case
170-
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
171-
d = DateTime.parse(now.utc.to_s) # of = 0
172-
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
166+
d = DateTime.parse(now.to_s, :create_additions => true) # usual case
167+
assert_equal d, parse(d.to_json, :create_additions => true)
168+
d = DateTime.parse(now.utc.to_s) # of = 0
169+
assert_equal d, parse(d.to_json, :create_additions => true)
173170
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24))
174-
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
171+
assert_equal d, parse(d.to_json, :create_additions => true)
175172
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
176-
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
173+
assert_equal d, parse(d.to_json, :create_additions => true)
177174
end
178175

179176
def test_rational_complex
180-
assert_equal Rational(2, 9), JSON.parse(JSON(Rational(2, 9)), :create_additions => true)
181-
assert_equal Complex(2, 9), JSON.parse(JSON(Complex(2, 9)), :create_additions => true)
177+
assert_equal Rational(2, 9), parse(JSON(Rational(2, 9)), :create_additions => true)
178+
assert_equal Complex(2, 9), parse(JSON(Complex(2, 9)), :create_additions => true)
182179
end
183180

184181
def test_bigdecimal
@@ -190,6 +187,6 @@ def test_ostruct
190187
o = OpenStruct.new
191188
# XXX this won't work; o.foo = { :bar => true }
192189
o.foo = { 'bar' => true }
193-
assert_equal o, JSON.parse(JSON(o), :create_additions => true)
190+
assert_equal o, parse(JSON(o), :create_additions => true)
194191
end
195192
end

0 commit comments

Comments
 (0)