| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 1 | # 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 | |
| 21 | require 'spec_helper' |
| 22 | |
| 23 | describe Thrift::Bytes do |
| Dmytro Shteflyuk | d24b8a0 | 2025-12-01 18:41:20 -0500 | [diff] [blame^] | 24 | 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 Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 29 | end |
| 30 | |
| Dmytro Shteflyuk | d24b8a0 | 2025-12-01 18:41:20 -0500 | [diff] [blame^] | 31 | 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 Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 75 | end |
| 76 | |
| Dmytro Shteflyuk | d24b8a0 | 2025-12-01 18:41:20 -0500 | [diff] [blame^] | 77 | 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 Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 87 | end |
| Dmytro Shteflyuk | d24b8a0 | 2025-12-01 18:41:20 -0500 | [diff] [blame^] | 88 | end |
| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 89 | |
| Dmytro Shteflyuk | d24b8a0 | 2025-12-01 18:41:20 -0500 | [diff] [blame^] | 90 | 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 Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 97 | end |
| 98 | end |
| 99 | end |