blob: a2ddc550c41f84e16280499138e5fb10e5aab09f [file] [log] [blame]
David Reiss72754e12008-07-25 21:06:03 +00001require File.dirname(__FILE__) + '/spec_helper'
2require 'thrift/protocol/binaryprotocolaccelerated'
3require File.dirname(__FILE__) + '/binaryprotocol_spec_shared'
4require File.dirname(__FILE__) + '/gen-rb/ThriftSpec_types'
5
6class ThriftBinaryProtocolAcceleratedSpec < Spec::ExampleGroup
7 include Thrift
8
Bryan Duxburyc0166282009-02-02 00:48:17 +00009 describe Thrift::BinaryProtocolAccelerated do
10 # since BinaryProtocolAccelerated should be directly equivalent to
11 # BinaryProtocol, we don't need any custom specs!
David Reiss72754e12008-07-25 21:06:03 +000012 it_should_behave_like 'a binary protocol'
13
14 def protocol_class
15 BinaryProtocolAccelerated
16 end
17
Bryan Duxburyc0166282009-02-02 00:48:17 +000018# before(:each) do
19# @buffer = ""
20# @trans.stub!(:borrow).and_return { @buffer }
21# @trans.stub!(:consume!).and_return do |*args|
22# n = args.first || 0
23# @buffer.slice!(0,n)
24# end
25# end
26#
27#
28# it "should raise an exception if the message header has the wrong version" do
29# @buffer = "\000\000\000\v"
30# # @prot.should_receive(:read_i32).and_return(42)
31# lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'Missing version identifier') do |e|
32# e.type == Thrift::ProtocolException::BAD_VERSION
33# end
34# end
35#
36# it "should encode a string with null bytes in it" do
37# foo = SpecNamespace::Hello.new(:greeting => "Hello\000World!")
38# @prot.encode_binary(foo).should == "\v\000\001\000\000\000\fHello\000World!\000"
39# end
40#
41# it "should decode a string with null bytes in it" do
42# trans = Thrift::MemoryBuffer.new("\v\000\001\000\000\000\fHello\000World!\000")
43# @prot.decode_binary(SpecNamespace::Hello.new, trans).should == SpecNamespace::Hello.new(:greeting => "Hello\000World!")
44# end
45#
46# it "should error when encoding a struct with a nil value in a list" do
47# Thrift.type_checking = false
48# sl = SpecNamespace::SimpleList
49# hello = SpecNamespace::Hello
50# # nil counts as false for bools
51# # lambda { @prot.encode_binary(sl.new(:bools => [true, false, nil, false])) }.should raise_error
52# lambda { @prot.encode_binary(sl.new(:bytes => [1, 2, nil, 3])) }.should raise_error
53# lambda { @prot.encode_binary(sl.new(:i16s => [1, 2, nil, 3])) }.should raise_error
54# lambda { @prot.encode_binary(sl.new(:i32s => [1, 2, nil, 3])) }.should raise_error
55# lambda { @prot.encode_binary(sl.new(:i64s => [1, 2, nil, 3])) }.should raise_error
56# lambda { @prot.encode_binary(sl.new(:doubles => [1.0, 2.0, nil, 3.0])) }.should raise_error
57# lambda { @prot.encode_binary(sl.new(:strings => ["one", "two", nil, "three"])) }.should raise_error
58# lambda { @prot.encode_binary(sl.new(:lists => [[1, 2], nil, [3, 4]])) }.should raise_error
59# lambda { @prot.encode_binary(sl.new(:maps => [{1 => 2}, nil, {3 => 4}])) }.should raise_error
60# lambda { @prot.encode_binary(sl.new(:sets => [Set.new([1, 2]), nil, Set.new([3, 4])])) }.should raise_error
61# lambda { @prot.encode_binary(sl.new(:structs => [hello.new, nil, hello.new(:greeting => "hi")])) }.should raise_error
62# end
63#
64# it "should error when encoding a non-nil, non-correctly-typed value in a list" do
65# Thrift.type_checking = false
66# sl = SpecNamespace::SimpleList
67# hello = SpecNamespace::Hello
68# # bool should accept any value
69# # lambda { @prot.encode_binary(sl.new(:bools => [true, false, 3])) }.should raise_error
70# lambda { @prot.encode_binary(sl.new(:bytes => [1, 2, "3", 5])) }.should raise_error
71# lambda { @prot.encode_binary(sl.new(:i16s => ["one", 2, 3])) }.should raise_error
72# lambda { @prot.encode_binary(sl.new(:i32s => [[1,2], 3, 4])) }.should raise_error
73# lambda { @prot.encode_binary(sl.new(:i64s => [{1 => 2}, 3, 4])) }.should raise_error
74# lambda { @prot.encode_binary(sl.new(:doubles => ["one", 2.3, 3.4])) }.should raise_error
75# lambda { @prot.encode_binary(sl.new(:strings => ["one", "two", 3, 4])) }.should raise_error
76# lambda { @prot.encode_binary(sl.new(:lists => [{1 => 2}, [3, 4]])) }.should raise_error
77# lambda { @prot.encode_binary(sl.new(:maps => [{1 => 2}, [3, 4]])) }.should raise_error
78# lambda { @prot.encode_binary(sl.new(:sets => [Set.new([1, 2]), 3, 4])) }.should raise_error
79# lambda { @prot.encode_binary(sl.new(:structs => [3, "four"])) }.should raise_error
80# end
81#
82# it "should error when encoding an improper object where a container is expected" do
83# Thrift.type_checking = false
84# sl = SpecNamespace::SimpleList
85# lambda { @prot.encode_binary(sl.new(:strings => {"one" => "two", nil => "three"})) }.should raise_error
86# lambda { @prot.encode_binary(sl.new(:maps => [[1, 2]])) }.should raise_error
87# end
88#
89# it "should accept arrays and hashes as sets" do
90# Thrift.type_checking = false
91# sl = SpecNamespace::SimpleList
92# lambda { @prot.encode_binary(sl.new(:sets => [[1, 2], {3 => true, 4 => true}])) }.should_not raise_error
93# end
David Reiss72754e12008-07-25 21:06:03 +000094 end
95
96 describe BinaryProtocolAcceleratedFactory do
97 it "should create a BinaryProtocolAccelerated" do
98 BinaryProtocolAcceleratedFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocolAccelerated)
99 end
100 end
101end