blob: 599b454bb169b1c7669c4c4e1e1811431962192b [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
Jake Farrella87810f2012-09-28 01:59:04 +000020require 'spec_helper'
Kevin Clark138c0e12008-06-18 01:18:28 +000021
Jake Farrella87810f2012-09-28 01:59:04 +000022describe 'Serializer' do
Kevin Clark138c0e12008-06-18 01:18:28 +000023
Jake Farrella87810f2012-09-28 01:59:04 +000024 describe Thrift::Serializer do
Kevin Clark138c0e12008-06-18 01:18:28 +000025 it "should serialize structs to binary by default" do
Jake Farrella87810f2012-09-28 01:59:04 +000026 serializer = Thrift::Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
27 data = serializer.serialize(SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!"))
Kevin Clark138c0e12008-06-18 01:18:28 +000028 data.should == "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
29 end
30
31 it "should serialize structs to the given protocol" do
Jake Farrella87810f2012-09-28 01:59:04 +000032 protocol = Thrift::BaseProtocol.new(mock("transport"))
Kevin Clark138c0e12008-06-18 01:18:28 +000033 protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
Jake Farrella87810f2012-09-28 01:59:04 +000034 protocol.should_receive(:write_field_begin).with("greeting", Thrift::Types::STRING, 1)
Bryan Duxburyc0166282009-02-02 00:48:17 +000035 protocol.should_receive(:write_string).with("Good day")
36 protocol.should_receive(:write_field_end)
Kevin Clark138c0e12008-06-18 01:18:28 +000037 protocol.should_receive(:write_field_stop)
38 protocol.should_receive(:write_struct_end)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000039 protocol_factory = mock("ProtocolFactory")
40 protocol_factory.stub!(:get_protocol).and_return(protocol)
Jake Farrella87810f2012-09-28 01:59:04 +000041 serializer = Thrift::Serializer.new(protocol_factory)
42 serializer.serialize(SpecNamespace::Hello.new(:greeting => "Good day"))
Kevin Clark138c0e12008-06-18 01:18:28 +000043 end
44 end
45
Jake Farrella87810f2012-09-28 01:59:04 +000046 describe Thrift::Deserializer do
Kevin Clark138c0e12008-06-18 01:18:28 +000047 it "should deserialize structs from binary by default" do
Jake Farrella87810f2012-09-28 01:59:04 +000048 deserializer = Thrift::Deserializer.new
Kevin Clark138c0e12008-06-18 01:18:28 +000049 data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
Jake Farrella87810f2012-09-28 01:59:04 +000050 deserializer.deserialize(SpecNamespace::Hello.new, data).should == SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!")
Kevin Clark138c0e12008-06-18 01:18:28 +000051 end
52
53 it "should deserialize structs from the given protocol" do
Jake Farrella87810f2012-09-28 01:59:04 +000054 protocol = Thrift::BaseProtocol.new(mock("transport"))
Kevin Clark138c0e12008-06-18 01:18:28 +000055 protocol.should_receive(:read_struct_begin).and_return("SpecNamespace::Hello")
Jake Farrella87810f2012-09-28 01:59:04 +000056 protocol.should_receive(:read_field_begin).and_return(["greeting", Thrift::Types::STRING, 1],
57 [nil, Thrift::Types::STOP, 0])
Bryan Duxburyc0166282009-02-02 00:48:17 +000058 protocol.should_receive(:read_string).and_return("Good day")
Kevin Clark138c0e12008-06-18 01:18:28 +000059 protocol.should_receive(:read_field_end)
60 protocol.should_receive(:read_struct_end)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000061 protocol_factory = mock("ProtocolFactory")
62 protocol_factory.stub!(:get_protocol).and_return(protocol)
Jake Farrella87810f2012-09-28 01:59:04 +000063 deserializer = Thrift::Deserializer.new(protocol_factory)
64 deserializer.deserialize(SpecNamespace::Hello.new, "").should == SpecNamespace::Hello.new(:greeting => "Good day")
Kevin Clark138c0e12008-06-18 01:18:28 +000065 end
66 end
67end