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 |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame^] | 11 | serializer = Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new) |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 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 |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame^] | 17 | protocol = Protocol.new(mock("transport")) |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 18 | protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello") |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame^] | 19 | 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 Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 22 | 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 Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame^] | 39 | protocol = Protocol.new(mock("transport")) |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 40 | 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 Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame^] | 43 | protocol.should_receive(:read_string).and_return("Good day") |
Kevin Clark | 138c0e1 | 2008-06-18 01:18:28 +0000 | [diff] [blame] | 44 | 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 |
| 52 | end |