blob: 78a11ab66ee18ff7eff8ec40995b08930aeea650 [file] [log] [blame]
Kevin Clark138c0e12008-06-18 01:18:28 +00001require File.dirname(__FILE__) + '/spec_helper'
2require 'thrift/serializer'
Kevin Clark2bd3a302008-06-26 17:49:49 +00003require File.dirname(__FILE__) + '/gen-rb/ThriftSpec_types'
Kevin Clark138c0e12008-06-18 01:18:28 +00004
5class ThriftSerializerSpec < Spec::ExampleGroup
6 include Thrift
7 include SpecNamespace
8
9 describe Serializer do
10 it "should serialize structs to binary by default" do
Bryan Duxburyc0166282009-02-02 00:48:17 +000011 serializer = Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
Kevin Clark138c0e12008-06-18 01:18:28 +000012 data = serializer.serialize(Hello.new(:greeting => "'Ello guv'nor!"))
13 data.should == "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
14 end
15
16 it "should serialize structs to the given protocol" do
Bryan Duxburyc0166282009-02-02 00:48:17 +000017 protocol = Protocol.new(mock("transport"))
Kevin Clark138c0e12008-06-18 01:18:28 +000018 protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
Bryan Duxburyc0166282009-02-02 00:48:17 +000019 protocol.should_receive(:write_field_begin).with("greeting", Types::STRING, 1)
20 protocol.should_receive(:write_string).with("Good day")
21 protocol.should_receive(:write_field_end)
Kevin Clark138c0e12008-06-18 01:18:28 +000022 protocol.should_receive(:write_field_stop)
23 protocol.should_receive(:write_struct_end)
24 protocolFactory = mock("ProtocolFactory")
25 protocolFactory.stub!(:get_protocol).and_return(protocol)
26 serializer = Serializer.new(protocolFactory)
27 serializer.serialize(Hello.new(:greeting => "Good day"))
28 end
29 end
30
31 describe Deserializer do
32 it "should deserialize structs from binary by default" do
33 deserializer = Deserializer.new
34 data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
35 deserializer.deserialize(Hello.new, data).should == Hello.new(:greeting => "'Ello guv'nor!")
36 end
37
38 it "should deserialize structs from the given protocol" do
Bryan Duxburyc0166282009-02-02 00:48:17 +000039 protocol = Protocol.new(mock("transport"))
Kevin Clark138c0e12008-06-18 01:18:28 +000040 protocol.should_receive(:read_struct_begin).and_return("SpecNamespace::Hello")
41 protocol.should_receive(:read_field_begin).and_return(["greeting", Types::STRING, 1],
42 [nil, Types::STOP, 0])
Bryan Duxburyc0166282009-02-02 00:48:17 +000043 protocol.should_receive(:read_string).and_return("Good day")
Kevin Clark138c0e12008-06-18 01:18:28 +000044 protocol.should_receive(:read_field_end)
45 protocol.should_receive(:read_struct_end)
46 protocolFactory = mock("ProtocolFactory")
47 protocolFactory.stub!(:get_protocol).and_return(protocol)
48 deserializer = Deserializer.new(protocolFactory)
49 deserializer.deserialize(Hello.new, "").should == Hello.new(:greeting => "Good day")
50 end
51 end
52end