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

Refactor to use rspec filtering #215

Merged
merged 7 commits into from
Mar 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ gem "rubocop-rake", ">= 0.6.0"
gem "rubocop-rspec", ">= 2.27.1"
gem "standard", ">= 1.35.1"

if RUBY_DESCRIPTION.start_with?("jruby")
gem "gson", ">= 0.6", require: false
else
gem "oj", "~> 3.0", require: false
gem "yajl-ruby", "~> 1.3", require: false
end
gem "gson", ">= 0.6", platforms: [:jruby], require: false
gem "jrjackson", ">= 0.4.18", platforms: [:jruby], require: false
gem "oj", "~> 3.0", platforms: [:ruby], require: false
gem "yajl-ruby", "~> 1.3", platforms: [:ruby], require: false

gemspec
5 changes: 2 additions & 3 deletions spec/multi_json/adapters/gson_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require "spec_helper"

return if skip_adapter?("gson")
return unless RSpec.configuration.gson?

require "shared/adapter"
require "multi_json/adapters/gson"

describe MultiJson::Adapters::Gson do
RSpec.describe MultiJson::Adapters::Gson, :gson => true do
it_behaves_like "an adapter", described_class
end
5 changes: 2 additions & 3 deletions spec/multi_json/adapters/jr_jackson_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require "spec_helper"

return if skip_adapter?("jr_jackson")
return unless RSpec.configuration.jrjackson?

require "shared/adapter"
require "multi_json/adapters/jr_jackson"

describe MultiJson::Adapters::JrJackson do
RSpec.describe MultiJson::Adapters::JrJackson, :jrjackson => true do
it_behaves_like "an adapter", described_class
end
2 changes: 1 addition & 1 deletion spec/multi_json/adapters/json_gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "shared/json_common_adapter"
require "multi_json/adapters/json_gem"

describe MultiJson::Adapters::JsonGem do
RSpec.describe MultiJson::Adapters::JsonGem, :json => true do
it_behaves_like "an adapter", described_class
it_behaves_like "JSON-like adapter", described_class
end
5 changes: 1 addition & 4 deletions spec/multi_json/adapters/json_pure_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
require "spec_helper"

return if skip_adapter?("json_pure")

require "shared/adapter"
require "shared/json_common_adapter"
require "multi_json/adapters/json_pure"

describe MultiJson::Adapters::JsonPure do
RSpec.describe MultiJson::Adapters::JsonPure, :json_pure => true do
it_behaves_like "an adapter", described_class
it_behaves_like "JSON-like adapter", described_class
end
5 changes: 2 additions & 3 deletions spec/multi_json/adapters/oj_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
require "spec_helper"

return if skip_adapter?("oj")
return unless RSpec.configuration.oj?

require "shared/adapter"
require "multi_json/adapters/oj"

describe MultiJson::Adapters::Oj do
RSpec.describe MultiJson::Adapters::Oj, :oj => true do
it_behaves_like "an adapter", described_class

describe ".dump" do
Expand Down
4 changes: 3 additions & 1 deletion spec/multi_json/adapters/ok_json_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require "spec_helper"
return unless RSpec.configuration.ok_json?

require "shared/adapter"
require "multi_json/adapters/ok_json"

describe MultiJson::Adapters::OkJson do
RSpec.describe MultiJson::Adapters::OkJson, :ok_json => true do
it_behaves_like "an adapter", described_class
end
5 changes: 2 additions & 3 deletions spec/multi_json/adapters/yajl_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require "spec_helper"

return if skip_adapter?("yajl")
return unless RSpec.configuration.yajl?

require "shared/adapter"
require "multi_json/adapters/yajl"

describe MultiJson::Adapters::Yajl do
RSpec.describe MultiJson::Adapters::Yajl, :yajl => true do
it_behaves_like "an adapter", described_class
end
58 changes: 31 additions & 27 deletions spec/multi_json_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require "spec_helper"
require "shared/options"

describe MultiJson do
RSpec.describe MultiJson do
let(:config){ RSpec.configuration }

before(:all) do
# make sure all available libs are required
MultiJson::REQUIREMENT_MAP.each_value do |library|
Expand Down Expand Up @@ -67,23 +69,27 @@
end
end

it "defaults to the best available gem" do
# Clear cache variable already set by previous tests
described_class.send(:remove_instance_variable, :@adapter) if described_class.instance_variable_defined?(:@adapter)
context "automatic adapter loading" do
before do
if described_class.instance_variable_defined?(:@adapter)
described_class.send(:remove_instance_variable, :@adapter)
end
end

if jruby? && !skip_adapter?("jr_jackson")
expect(described_class.adapter.to_s).to eq("MultiJson::Adapters::JrJackson")
elsif jruby?
expect(described_class.adapter.to_s).to eq("MultiJson::Adapters::JsonGem")
else
expect(described_class.adapter.to_s).to eq("MultiJson::Adapters::Oj")
it "defaults to the best available gem" do
if config.java? && config.jrjackson?
expect(described_class.adapter.to_s).to eq("MultiJson::Adapters::JrJackson")
elsif config.java? && config.json?
expect(described_class.adapter.to_s).to eq("MultiJson::Adapters::JsonGem")
else
expect(described_class.adapter.to_s).to eq("MultiJson::Adapters::Oj")
end
end
end

it "looks for adapter even if @adapter variable is nil" do
described_class.send(:instance_variable_set, :@adapter, nil)
allow(described_class).to receive(:default_adapter).and_return(:ok_json)
expect(described_class.adapter).to eq(MultiJson::Adapters::OkJson)
it "looks for adapter even if @adapter variable is nil" do
allow(described_class).to receive(:default_adapter).and_return(:ok_json)
expect(described_class.adapter).to eq(MultiJson::Adapters::OkJson)
end
end

it "is settable via a symbol" do
Expand Down Expand Up @@ -142,7 +148,7 @@
end

it "JSON gem does not create symbols on parse" do
skip "breaks in JRuby" if jruby?
skip "java based implementations" if config.java?

described_class.with_engine(:json_gem) do
described_class.load('{"json_class":"ZOMG"}')
Expand Down Expand Up @@ -170,18 +176,16 @@

it_behaves_like "has options", described_class

describe "aliases" do
unless skip_adapter?("jr_jackson")
describe "jrjackson" do
it "allows jrjackson alias as symbol" do
expect { described_class.use :jrjackson }.not_to raise_error
expect(described_class.adapter).to eq(MultiJson::Adapters::JrJackson)
end
describe "aliases", :jrjackson => true do
describe "jrjackson" do
it "allows jrjackson alias as symbol" do
expect { described_class.use :jrjackson }.not_to raise_error
expect(described_class.adapter).to eq(MultiJson::Adapters::JrJackson)
end

it "allows jrjackson alias as string" do
expect { described_class.use "jrjackson" }.not_to raise_error
expect(described_class.adapter).to eq(MultiJson::Adapters::JrJackson)
end
it "allows jrjackson alias as string" do
expect { described_class.use "jrjackson" }.not_to raise_error
expect(described_class.adapter).to eq(MultiJson::Adapters::JrJackson)
end
end
end
Expand Down
15 changes: 8 additions & 7 deletions spec/shared/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@
end
end

unless %w[json_pure json_gem].include?(adapter)
it "dumps time in correct format" do
time = Time.at(1_355_218_745).utc
let(:json_pure){ Kernel.const_get('MultiJson::Adapters::JsonPure') rescue nil }

dumped_json = MultiJson.dump(time)
expected = "2012-12-11 09:39:05 UTC"
expect(MultiJson.load(dumped_json)).to eq(expected)
end
it "dumps time in correct format" do
pending "https://github.com/flori/json/issues/573" if json_pure
time = Time.at(1_355_218_745).utc

dumped_json = MultiJson.dump(time)
expected = "2012-12-11 09:39:05 UTC"
expect(MultiJson.load(dumped_json)).to eq(expected)
end

it "dumps symbol and fixnum keys as strings" do
Expand Down
46 changes: 24 additions & 22 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@
config.expect_with :rspec do |c|
c.syntax = :expect
end

# You must run 'bundle exec rake' for this to work properly
loaded_specs = Gem.loaded_specs

config.add_setting :java, :default => RUBY_PLATFORM == 'java'
config.add_setting :gson, :default => loaded_specs.has_key?('gson')
config.add_setting :json, :default => loaded_specs.has_key?('json')
config.add_setting :json_pure, :default => loaded_specs.has_key?('json_pure')
config.add_setting :jrjackson, :default => loaded_specs.has_key?('jrjackson')
config.add_setting :ok_json, :default => loaded_specs.has_key?('ok_json')
config.add_setting :oj, :default => loaded_specs.has_key?('oj')
config.add_setting :yajl, :default => loaded_specs.has_key?('yajl')

config.filter_run_excluding(:jrjackson) unless config.jrjackson?
config.filter_run_excluding(:json) unless config.json?
config.filter_run_excluding(:json_pure) unless config.json_pure?
config.filter_run_excluding(:ok_json) unless config.ok_json?
config.filter_run_excluding(:oj) unless config.oj?
config.filter_run_excluding(:yajl) unless config.yajl?

unless config.java?
config.filter_run_excluding(:java)
config.filter_run_excluding(:gson) unless config.gson?
end
end

def silence_warnings
Expand All @@ -15,28 +39,6 @@ def silence_warnings
$VERBOSE = old_verbose
end

def macruby?
defined?(RUBY_ENGINE) && RUBY_ENGINE == "macruby"
end

def jruby?
defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
end

def skip_adapter?(adapter_name)
@skip ||=
ENV.fetch("SKIP_ADAPTERS", "")
.split(",")
.then do |skip|
if jruby?
%w[oj yajl] + skip
else
%w[gson] + skip
end
end
@skip.include?(adapter_name)
end

def undefine_constants(*consts)
values = {}
consts.each do |const|
Expand Down