aboutsummaryrefslogtreecommitdiffstats
path: root/test/json/json_parser_test.rb
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2020-09-09 15:24:22 +0200
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2020-10-20 21:40:25 +0900
commit520e0916af0fe53a5ca57269a2bae50cc60e4241 (patch)
tree0e5b9d615852b0557cc6cd95ce851d59df9d6121 /test/json/json_parser_test.rb
parentf6680c9ad1b334f094144d788887cd33f7043f41 (diff)
downloadruby-520e0916af0fe53a5ca57269a2bae50cc60e4241.tar.gz
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.
Diffstat (limited to 'test/json/json_parser_test.rb')
-rw-r--r--test/json/json_parser_test.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/test/json/json_parser_test.rb b/test/json/json_parser_test.rb
index 514441efce..e29f3f126f 100644
--- a/test/json/json_parser_test.rb
+++ b/test/json/json_parser_test.rb
@@ -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