| 30 |
ashish |
1 |
#
|
|
|
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 |
|
|
|
20 |
require File.dirname(__FILE__) + '/spec_helper'
|
|
|
21 |
|
|
|
22 |
class ThriftSerializerSpec < Spec::ExampleGroup
|
|
|
23 |
include Thrift
|
|
|
24 |
include SpecNamespace
|
|
|
25 |
|
|
|
26 |
describe Serializer do
|
|
|
27 |
it "should serialize structs to binary by default" do
|
|
|
28 |
serializer = Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
|
|
|
29 |
data = serializer.serialize(Hello.new(:greeting => "'Ello guv'nor!"))
|
|
|
30 |
data.should == "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
|
|
|
31 |
end
|
|
|
32 |
|
|
|
33 |
it "should serialize structs to the given protocol" do
|
|
|
34 |
protocol = BaseProtocol.new(mock("transport"))
|
|
|
35 |
protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
|
|
|
36 |
protocol.should_receive(:write_field_begin).with("greeting", Types::STRING, 1)
|
|
|
37 |
protocol.should_receive(:write_string).with("Good day")
|
|
|
38 |
protocol.should_receive(:write_field_end)
|
|
|
39 |
protocol.should_receive(:write_field_stop)
|
|
|
40 |
protocol.should_receive(:write_struct_end)
|
|
|
41 |
protocol_factory = mock("ProtocolFactory")
|
|
|
42 |
protocol_factory.stub!(:get_protocol).and_return(protocol)
|
|
|
43 |
serializer = Serializer.new(protocol_factory)
|
|
|
44 |
serializer.serialize(Hello.new(:greeting => "Good day"))
|
|
|
45 |
end
|
|
|
46 |
end
|
|
|
47 |
|
|
|
48 |
describe Deserializer do
|
|
|
49 |
it "should deserialize structs from binary by default" do
|
|
|
50 |
deserializer = Deserializer.new
|
|
|
51 |
data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
|
|
|
52 |
deserializer.deserialize(Hello.new, data).should == Hello.new(:greeting => "'Ello guv'nor!")
|
|
|
53 |
end
|
|
|
54 |
|
|
|
55 |
it "should deserialize structs from the given protocol" do
|
|
|
56 |
protocol = BaseProtocol.new(mock("transport"))
|
|
|
57 |
protocol.should_receive(:read_struct_begin).and_return("SpecNamespace::Hello")
|
|
|
58 |
protocol.should_receive(:read_field_begin).and_return(["greeting", Types::STRING, 1],
|
|
|
59 |
[nil, Types::STOP, 0])
|
|
|
60 |
protocol.should_receive(:read_string).and_return("Good day")
|
|
|
61 |
protocol.should_receive(:read_field_end)
|
|
|
62 |
protocol.should_receive(:read_struct_end)
|
|
|
63 |
protocol_factory = mock("ProtocolFactory")
|
|
|
64 |
protocol_factory.stub!(:get_protocol).and_return(protocol)
|
|
|
65 |
deserializer = Deserializer.new(protocol_factory)
|
|
|
66 |
deserializer.deserialize(Hello.new, "").should == Hello.new(:greeting => "Good day")
|
|
|
67 |
end
|
|
|
68 |
end
|
|
|
69 |
end
|