blob: c3ec087dbba02226bc8761422536d7a44f7825e6 [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
Dmytro Shteflyukd24b8a02025-12-01 18:41:20 -050024 describe '.empty_byte_buffer' do
25 it 'should create an empty buffer' do
26 b = Thrift::Bytes.empty_byte_buffer
27 expect(b.length).to eq(0)
28 expect(b.encoding).to eq(Encoding::BINARY)
Jake Farrellb5a18a12012-10-09 01:10:43 +000029 end
30
Dmytro Shteflyukd24b8a02025-12-01 18:41:20 -050031 it 'should create an empty buffer of given size' do
32 b = Thrift::Bytes.empty_byte_buffer 2
33 expect(b.length).to eq(2)
34 expect(b.getbyte(0)).to eq(0)
35 expect(b.getbyte(1)).to eq(0)
36 expect(b.encoding).to eq(Encoding::BINARY)
37 end
38 end
39
40 describe '.force_binary_encoding' do
41 it 'should change encoding' do
42 e = 'STRING'.encode('UTF-8')
43 expect(e.encoding).not_to eq(Encoding::BINARY)
44 a = Thrift::Bytes.force_binary_encoding e
45 expect(a.encoding).to eq(Encoding::BINARY)
46 end
47 end
48
49 describe '.get_string_byte' do
50 it 'should get the byte at index' do
51 s = "\x41\x42"
52 expect(Thrift::Bytes.get_string_byte(s, 0)).to eq(0x41)
53 expect(Thrift::Bytes.get_string_byte(s, 1)).to eq(0x42)
54 end
55 end
56
57 describe '.set_string_byte' do
58 it 'should set byte value at index' do
59 s = "\x41\x42"
60 Thrift::Bytes.set_string_byte(s, 0, 0x43)
61 expect(s.getbyte(0)).to eq(0x43)
62 expect(s).to eq('CB')
63 end
64 end
65
66 describe '.convert_to_utf8_byte_buffer' do
67 it 'should convert UTF-8 String to byte buffer' do
68 e = "\u20AC".encode('UTF-8') # a string with euro sign character U+20AC
69 expect(e.length).to eq(1)
70
71 a = Thrift::Bytes.convert_to_utf8_byte_buffer e
72 expect(a.encoding).to eq(Encoding::BINARY)
73 expect(a.length).to eq(3)
74 expect(a.unpack('C*')).to eq([0xE2, 0x82, 0xAC])
Jake Farrellb5a18a12012-10-09 01:10:43 +000075 end
76
Dmytro Shteflyukd24b8a02025-12-01 18:41:20 -050077 it 'should convert ISO-8859-15 String to UTF-8 byte buffer' do
78 # Assumptions
79 e = "\u20AC".encode('ISO-8859-15') # a string with euro sign character U+20AC, then converted to ISO-8859-15
80 expect(e.length).to eq(1)
81 expect(e.unpack('C*')).to eq([0xA4]) # euro sign is a different code point in ISO-8859-15
82
83 a = Thrift::Bytes.convert_to_utf8_byte_buffer e
84 expect(a.encoding).to eq(Encoding::BINARY)
85 expect(a.length).to eq(3)
86 expect(a.unpack('C*')).to eq([0xE2, 0x82, 0xAC])
Jake Farrellb5a18a12012-10-09 01:10:43 +000087 end
Dmytro Shteflyukd24b8a02025-12-01 18:41:20 -050088 end
Jake Farrellb5a18a12012-10-09 01:10:43 +000089
Dmytro Shteflyukd24b8a02025-12-01 18:41:20 -050090 describe '.convert_to_string' do
91 it 'should convert UTF-8 byte buffer to a UTF-8 String' do
92 e = [0xE2, 0x82, 0xAC].pack("C*")
93 expect(e.encoding).to eq(Encoding::BINARY)
94 a = Thrift::Bytes.convert_to_string e
95 expect(a.encoding).to eq(Encoding::UTF_8)
96 expect(a).to eq("\u20AC")
Jake Farrellb5a18a12012-10-09 01:10:43 +000097 end
98 end
99end