Cleaned up code supporting Ruby < 2.0
diff --git a/lib/rb/lib/thrift/bytes.rb b/lib/rb/lib/thrift/bytes.rb
index 169a950..1c9922d 100644
--- a/lib/rb/lib/thrift/bytes.rb
+++ b/lib/rb/lib/thrift/bytes.rb
@@ -1,5 +1,5 @@
# encoding: ascii-8bit
-#
+#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
@@ -7,9 +7,9 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -21,111 +21,78 @@
module Thrift
# A collection of utilities for working with bytes and byte buffers.
module Bytes
- if RUBY_VERSION >= '1.9'
- # Creates and empty byte buffer (String with BINARY encoding)
- #
- # size - The Integer size of the buffer (default: nil) to create
- #
- # Returns a String with BINARY encoding, filled with null characters
- # if size is greater than zero
- def self.empty_byte_buffer(size = nil)
- if (size && size > 0)
- "\0".b * size
- else
- "".b
- end
+ # Creates and empty byte buffer (String with BINARY encoding)
+ #
+ # size - The Integer size of the buffer (default: nil) to create
+ #
+ # Returns a String with BINARY encoding, filled with null characters
+ # if size is greater than zero
+ def self.empty_byte_buffer(size = nil)
+ if (size && size > 0)
+ "\0".b * size
+ else
+ ''.b
end
+ end
- # Forces the encoding of the buffer to BINARY. If the buffer
- # passed is frozen, then it will be duplicated.
- #
- # buffer - The String to force the encoding of.
- #
- # Returns the String passed with an encoding of BINARY; returned
- # String may be a duplicate.
- def self.force_binary_encoding(buffer)
- buffer = buffer.dup if buffer.frozen?
- buffer.force_encoding(Encoding::BINARY)
- end
+ # Forces the encoding of the buffer to BINARY. If the buffer
+ # passed is frozen, then it will be duplicated.
+ #
+ # buffer - The String to force the encoding of.
+ #
+ # Returns the String passed with an encoding of BINARY; returned
+ # String may be a duplicate.
+ def self.force_binary_encoding(buffer)
+ buffer = buffer.dup if buffer.frozen?
+ buffer.force_encoding(Encoding::BINARY)
+ end
- # Gets the byte value of a given position in a String.
- #
- # string - The String to retrive the byte value from.
- # index - The Integer location of the byte value to retrieve.
- #
- # Returns an Integer value between 0 and 255.
- def self.get_string_byte(string, index)
- string.getbyte(index)
- end
+ # Gets the byte value of a given position in a String.
+ #
+ # string - The String to retrive the byte value from.
+ # index - The Integer location of the byte value to retrieve.
+ #
+ # Returns an Integer value between 0 and 255.
+ def self.get_string_byte(string, index)
+ string.getbyte(index)
+ end
- # Sets the byte value given to a given index in a String.
- #
- # string - The String to set the byte value in.
- # index - The Integer location to set the byte value at.
- # byte - The Integer value (0 to 255) to set in the string.
- #
- # Returns an Integer value of the byte value to set.
- def self.set_string_byte(string, index, byte)
- string.setbyte(index, byte)
- end
+ # Sets the byte value given to a given index in a String.
+ #
+ # string - The String to set the byte value in.
+ # index - The Integer location to set the byte value at.
+ # byte - The Integer value (0 to 255) to set in the string.
+ #
+ # Returns an Integer value of the byte value to set.
+ def self.set_string_byte(string, index, byte)
+ string.setbyte(index, byte)
+ end
- # Converts the given String to a UTF-8 byte buffer.
- #
- # string - The String to convert.
- #
- # Returns a new String with BINARY encoding, containing the UTF-8
- # bytes of the original string.
- def self.convert_to_utf8_byte_buffer(string)
- if string.encoding != Encoding::UTF_8
- # transcode to UTF-8
- string = string.encode(Encoding::UTF_8)
- else
- # encoding is already UTF-8, but a duplicate is needed
- string = string.dup
- end
- string.force_encoding(Encoding::BINARY)
+ # Converts the given String to a UTF-8 byte buffer.
+ #
+ # string - The String to convert.
+ #
+ # Returns a new String with BINARY encoding, containing the UTF-8
+ # bytes of the original string.
+ def self.convert_to_utf8_byte_buffer(string)
+ if string.encoding != Encoding::UTF_8
+ # transcode to UTF-8
+ string = string.encode(Encoding::UTF_8)
+ else
+ # encoding is already UTF-8, but a duplicate is needed
+ string = string.dup
end
+ string.force_encoding(Encoding::BINARY)
+ end
- # Converts the given UTF-8 byte buffer into a String
- #
- # utf8_buffer - A String, with BINARY encoding, containing UTF-8 bytes
- #
- # Returns a new String with UTF-8 encoding,
- def self.convert_to_string(utf8_buffer)
- # duplicate the buffer, force encoding to UTF-8
- utf8_buffer.dup.force_encoding(Encoding::UTF_8)
- end
- else
- def self.empty_byte_buffer(size = nil)
- if (size && size > 0)
- "\0" * size
- else
- ''
- end
- end
-
- def self.force_binary_encoding(buffer)
- buffer
- end
-
- def self.get_string_byte(string, index)
- string[index]
- end
-
- def self.set_string_byte(string, index, byte)
- string[index] = byte
- end
-
- def self.convert_to_utf8_byte_buffer(string)
- # This assumes $KCODE is 'UTF8'/'U', which would mean the String is already a UTF-8 byte buffer
- # TODO consider handling other $KCODE values and transcoding with iconv
- string
- end
-
- def self.convert_to_string(utf8_buffer)
- # See comment in 'convert_to_utf8_byte_buffer' for relevant assumptions.
- utf8_buffer
- end
+ # Converts the given UTF-8 byte buffer into a String
+ #
+ # utf8_buffer - A String, with BINARY encoding, containing UTF-8 bytes
+ #
+ # Returns a new String with UTF-8 encoding,
+ def self.convert_to_string(utf8_buffer)
+ # duplicate the buffer, force encoding to UTF-8
+ utf8_buffer.dup.force_encoding(Encoding::UTF_8)
end
end
end
diff --git a/lib/rb/lib/thrift/protocol/base_protocol.rb b/lib/rb/lib/thrift/protocol/base_protocol.rb
index dfce44a..f2ee8dd 100644
--- a/lib/rb/lib/thrift/protocol/base_protocol.rb
+++ b/lib/rb/lib/thrift/protocol/base_protocol.rb
@@ -1,4 +1,4 @@
-#
+#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
@@ -6,16 +6,16 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-#
+#
# this require is to make generated struct definitions happy
require 'set'
@@ -116,7 +116,7 @@
raise NotImplementedError
end
- # Writes a Thrift String. In Ruby 1.9+, the String passed will be transcoded to UTF-8.
+ # Writes a Thrift String. The String passed will be transcoded to UTF-8.
#
# str - The String to write.
#
@@ -127,7 +127,7 @@
raise NotImplementedError
end
- # Writes a Thrift Binary (Thrift String with no encoding). In Ruby 1.9+, the String passed
+ # Writes a Thrift Binary (Thrift String with no encoding). The String passed
# will forced into BINARY encoding.
#
# buf - The String to write.
@@ -206,14 +206,14 @@
raise NotImplementedError
end
- # Reads a Thrift String. In Ruby 1.9+, all Strings will be returned with an Encoding of UTF-8.
+ # Reads a Thrift String. All Strings will be returned with an Encoding of UTF-8.
#
# Returns a String.
def read_string
raise NotImplementedError
end
- # Reads a Thrift Binary (Thrift String without encoding). In Ruby 1.9+, all Strings will be returned
+ # Reads a Thrift Binary (Thrift String without encoding). All Strings will be returned
# with an Encoding of BINARY.
#
# Returns a String.
@@ -391,7 +391,7 @@
raise ProtocolException.new(ProtocolException::INVALID_DATA, 'Invalid data')
end
end
-
+
def to_s
"#{trans.to_s}"
end
@@ -401,7 +401,7 @@
def get_protocol(trans)
raise NotImplementedError
end
-
+
def to_s
"base"
end
diff --git a/lib/rb/lib/thrift/protocol/json_protocol.rb b/lib/rb/lib/thrift/protocol/json_protocol.rb
index 09e3166..84f7fc8 100644
--- a/lib/rb/lib/thrift/protocol/json_protocol.rb
+++ b/lib/rb/lib/thrift/protocol/json_protocol.rb
@@ -1,5 +1,5 @@
# encoding: UTF-8
-#
+#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
@@ -7,9 +7,9 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -504,11 +504,7 @@
str += @reader.read
str += @reader.read
str += @reader.read
- if RUBY_VERSION >= '1.9'
- str.hex.chr(Encoding::UTF_8)
- else
- str.hex.chr
- end
+ str.hex.chr(Encoding::UTF_8)
end
# Decodes a JSON string, including unescaping, and returns the string via str
diff --git a/lib/rb/lib/thrift/transport/base_transport.rb b/lib/rb/lib/thrift/transport/base_transport.rb
index 12dd673..891b10e 100644
--- a/lib/rb/lib/thrift/transport/base_transport.rb
+++ b/lib/rb/lib/thrift/transport/base_transport.rb
@@ -1,5 +1,5 @@
# encoding: ascii-8bit
-#
+#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
@@ -7,16 +7,16 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-#
+#
module Thrift
class TransportException < Exception
@@ -48,12 +48,12 @@
class BaseTransport
def open?; end
-
+
def open; end
def close; end
- # Reads a number of bytes from the transports. In Ruby 1.9+, the String returned will have a BINARY (aka ASCII8BIT) encoding.
+ # Reads a number of bytes from the transports. The String returned will have a BINARY (aka ASCII-8BIT) encoding.
#
# sz - The number of bytes to read from the transport.
#
@@ -86,11 +86,11 @@
chunk = read(size - buf.length)
buf << chunk
end
-
+
buf
end
- # Writes the byte buffer to the transport. In Ruby 1.9+, the buffer will be forced into BINARY encoding.
+ # Writes the byte buffer to the transport. The buffer will be forced into BINARY encoding.
#
# buf - A String acting as a byte buffer.
#
@@ -104,12 +104,12 @@
"base"
end
end
-
+
class BaseTransportFactory
def get_transport(trans)
return trans
end
-
+
def to_s
"base"
end
diff --git a/lib/rb/spec/binary_protocol_spec_shared.rb b/lib/rb/spec/binary_protocol_spec_shared.rb
index c83f5ae..d8edf85 100644
--- a/lib/rb/spec/binary_protocol_spec_shared.rb
+++ b/lib/rb/spec/binary_protocol_spec_shared.rb
@@ -38,25 +38,25 @@
it "should make strict_write readable" do
expect(@prot.strict_write).to eql(true)
- end
+ end
it "should write the message header" do
@prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
expect(@trans.read(@trans.available)).to eq([protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::CALL, "testMessage".size, "testMessage", 17].pack("NNa11N"))
end
-
+
it "should write the message header without version when writes are not strict" do
@prot = protocol_class.new(@trans, true, false) # no strict write
@prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
expect(@trans.read(@trans.available)).to eq("\000\000\000\vtestMessage\001\000\000\000\021")
end
-
+
it "should write the message header with a version when writes are strict" do
@prot = protocol_class.new(@trans) # strict write
@prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
expect(@trans.read(@trans.available)).to eq("\200\001\000\001\000\000\000\vtestMessage\000\000\000\021")
end
-
+
# message footer is a noop
@@ -64,44 +64,44 @@
@prot.write_field_begin('foo', Thrift::Types::DOUBLE, 3)
expect(@trans.read(@trans.available)).to eq([Thrift::Types::DOUBLE, 3].pack("cn"))
end
-
+
# field footer is a noop
-
+
it "should write the STOP field" do
@prot.write_field_stop
expect(@trans.read(1)).to eq("\000")
end
-
+
it "should write the map header" do
@prot.write_map_begin(Thrift::Types::STRING, Thrift::Types::LIST, 17)
expect(@trans.read(@trans.available)).to eq([Thrift::Types::STRING, Thrift::Types::LIST, 17].pack("ccN"));
end
-
+
# map footer is a noop
-
+
it "should write the list header" do
@prot.write_list_begin(Thrift::Types::I16, 42)
expect(@trans.read(@trans.available)).to eq([Thrift::Types::I16, 42].pack("cN"))
end
-
+
# list footer is a noop
-
+
it "should write the set header" do
@prot.write_set_begin(Thrift::Types::I16, 42)
expect(@trans.read(@trans.available)).to eq([Thrift::Types::I16, 42].pack("cN"))
end
-
+
it "should write a bool" do
@prot.write_bool(true)
@prot.write_bool(false)
expect(@trans.read(@trans.available)).to eq("\001\000")
end
-
+
it "should treat a nil bool as false" do
@prot.write_bool(nil)
expect(@trans.read(1)).to eq("\000")
end
-
+
it "should write a byte" do
# byte is small enough, let's check -128..127
(-128..127).each do |i|
@@ -120,11 +120,11 @@
it "errors out with a Bignum" do
expect { @prot.write_byte(2**65) }.to raise_error(RangeError)
end
-
+
it "should error gracefully when trying to write a nil byte" do
expect { @prot.write_byte(nil) }.to raise_error(StandardError, 'nil argument not allowed!')
end
-
+
it "should write an i16" do
# try a random scattering of values
# include the signed i16 minimum/maximum
@@ -133,17 +133,17 @@
end
# and try something out of signed range, it should clip
@prot.write_i16(2**15 + 5)
-
+
expect(@trans.read(@trans.available)).to eq("\200\000\374\000\000\021\000\000\330\360\006\273\177\377\200\005")
-
+
# a Bignum should error
# lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
end
-
+
it "should error gracefully when trying to write a nil i16" do
expect { @prot.write_i16(nil) }.to raise_error(StandardError, 'nil argument not allowed!')
end
-
+
it "should write an i32" do
# try a random scattering of values
# include the signed i32 minimum/maximum
@@ -153,14 +153,14 @@
# try something out of signed range, it should clip
expect(@trans.read(@trans.available)).to eq("\200\000\000\000" + "\377\376\037\r" + "\377\377\366\034" + "\377\377\377\375" + "\000\000\000\000" + "\000#\340\203" + "\000\0000+" + "\177\377\377\377")
[2 ** 31 + 5, 2 ** 65 + 5].each do |i|
- expect { @prot.write_i32(i) }.to raise_error(RangeError)
+ expect { @prot.write_i32(i) }.to raise_error(RangeError)
end
end
-
+
it "should error gracefully when trying to write a nil i32" do
expect { @prot.write_i32(nil) }.to raise_error(StandardError, 'nil argument not allowed!')
end
-
+
it "should write an i64" do
# try a random scattering of values
# try the signed i64 minimum/maximum
@@ -179,11 +179,11 @@
"\177\377\377\377\377\377\377\377"].join(""))
expect { @prot.write_i64(2 ** 65 + 5) }.to raise_error(RangeError)
end
-
+
it "should error gracefully when trying to write a nil i64" do
expect { @prot.write_i64(nil) }.to raise_error(StandardError, 'nil argument not allowed!')
end
-
+
it "should write a double" do
# try a random scattering of values, including min/max
values = [Float::MIN,-1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX]
@@ -192,58 +192,42 @@
expect(@trans.read(@trans.available)).to eq([f].pack("G"))
end
end
-
+
it "should error gracefully when trying to write a nil double" do
expect { @prot.write_double(nil) }.to raise_error(StandardError, 'nil argument not allowed!')
end
- if RUBY_VERSION >= '1.9'
- it 'should write a string' do
- str = 'abc'
- @prot.write_string(str)
- a = @trans.read(@trans.available)
- expect(a.encoding).to eq(Encoding::BINARY)
- expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63])
- end
+ it 'should write a string' do
+ str = 'abc'
+ @prot.write_string(str)
+ a = @trans.read(@trans.available)
+ expect(a.encoding).to eq(Encoding::BINARY)
+ expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63])
+ end
- it 'should write a string with unicode characters' do
- str = "abc \u20AC \u20AD".encode('UTF-8')
- @prot.write_string(str)
- a = @trans.read(@trans.available)
- expect(a.encoding).to eq(Encoding::BINARY)
- expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x0B, 0x61, 0x62, 0x63, 0x20,
- 0xE2, 0x82, 0xAC, 0x20, 0xE2, 0x82, 0xAD])
- end
+ it 'should write a string with unicode characters' do
+ str = "abc \u20AC \u20AD".encode('UTF-8')
+ @prot.write_string(str)
+ a = @trans.read(@trans.available)
+ expect(a.encoding).to eq(Encoding::BINARY)
+ expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x0B, 0x61, 0x62, 0x63, 0x20,
+ 0xE2, 0x82, 0xAC, 0x20, 0xE2, 0x82, 0xAD])
+ end
- it 'should write should write a string with unicode characters and transcoding' do
- str = "abc \u20AC".encode('ISO-8859-15')
- @prot.write_string(str)
- a = @trans.read(@trans.available)
- expect(a.encoding).to eq(Encoding::BINARY)
- expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x07, 0x61, 0x62, 0x63, 0x20, 0xE2, 0x82, 0xAC])
- end
+ it 'should write should write a string with unicode characters and transcoding' do
+ str = "abc \u20AC".encode('ISO-8859-15')
+ @prot.write_string(str)
+ a = @trans.read(@trans.available)
+ expect(a.encoding).to eq(Encoding::BINARY)
+ expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x07, 0x61, 0x62, 0x63, 0x20, 0xE2, 0x82, 0xAC])
+ end
- it 'should write a binary string' do
- buffer = [0, 1, 2, 3].pack('C*')
- @prot.write_binary(buffer)
- a = @trans.read(@trans.available)
- expect(a.encoding).to eq(Encoding::BINARY)
- expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03])
- end
- else
- it 'should write a string' do
- str = 'abc'
- @prot.write_string(str)
- a = @trans.read(@trans.available)
- expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63])
- end
-
- it 'should write a binary string' do
- buffer = [0, 1, 2, 3].pack('C*')
- @prot.write_binary(buffer)
- a = @trans.read(@trans.available)
- expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03])
- end
+ it 'should write a binary string' do
+ buffer = [0, 1, 2, 3].pack('C*')
+ @prot.write_binary(buffer)
+ a = @trans.read(@trans.available)
+ expect(a.encoding).to eq(Encoding::BINARY)
+ expect(a.unpack('C*')).to eq([0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03])
end
it "should error gracefully when trying to write a nil string" do
@@ -300,36 +284,36 @@
@trans.write([Thrift::Types::DOUBLE, Thrift::Types::I64, 42].pack("ccN"))
expect(@prot.read_map_begin).to eq([Thrift::Types::DOUBLE, Thrift::Types::I64, 42])
end
-
+
# map footer is a noop
-
+
it "should read a list header" do
@trans.write([Thrift::Types::STRING, 17].pack("cN"))
expect(@prot.read_list_begin).to eq([Thrift::Types::STRING, 17])
end
-
+
# list footer is a noop
-
+
it "should read a set header" do
@trans.write([Thrift::Types::STRING, 17].pack("cN"))
expect(@prot.read_set_begin).to eq([Thrift::Types::STRING, 17])
end
-
+
# set footer is a noop
-
+
it "should read a bool" do
@trans.write("\001\000");
expect(@prot.read_bool).to eq(true)
expect(@prot.read_bool).to eq(false)
end
-
+
it "should read a byte" do
[-128, -57, -3, 0, 17, 24, 127].each do |i|
@trans.write([i].pack("c"))
expect(@prot.read_byte).to eq(i)
end
end
-
+
it "should read an i16" do
# try a scattering of values, including min/max
[-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
@@ -337,7 +321,7 @@
expect(@prot.read_i16).to eq(i)
end
end
-
+
it "should read an i32" do
# try a scattering of values, including min/max
[-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
@@ -345,7 +329,7 @@
expect(@prot.read_i32).to eq(i)
end
end
-
+
it "should read an i64" do
# try a scattering of values, including min/max
[-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
@@ -353,7 +337,7 @@
expect(@prot.read_i64).to eq(i)
end
end
-
+
it "should read a double" do
# try a random scattering of values, including min/max
[Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
@@ -362,46 +346,30 @@
end
end
- if RUBY_VERSION >= '1.9'
- it 'should read a string' do
- # i32 of value 3, followed by three characters/UTF-8 bytes 'a', 'b', 'c'
- buffer = [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63].pack('C*')
- @trans.write(buffer)
- a = @prot.read_string
- expect(a).to eq('abc'.encode('UTF-8'))
- expect(a.encoding).to eq(Encoding::UTF_8)
- end
+ it 'should read a string' do
+ # i32 of value 3, followed by three characters/UTF-8 bytes 'a', 'b', 'c'
+ buffer = [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63].pack('C*')
+ @trans.write(buffer)
+ a = @prot.read_string
+ expect(a).to eq('abc'.encode('UTF-8'))
+ expect(a.encoding).to eq(Encoding::UTF_8)
+ end
- it 'should read a string containing unicode characters from UTF-8 encoded buffer' do
- # i32 of value 3, followed by one character U+20AC made up of three bytes
- buffer = [0x00, 0x00, 0x00, 0x03, 0xE2, 0x82, 0xAC].pack('C*')
- @trans.write(buffer)
- a = @prot.read_string
- expect(a).to eq("\u20AC".encode('UTF-8'))
- expect(a.encoding).to eq(Encoding::UTF_8)
- end
+ it 'should read a string containing unicode characters from UTF-8 encoded buffer' do
+ # i32 of value 3, followed by one character U+20AC made up of three bytes
+ buffer = [0x00, 0x00, 0x00, 0x03, 0xE2, 0x82, 0xAC].pack('C*')
+ @trans.write(buffer)
+ a = @prot.read_string
+ expect(a).to eq("\u20AC".encode('UTF-8'))
+ expect(a.encoding).to eq(Encoding::UTF_8)
+ end
- it 'should read a binary string' do
- buffer = [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03].pack('C*')
- @trans.write(buffer)
- a = @prot.read_binary
- expect(a).to eq([0x00, 0x01, 0x02, 0x03].pack('C*'))
- expect(a.encoding).to eq(Encoding::BINARY)
- end
- else
- it 'should read a string' do
- # i32 of value 3, followed by three characters/UTF-8 bytes 'a', 'b', 'c'
- buffer = [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63].pack('C*')
- @trans.write(buffer)
- expect(@prot.read_string).to eq('abc')
- end
-
- it 'should read a binary string' do
- buffer = [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03].pack('C*')
- @trans.write(buffer)
- a = @prot.read_binary
- expect(a).to eq([0x00, 0x01, 0x02, 0x03].pack('C*'))
- end
+ it 'should read a binary string' do
+ buffer = [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03].pack('C*')
+ @trans.write(buffer)
+ a = @prot.read_binary
+ expect(a).to eq([0x00, 0x01, 0x02, 0x03].pack('C*'))
+ expect(a.encoding).to eq(Encoding::BINARY)
end
it "should perform a complete rpc with no args or return" do
@@ -433,7 +401,7 @@
def get_socket_connection
server = Thrift::ServerSocket.new("localhost", 9090)
server.listen
-
+
clientside = Thrift::Socket.new("localhost", 9090)
clientside.open
serverside = server.accept
@@ -463,14 +431,14 @@
server.close
end
- class SrvHandler
+ class SrvHandler
def voidMethod()
end
-
+
def primitiveMethod
1
end
-
+
def structMethod
Fixtures::COMPACT_PROTOCOL_TEST_STRUCT
end
diff --git a/lib/rb/spec/bytes_spec.rb b/lib/rb/spec/bytes_spec.rb
index 2e8653c..c3ec087 100644
--- a/lib/rb/spec/bytes_spec.rb
+++ b/lib/rb/spec/bytes_spec.rb
@@ -21,140 +21,79 @@
require 'spec_helper'
describe Thrift::Bytes do
- if RUBY_VERSION >= '1.9'
- describe '.empty_byte_buffer' do
- it 'should create an empty buffer' do
- b = Thrift::Bytes.empty_byte_buffer
- expect(b.length).to eq(0)
- expect(b.encoding).to eq(Encoding::BINARY)
- end
-
- it 'should create an empty buffer of given size' do
- b = Thrift::Bytes.empty_byte_buffer 2
- expect(b.length).to eq(2)
- expect(b.getbyte(0)).to eq(0)
- expect(b.getbyte(1)).to eq(0)
- expect(b.encoding).to eq(Encoding::BINARY)
- end
+ describe '.empty_byte_buffer' do
+ it 'should create an empty buffer' do
+ b = Thrift::Bytes.empty_byte_buffer
+ expect(b.length).to eq(0)
+ expect(b.encoding).to eq(Encoding::BINARY)
end
- describe '.force_binary_encoding' do
- it 'should change encoding' do
- e = 'STRING'.encode('UTF-8')
- expect(e.encoding).not_to eq(Encoding::BINARY)
- a = Thrift::Bytes.force_binary_encoding e
- expect(a.encoding).to eq(Encoding::BINARY)
- end
+ it 'should create an empty buffer of given size' do
+ b = Thrift::Bytes.empty_byte_buffer 2
+ expect(b.length).to eq(2)
+ expect(b.getbyte(0)).to eq(0)
+ expect(b.getbyte(1)).to eq(0)
+ expect(b.encoding).to eq(Encoding::BINARY)
+ end
+ end
+
+ describe '.force_binary_encoding' do
+ it 'should change encoding' do
+ e = 'STRING'.encode('UTF-8')
+ expect(e.encoding).not_to eq(Encoding::BINARY)
+ a = Thrift::Bytes.force_binary_encoding e
+ expect(a.encoding).to eq(Encoding::BINARY)
+ end
+ end
+
+ describe '.get_string_byte' do
+ it 'should get the byte at index' do
+ s = "\x41\x42"
+ expect(Thrift::Bytes.get_string_byte(s, 0)).to eq(0x41)
+ expect(Thrift::Bytes.get_string_byte(s, 1)).to eq(0x42)
+ end
+ end
+
+ describe '.set_string_byte' do
+ it 'should set byte value at index' do
+ s = "\x41\x42"
+ Thrift::Bytes.set_string_byte(s, 0, 0x43)
+ expect(s.getbyte(0)).to eq(0x43)
+ expect(s).to eq('CB')
+ end
+ end
+
+ describe '.convert_to_utf8_byte_buffer' do
+ it 'should convert UTF-8 String to byte buffer' do
+ e = "\u20AC".encode('UTF-8') # a string with euro sign character U+20AC
+ expect(e.length).to eq(1)
+
+ a = Thrift::Bytes.convert_to_utf8_byte_buffer e
+ expect(a.encoding).to eq(Encoding::BINARY)
+ expect(a.length).to eq(3)
+ expect(a.unpack('C*')).to eq([0xE2, 0x82, 0xAC])
end
- describe '.get_string_byte' do
- it 'should get the byte at index' do
- s = "\x41\x42"
- expect(Thrift::Bytes.get_string_byte(s, 0)).to eq(0x41)
- expect(Thrift::Bytes.get_string_byte(s, 1)).to eq(0x42)
- end
+ it 'should convert ISO-8859-15 String to UTF-8 byte buffer' do
+ # Assumptions
+ e = "\u20AC".encode('ISO-8859-15') # a string with euro sign character U+20AC, then converted to ISO-8859-15
+ expect(e.length).to eq(1)
+ expect(e.unpack('C*')).to eq([0xA4]) # euro sign is a different code point in ISO-8859-15
+
+ a = Thrift::Bytes.convert_to_utf8_byte_buffer e
+ expect(a.encoding).to eq(Encoding::BINARY)
+ expect(a.length).to eq(3)
+ expect(a.unpack('C*')).to eq([0xE2, 0x82, 0xAC])
end
+ end
- describe '.set_string_byte' do
- it 'should set byte value at index' do
- s = "\x41\x42"
- Thrift::Bytes.set_string_byte(s, 0, 0x43)
- expect(s.getbyte(0)).to eq(0x43)
- expect(s).to eq('CB')
- end
- end
-
- describe '.convert_to_utf8_byte_buffer' do
- it 'should convert UTF-8 String to byte buffer' do
- e = "\u20AC".encode('UTF-8') # a string with euro sign character U+20AC
- expect(e.length).to eq(1)
-
- a = Thrift::Bytes.convert_to_utf8_byte_buffer e
- expect(a.encoding).to eq(Encoding::BINARY)
- expect(a.length).to eq(3)
- expect(a.unpack('C*')).to eq([0xE2, 0x82, 0xAC])
- end
-
- it 'should convert ISO-8859-15 String to UTF-8 byte buffer' do
- # Assumptions
- e = "\u20AC".encode('ISO-8859-15') # a string with euro sign character U+20AC, then converted to ISO-8859-15
- expect(e.length).to eq(1)
- expect(e.unpack('C*')).to eq([0xA4]) # euro sign is a different code point in ISO-8859-15
-
- a = Thrift::Bytes.convert_to_utf8_byte_buffer e
- expect(a.encoding).to eq(Encoding::BINARY)
- expect(a.length).to eq(3)
- expect(a.unpack('C*')).to eq([0xE2, 0x82, 0xAC])
- end
- end
-
- describe '.convert_to_string' do
- it 'should convert UTF-8 byte buffer to a UTF-8 String' do
- e = [0xE2, 0x82, 0xAC].pack("C*")
- expect(e.encoding).to eq(Encoding::BINARY)
- a = Thrift::Bytes.convert_to_string e
- expect(a.encoding).to eq(Encoding::UTF_8)
- expect(a).to eq("\u20AC")
- end
- end
-
- else # RUBY_VERSION
- describe '.empty_byte_buffer' do
- it 'should create an empty buffer' do
- b = Thrift::Bytes.empty_byte_buffer
- expect(b.length).to eq(0)
- end
-
- it 'should create an empty buffer of given size' do
- b = Thrift::Bytes.empty_byte_buffer 2
- expect(b.length).to eq(2)
- expect(b[0]).to eq(0)
- expect(b[1]).to eq(0)
- end
- end
-
- describe '.force_binary_encoding' do
- it 'should be a no-op' do
- e = 'STRING'
- a = Thrift::Bytes.force_binary_encoding e
- expect(a).to eq(e)
- expect(a).to be(e)
- end
- end
-
- describe '.get_string_byte' do
- it 'should get the byte at index' do
- s = "\x41\x42"
- expect(Thrift::Bytes.get_string_byte(s, 0)).to eq(0x41)
- expect(Thrift::Bytes.get_string_byte(s, 1)).to eq(0x42)
- end
- end
-
- describe '.set_string_byte' do
- it 'should set byte value at index' do
- s = "\x41\x42"
- Thrift::Bytes.set_string_byte(s, 0, 0x43)
- expect(s[0]).to eq(0x43)
- expect(s).to eq('CB')
- end
- end
-
- describe '.convert_to_utf8_byte_buffer' do
- it 'should be a no-op' do
- e = 'STRING'
- a = Thrift::Bytes.convert_to_utf8_byte_buffer e
- expect(a).to eq(e)
- expect(a).to be(e)
- end
- end
-
- describe '.convert_to_string' do
- it 'should be a no-op' do
- e = 'STRING'
- a = Thrift::Bytes.convert_to_string e
- expect(a).to eq(e)
- expect(a).to be(e)
- end
+ describe '.convert_to_string' do
+ it 'should convert UTF-8 byte buffer to a UTF-8 String' do
+ e = [0xE2, 0x82, 0xAC].pack("C*")
+ expect(e.encoding).to eq(Encoding::BINARY)
+ a = Thrift::Bytes.convert_to_string e
+ expect(a.encoding).to eq(Encoding::UTF_8)
+ expect(a).to eq("\u20AC")
end
end
end
diff --git a/lib/rb/spec/json_protocol_spec.rb b/lib/rb/spec/json_protocol_spec.rb
index 9acaf86..e53fd3e 100644
--- a/lib/rb/spec/json_protocol_spec.rb
+++ b/lib/rb/spec/json_protocol_spec.rb
@@ -221,25 +221,18 @@
expect(@trans.read(@trans.available)).to eq("\"-Infinity\"")
end
- if RUBY_VERSION >= '1.9'
- it 'should write string' do
- @prot.write_string('this is a test string')
- a = @trans.read(@trans.available)
- expect(a).to eq('"this is a test string"'.force_encoding(Encoding::BINARY))
- expect(a.encoding).to eq(Encoding::BINARY)
- end
+ it 'should write string' do
+ @prot.write_string('this is a test string')
+ a = @trans.read(@trans.available)
+ expect(a).to eq('"this is a test string"'.force_encoding(Encoding::BINARY))
+ expect(a.encoding).to eq(Encoding::BINARY)
+ end
- it 'should write string with unicode characters' do
- @prot.write_string("this is a test string with unicode characters: \u20AC \u20AD")
- a = @trans.read(@trans.available)
- expect(a).to eq("\"this is a test string with unicode characters: \u20AC \u20AD\"".force_encoding(Encoding::BINARY))
- expect(a.encoding).to eq(Encoding::BINARY)
- end
- else
- it 'should write string' do
- @prot.write_string('this is a test string')
- expect(@trans.read(@trans.available)).to eq('"this is a test string"')
- end
+ it 'should write string with unicode characters' do
+ @prot.write_string("this is a test string with unicode characters: \u20AC \u20AD")
+ a = @trans.read(@trans.available)
+ expect(a).to eq("\"this is a test string with unicode characters: \u20AC \u20AD\"".force_encoding(Encoding::BINARY))
+ expect(a.encoding).to eq(Encoding::BINARY)
end
it "should write binary" do
@@ -509,25 +502,18 @@
expect(@prot.read_double).to eq(12.23)
end
- if RUBY_VERSION >= '1.9'
- it 'should read string' do
- @trans.write('"this is a test string"'.force_encoding(Encoding::BINARY))
- a = @prot.read_string
- expect(a).to eq('this is a test string')
- expect(a.encoding).to eq(Encoding::UTF_8)
- end
+ it 'should read string' do
+ @trans.write('"this is a test string"'.force_encoding(Encoding::BINARY))
+ a = @prot.read_string
+ expect(a).to eq('this is a test string')
+ expect(a.encoding).to eq(Encoding::UTF_8)
+ end
- it 'should read string with unicode characters' do
- @trans.write('"this is a test string with unicode characters: \u20AC \u20AD"'.force_encoding(Encoding::BINARY))
- a = @prot.read_string
- expect(a).to eq("this is a test string with unicode characters: \u20AC \u20AD")
- expect(a.encoding).to eq(Encoding::UTF_8)
- end
- else
- it 'should read string' do
- @trans.write('"this is a test string"')
- expect(@prot.read_string).to eq('this is a test string')
- end
+ it 'should read string with unicode characters' do
+ @trans.write('"this is a test string with unicode characters: \u20AC \u20AD"'.force_encoding(Encoding::BINARY))
+ a = @prot.read_string
+ expect(a).to eq("this is a test string with unicode characters: \u20AC \u20AD")
+ expect(a.encoding).to eq(Encoding::UTF_8)
end
it "should read binary" do