David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 20 | require 'spec_helper' |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 21 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 22 | describe 'BaseProtocol' do |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 23 | |
| 24 | before(:each) do |
Kevin Clark | 9db1c2e | 2008-06-18 01:06:42 +0000 | [diff] [blame] | 25 | @trans = mock("MockTransport") |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 26 | @prot = Thrift::BaseProtocol.new(@trans) |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 27 | end |
| 28 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 29 | describe Thrift::BaseProtocol do |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 30 | # most of the methods are stubs, so we can ignore them |
Kevin Clark | 9db1c2e | 2008-06-18 01:06:42 +0000 | [diff] [blame] | 31 | |
| 32 | it "should make trans accessible" do |
| 33 | @prot.trans.should eql(@trans) |
| 34 | end |
| 35 | |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 36 | it "should write out a field nicely" do |
| 37 | @prot.should_receive(:write_field_begin).with('field', 'type', 'fid').ordered |
| 38 | @prot.should_receive(:write_type).with('type', 'value').ordered |
| 39 | @prot.should_receive(:write_field_end).ordered |
| 40 | @prot.write_field('field', 'type', 'fid', 'value') |
| 41 | end |
| 42 | |
| 43 | it "should write out the different types" do |
| 44 | @prot.should_receive(:write_bool).with('bool').ordered |
| 45 | @prot.should_receive(:write_byte).with('byte').ordered |
| 46 | @prot.should_receive(:write_double).with('double').ordered |
| 47 | @prot.should_receive(:write_i16).with('i16').ordered |
| 48 | @prot.should_receive(:write_i32).with('i32').ordered |
| 49 | @prot.should_receive(:write_i64).with('i64').ordered |
| 50 | @prot.should_receive(:write_string).with('string').ordered |
| 51 | struct = mock('Struct') |
| 52 | struct.should_receive(:write).with(@prot).ordered |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 53 | @prot.write_type(Thrift::Types::BOOL, 'bool') |
| 54 | @prot.write_type(Thrift::Types::BYTE, 'byte') |
| 55 | @prot.write_type(Thrift::Types::DOUBLE, 'double') |
| 56 | @prot.write_type(Thrift::Types::I16, 'i16') |
| 57 | @prot.write_type(Thrift::Types::I32, 'i32') |
| 58 | @prot.write_type(Thrift::Types::I64, 'i64') |
| 59 | @prot.write_type(Thrift::Types::STRING, 'string') |
| 60 | @prot.write_type(Thrift::Types::STRUCT, struct) |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 61 | # all other types are not implemented |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 62 | [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP, Thrift::Types::SET, Thrift::Types::LIST].each do |type| |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 63 | lambda { @prot.write_type(type, type.to_s) }.should raise_error(NotImplementedError) |
| 64 | end |
| 65 | end |
| 66 | |
| 67 | it "should read the different types" do |
| 68 | @prot.should_receive(:read_bool).ordered |
| 69 | @prot.should_receive(:read_byte).ordered |
| 70 | @prot.should_receive(:read_i16).ordered |
| 71 | @prot.should_receive(:read_i32).ordered |
| 72 | @prot.should_receive(:read_i64).ordered |
| 73 | @prot.should_receive(:read_double).ordered |
| 74 | @prot.should_receive(:read_string).ordered |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 75 | @prot.read_type(Thrift::Types::BOOL) |
| 76 | @prot.read_type(Thrift::Types::BYTE) |
| 77 | @prot.read_type(Thrift::Types::I16) |
| 78 | @prot.read_type(Thrift::Types::I32) |
| 79 | @prot.read_type(Thrift::Types::I64) |
| 80 | @prot.read_type(Thrift::Types::DOUBLE) |
| 81 | @prot.read_type(Thrift::Types::STRING) |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 82 | # all other types are not implemented |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 83 | [Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP, Thrift::Types::SET, Thrift::Types::LIST].each do |type| |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 84 | lambda { @prot.read_type(type) }.should raise_error(NotImplementedError) |
| 85 | end |
| 86 | end |
| 87 | |
| 88 | it "should skip the basic types" do |
| 89 | @prot.should_receive(:read_bool).ordered |
| 90 | @prot.should_receive(:read_byte).ordered |
| 91 | @prot.should_receive(:read_i16).ordered |
| 92 | @prot.should_receive(:read_i32).ordered |
| 93 | @prot.should_receive(:read_i64).ordered |
| 94 | @prot.should_receive(:read_double).ordered |
| 95 | @prot.should_receive(:read_string).ordered |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 96 | @prot.skip(Thrift::Types::BOOL) |
| 97 | @prot.skip(Thrift::Types::BYTE) |
| 98 | @prot.skip(Thrift::Types::I16) |
| 99 | @prot.skip(Thrift::Types::I32) |
| 100 | @prot.skip(Thrift::Types::I64) |
| 101 | @prot.skip(Thrift::Types::DOUBLE) |
| 102 | @prot.skip(Thrift::Types::STRING) |
| 103 | @prot.skip(Thrift::Types::STOP) # should do absolutely nothing |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 104 | end |
| 105 | |
| 106 | it "should skip structs" do |
| 107 | real_skip = @prot.method(:skip) |
| 108 | @prot.should_receive(:read_struct_begin).ordered |
| 109 | @prot.should_receive(:read_field_begin).exactly(4).times.and_return( |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 110 | ['field 1', Thrift::Types::STRING, 1], |
| 111 | ['field 2', Thrift::Types::I32, 2], |
| 112 | ['field 3', Thrift::Types::MAP, 3], |
| 113 | [nil, Thrift::Types::STOP, 0] |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 114 | ) |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 115 | @prot.should_receive(:read_field_end).exactly(3).times |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 116 | @prot.should_receive(:read_string).exactly(3).times |
| 117 | @prot.should_receive(:read_i32).ordered |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 118 | @prot.should_receive(:read_map_begin).ordered.and_return([Thrift::Types::STRING, Thrift::Types::STRING, 1]) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 119 | # @prot.should_receive(:read_string).exactly(2).times |
| 120 | @prot.should_receive(:read_map_end).ordered |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 121 | @prot.should_receive(:read_struct_end).ordered |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 122 | real_skip.call(Thrift::Types::STRUCT) |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 123 | end |
Kevin Clark | 9db1c2e | 2008-06-18 01:06:42 +0000 | [diff] [blame] | 124 | |
| 125 | it "should skip maps" do |
| 126 | real_skip = @prot.method(:skip) |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 127 | @prot.should_receive(:read_map_begin).ordered.and_return([Thrift::Types::STRING, Thrift::Types::STRUCT, 1]) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 128 | @prot.should_receive(:read_string).ordered |
| 129 | @prot.should_receive(:read_struct_begin).ordered.and_return(["some_struct"]) |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 130 | @prot.should_receive(:read_field_begin).ordered.and_return([nil, Thrift::Types::STOP, nil]); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 131 | @prot.should_receive(:read_struct_end).ordered |
Kevin Clark | 9db1c2e | 2008-06-18 01:06:42 +0000 | [diff] [blame] | 132 | @prot.should_receive(:read_map_end).ordered |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 133 | real_skip.call(Thrift::Types::MAP) |
Kevin Clark | 9db1c2e | 2008-06-18 01:06:42 +0000 | [diff] [blame] | 134 | end |
| 135 | |
| 136 | it "should skip sets" do |
| 137 | real_skip = @prot.method(:skip) |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 138 | @prot.should_receive(:read_set_begin).ordered.and_return([Thrift::Types::I64, 9]) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 139 | @prot.should_receive(:read_i64).ordered.exactly(9).times |
Kevin Clark | 9db1c2e | 2008-06-18 01:06:42 +0000 | [diff] [blame] | 140 | @prot.should_receive(:read_set_end) |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 141 | real_skip.call(Thrift::Types::SET) |
Kevin Clark | 9db1c2e | 2008-06-18 01:06:42 +0000 | [diff] [blame] | 142 | end |
| 143 | |
| 144 | it "should skip lists" do |
| 145 | real_skip = @prot.method(:skip) |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 146 | @prot.should_receive(:read_list_begin).ordered.and_return([Thrift::Types::DOUBLE, 11]) |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 147 | @prot.should_receive(:read_double).ordered.exactly(11).times |
Kevin Clark | 9db1c2e | 2008-06-18 01:06:42 +0000 | [diff] [blame] | 148 | @prot.should_receive(:read_list_end) |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 149 | real_skip.call(Thrift::Types::LIST) |
Kevin Clark | 9db1c2e | 2008-06-18 01:06:42 +0000 | [diff] [blame] | 150 | end |
| 151 | end |
| 152 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 153 | describe Thrift::BaseProtocolFactory do |
Bryan Duxbury | e8ae5d3 | 2009-03-24 05:23:52 +0000 | [diff] [blame] | 154 | it "should raise NotImplementedError" do |
Kevin Clark | 9db1c2e | 2008-06-18 01:06:42 +0000 | [diff] [blame] | 155 | # returning nil since Protocol is just an abstract class |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 156 | lambda {Thrift::BaseProtocolFactory.new.get_protocol(mock("MockTransport"))}.should raise_error(NotImplementedError) |
Kevin Clark | 9db1c2e | 2008-06-18 01:06:42 +0000 | [diff] [blame] | 157 | end |
Kevin Clark | dc39973 | 2008-06-18 01:06:15 +0000 | [diff] [blame] | 158 | end |
| 159 | end |