mirror of
https://github.com/ruby/ruby.git
synced 2025-08-16 05:59:00 +02:00
Introduce JSON::Coder
Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
This commit is contained in:
parent
53cf2170f9
commit
89e316ad06
5 changed files with 177 additions and 3 deletions
38
test/json/json_coder_test.rb
Executable file
38
test/json/json_coder_test.rb
Executable file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'test_helper'
|
||||
|
||||
class JSONCoderTest < Test::Unit::TestCase
|
||||
def test_json_coder_with_proc
|
||||
coder = JSON::Coder.new do |object|
|
||||
"[Object object]"
|
||||
end
|
||||
assert_equal %(["[Object object]"]), coder.dump([Object.new])
|
||||
end
|
||||
|
||||
def test_json_coder_with_proc_with_unsupported_value
|
||||
coder = JSON::Coder.new do |object|
|
||||
Object.new
|
||||
end
|
||||
assert_raise(JSON::GeneratorError) { coder.dump([Object.new]) }
|
||||
end
|
||||
|
||||
def test_json_coder_options
|
||||
coder = JSON::Coder.new(array_nl: "\n") do |object|
|
||||
42
|
||||
end
|
||||
|
||||
assert_equal "[\n42\n]", coder.dump([Object.new])
|
||||
end
|
||||
|
||||
def test_json_coder_load
|
||||
coder = JSON::Coder.new
|
||||
assert_equal [1,2,3], coder.load("[1,2,3]")
|
||||
end
|
||||
|
||||
def test_json_coder_load_options
|
||||
coder = JSON::Coder.new(symbolize_names: true)
|
||||
assert_equal({a: 1}, coder.load('{"a":1}'))
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue