-
-
Notifications
You must be signed in to change notification settings - Fork 267
Hand to_json an indent String when Oj.dump was given an Integer #1107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #!/usr/bin/env ruby | ||
| # frozen_string_literal: true | ||
|
|
||
| # The json gem is loaded before Oj on purpose. Time, Date, Symbol, and the | ||
| # other core classes then answer to_json with the json gem's generator, which | ||
| # is what Oj.dump calls in compat mode with use_to_json. Kept in its own file | ||
| # because loading the json gem changes what every other compat test would | ||
| # dump. | ||
|
|
||
| $LOAD_PATH << __dir__ | ||
| @oj_dir = File.dirname(File.expand_path(__dir__)) | ||
| %w(lib ext).each do |dir| | ||
| $LOAD_PATH << File.join(@oj_dir, dir) | ||
| end | ||
|
|
||
| require 'minitest' | ||
| require 'minitest/autorun' | ||
| require 'json' | ||
| require 'oj' | ||
|
|
||
| class ToJsonJsonGemJuice < Minitest::Test | ||
|
|
||
| def setup | ||
| @default_options = Oj.default_options | ||
| # Set here rather than per call so the hash Oj.dump forwards to to_json | ||
| # holds only the keys under test. | ||
| Oj.default_options = { :mode => :compat, :use_to_json => true } | ||
| end | ||
|
|
||
| def teardown | ||
| Oj.default_options = @default_options | ||
| end | ||
|
|
||
| # The json gem's indent is a String. Oj's Integer indent must reach it as | ||
| # that many spaces. | ||
| def test_dump_time_integer_indent | ||
| t = Time.at(1_355_218_745).utc | ||
| assert_equal('"2012-12-11 09:39:05 UTC"', Oj.dump(t, :indent => 2)) | ||
| end | ||
|
|
||
| def test_dump_time_string_indent | ||
| t = Time.at(1_355_218_745).utc | ||
| assert_equal('"2012-12-11 09:39:05 UTC"', Oj.dump(t, :indent => ' ')) | ||
| end | ||
|
|
||
| # use_to_json only gates the top level value. Nested values always reach | ||
| # to_json, so a Time inside an Array raised even without the option. | ||
| def test_dump_nested_time_integer_indent_without_use_to_json | ||
| Oj.default_options = { :mode => :compat, :use_to_json => false } | ||
| t = Time.at(1_355_218_745).utc | ||
| json = Oj.dump([t], :indent => 2) | ||
| assert_equal(%|[\n "2012-12-11 09:39:05 UTC"\n]\n|, json) | ||
| end | ||
|
|
||
| def test_dump_nested_time_integer_indent | ||
| t = Time.at(1_355_218_745).utc | ||
| json = Oj.dump({ 'at' => t }, :indent => 2) | ||
| assert_equal(%|{\n "at":"2012-12-11 09:39:05 UTC"\n}\n|, json) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.