mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Implement a freeze: parser option
If set to true all parsed objects will be immediately frozen, and strings will be deduplicated if the Ruby implementation allows it.
This commit is contained in:
parent
f6680c9ad1
commit
520e0916af
6 changed files with 147 additions and 45 deletions
|
@ -218,6 +218,17 @@ class JSONParserTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_freeze
|
||||
assert_predicate parse('{}', :freeze => true), :frozen?
|
||||
assert_predicate parse('[]', :freeze => true), :frozen?
|
||||
assert_predicate parse('"foo"', :freeze => true), :frozen?
|
||||
|
||||
if string_deduplication_available?
|
||||
assert_same -'foo', parse('"foo"', :freeze => true)
|
||||
assert_same -'foo', parse('{"foo": 1}', :freeze => true).keys.first
|
||||
end
|
||||
end
|
||||
|
||||
def test_parse_comments
|
||||
json = <<EOT
|
||||
{
|
||||
|
@ -468,6 +479,16 @@ EOT
|
|||
|
||||
private
|
||||
|
||||
def string_deduplication_available?
|
||||
r1 = rand.to_s
|
||||
r2 = r1.dup
|
||||
begin
|
||||
(-r1).equal?(-r2)
|
||||
rescue NoMethodError
|
||||
false # No String#-@
|
||||
end
|
||||
end
|
||||
|
||||
def assert_equal_float(expected, actual, delta = 1e-2)
|
||||
Array === expected and expected = expected.first
|
||||
Array === actual and actual = actual.first
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue