Skip to content
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

Some small changes before 1.6.0 release #85

Merged
merged 3 commits into from Feb 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 7 additions & 32 deletions spec/adapter_shared_example.rb
Expand Up @@ -73,40 +73,24 @@ class << time
MultiJson.dump('foo', :bar => :baz)
end

if adapter == 'json_gem' || adapter == 'json_pure'
describe 'with :pretty option set to true' do
it 'passes default pretty options' do
::JSON.should_receive(:generate).with(['foo'], JSON::PRETTY_STATE_PROTOTYPE.to_h).and_return('["foo"]')
MultiJson.dump('foo', :pretty => true)
end
end

describe 'with :indent option' do
it 'passes it on dump' do
::JSON.should_receive(:generate).with(['foo'], {:indent => "\t"}).and_return('["foo"]')
MultiJson.dump('foo', :indent => "\t")
end
end
end

# This behavior is currently not supported by gson.rb
# See discussion at https://github.com/intridea/multi_json/pull/71
unless adapter == 'gson'
it 'dumps custom objects which implements to_json' do
it 'dumps custom objects that implement to_json' do
klass = Class.new do
def to_json(*)
"\"foobar\""
'"foobar"'
end
end
expect(MultiJson.dump(klass.new)).to eq "\"foobar\""
expect(MultiJson.dump(klass.new)).to eq '"foobar"'
end
end

it 'allow to dump JSON values' do
it 'allows to dump JSON values' do
expect(MultiJson.dump(42)).to eq '42'
end

it 'allow to dump JSON with UTF-8 characters' do
it 'allows to dump JSON with UTF-8 characters' do
expect(MultiJson.dump({'color' => 'żółć'})).to eq('{"color":"żółć"}')
end
end
Expand Down Expand Up @@ -167,20 +151,11 @@ def to_json(*)
end
end

if adapter == 'json_gem' || adapter == 'json_pure'
describe 'with :quirks_mode option' do
it 'passes it on load' do
::JSON.should_receive(:parse).with('["foo"]', {:quirks_mode => true, :create_additions => false}).and_return(['foo'])
MultiJson.load('"foo"', :quirks_mode => true)
end
end
end

it 'allow to load JSON values' do
it 'allows to load JSON values' do
expect(MultiJson.load('42')).to eq 42
end

it 'allow to load JSON with UTF-8 characters' do
it 'allows to load JSON with UTF-8 characters' do
expect(MultiJson.load('{"color":"żółć"}')).to eq({'color' => 'żółć'})
end
end
Expand Down
34 changes: 34 additions & 0 deletions spec/json_common_shared_example.rb
@@ -0,0 +1,34 @@
shared_examples_for 'JSON-like adapter' do |adapter|
before do
begin
MultiJson.use adapter
rescue LoadError
pending "Adapter #{adapter} couldn't be loaded (not installed?)"
end
end

describe '.dump' do
describe 'with :pretty option set to true' do
it 'passes default pretty options' do
::JSON.should_receive(:generate).with(['foo'], JSON::PRETTY_STATE_PROTOTYPE.to_h).and_return('["foo"]')
MultiJson.dump('foo', :pretty => true)
end
end

describe 'with :indent option' do
it 'passes it on dump' do
::JSON.should_receive(:generate).with(['foo'], {:indent => "\t"}).and_return('["foo"]')
MultiJson.dump('foo', :indent => "\t")
end
end
end

describe '.load' do
describe 'with :quirks_mode option' do
it 'passes it on load' do
::JSON.should_receive(:parse).with('["foo"]', {:quirks_mode => true, :create_additions => false}).and_return(['foo'])
MultiJson.load('"foo"', :quirks_mode => true)
end
end
end
end
23 changes: 15 additions & 8 deletions spec/multi_json_spec.rb
@@ -1,5 +1,6 @@
require 'helper'
require 'adapter_shared_example'
require 'json_common_shared_example'
require 'stringio'

describe 'MultiJson' do
Expand Down Expand Up @@ -106,21 +107,21 @@
it 'JSON gem does not create symbols on parse' do
MultiJson.with_engine(:json_gem) do
MultiJson.load('{"json_class":"ZOMG"}') rescue nil
before = Symbol.all_symbols
MultiJson.load('{"json_class":"OMG"}') rescue nil
after = Symbol.all_symbols - before
expect(after).to eq []

expect{
MultiJson.load('{"json_class":"OMG"}') rescue nil
}.to_not change{Symbol.all_symbols.count}
end
end

unless jruby?
it 'Oj does not create symbols on parse' do
MultiJson.with_engine(:oj) do
MultiJson.load('{"json_class":"ZOMG"}') rescue nil
before = Symbol.all_symbols
MultiJson.load('{"json_class":"OMG"}') rescue nil
after = Symbol.all_symbols - before
expect(after).to eq []

expect{
MultiJson.load('{"json_class":"OMG"}') rescue nil
}.to_not change{Symbol.all_symbols.count}
end
end
end
Expand All @@ -141,4 +142,10 @@
it_behaves_like 'an adapter', adapter
end
end

%w(json_gem json_pure).each do |adapter|
context adapter do
it_behaves_like 'JSON-like adapter', adapter
end
end
end