blob: 2e8653cfc4ab8b04ed1bd845eb24434426d92ca3 [file] [log] [blame]
Jake Farrellb5a18a12012-10-09 01:10:43 +00001# encoding: UTF-8
2#
3# Licensed to the Apache Software Foundation (ASF) under one
4# or more contributor license agreements. See the NOTICE file
5# distributed with this work for additional information
6# regarding copyright ownership. The ASF licenses this file
7# to you under the Apache License, Version 2.0 (the
8# "License"); you may not use this file except in compliance
9# with the License. You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing,
14# software distributed under the License is distributed on an
15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16# KIND, either express or implied. See the License for the
17# specific language governing permissions and limitations
18# under the License.
19#
20
21require 'spec_helper'
22
23describe Thrift::Bytes do
24 if RUBY_VERSION >= '1.9'
25 describe '.empty_byte_buffer' do
26 it 'should create an empty buffer' do
27 b = Thrift::Bytes.empty_byte_buffer
James E. King III27247072018-03-22 20:50:23 -040028 expect(b.length).to eq(0)
29 expect(b.encoding).to eq(Encoding::BINARY)
Jake Farrellb5a18a12012-10-09 01:10:43 +000030 end
31
32 it 'should create an empty buffer of given size' do
33 b = Thrift::Bytes.empty_byte_buffer 2
James E. King III27247072018-03-22 20:50:23 -040034 expect(b.length).to eq(2)
35 expect(b.getbyte(0)).to eq(0)
36 expect(b.getbyte(1)).to eq(0)
37 expect(b.encoding).to eq(Encoding::BINARY)
Jake Farrellb5a18a12012-10-09 01:10:43 +000038 end
39 end
40
41 describe '.force_binary_encoding' do
42 it 'should change encoding' do
43 e = 'STRING'.encode('UTF-8')
James E. King III27247072018-03-22 20:50:23 -040044 expect(e.encoding).not_to eq(Encoding::BINARY)
Jake Farrellb5a18a12012-10-09 01:10:43 +000045 a = Thrift::Bytes.force_binary_encoding e
James E. King III27247072018-03-22 20:50:23 -040046 expect(a.encoding).to eq(Encoding::BINARY)
Jake Farrellb5a18a12012-10-09 01:10:43 +000047 end
48 end
49
50 describe '.get_string_byte' do
51 it 'should get the byte at index' do
52 s = "\x41\x42"
James E. King III27247072018-03-22 20:50:23 -040053 expect(Thrift::Bytes.get_string_byte(s, 0)).to eq(0x41)
54 expect(Thrift::Bytes.get_string_byte(s, 1)).to eq(0x42)
Jake Farrellb5a18a12012-10-09 01:10:43 +000055 end
56 end
57
58 describe '.set_string_byte' do
59 it 'should set byte value at index' do
60 s = "\x41\x42"
61 Thrift::Bytes.set_string_byte(s, 0, 0x43)
James E. King III27247072018-03-22 20:50:23 -040062 expect(s.getbyte(0)).to eq(0x43)
63 expect(s).to eq('CB')
Jake Farrellb5a18a12012-10-09 01:10:43 +000064 end
65 end
66
67 describe '.convert_to_utf8_byte_buffer' do
68 it 'should convert UTF-8 String to byte buffer' do
69 e = "\u20AC".encode('UTF-8') # a string with euro sign character U+20AC
James E. King III27247072018-03-22 20:50:23 -040070 expect(e.length).to eq(1)
Jake Farrellb5a18a12012-10-09 01:10:43 +000071
72 a = Thrift::Bytes.convert_to_utf8_byte_buffer e
James E. King III27247072018-03-22 20:50:23 -040073 expect(a.encoding).to eq(Encoding::BINARY)
74 expect(a.length).to eq(3)
75 expect(a.unpack('C*')).to eq([0xE2, 0x82, 0xAC])
Jake Farrellb5a18a12012-10-09 01:10:43 +000076 end
77
78 it 'should convert ISO-8859-15 String to UTF-8 byte buffer' do
79 # Assumptions
80 e = "\u20AC".encode('ISO-8859-15') # a string with euro sign character U+20AC, then converted to ISO-8859-15
James E. King III27247072018-03-22 20:50:23 -040081 expect(e.length).to eq(1)
82 expect(e.unpack('C*')).to eq([0xA4]) # euro sign is a different code point in ISO-8859-15
Jake Farrellb5a18a12012-10-09 01:10:43 +000083
84 a = Thrift::Bytes.convert_to_utf8_byte_buffer e
James E. King III27247072018-03-22 20:50:23 -040085 expect(a.encoding).to eq(Encoding::BINARY)
86 expect(a.length).to eq(3)
87 expect(a.unpack('C*')).to eq([0xE2, 0x82, 0xAC])
Jake Farrellb5a18a12012-10-09 01:10:43 +000088 end
89 end
90
91 describe '.convert_to_string' do
92 it 'should convert UTF-8 byte buffer to a UTF-8 String' do
93 e = [0xE2, 0x82, 0xAC].pack("C*")
James E. King III27247072018-03-22 20:50:23 -040094 expect(e.encoding).to eq(Encoding::BINARY)
Jake Farrellb5a18a12012-10-09 01:10:43 +000095 a = Thrift::Bytes.convert_to_string e
James E. King III27247072018-03-22 20:50:23 -040096 expect(a.encoding).to eq(Encoding::UTF_8)
97 expect(a).to eq("\u20AC")
Jake Farrellb5a18a12012-10-09 01:10:43 +000098 end
99 end
100
101 else # RUBY_VERSION
102 describe '.empty_byte_buffer' do
103 it 'should create an empty buffer' do
104 b = Thrift::Bytes.empty_byte_buffer
James E. King III27247072018-03-22 20:50:23 -0400105 expect(b.length).to eq(0)
Jake Farrellb5a18a12012-10-09 01:10:43 +0000106 end
107
108 it 'should create an empty buffer of given size' do
109 b = Thrift::Bytes.empty_byte_buffer 2
James E. King III27247072018-03-22 20:50:23 -0400110 expect(b.length).to eq(2)
111 expect(b[0]).to eq(0)
112 expect(b[1]).to eq(0)
Jake Farrellb5a18a12012-10-09 01:10:43 +0000113 end
114 end
115
116 describe '.force_binary_encoding' do
117 it 'should be a no-op' do
118 e = 'STRING'
119 a = Thrift::Bytes.force_binary_encoding e
James E. King III27247072018-03-22 20:50:23 -0400120 expect(a).to eq(e)
121 expect(a).to be(e)
Jake Farrellb5a18a12012-10-09 01:10:43 +0000122 end
123 end
124
125 describe '.get_string_byte' do
126 it 'should get the byte at index' do
127 s = "\x41\x42"
James E. King III27247072018-03-22 20:50:23 -0400128 expect(Thrift::Bytes.get_string_byte(s, 0)).to eq(0x41)
129 expect(Thrift::Bytes.get_string_byte(s, 1)).to eq(0x42)
Jake Farrellb5a18a12012-10-09 01:10:43 +0000130 end
131 end
132
133 describe '.set_string_byte' do
134 it 'should set byte value at index' do
135 s = "\x41\x42"
136 Thrift::Bytes.set_string_byte(s, 0, 0x43)
James E. King III27247072018-03-22 20:50:23 -0400137 expect(s[0]).to eq(0x43)
138 expect(s).to eq('CB')
Jake Farrellb5a18a12012-10-09 01:10:43 +0000139 end
140 end
141
142 describe '.convert_to_utf8_byte_buffer' do
143 it 'should be a no-op' do
144 e = 'STRING'
145 a = Thrift::Bytes.convert_to_utf8_byte_buffer e
James E. King III27247072018-03-22 20:50:23 -0400146 expect(a).to eq(e)
147 expect(a).to be(e)
Jake Farrellb5a18a12012-10-09 01:10:43 +0000148 end
149 end
150
151 describe '.convert_to_string' do
152 it 'should be a no-op' do
153 e = 'STRING'
154 a = Thrift::Bytes.convert_to_string e
James E. King III27247072018-03-22 20:50:23 -0400155 expect(a).to eq(e)
156 expect(a).to be(e)
Jake Farrellb5a18a12012-10-09 01:10:43 +0000157 end
158 end
159 end
160end