Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: a61cfc8037
Fetching contributors…

Cannot retrieve contributors at this time

102 lines (89 sloc) 3.366 kB
$LOAD_PATH << File.dirname(__FILE__)
require 'spec_helper'
class Hash
def attributes
self
end
def method_missing(method)
self[method.to_s]
end
end
describe Babel do
let(:data) do
data = {
'id' => 987,
'name' => 'me and the corner',
'address' => { 'street' => 'Foo 12', 'zipcode' => '12345' },
'phone_numbers' => {
'prefix' => 12,
'number' => '123',
'area' => { 'code' => '001', 'iso' => 'us'}
}
}
class Hash
def self.new(hash = nil)
if hash
self[hash]
else
super
end
end
end
data
end
let(:serializer) { Babel::Serializer.new(data) }
let(:deserializer) { Babel::Deserializer.new(Hash) }
it 'should serialize and deserialize a hash' do
json = serializer.to_json
result = deserializer.from_json(json)
result.must_equal Hash['id' => data['id'], 'name' => data['name']]
end
it 'should serialize and deserialize a hash with root' do
json = serializer.to_json :root => :my
result = deserializer.from_json(json, :root => :my)
result.must_equal Hash['id' => data['id'], 'name' => data['name']]
end
it 'should serialize and deserialize a hash with include list' do
json = serializer.to_json(:include => ['address', 'phone_numbers'])
result = deserializer.from_json(json, :include => ['address', 'phone_numbers'])
data['phone_numbers'].delete('area')
result.must_equal Hash[data]
end
it 'should serialize and deserialize a hash with except' do
json = serializer.to_json(:except => ['id'])
result = deserializer.from_json(json, :except => ['id'])
result.must_equal Hash['name' => data['name']]
result = deserializer.from_json(json)
result.must_equal Hash['name' => data['name']]
end
it 'should serialize and deserialize a hash with only' do
json = serializer.to_json(:only => ['name'])
result = deserializer.from_json(json, :only => ['name'])
result.must_equal Hash['name' => data['name']]
result = deserializer.from_json(json)
result.must_equal Hash['name' => data['name']]
end
it 'should serialize and deserialize a hash with nested only' do
json = serializer.to_json(:include => { 'address' => {:only => ['street']}})
data.delete('phone_numbers')
data['address'].delete('zipcode')
result = deserializer.from_json(json, :include => { 'address' => {:only => ['street']}})
result.must_equal data
result = deserializer.from_json(json, :include => ['address'])
result.must_equal data
end
it 'should serialize and deserialize a hash with nested except' do
json = serializer.to_json(:include => { 'address' => {:except => ['zipcode']}})
data.delete('phone_numbers')
data['address'].delete('zipcode')
result = deserializer.from_json(json, :include => { 'address' => {:except => ['zipcode']}})
result.must_equal data
result = deserializer.from_json(json, :include => ['address'])
result.must_equal data
end
it 'should serialize and deserialize a hash with nested include' do
json = serializer.to_json(:include => { 'address' => {}, 'phone_numbers' => { :include => ['area']}})
result = deserializer.from_json(json, :include => { 'address' => {}, 'phone_numbers' => { :include => ['area']}})
result.must_equal data
end
end
Jump to Line
Something went wrong with that request. Please try again.