Skip to content

Commit

Permalink
Modernize heredocs
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Oct 24, 2024
1 parent 7a8c0b7 commit fb25e94
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 112 deletions.
28 changes: 14 additions & 14 deletions lib/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,13 @@
# json1 = JSON.generate(ruby)
# ruby1 = JSON.parse(json1, create_additions: true)
# # Make a nice display.
# display = <<EOT
# Generated JSON:
# Without addition: #{json0} (#{json0.class})
# With addition: #{json1} (#{json1.class})
# Parsed JSON:
# Without addition: #{ruby0.inspect} (#{ruby0.class})
# With addition: #{ruby1.inspect} (#{ruby1.class})
# display = <<~EOT
# Generated JSON:
# Without addition: #{json0} (#{json0.class})
# With addition: #{json1} (#{json1.class})
# Parsed JSON:
# Without addition: #{ruby0.inspect} (#{ruby0.class})
# With addition: #{ruby1.inspect} (#{ruby1.class})
# EOT
# puts display
#
Expand Down Expand Up @@ -562,13 +562,13 @@
# json1 = JSON.generate(foo1)
# obj1 = JSON.parse(json1, create_additions: true)
# # Make a nice display.
# display = <<EOT
# Generated JSON:
# Without custom addition: #{json0} (#{json0.class})
# With custom addition: #{json1} (#{json1.class})
# Parsed JSON:
# Without custom addition: #{obj0.inspect} (#{obj0.class})
# With custom addition: #{obj1.inspect} (#{obj1.class})
# display = <<~EOT
# Generated JSON:
# Without custom addition: #{json0} (#{json0.class})
# With custom addition: #{json1} (#{json1.class})
# Parsed JSON:
# Without custom addition: #{obj0.inspect} (#{obj0.class})
# With custom addition: #{obj1.inspect} (#{obj1.class})
# EOT
# puts display
#
Expand Down
44 changes: 22 additions & 22 deletions lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,17 @@ class MissingUnicodeSupport < JSONError; end
# {Parsing \JSON}[#module-JSON-label-Parsing+JSON].
#
# Parses nested JSON objects:
# source = <<-EOT
# {
# "name": "Dave",
# "age" :40,
# "hats": [
# "Cattleman's",
# "Panama",
# "Tophat"
# ]
# }
# EOT
# source = <<~JSON
# {
# "name": "Dave",
# "age" :40,
# "hats": [
# "Cattleman's",
# "Panama",
# "Tophat"
# ]
# }
# JSON
# ruby = JSON.parse(source)
# ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
#
Expand Down Expand Up @@ -445,17 +445,17 @@ class << self
# <tt>parse(source, opts)</tt>; see #parse.
#
# Source for following examples:
# source = <<-EOT
# {
# "name": "Dave",
# "age" :40,
# "hats": [
# "Cattleman's",
# "Panama",
# "Tophat"
# ]
# }
# EOT
# source = <<~JSON
# {
# "name": "Dave",
# "age" :40,
# "hats": [
# "Cattleman's",
# "Panama",
# "Tophat"
# ]
# }
# JSON
#
# Load a \String:
# ruby = JSON.load(source)
Expand Down
76 changes: 38 additions & 38 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ def setup
}
@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}'
@json3 = <<'EOT'.chomp
{
"a": 2,
"b": 3.141,
"c": "c",
"d": [
1,
"b",
3.14
],
"e": {
"foo": "bar"
},
"g": "\"\u0000\u001f",
"h": 1000.0,
"i": 0.001
}
EOT
@json3 = <<~'JSON'.chomp
{
"a": 2,
"b": 3.141,
"c": "c",
"d": [
1,
"b",
3.14
],
"e": {
"foo": "bar"
},
"g": "\"\u0000\u001f",
"h": 1000.0,
"i": 0.001
}
JSON
end

def silence
Expand Down Expand Up @@ -93,13 +93,13 @@ def test_generate_pretty
assert_equal('{}', json)

json = pretty_generate({1=>{}, 2=>[], 3=>4})
assert_equal(<<'EOT'.chomp, json)
{
"1": {},
"2": [],
"3": 4
}
EOT
assert_equal(<<~'JSON'.chomp, json)
{
"1": {},
"2": [],
"3": 4
}
JSON

json = pretty_generate(@hash)
# hashes aren't (insertion) ordered on every ruby implementation
Expand All @@ -108,11 +108,11 @@ def test_generate_pretty
parsed_json = parse(json)
assert_equal(@hash, parsed_json)
json = pretty_generate({1=>2})
assert_equal(<<'EOT'.chomp, json)
{
"1": 2
}
EOT
assert_equal(<<~'JSON'.chomp, json)
{
"1": 2
}
JSON
parsed_json = parse(json)
assert_equal({"1"=>2}, parsed_json)
assert_equal '666', pretty_generate(666)
Expand All @@ -121,14 +121,14 @@ def test_generate_pretty
def test_generate_custom
state = State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>")
json = generate({1=>{2=>3,4=>[5,6]}}, state)
assert_equal(<<'EOT'.chomp, json)
{
<i>"1" : {
<i><i>"2" : 3,
<i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>]
<i>}
}
EOT
assert_equal(<<~'JSON'.chomp, json)
{
<i>"1" : {
<i><i>"2" : 3,
<i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>]
<i>}
}
JSON
end

def test_fast_generate
Expand Down
76 changes: 38 additions & 38 deletions test/json/json_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,50 +247,50 @@ def test_freeze
end

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

Expand Down

0 comments on commit fb25e94

Please sign in to comment.