[ruby/json] Modernize heredocs

https://github.com/ruby/json/commit/fb25e94aea
This commit is contained in:
Jean Boussier 2024-10-24 11:42:02 +02:00 committed by Hiroshi SHIBATA
parent bfdf02ea72
commit 1045b9f820
4 changed files with 112 additions and 112 deletions

View File

@ -378,13 +378,13 @@ require 'json/common'
# json1 = JSON.generate(ruby) # json1 = JSON.generate(ruby)
# ruby1 = JSON.parse(json1, create_additions: true) # ruby1 = JSON.parse(json1, create_additions: true)
# # Make a nice display. # # Make a nice display.
# display = <<EOT # display = <<~EOT
# Generated JSON: # Generated JSON:
# Without addition: #{json0} (#{json0.class}) # Without addition: #{json0} (#{json0.class})
# With addition: #{json1} (#{json1.class}) # With addition: #{json1} (#{json1.class})
# Parsed JSON: # Parsed JSON:
# Without addition: #{ruby0.inspect} (#{ruby0.class}) # Without addition: #{ruby0.inspect} (#{ruby0.class})
# With addition: #{ruby1.inspect} (#{ruby1.class}) # With addition: #{ruby1.inspect} (#{ruby1.class})
# EOT # EOT
# puts display # puts display
# #
@ -562,13 +562,13 @@ require 'json/common'
# json1 = JSON.generate(foo1) # json1 = JSON.generate(foo1)
# obj1 = JSON.parse(json1, create_additions: true) # obj1 = JSON.parse(json1, create_additions: true)
# # Make a nice display. # # Make a nice display.
# display = <<EOT # display = <<~EOT
# Generated JSON: # Generated JSON:
# Without custom addition: #{json0} (#{json0.class}) # Without custom addition: #{json0} (#{json0.class})
# With custom addition: #{json1} (#{json1.class}) # With custom addition: #{json1} (#{json1.class})
# Parsed JSON: # Parsed JSON:
# Without custom addition: #{obj0.inspect} (#{obj0.class}) # Without custom addition: #{obj0.inspect} (#{obj0.class})
# With custom addition: #{obj1.inspect} (#{obj1.class}) # With custom addition: #{obj1.inspect} (#{obj1.class})
# EOT # EOT
# puts display # puts display
# #

View File

@ -195,17 +195,17 @@ module JSON
# {Parsing \JSON}[#module-JSON-label-Parsing+JSON]. # {Parsing \JSON}[#module-JSON-label-Parsing+JSON].
# #
# Parses nested JSON objects: # Parses nested JSON objects:
# source = <<-EOT # source = <<~JSON
# { # {
# "name": "Dave", # "name": "Dave",
# "age" :40, # "age" :40,
# "hats": [ # "hats": [
# "Cattleman's", # "Cattleman's",
# "Panama", # "Panama",
# "Tophat" # "Tophat"
# ] # ]
# } # }
# EOT # JSON
# ruby = JSON.parse(source) # ruby = JSON.parse(source)
# ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]} # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
# #
@ -445,17 +445,17 @@ module JSON
# <tt>parse(source, opts)</tt>; see #parse. # <tt>parse(source, opts)</tt>; see #parse.
# #
# Source for following examples: # Source for following examples:
# source = <<-EOT # source = <<~JSON
# { # {
# "name": "Dave", # "name": "Dave",
# "age" :40, # "age" :40,
# "hats": [ # "hats": [
# "Cattleman's", # "Cattleman's",
# "Panama", # "Panama",
# "Tophat" # "Tophat"
# ] # ]
# } # }
# EOT # JSON
# #
# Load a \String: # Load a \String:
# ruby = JSON.load(source) # ruby = JSON.load(source)

View File

@ -19,24 +19,24 @@ class JSONGeneratorTest < Test::Unit::TestCase
} }
@json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' + @json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}' '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
@json3 = <<'EOT'.chomp @json3 = <<~'JSON'.chomp
{ {
"a": 2, "a": 2,
"b": 3.141, "b": 3.141,
"c": "c", "c": "c",
"d": [ "d": [
1, 1,
"b", "b",
3.14 3.14
], ],
"e": { "e": {
"foo": "bar" "foo": "bar"
}, },
"g": "\"\u0000\u001f", "g": "\"\u0000\u001f",
"h": 1000.0, "h": 1000.0,
"i": 0.001 "i": 0.001
} }
EOT JSON
end end
def silence def silence
@ -93,13 +93,13 @@ EOT
assert_equal('{}', json) assert_equal('{}', json)
json = pretty_generate({1=>{}, 2=>[], 3=>4}) json = pretty_generate({1=>{}, 2=>[], 3=>4})
assert_equal(<<'EOT'.chomp, json) assert_equal(<<~'JSON'.chomp, json)
{ {
"1": {}, "1": {},
"2": [], "2": [],
"3": 4 "3": 4
} }
EOT JSON
json = pretty_generate(@hash) json = pretty_generate(@hash)
# hashes aren't (insertion) ordered on every ruby implementation # hashes aren't (insertion) ordered on every ruby implementation
@ -108,11 +108,11 @@ EOT
parsed_json = parse(json) parsed_json = parse(json)
assert_equal(@hash, parsed_json) assert_equal(@hash, parsed_json)
json = pretty_generate({1=>2}) json = pretty_generate({1=>2})
assert_equal(<<'EOT'.chomp, json) assert_equal(<<~'JSON'.chomp, json)
{ {
"1": 2 "1": 2
} }
EOT JSON
parsed_json = parse(json) parsed_json = parse(json)
assert_equal({"1"=>2}, parsed_json) assert_equal({"1"=>2}, parsed_json)
assert_equal '666', pretty_generate(666) assert_equal '666', pretty_generate(666)
@ -121,14 +121,14 @@ EOT
def test_generate_custom def test_generate_custom
state = State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>") state = State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>")
json = generate({1=>{2=>3,4=>[5,6]}}, state) json = generate({1=>{2=>3,4=>[5,6]}}, state)
assert_equal(<<'EOT'.chomp, json) assert_equal(<<~'JSON'.chomp, json)
{ {
<i>"1" : { <i>"1" : {
<i><i>"2" : 3, <i><i>"2" : 3,
<i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>] <i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>]
<i>} <i>}
} }
EOT JSON
end end
def test_fast_generate def test_fast_generate

View File

@ -247,50 +247,50 @@ class JSONParserTest < Test::Unit::TestCase
end end
def test_parse_comments def test_parse_comments
json = <<EOT json = <<~JSON
{ {
"key1":"value1", // eol comment "key1":"value1", // eol comment
"key2":"value2" /* multi line "key2":"value2" /* multi line
* comment */, * comment */,
"key3":"value3" /* multi line "key3":"value3" /* multi line
// nested eol comment // nested eol comment
* comment */ * comment */
} }
EOT JSON
assert_equal( assert_equal(
{ "key1" => "value1", "key2" => "value2", "key3" => "value3" }, { "key1" => "value1", "key2" => "value2", "key3" => "value3" },
parse(json)) parse(json))
json = <<EOT json = <<~JSON
{ {
"key1":"value1" /* multi line "key1":"value1" /* multi line
// nested eol comment // nested eol comment
/* illegal nested multi line comment */ /* illegal nested multi line comment */
* comment */ * comment */
} }
EOT JSON
assert_raise(ParserError) { parse(json) } assert_raise(ParserError) { parse(json) }
json = <<EOT json = <<~JSON
{ {
"key1":"value1" /* multi line "key1":"value1" /* multi line
// nested eol comment // nested eol comment
/* legal nested multi line comment start sequence */ /* legal nested multi line comment start sequence */
} }
EOT JSON
assert_equal({ "key1" => "value1" }, parse(json)) assert_equal({ "key1" => "value1" }, parse(json))
json = <<EOT json = <<~JSON
{ {
"key1":"value1" /* multi line "key1":"value1" /* multi line
// nested eol comment // nested eol comment
closed multi comment */ closed multi comment */
and again, throw an Error */ and again, throw an Error */
} }
EOT JSON
assert_raise(ParserError) { parse(json) } assert_raise(ParserError) { parse(json) }
json = <<EOT json = <<~JSON
{ {
"key1":"value1" /*/*/ "key1":"value1" /*/*/
} }
EOT JSON
assert_equal({ "key1" => "value1" }, parse(json)) assert_equal({ "key1" => "value1" }, parse(json))
end end