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 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 28 expect(b.length).to eq(0) 29 expect(b.encoding).to eq(Encoding::BINARY) 30 end 31 32 it 'should create an empty buffer of given size' do 33 b = Thrift::Bytes.empty_byte_buffer 2 34 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) 38 end 39 end 40 41 describe '.force_binary_encoding' do 42 it 'should change encoding' do 43 e = 'STRING'.encode('UTF-8') 44 expect(e.encoding).not_to eq(Encoding::BINARY) 45 a = Thrift::Bytes.force_binary_encoding e 46 expect(a.encoding).to eq(Encoding::BINARY) 47 end 48 end 49 50 describe '.get_string_byte' do 51 it 'should get the byte at index' do 52 s = "\x41\x42" 53 expect(Thrift::Bytes.get_string_byte(s, 0)).to eq(0x41) 54 expect(Thrift::Bytes.get_string_byte(s, 1)).to eq(0x42) 55 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) 62 expect(s.getbyte(0)).to eq(0x43) 63 expect(s).to eq('CB') 64 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 70 expect(e.length).to eq(1) 71 72 a = Thrift::Bytes.convert_to_utf8_byte_buffer e 73 expect(a.encoding).to eq(Encoding::BINARY) 74 expect(a.length).to eq(3) 75 expect(a.unpack('C*')).to eq([0xE2, 0x82, 0xAC]) 76 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 81 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 83 84 a = Thrift::Bytes.convert_to_utf8_byte_buffer e 85 expect(a.encoding).to eq(Encoding::BINARY) 86 expect(a.length).to eq(3) 87 expect(a.unpack('C*')).to eq([0xE2, 0x82, 0xAC]) 88 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*") 94 expect(e.encoding).to eq(Encoding::BINARY) 95 a = Thrift::Bytes.convert_to_string e 96 expect(a.encoding).to eq(Encoding::UTF_8) 97 expect(a).to eq("\u20AC") 98 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 105 expect(b.length).to eq(0) 106 end 107 108 it 'should create an empty buffer of given size' do 109 b = Thrift::Bytes.empty_byte_buffer 2 110 expect(b.length).to eq(2) 111 expect(b[0]).to eq(0) 112 expect(b[1]).to eq(0) 113 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 120 expect(a).to eq(e) 121 expect(a).to be(e) 122 end 123 end 124 125 describe '.get_string_byte' do 126 it 'should get the byte at index' do 127 s = "\x41\x42" 128 expect(Thrift::Bytes.get_string_byte(s, 0)).to eq(0x41) 129 expect(Thrift::Bytes.get_string_byte(s, 1)).to eq(0x42) 130 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) 137 expect(s[0]).to eq(0x43) 138 expect(s).to eq('CB') 139 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 146 expect(a).to eq(e) 147 expect(a).to be(e) 148 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 155 expect(a).to eq(e) 156 expect(a).to be(e) 157 end 158 end 159 end 160end 161