Skip to content

Commit

Permalink
Convert specs to the new RSpec expectation syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Oct 10, 2012
1 parent 56f9058 commit f161861
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
24 changes: 11 additions & 13 deletions spec/adapter_shared_example.rb
Expand Up @@ -14,7 +14,7 @@
{'abc' => 'def'},
[1, 2, 3, "4"],
].each do |example|
MultiJson.load(MultiJson.dump(example)).should eq example
expect(MultiJson.load(MultiJson.dump(example))).to eq example
end
end

Expand All @@ -34,13 +34,13 @@
]
].each do |example, expected|
dumped_json = MultiJson.dump(example)
MultiJson.load(dumped_json).should eq expected
expect(MultiJson.load(dumped_json)).to eq expected
end
end

it 'dumps rootless JSON' do
MultiJson.dump("random rootless string").should eq "\"random rootless string\""
MultiJson.dump(123).should eq "123"
expect(MultiJson.dump("random rootless string")).to eq "\"random rootless string\""
expect(MultiJson.dump(123)).to eq "123"
end

it 'passes options to the adapter' do
Expand All @@ -59,38 +59,36 @@
end

it 'dumps custom objects which implement as_json' do
MultiJson.dump(TimeWithZone.new).should eq "\"2005-02-01T15:15:10Z\""
expect(MultiJson.dump(TimeWithZone.new)).to eq "\"2005-02-01T15:15:10Z\""
end
end

describe '.load' do
it 'properly loads valid JSON' do
MultiJson.load('{"abc":"def"}').should eq({'abc' => 'def'})
expect(MultiJson.load('{"abc":"def"}')).to eq({'abc' => 'def'})
end

it 'raises MultiJson::DecodeError on invalid JSON' do
lambda do
MultiJson.load('{"abc"}')
end.should raise_error(MultiJson::DecodeError)
expect{MultiJson.load('{"abc"}')}.to raise_error(MultiJson::DecodeError)
end

it 'raises MultiJson::DecodeError with data on invalid JSON' do
data = '{invalid}'
begin
MultiJson.load(data)
rescue MultiJson::DecodeError => de
de.data.should eq data
expect(de.data).to eq data
end
end

it 'stringifys symbol keys when encoding' do
dumped_json = MultiJson.dump(:a => 1, :b => {:c => 2})
MultiJson.load(dumped_json).should eq({"a" => 1, "b" => {"c" => 2}})
expect(MultiJson.load(dumped_json)).to eq({"a" => 1, "b" => {"c" => 2}})
end

it 'properly loads valid JSON in StringIOs' do
json = StringIO.new('{"abc":"def"}')
MultiJson.load(json).should eq({'abc' => 'def'})
expect(MultiJson.load(json)).to eq({'abc' => 'def'})
end

it 'allows for symbolization of keys' do
Expand All @@ -108,7 +106,7 @@
{:abc => [{:def => 'hgi'}]},
],
].each do |example, expected|
MultiJson.load(example, :symbolize_keys => true).should eq expected
expect(MultiJson.load(example, :symbolize_keys => true)).to eq expected
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/helper.rb
Expand Up @@ -12,9 +12,16 @@ def macruby?
add_filter 'spec'
end
end

require 'multi_json'
require 'rspec'

RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = :expect
end
end

class MockDecoder
def self.load(string, options={})
{'abc' => 'def'}
Expand Down
16 changes: 8 additions & 8 deletions spec/multi_json_spec.rb
Expand Up @@ -31,7 +31,7 @@
end

it 'defaults to ok_json if no other json implementions are available' do
MultiJson.default_adapter.should eq :ok_json
expect(MultiJson.default_adapter).to eq :ok_json
end

it 'prints a warning' do
Expand All @@ -43,21 +43,21 @@
it 'defaults to the best available gem' do
unless jruby?
require 'oj'
MultiJson.adapter.name.should eq 'MultiJson::Adapters::Oj'
expect(MultiJson.adapter.name).to eq 'MultiJson::Adapters::Oj'
else
require 'json'
MultiJson.adapter.name.should eq 'MultiJson::Adapters::JsonGem'
expect(MultiJson.adapter.name).to eq 'MultiJson::Adapters::JsonGem'
end
end

it 'is settable via a symbol' do
MultiJson.use :json_gem
MultiJson.adapter.name.should eq 'MultiJson::Adapters::JsonGem'
expect(MultiJson.adapter.name).to eq 'MultiJson::Adapters::JsonGem'
end

it 'is settable via a class' do
MultiJson.use MockDecoder
MultiJson.adapter.name.should eq 'MockDecoder'
expect(MultiJson.adapter.name).to eq 'MockDecoder'
end

context "using one-shot parser" do
Expand All @@ -69,9 +69,9 @@

it "should use the defined parser just for the call" do
MultiJson.use :json_gem
MultiJson.dump('', :adapter => :json_pure).should eq 'dump_something'
MultiJson.load('', :adapter => :json_pure).should eq 'load_something'
MultiJson.adapter.to_s.should eq "MultiJson::Adapters::JsonGem"
expect(MultiJson.dump('', :adapter => :json_pure)).to eq 'dump_something'
expect(MultiJson.load('', :adapter => :json_pure)).to eq 'load_something'
expect(MultiJson.adapter.to_s).to eq "MultiJson::Adapters::JsonGem"
end
end
end
Expand Down

0 comments on commit f161861

Please sign in to comment.