[ruby/json] fix for pretty_generate throwing wrong number of arguments error

https://github.com/ruby/json/commit/8433571dcf
This commit is contained in:
Cody Horton 2025-05-16 09:58:27 -05:00 committed by Hiroshi SHIBATA
parent 57e4176649
commit 3468811ed1
No known key found for this signature in database
GPG Key ID: F9CF13417264FAC2
2 changed files with 17 additions and 1 deletions

View File

@ -490,7 +490,7 @@ module JSON
# }
#
def pretty_generate(obj, opts = nil)
return state.generate(obj) if State === opts
return opts.generate(obj) if State === opts
options = PRETTY_GENERATE_OPTIONS

View File

@ -122,6 +122,22 @@ class JSONGeneratorTest < Test::Unit::TestCase
assert_equal '666', pretty_generate(666)
end
def test_generate_pretty_custom
state = State.new(:space_before => "<psb>", :space => "<ps>", :indent => "<pi>", :object_nl => "\n<po_nl>\n", :array_nl => "<pa_nl>")
json = pretty_generate({1=>{}, 2=>['a','b'], 3=>4}, state)
assert_equal(<<~'JSON'.chomp, json)
{
<po_nl>
<pi>"1"<psb>:<ps>{},
<po_nl>
<pi>"2"<psb>:<ps>[<pa_nl><pi><pi>"a",<pa_nl><pi><pi>"b"<pa_nl><pi>],
<po_nl>
<pi>"3"<psb>:<ps>4
<po_nl>
}
JSON
end
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)