Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | require 'thrift/serializer' |
Kevin Clark | 2bd3a30 | 2008-06-26 17:49:49 +0000 | [diff] [blame^] | 3 | require File.dirname(__FILE__) + '/gen-rb/ThriftSpec_types' |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 4 | |
| 5 | class 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 |
| 50 | end |