blob: 82f374b1306ddea3a8f2fb4028848e33fd2d7061 [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
Kevin Clark138c0e12008-06-18 01:18:28 +000020require File.dirname(__FILE__) + '/spec_helper'
Kevin Clark19f8d1f2009-04-07 19:09:28 +000021require File.dirname(__FILE__) + '/gen-rb/thrift_spec_types'
Kevin Clark138c0e12008-06-18 01:18:28 +000022
23class ThriftSerializerSpec < Spec::ExampleGroup
24 include Thrift
25 include SpecNamespace
26
27 describe Serializer do
28 it "should serialize structs to binary by default" do
Bryan Duxburyc0166282009-02-02 00:48:17 +000029 serializer = Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
Kevin Clark138c0e12008-06-18 01:18:28 +000030 data = serializer.serialize(Hello.new(:greeting => "'Ello guv'nor!"))
31 data.should == "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
32 end
33
34 it "should serialize structs to the given protocol" do
Bryan Duxburyd1d15422009-04-04 00:58:03 +000035 protocol = BaseProtocol.new(mock("transport"))
Kevin Clark138c0e12008-06-18 01:18:28 +000036 protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
Bryan Duxburyc0166282009-02-02 00:48:17 +000037 protocol.should_receive(:write_field_begin).with("greeting", Types::STRING, 1)
38 protocol.should_receive(:write_string).with("Good day")
39 protocol.should_receive(:write_field_end)
Kevin Clark138c0e12008-06-18 01:18:28 +000040 protocol.should_receive(:write_field_stop)
41 protocol.should_receive(:write_struct_end)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000042 protocol_factory = mock("ProtocolFactory")
43 protocol_factory.stub!(:get_protocol).and_return(protocol)
44 serializer = Serializer.new(protocol_factory)
Kevin Clark138c0e12008-06-18 01:18:28 +000045 serializer.serialize(Hello.new(:greeting => "Good day"))
46 end
47 end
48
49 describe Deserializer do
50 it "should deserialize structs from binary by default" do
51 deserializer = Deserializer.new
52 data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
53 deserializer.deserialize(Hello.new, data).should == Hello.new(:greeting => "'Ello guv'nor!")
54 end
55
56 it "should deserialize structs from the given protocol" do
Bryan Duxburyd1d15422009-04-04 00:58:03 +000057 protocol = BaseProtocol.new(mock("transport"))
Kevin Clark138c0e12008-06-18 01:18:28 +000058 protocol.should_receive(:read_struct_begin).and_return("SpecNamespace::Hello")
59 protocol.should_receive(:read_field_begin).and_return(["greeting", Types::STRING, 1],
60 [nil, Types::STOP, 0])
Bryan Duxburyc0166282009-02-02 00:48:17 +000061 protocol.should_receive(:read_string).and_return("Good day")
Kevin Clark138c0e12008-06-18 01:18:28 +000062 protocol.should_receive(:read_field_end)
63 protocol.should_receive(:read_struct_end)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000064 protocol_factory = mock("ProtocolFactory")
65 protocol_factory.stub!(:get_protocol).and_return(protocol)
66 deserializer = Deserializer.new(protocol_factory)
Kevin Clark138c0e12008-06-18 01:18:28 +000067 deserializer.deserialize(Hello.new, "").should == Hello.new(:greeting => "Good day")
68 end
69 end
70end