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 |
| 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 | b.length.should == 0 |
| 29 | b.encoding.should == 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 | b.length.should == 2 |
| 35 | b.getbyte(0).should == 0 |
| 36 | b.getbyte(1).should == 0 |
| 37 | b.encoding.should == 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 | e.encoding.should_not == Encoding::BINARY |
| 45 | a = Thrift::Bytes.force_binary_encoding e |
| 46 | a.encoding.should == 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 | Thrift::Bytes.get_string_byte(s, 0).should == 0x41 |
| 54 | Thrift::Bytes.get_string_byte(s, 1).should == 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 | s.getbyte(0).should == 0x43 |
| 63 | s.should == '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 | e.length.should == 1 |
| 71 | |
| 72 | a = Thrift::Bytes.convert_to_utf8_byte_buffer e |
| 73 | a.encoding.should == Encoding::BINARY |
| 74 | a.length.should == 3 |
| 75 | a.unpack('C*').should == [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 | e.length.should == 1 |
| 82 | e.unpack('C*').should == [0xA4] # euro sign is a different code point in ISO-8859-15 |
| 83 | |
| 84 | a = Thrift::Bytes.convert_to_utf8_byte_buffer e |
| 85 | a.encoding.should == Encoding::BINARY |
| 86 | a.length.should == 3 |
| 87 | a.unpack('C*').should == [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 | e.encoding.should == Encoding::BINARY |
| 95 | a = Thrift::Bytes.convert_to_string e |
| 96 | a.encoding.should == Encoding::UTF_8 |
| 97 | a.should == "\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 | b.length.should == 0 |
| 106 | end |
| 107 | |
| 108 | it 'should create an empty buffer of given size' do |
| 109 | b = Thrift::Bytes.empty_byte_buffer 2 |
| 110 | b.length.should == 2 |
| 111 | b[0].should == 0 |
| 112 | b[1].should == 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 | a.should == e |
| 121 | a.should 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 | Thrift::Bytes.get_string_byte(s, 0).should == 0x41 |
| 129 | Thrift::Bytes.get_string_byte(s, 1).should == 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 | s[0].should == 0x43 |
| 138 | s.should == '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 | a.should == e |
| 147 | a.should 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 | a.should == e |
| 156 | a.should be(e) |
| 157 | end |
| 158 | end |
| 159 | end |
| 160 | end |