Skip to content

Commit

Permalink
Add failing tests to demonstrate issues in <=>
Browse files Browse the repository at this point in the history
Plus begin directly testing <=> with a separate set of tests for additional peace of mind
  • Loading branch information
samuelgiles committed Apr 21, 2021
1 parent aaabd02 commit 439195e
Showing 1 changed file with 69 additions and 1 deletion.
70 changes: 69 additions & 1 deletion spec/lib/t/struct/acts_as_comparable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,74 @@
module T
class Struct
describe ActsAsComparable do
describe '#<=>' do
subject(:three_way_comparison) { struct_a <=> struct_b }

let(:struct_a) do
SorbetStructComparable::Examples::Interest.new(
topic: SorbetStructComparable::Examples::Interest::Topic::Walking,
rating: 10
)
end
let(:struct_b) do
SorbetStructComparable::Examples::Interest.new(
topic: SorbetStructComparable::Examples::Interest::Topic::Walking,
rating: 10
)
end

context 'when structs are equal' do
it { is_expected.to eq 0 }
end

context 'when first struct is lesser than the second' do
let(:struct_a) do
SorbetStructComparable::Examples::Interest.new(
topic: SorbetStructComparable::Examples::Interest::Topic::Walking,
rating: 9
)
end

it { is_expected.to eq -1 }
end

context 'when first struct is greater than the second' do
let(:struct_a) do
SorbetStructComparable::Examples::Interest.new(
topic: SorbetStructComparable::Examples::Interest::Topic::Walking,
rating: 11
)
end

it { is_expected.to eq 1 }
end

context 'when first struct is of a different type to the second' do
let(:struct_a) do
SorbetStructComparable::Examples::Incomparable.new(
my_incomparable_attribute: BigDecimal(22)
)
end

it { is_expected.to be_nil }
end

context 'when first struct has an incomparable attribute to the second' do
let(:struct_a) do
SorbetStructComparable::Examples::Incomparable.new(
my_incomparable_attribute: BigDecimal(22)
)
end
let(:struct_b) do
SorbetStructComparable::Examples::Incomparable.new(
my_incomparable_attribute: ['Hello']
)
end

it { is_expected.to be_nil }
end
end

describe '#==' do
subject(:comparison) { person_a }

Expand Down Expand Up @@ -66,7 +134,7 @@ class Struct
it { is_expected.to eq person_b }
end

context 'when presented with structs that are incomparable' do
context 'when presented with structs that have incomparable attributes' do
let(:person_a) do
SorbetStructComparable::Examples::Incomparable.new(
my_incomparable_attribute: BigDecimal(22)
Expand Down

0 comments on commit 439195e

Please sign in to comment.