Browse files

Make adapter specs to run separately

  • Loading branch information...
1 parent 421e378 commit d9dce8734a4d4657da79204f0ef4ec2ee1567b5a @rwz rwz committed Sep 8, 2013
View
2 Gemfile
@@ -19,7 +19,7 @@ end
group :development do
gem 'kramdown', '>= 0.14'
gem 'pry'
- gem 'pry-debugger', :platforms => :mri_19
+ gem 'pry-debugger', :platforms => :mri
end
group :test do
View
17 Rakefile
@@ -2,8 +2,21 @@ require 'bundler'
Bundler::GemHelper.install_tasks
require 'rspec/core/rake_task'
-desc "Run all examples"
-RSpec::Core::RakeTask.new(:spec)
+RSpec::Core::RakeTask.new(:base_spec) do |task|
+ task.pattern = 'spec/multi_json_spec.rb'
+end
+
+namespace :adapters do
+ Dir['spec/*_adapter_spec.rb'].each do |adapter_spec|
+ adapter_name = adapter_spec[/(\w+)_adapter_spec/, 1]
+ desc "Run #{adapter_name} adapter specs"
+ RSpec::Core::RakeTask.new(adapter_name) do |task|
+ task.pattern = adapter_spec
+ end
+ end
+end
+
+task :spec => %w[base_spec adapters:oj adapters:yajl adapters:json_gem adapters:json_pure adapters:ok_json adapters:gson adapters:jr_jackson adapters:nsjsonserialization]
task :default => :spec
task :test => :spec
View
4 lib/multi_json.rb
@@ -59,7 +59,9 @@ def default_adapter
end
end
- Kernel.warn '[WARNING] MultiJson is using the default adapter (ok_json). We recommend loading a different JSON library to improve performance.'
+ Kernel.warn '[WARNING] MultiJson is using the default adapter (ok_json).' +
+ 'We recommend loading a different JSON library to improve performance.'
+
:ok_json
end
alias default_engine default_adapter
View
2 lib/multi_json/adapters/gson.rb
@@ -1,4 +1,4 @@
-require 'gson' unless defined?(::Gson)
+require 'gson'
require 'multi_json/adapter'
module MultiJson
View
2 lib/multi_json/adapters/oj.rb
@@ -1,4 +1,4 @@
-require 'oj' unless defined?(::Oj)
+require 'oj'
require 'multi_json/adapter'
module MultiJson
View
2 lib/multi_json/adapters/yajl.rb
@@ -1,4 +1,4 @@
-require 'yajl' unless defined?(::Yajl)
+require 'yajl'
require 'multi_json/adapter'
module MultiJson
View
10 spec/gson_adapter_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+exit true unless jruby?
+
+require 'shared/adapter'
+require 'multi_json/adapters/gson'
+
+describe MultiJson::Adapters::Gson do
+ it_behaves_like 'an adapter', described_class
+end
View
10 spec/jr_jackson_adapter_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+exit true unless jruby?
+
+require 'shared/adapter'
+require 'multi_json/adapters/jr_jackson'
+
+describe MultiJson::Adapters::JrJackson do
+ it_behaves_like 'an adapter', described_class
+end
View
9 spec/json_gem_adapter_spec.rb
@@ -0,0 +1,9 @@
+require 'spec_helper'
+require 'shared/adapter'
+require 'shared/json_common_adapter'
+require 'multi_json/adapters/json_gem'
+
+describe MultiJson::Adapters::JsonGem do
+ it_behaves_like 'an adapter', described_class
+ it_behaves_like 'JSON-like adapter', described_class
+end
View
9 spec/json_pure_adapter_spec.rb
@@ -0,0 +1,9 @@
+require 'spec_helper'
+require 'shared/adapter'
+require 'shared/json_common_adapter'
+require 'multi_json/adapters/json_pure'
+
+describe MultiJson::Adapters::JsonPure do
+ it_behaves_like 'an adapter', described_class
+ it_behaves_like 'JSON-like adapter', described_class
+end
View
199 spec/multi_json_spec.rb
@@ -1,112 +1,116 @@
-require 'helper'
-require 'adapter_shared_example'
-require 'json_common_shared_example'
-require 'has_options'
-require 'stringio'
-
-describe 'MultiJson' do
- context 'adapters' do
- before{ MultiJson.use nil }
-
- context 'when no other json implementations are available' do
- around do |example|
- simulate_no_adapters{ example.call }
+require 'spec_helper'
+require 'shared/options'
+
+describe MultiJson do
+ before(:all) do
+ # make sure all available libs are required
+ MultiJson::REQUIREMENT_MAP.each do |library, adapter|
+ begin
+ require library
+ rescue ::LoadError
+ next
end
+ end
+ end
- it 'defaults to ok_json if no other json implementions are available' do
- silence_warnings do
- expect(MultiJson.default_adapter).to eq :ok_json
- end
- end
+ context 'when no other json implementations are available' do
+ around do |example|
+ simulate_no_adapters{ example.call }
+ end
- it 'prints a warning' do
- expect(Kernel).to receive(:warn).with(/warning/i)
- MultiJson.default_adapter
+ it 'defaults to ok_json if no other json implementions are available' do
+ silence_warnings do
+ expect(MultiJson.default_adapter).to eq(:ok_json)
end
end
- context 'caching' do
- before{ MultiJson.use adapter }
- let(:adapter){ MultiJson::Adapters::JsonGem }
- let(:json_string){ '{"abc":"def"}' }
-
- it 'busts caches on global options change' do
- MultiJson.load_options = { :symbolize_keys => true }
- expect(MultiJson.load(json_string)).to eq(:abc => 'def')
- MultiJson.load_options = nil
- expect(MultiJson.load(json_string)).to eq('abc' => 'def')
- end
+ it 'prints a warning' do
+ expect(Kernel).to receive(:warn).with(/warning/i)
+ MultiJson.default_adapter
+ end
+ end
- it 'busts caches on per-adapter options change' do
- adapter.load_options = { :symbolize_keys => true }
- expect(MultiJson.load(json_string)).to eq(:abc => 'def')
- adapter.load_options = nil
- expect(MultiJson.load(json_string)).to eq('abc' => 'def')
- end
+ context 'caching' do
+ before{ MultiJson.use adapter }
+ let(:adapter){ MultiJson::Adapters::JsonGem }
+ let(:json_string){ '{"abc":"def"}' }
+ it 'busts caches on global options change' do
+ MultiJson.load_options = { :symbolize_keys => true }
+ expect(MultiJson.load(json_string)).to eq(:abc => 'def')
+ MultiJson.load_options = nil
+ expect(MultiJson.load(json_string)).to eq('abc' => 'def')
end
- it 'defaults to the best available gem' do
- # Clear cache variable already set by previous tests
- MultiJson.send(:remove_instance_variable, :@adapter) if MultiJson.instance_variable_defined?(:@adapter)
- if jruby?
- expect(MultiJson.adapter.to_s).to eq 'MultiJson::Adapters::JrJackson'
- else
- expect(MultiJson.adapter.to_s).to eq 'MultiJson::Adapters::Oj'
- end
+ it 'busts caches on per-adapter options change' do
+ adapter.load_options = { :symbolize_keys => true }
+ expect(MultiJson.load(json_string)).to eq(:abc => 'def')
+ adapter.load_options = nil
+ expect(MultiJson.load(json_string)).to eq('abc' => 'def')
end
+ end
- it 'looks for adapter even if @adapter variable is nil' do
- MultiJson.send(:instance_variable_set, :@adapter, nil)
- expect(MultiJson).to receive(:default_adapter).and_return(:ok_json)
- expect(MultiJson.adapter).to eq MultiJson::Adapters::OkJson
- end
+ it 'defaults to the best available gem' do
+ # Clear cache variable already set by previous tests
+ MultiJson.send(:remove_instance_variable, :@adapter) if MultiJson.instance_variable_defined?(:@adapter)
- it 'is settable via a symbol' do
- MultiJson.use :json_gem
- expect(MultiJson.adapter).to eq MultiJson::Adapters::JsonGem
+ if jruby?
+ expect(MultiJson.adapter.to_s).to eq('MultiJson::Adapters::JrJackson')
+ else
+ expect(MultiJson.adapter.to_s).to eq('MultiJson::Adapters::Oj')
end
+ end
- it 'is settable via a class' do
- adapter = Class.new
- MultiJson.use adapter
- expect(MultiJson.adapter).to eq adapter
- end
+ it 'looks for adapter even if @adapter variable is nil' do
+ MultiJson.send(:instance_variable_set, :@adapter, nil)
+ expect(MultiJson).to receive(:default_adapter).and_return(:ok_json)
+ expect(MultiJson.adapter).to eq(MultiJson::Adapters::OkJson)
+ end
- it 'is settable via a module' do
- adapter = Module.new
- MultiJson.use adapter
- expect(MultiJson.adapter).to eq adapter
- end
+ it 'is settable via a symbol' do
+ MultiJson.use :json_gem
+ expect(MultiJson.adapter).to eq(MultiJson::Adapters::JsonGem)
+ end
- it 'throws ArgumentError on bad input' do
- expect{ MultiJson.use 'bad adapter' }.to raise_error(ArgumentError)
- end
+ it 'is settable via a class' do
+ adapter = Class.new
+ MultiJson.use adapter
+ expect(MultiJson.adapter).to eq(adapter)
+ end
- context 'using one-shot parser' do
- before do
- expect(MultiJson::Adapters::JsonPure).to receive(:dump).once.and_return('dump_something')
- expect(MultiJson::Adapters::JsonPure).to receive(:load).once.and_return('load_something')
- end
+ it 'is settable via a module' do
+ adapter = Module.new
+ MultiJson.use adapter
+ expect(MultiJson.adapter).to eq(adapter)
+ end
- it 'should use the defined parser just for the call' do
- MultiJson.use :json_gem
- expect(MultiJson.dump('', :adapter => :json_pure)).to eq 'dump_something'
- expect(MultiJson.load('', :adapter => :json_pure)).to eq 'load_something'
- expect(MultiJson.adapter).to eq MultiJson::Adapters::JsonGem
- end
+ it 'throws ArgumentError on bad input' do
+ expect{ MultiJson.use 'bad adapter' }.to raise_error(ArgumentError)
+ end
+
+ context 'using one-shot parser' do
+ before do
+ expect(MultiJson::Adapters::JsonPure).to receive(:dump).once.and_return('dump_something')
+ expect(MultiJson::Adapters::JsonPure).to receive(:load).once.and_return('load_something')
+ end
+
+ it 'should use the defined parser just for the call' do
+ MultiJson.use :json_gem
+ expect(MultiJson.dump('', :adapter => :json_pure)).to eq('dump_something')
+ expect(MultiJson.load('', :adapter => :json_pure)).to eq('load_something')
+ expect(MultiJson.adapter).to eq(MultiJson::Adapters::JsonGem)
end
end
it 'can set adapter for a block' do
MultiJson.use :ok_json
MultiJson.with_adapter(:json_pure) do
MultiJson.with_engine(:json_gem) do
- expect(MultiJson.adapter).to eq MultiJson::Adapters::JsonGem
+ expect(MultiJson.adapter).to eq(MultiJson::Adapters::JsonGem)
end
- expect(MultiJson.adapter).to eq MultiJson::Adapters::JsonPure
+ expect(MultiJson.adapter).to eq(MultiJson::Adapters::JsonPure)
end
- expect(MultiJson.adapter).to eq MultiJson::Adapters::OkJson
+ expect(MultiJson.adapter).to eq(MultiJson::Adapters::OkJson)
end
it 'JSON gem does not create symbols on parse' do
@@ -131,7 +135,6 @@
end
context 'with Oj.default_settings' do
-
around do |example|
options = Oj.default_options
Oj.default_options = { :symbol_keys => true }
@@ -143,7 +146,7 @@
MultiJson.with_engine(:oj) do
example = '{"a": 1, "b": 2}'
expected = { 'a' => 1, 'b' => 2 }
- expect(MultiJson.load(example)).to eq expected
+ expect(MultiJson.load(example)).to eq(expected)
end
end
end
@@ -166,21 +169,21 @@
it_behaves_like 'has options', MultiJson
- %w(gson jr_jackson json_gem json_pure nsjsonserialization oj ok_json yajl).each do |adapter|
- next if !jruby? && %w(gson jr_jackson).include?(adapter)
- next if !macruby? && adapter == 'nsjsonserialization'
- next if jruby? && %w(oj yajl).include?(adapter)
-
- context adapter do
- 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
+ # %w(gson jr_jackson json_gem json_pure nsjsonserialization oj ok_json yajl).each do |adapter|
+ # next if !jruby? && %w(gson jr_jackson).include?(adapter)
+ # next if !macruby? && adapter == 'nsjsonserialization'
+ # next if jruby? && %w(oj yajl).include?(adapter)
+
+ # context adapter do
+ # 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
describe 'aliases' do
if jruby?
View
10 spec/nsjsonserialization_adapter_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+exit true unless macruby?
+
+require 'shared/adapter'
+require 'multi_json/adapters/nsjsonserialization'
+
+describe MultiJson::Adapters::Nsjsonserialization do
+ it_behaves_like 'an adapter', described_class
+end
View
10 spec/oj_adapter_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+exit true if jruby?
+
+require 'shared/adapter'
+require 'multi_json/adapters/oj'
+
+describe MultiJson::Adapters::Oj do
+ it_behaves_like 'an adapter', described_class
+end
View
7 spec/ok_json_adapter_spec.rb
@@ -0,0 +1,7 @@
+require 'spec_helper'
+require 'shared/adapter'
+require 'multi_json/adapters/ok_json'
+
+describe MultiJson::Adapters::OkJson do
+ it_behaves_like 'an adapter', described_class
+end
View
19 spec/adapter_shared_example.rb → spec/shared/adapter.rb
@@ -1,19 +1,15 @@
# encoding: UTF-8
+require 'shared/options'
+
shared_examples_for 'an adapter' do |adapter|
- before do
- begin
- MultiJson.use adapter
- rescue LoadError
- pending "Adapter #{adapter} couldn't be loaded (not installed?)"
- end
- end
+ before{ MultiJson.use adapter }
- it_behaves_like 'has options', lambda{ MultiJson.adapter }
+ it_behaves_like 'has options', adapter
it 'does not modify argument hashes' do
- options = { :symbolize_keys => true, :pretty => false, :adapter => :json_pure }
+ options = { :symbolize_keys => true, :pretty => false, :adapter => :ok_json }
expect{MultiJson.load('{}', options)}.to_not change{options}
expect{MultiJson.dump([42], options)}.to_not change{options}
end
@@ -55,11 +51,6 @@
it 'dumps time in correct format' do
time = Time.at(1355218745).utc
- # time does not respond to to_json method
- class << time
- undef_method :to_json
- end
-
dumped_json = MultiJson.dump(time)
expected = if RUBY_VERSION > '1.9'
'2012-12-11 09:39:05 UTC'
View
0 spec/json_common_shared_example.rb → spec/shared/json_common_adapter.rb
File renamed without changes.
View
0 spec/has_options.rb → spec/shared/options.rb
File renamed without changes.
View
18 spec/helper.rb → spec/spec_helper.rb
@@ -1,14 +1,14 @@
require 'simplecov'
-require 'coveralls'
+# require 'coveralls'
-SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
- SimpleCov::Formatter::HTMLFormatter,
- Coveralls::SimpleCov::Formatter
-]
-SimpleCov.start do
- add_filter 'spec'
- add_filter 'vendor'
-end
+# SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
+# SimpleCov::Formatter::HTMLFormatter,
+# Coveralls::SimpleCov::Formatter
+# ]
+# SimpleCov.start do
+# add_filter 'spec'
+# add_filter 'vendor'
+# end
require 'multi_json'
require 'rspec'
View
10 spec/yajl_adapter_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+exit true if jruby?
+
+require 'shared/adapter'
+require 'multi_json/adapters/yajl'
+
+describe MultiJson::Adapters::Yajl do
+ it_behaves_like 'an adapter', described_class
+end

1 comment on commit d9dce87

@tommyh

+1

i started working on a pull request on friday and i figured out that i needed todo the exact same thing! with the different gems clobbering the ::JSON namespace, adapter specs which don't pollute eachother is very helpful.

Please sign in to comment.