diff options
author | Étienne Barrié <[email protected]> | 2024-12-16 12:25:48 +0100 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2025-01-28 15:41:47 +0900 |
commit | 89e316ad06f28f473030da04f33711328dc143c5 (patch) | |
tree | e4cb2ae7b487c195576f58b835ad0f91191d4098 /test/json/json_coder_test.rb | |
parent | 53cf2170f9fb1b2686ba3927609833a821c64b46 (diff) |
Introduce JSON::Coder
Co-authored-by: Jean Boussier <[email protected]>
Diffstat (limited to 'test/json/json_coder_test.rb')
-rwxr-xr-x | test/json/json_coder_test.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/test/json/json_coder_test.rb b/test/json/json_coder_test.rb new file mode 100755 index 0000000000..37331c4eb8 --- /dev/null +++ b/test/json/json_coder_test.rb @@ -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 |