|
1 | 1 | require File.dirname(__FILE__) + '/../spec_helper' |
2 | 2 |
|
3 | | -# Complex numbers are a bit strange |
4 | | -# Technically you can't really compare them using <=>, and ruby 1.9 does not implement this method for them |
5 | | -# so it stands to reason that complex units should also not support :> or :< |
6 | | - |
7 | 3 | describe Complex do |
8 | 4 | subject { Complex(1, 1) } |
9 | 5 | it { is_expected.to respond_to :to_unit } |
10 | 6 | end |
11 | 7 |
|
| 8 | +# Complex numbers are a bit strange. Technically you can't really compare them |
| 9 | +# using :<=>, and Ruby < 2.7 does not implement this method for them so it stands |
| 10 | +# to reason that complex units should also not support :> or :<. |
| 11 | +# |
| 12 | +# This inconsistency was corrected in Ruby 2.7. |
| 13 | +# @see https://rubyreferences.github.io/rubychanges/2.7.html#complex |
| 14 | +# @see https://bugs.ruby-lang.org/issues/15857 |
12 | 15 | describe 'Complex Unit' do |
13 | 16 | subject { Complex(1.0, -1.0).to_unit } |
14 | 17 |
|
|
19 | 22 | it { is_expected.to be === '1-1i'.to_unit } |
20 | 23 |
|
21 | 24 | it 'is not comparable' do |
22 | | - expect { subject > '1+1i'.to_unit }.to raise_error(NoMethodError) |
23 | | - expect { subject < '1+1i'.to_unit }.to raise_error(NoMethodError) |
| 25 | + if subject.scalar.respond_to?(:<=>) # this is true for Ruby >= 2.7 |
| 26 | + expect { subject > '1+1i'.to_unit }.to raise_error(ArgumentError) |
| 27 | + expect { subject < '1+1i'.to_unit }.to raise_error(ArgumentError) |
| 28 | + else |
| 29 | + expect { subject > '1+1i'.to_unit }.to raise_error(NoMethodError) |
| 30 | + expect { subject < '1+1i'.to_unit }.to raise_error(NoMethodError) |
| 31 | + end |
24 | 32 | end |
25 | 33 | end |
0 commit comments