Add unit tests for bytes.rb

This commit is contained in:
ChillerDragon 2022-11-13 11:42:46 +01:00
parent dd6e12e9c2
commit 36a5100230
2 changed files with 43 additions and 0 deletions

View file

@ -14,6 +14,7 @@ def bytes_to_str(data)
data.unpack('H*').join data.unpack('H*').join
end end
# TODO: remove?
def get_byte(data, start = 0, num = 1) def get_byte(data, start = 0, num = 1)
data[start...(start + num)].unpack('H*').join.upcase data[start...(start + num)].unpack('H*').join.upcase
end end

42
spec/07_bytes_spec.rb Normal file
View file

@ -0,0 +1,42 @@
# frozen_string_literal: true
require_relative '../lib/bytes'
describe 'Bytes', :bytes do
context '#str_hex' do
it 'Should format "\xFF" to "FF"' do
expect(str_hex("\xFF")).to eq('FF')
end
it 'Should format "\x01" to "01"' do
expect(str_hex("\x01")).to eq('01')
end
it 'Should format "\x01\x02" to "01 02"' do
expect(str_hex("\x01\x02")).to eq('01 02')
end
end
context '#str_bytes' do
it 'Should format "ff" to [255]' do
expect(str_bytes('ff')).to eq([255])
end
it 'Should format "FF" to [255]' do
expect(str_bytes('FF')).to eq([255])
end
it 'Should format "01" to [1]' do
expect(str_bytes('01')).to eq([1])
end
it 'Should format "0101" to [1, 1]' do
expect(str_bytes('0101')).to eq([1, 1])
end
end
context '#bytes_to_str' do
it 'Should format "\xff" to "ff"' do
expect(bytes_to_str("\xff")).to eq('ff')
end
it 'Should format "\x01" to "01"' do
expect(bytes_to_str("\x01")).to eq('01')
end
it 'Should format "\x01\x01" to "0101"' do
expect(bytes_to_str("\x01\x01")).to eq('0101')
end
end
end