spec/unit/yaks/primitivize_spec.rb in yaks-0.9.0 vs spec/unit/yaks/primitivize_spec.rb in yaks-0.10.0
- old
+ new
@@ -1,8 +1,14 @@
RSpec.describe Yaks::Primitivize do
subject(:primitivizer) { described_class.create }
+ describe "#initialize" do
+ it "should not create any mappings" do
+ expect(described_class.new.mappings).to eql Hash[]
+ end
+ end
+
describe '.create' do
it 'should map String, true, false, nil, numbers to themselves' do
[
'hello',
true,
@@ -20,19 +26,22 @@
expect(primitivizer.call(:foo)).to eql 'foo'
end
it 'should recursively handle hashes' do
expect(primitivizer.call(
- :foo => {:wassup => :friends, 123 => '456'}
+ foo: {:wassup => :friends, 123 => '456'}
)).to eql('foo' => {'wassup' => 'friends', 123 => '456'})
end
it 'should handle arrays recursively' do
- expect(primitivizer.call(
- [:foo, [:wassup, :friends], 123, '456']
- )).to eql( ['foo', ['wassup', 'friends'], 123, '456'] )
+ expect(primitivizer.call([:foo, [:wassup, :friends], 123, '456']))
+ .to eql(['foo', %w[wassup friends], 123, '456'])
end
+
+ it "should handle URIs by turning them to strings" do
+ expect(primitivizer.call(URI("http://foo.bar/baz"))).to eql "http://foo.bar/baz"
+ end
end
describe '#call' do
require 'ostruct'
@@ -43,11 +52,12 @@
it 'should raise an error when passed an unkown type' do
def funny_object.inspect
"I am funny"
end
- expect { primitivizer.call(funny_object) }.to raise_error "don't know how to turn OpenStruct (I am funny) into a primitive"
+ expect { primitivizer.call(funny_object) }
+ .to raise_error Yaks::PrimitivizeError, "don't know how to turn OpenStruct (I am funny) into a primitive"
end
context 'with custom mapping' do
require 'matrix'
@@ -64,12 +74,21 @@
end
end
end
it 'should evaluate in the context of primitivize' do
- expect( primitivizer.call( Vector[:foo, :baxxx, :bazz] ) ).to eql( [3, 5, 4] )
+ expect(primitivizer.call(Vector[:foo, :baxxx, :bazz])).to eql([3, 5, 4])
end
end
+ end
+ describe "#map" do
+ let(:primitivizer) { described_class.new }
+ it "should add new mappings" do
+ primitivizer.map(String) {|s| s.upcase }
+ primitivizer.map(Numeric) {|n| n.next }
+
+ expect(primitivizer.call("foo")).to eql "FOO"
+ end
end
end