blob: fe5a86af1518ee310c43494b8986cdd45abf846d [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
11 serializer = Serializer.new
12 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
17 protocol = mock("Protocol")
18 protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
19 protocol.should_receive(:write_field).with("greeting", Types::STRING, 1, "Good day")
20 protocol.should_receive(:write_field_stop)
21 protocol.should_receive(:write_struct_end)
22 protocolFactory = mock("ProtocolFactory")
23 protocolFactory.stub!(:get_protocol).and_return(protocol)
24 serializer = Serializer.new(protocolFactory)
25 serializer.serialize(Hello.new(:greeting => "Good day"))
26 end
27 end
28
29 describe Deserializer do
30 it "should deserialize structs from binary by default" do
31 deserializer = Deserializer.new
32 data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
33 deserializer.deserialize(Hello.new, data).should == Hello.new(:greeting => "'Ello guv'nor!")
34 end
35
36 it "should deserialize structs from the given protocol" do
37 protocol = mock("Protocol")
38 protocol.should_receive(:read_struct_begin).and_return("SpecNamespace::Hello")
39 protocol.should_receive(:read_field_begin).and_return(["greeting", Types::STRING, 1],
40 [nil, Types::STOP, 0])
41 protocol.should_receive(:read_type).with(Types::STRING).and_return("Good day")
42 protocol.should_receive(:read_field_end)
43 protocol.should_receive(:read_struct_end)
44 protocolFactory = mock("ProtocolFactory")
45 protocolFactory.stub!(:get_protocol).and_return(protocol)
46 deserializer = Deserializer.new(protocolFactory)
47 deserializer.deserialize(Hello.new, "").should == Hello.new(:greeting => "Good day")
48 end
49 end
50end