Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jiikko committed Jan 6, 2024
1 parent 1aa6f2d commit 02c884e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/procon_bypass_man/device_connection/procon_color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class ProconBypassMan::DeviceConnection::ProconColor
white: ['ff ff ff', '00 00 00', 'ff ff ff', 'ff ff ff'],
}

BYTE_POSITION = 20...(20+(3*4))

attr_accessor :name

# @param [Symbol] color_name
Expand All @@ -20,12 +22,12 @@ def initialize(color_name)

# @return [String]
def to_bytes
COLOR_TABLE[self.name].join.gsub(/[,\s]/, '').pack('H*')
[COLOR_TABLE[self.name].join.gsub(/[,\s]/, '')].pack('H*')
end

# @return [Range]
def byte_position
20...(20+(3*4))
BYTE_POSITION
end

# @return [Boolean]
Expand Down
53 changes: 53 additions & 0 deletions spec/lib/procon_bypass_man/device_connection/procon_color_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require "spec_helper"

describe ProconBypassMan::DeviceConnection::ProconColor do
describe '#to_bytes' do
subject(:to_bytes) { described_class.new(color_name).to_bytes }

def decode_bytes(bytes)
bytes.unpack1('H*').scan(/\w{6}/).map { |x| x.scan(/\w{2}/).join(' ') }
end

context 'when color is red' do
let(:color_name) { :red }

it { expect(decode_bytes(subject)).to eq ['ff 00 00', 'ff ff ff', 'ff 00 00', 'ff 00 00'] }
end

context 'when color is white' do
let(:color_name) { :white }

it { expect(decode_bytes(subject)).to eq ['ff ff ff', '00 00 00', 'ff ff ff', 'ff ff ff'] }
end

context 'when color is white as String' do
let(:color_name) { 'white' }

it { expect(decode_bytes(subject)).to eq ['ff ff ff', '00 00 00', 'ff ff ff', 'ff ff ff'] }
end
end

describe '#valid?' do
subject(:valid?) { described_class.new(color_name).valid? }

context 'when color is red' do
let(:color_name) { :red }

it { expect(subject).to eq true }
end

context 'when color is not_found' do
let(:color_name) { :not_found }

it { expect(subject).to eq false }
end
end

describe '#byte_position' do
context 'when color is red' do
subject(:byte_position) { described_class.new(:red).byte_position }

it { expect(subject).to eq (20...(20+(3*4))) }
end
end
end

0 comments on commit 02c884e

Please sign in to comment.