Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Factor common specs into shared examples

  • Loading branch information...
commit 3fc18d8e42ac3fc55d2343019ac53f24e83bb495 1 parent cc6325e
@sferik sferik authored
Showing with 120 additions and 115 deletions.
  1. +115 −0 spec/engine_shared_example.rb
  2. +5 −115 spec/multi_json_spec.rb
View
115 spec/engine_shared_example.rb
@@ -0,0 +1,115 @@
+shared_examples_for "an engine" do |engine|
+
+ before do
+ begin
+ MultiJson.engine = engine
+ rescue LoadError
+ pending "Engine #{engine} couldn't be loaded (not installed?)"
+ end
+ end
+
+ describe '.encode' do
+ it 'writes decodable JSON' do
+ [
+ {'abc' => 'def'},
+ [1, 2, 3, "4"],
+ ].each do |example|
+ MultiJson.decode(MultiJson.encode(example)).should == example
+ end
+ end
+
+ it 'encodes symbol keys as strings' do
+ [
+ [
+ {:foo => {:bar => 'baz'}},
+ {'foo' => {'bar' => 'baz'}},
+ ],
+ [
+ [{:foo => {:bar => 'baz'}}],
+ [{'foo' => {'bar' => 'baz'}}],
+ ],
+ [
+ {:foo => [{:bar => 'baz'}]},
+ {'foo' => [{'bar' => 'baz'}]},
+ ]
+ ].each do |example, expected|
+ encoded_json = MultiJson.encode(example)
+ MultiJson.decode(encoded_json).should == expected
+ end
+ end
+
+ it 'encodes rootless JSON' do
+ MultiJson.encode("random rootless string").should == "\"random rootless string\""
+ MultiJson.encode(123).should == "123"
+ end
+
+ it 'passes options to the engine' do
+ MultiJson.engine.should_receive(:encode).with('foo', {:bar => :baz})
+ MultiJson.encode('foo', :bar => :baz)
+ end
+
+ if engine == 'json_gem' || engine == 'json_pure'
+ describe 'with :pretty option set to true' do
+ it 'passes default pretty options' do
+ object = 'foo'
+ object.should_receive(:to_json).with(JSON::PRETTY_STATE_PROTOTYPE.to_h)
+ MultiJson.encode(object,:pretty => true)
+ end
+ end
+ end
+
+ it "encodes custom objects which implement as_json" do
+ MultiJson.encode(TimeWithZone.new).should == "\"2005-02-01T15:15:10Z\""
+ end
+ end
+
+ describe '.decode' do
+ it 'properly decodes valid JSON' do
+ MultiJson.decode('{"abc":"def"}').should == {'abc' => 'def'}
+ end
+
+ it 'raises MultiJson::DecodeError on invalid JSON' do
+ lambda do
+ MultiJson.decode('{"abc"}')
+ end.should raise_error(MultiJson::DecodeError)
+ end
+
+ it 'raises MultiJson::DecodeError with data on invalid JSON' do
+ data = '{invalid}'
+ begin
+ MultiJson.decode(data)
+ rescue MultiJson::DecodeError => de
+ de.data.should == data
+ end
+ end
+
+ it 'stringifys symbol keys when encoding' do
+ encoded_json = MultiJson.encode(:a => 1, :b => {:c => 2})
+ MultiJson.decode(encoded_json).should == {"a" => 1, "b" => {"c" => 2}}
+ end
+
+ it "properly decodes valid JSON in StringIOs" do
+ json = StringIO.new('{"abc":"def"}')
+ MultiJson.decode(json).should == {'abc' => 'def'}
+ end
+
+ it 'allows for symbolization of keys' do
+ [
+ [
+ '{"abc":{"def":"hgi"}}',
+ {:abc => {:def => 'hgi'}},
+ ],
+ [
+ '[{"abc":{"def":"hgi"}}]',
+ [{:abc => {:def => 'hgi'}}],
+ ],
+ [
+ '{"abc":[{"def":"hgi"}]}',
+ {:abc => [{:def => 'hgi'}]},
+ ],
+ ].each do |example, expected|
+ MultiJson.decode(example, :symbolize_keys => true).should == expected
+ end
+ end
+ end
+end
View
120 spec/multi_json_spec.rb
@@ -1,4 +1,5 @@
require 'helper'
+require 'engine_shared_example'
require 'stringio'
describe 'MultiJson' do
@@ -9,24 +10,24 @@
context 'when no other json implementations are available' do
before do
@old_map = MultiJson::REQUIREMENT_MAP
+ @old_json = Object.const_get :JSON if Object.const_defined?(:JSON)
@old_oj = Object.const_get :Oj if Object.const_defined?(:Oj)
@old_yajl = Object.const_get :Yajl if Object.const_defined?(:Yajl)
- @old_json = Object.const_get :JSON if Object.const_defined?(:JSON)
MultiJson::REQUIREMENT_MAP.each_with_index do |(library, engine), index|
MultiJson::REQUIREMENT_MAP[index] = ["foo/#{library}", engine]
end
+ Object.send :remove_const, :JSON if @old_json
Object.send :remove_const, :Oj if @old_oj
Object.send :remove_const, :Yajl if @old_yajl
- Object.send :remove_const, :JSON if @old_json
end
after do
@old_map.each_with_index do |(library, engine), index|
MultiJson::REQUIREMENT_MAP[index] = [library, engine]
end
+ Object.const_set :JSON, @old_json if @old_json
Object.const_set :Oj, @old_oj if @old_oj
Object.const_set :Yajl, @old_yajl if @old_yajl
- Object.const_set :JSON, @old_json if @old_json
end
it 'defaults to ok_json if no other json implementions are available' do
@@ -65,118 +66,7 @@
next if jruby? && (engine == 'oj' || engine == 'yajl')
context engine do
- before do
- begin
- MultiJson.engine = engine
- rescue LoadError
- pending "Engine #{engine} couldn't be loaded (not installed?)"
- end
- end
-
- describe '.encode' do
- it 'writes decodable JSON' do
- [
- {'abc' => 'def'},
- [1, 2, 3, "4"],
- ].each do |example|
- MultiJson.decode(MultiJson.encode(example)).should == example
- end
- end
-
- it 'encodes symbol keys as strings' do
- [
- [
- {:foo => {:bar => 'baz'}},
- {'foo' => {'bar' => 'baz'}},
- ],
- [
- [{:foo => {:bar => 'baz'}}],
- [{'foo' => {'bar' => 'baz'}}],
- ],
- [
- {:foo => [{:bar => 'baz'}]},
- {'foo' => [{'bar' => 'baz'}]},
- ]
- ].each do |example, expected|
- encoded_json = MultiJson.encode(example)
- MultiJson.decode(encoded_json).should == expected
- end
- end
-
- it 'encodes rootless JSON' do
- MultiJson.encode("random rootless string").should == "\"random rootless string\""
- MultiJson.encode(123).should == "123"
- end
-
- it 'passes options to the engine' do
- MultiJson.engine.should_receive(:encode).with('foo', {:bar => :baz})
- MultiJson.encode('foo', :bar => :baz)
- end
-
- if engine == 'json_gem' || engine == 'json_pure'
- describe 'with :pretty option set to true' do
- it 'passes default pretty options' do
- object = 'foo'
- object.should_receive(:to_json).with(JSON::PRETTY_STATE_PROTOTYPE.to_h)
- MultiJson.encode(object,:pretty => true)
- end
- end
- end
-
- it "encodes custom objects which implement as_json" do
- MultiJson.encode(TimeWithZone.new).should == "\"2005-02-01T15:15:10Z\""
- end
- end
-
- describe '.decode' do
- it 'properly decodes valid JSON' do
- MultiJson.decode('{"abc":"def"}').should == {'abc' => 'def'}
- end
-
- it 'raises MultiJson::DecodeError on invalid JSON' do
- lambda do
- MultiJson.decode('{"abc"}')
- end.should raise_error(MultiJson::DecodeError)
- end
-
- it 'raises MultiJson::DecodeError with data on invalid JSON' do
- data = '{invalid}'
- begin
- MultiJson.decode(data)
- rescue MultiJson::DecodeError => de
- de.data.should == data
- end
- end
-
- it 'stringifys symbol keys when encoding' do
- encoded_json = MultiJson.encode(:a => 1, :b => {:c => 2})
- MultiJson.decode(encoded_json).should == {"a" => 1, "b" => {"c" => 2}}
- end
-
- it "properly decodes valid JSON in StringIOs" do
- json = StringIO.new('{"abc":"def"}')
- MultiJson.decode(json).should == {'abc' => 'def'}
- end
-
- it 'allows for symbolization of keys' do
- [
- [
- '{"abc":{"def":"hgi"}}',
- {:abc => {:def => 'hgi'}},
- ],
- [
- '[{"abc":{"def":"hgi"}}]',
- [{:abc => {:def => 'hgi'}}],
- ],
- [
- '{"abc":[{"def":"hgi"}]}',
- {:abc => [{:def => 'hgi'}]},
- ],
- ].each do |example, expected|
- MultiJson.decode(example, :symbolize_keys => true).should == expected
- end
- end
- end
+ it_should_behave_like "an engine", engine
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.