Skip to content

Commit

Permalink
Add tests to cover comparing against another Struct
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Jan 12, 2024
1 parent 775293e commit 79568d3
Showing 1 changed file with 71 additions and 33 deletions.
104 changes: 71 additions & 33 deletions spec/immutable_struct_ex_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@
expect { subject }.not_to raise_error
end
end

context 'when passing a block' do
subject do
described_class.new(**hash) do
def key?(key)
to_h.key? key
end

def key1?
key? :key1
end

def key2?
key? :key2
end

def key3?
key? :key3
end
end
end

it 'uses the block' do
expect(subject.key1?).to eq true
expect(subject.key2?).to eq true
expect(subject.key3?).to eq false
end
end
end

describe '#[]=' do
Expand All @@ -40,56 +68,66 @@
end

describe '#==' do
context 'when the hash equivalent is ==' do
it 'returns true' do
expect(subject == hash).to eq true
context 'when comparing against a Hash' do
context 'when the hash equivalent is ==' do
it 'returns true' do
expect(subject == hash).to eq true
end
end
end

context 'when the hash equivalent is not ==' do
context 'when the keys are different' do
let(:not_equal_hash) { { unequal: :value1, unequal: :value2 } }
context 'when the hash equivalent is not ==' do
context 'when the keys are different' do
let(:not_equal_hash) { { unequal: :value1, unequal: :value2 } }

it 'returns false' do
expect(subject == not_equal_hash).to eq false
it 'returns false' do
expect(subject == not_equal_hash).to eq false
end
end
end

context 'when the values are different' do
let(:not_equal_hash) { { key1: :unequal, key2: :unequal } }
context 'when the values are different' do
let(:not_equal_hash) { { key1: :unequal, key2: :unequal } }

it 'returns false' do
expect(subject == not_equal_hash).to eq false
it 'returns false' do
expect(subject == not_equal_hash).to eq false
end
end
end
end
end

context 'when passing a block' do
subject do
described_class.new(**hash) do
def key?(key)
to_h.key? key
end
context 'when comparing against a Struct' do
let(:struct) do
Struct.new(*hash.keys, keyword_init: true).new(**hash)
end

def key1?
key? :key1
context 'when the Struct equivalent is ==' do
it 'returns true' do
expect(subject == struct).to eq true
end
end

context 'when the Struct equivalent is not ==' do
context 'when the keys are different' do
let(:not_equal_struct) do
not_equal_hash = { unequal: :value1, unequal: :value2 }
Struct.new(*not_equal_hash.keys, keyword_init: true).new(**not_equal_hash)
end

def key2?
key? :key2
it 'returns false' do
expect(subject == not_equal_struct).to eq false
end
end

def key3?
key? :key3
context 'when the values are different' do
let(:not_equal_struct) do
not_equal_hash = { key1: :unequal, key2: :unequal }
Struct.new(*not_equal_hash.keys, keyword_init: true).new(**not_equal_hash)
end

it 'returns false' do
expect(subject == not_equal_struct).to eq false
end
end
end
end

it 'uses the block' do
expect(subject.key1?).to eq true
expect(subject.key2?).to eq true
expect(subject.key3?).to eq false
end
end
end

0 comments on commit 79568d3

Please sign in to comment.