Skip to content

Commit 476958e

Browse files
Earlopaineregon
authored andcommitted
Move Numeric to rely less on shared examples
1 parent 7caf62f commit 476958e

16 files changed

Lines changed: 149 additions & 173 deletions

core/numeric/abs_spec.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/abs'
2+
require_relative 'fixtures/classes'
33

44
describe "Numeric#abs" do
5-
it_behaves_like :numeric_abs, :abs
5+
before :each do
6+
@obj = NumericSpecs::Subclass.new
7+
end
8+
9+
it "returns self when self is greater than 0" do
10+
@obj.should_receive(:<).with(0).and_return(false)
11+
@obj.abs.should == @obj
12+
end
13+
14+
it "returns self\#@- when self is less than 0" do
15+
@obj.should_receive(:<).with(0).and_return(true)
16+
@obj.should_receive(:-@).and_return(:absolute_value)
17+
@obj.abs.should == :absolute_value
18+
end
619
end

core/numeric/angle_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/arg'
32

43
describe "Numeric#angle" do
5-
it_behaves_like :numeric_arg, :angle
4+
it "is an alias of Numeric#arg" do
5+
Numeric.instance_method(:angle).should == Numeric.instance_method(:arg)
6+
end
67
end

core/numeric/arg_spec.rb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/arg'
32

43
describe "Numeric#arg" do
5-
it_behaves_like :numeric_arg, :arg
4+
before :each do
5+
@numbers = [
6+
20,
7+
Rational(3, 4),
8+
bignum_value,
9+
infinity_value
10+
]
11+
end
12+
13+
it "returns 0 if positive" do
14+
@numbers.each do |number|
15+
number.arg.should == 0
16+
end
17+
end
18+
19+
it "returns Pi if negative" do
20+
@numbers.each do |number|
21+
(0-number).arg.should == Math::PI
22+
end
23+
end
24+
25+
describe "with a Numeric subclass" do
26+
it "returns 0 if self#<(0) returns false" do
27+
numeric = mock_numeric('positive')
28+
numeric.should_receive(:<).with(0).and_return(false)
29+
numeric.arg.should == 0
30+
end
31+
32+
it "returns Pi if self#<(0) returns true" do
33+
numeric = mock_numeric('positive')
34+
numeric.should_receive(:<).with(0).and_return(true)
35+
numeric.arg.should == Math::PI
36+
end
37+
end
638
end

core/numeric/conj_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/conj'
32

43
describe "Numeric#conj" do
5-
it_behaves_like :numeric_conj, :conj
4+
it "is an alias of Numeric#conjugate" do
5+
Numeric.instance_method(:conj).should == Numeric.instance_method(:conjugate)
6+
end
67
end

core/numeric/conjugate_spec.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/conj'
32

43
describe "Numeric#conjugate" do
5-
it_behaves_like :numeric_conj, :conjugate
4+
before :each do
5+
@numbers = [
6+
20, # Integer
7+
398.72, # Float
8+
Rational(3, 4), # Rational
9+
bignum_value,
10+
infinity_value,
11+
nan_value
12+
]
13+
end
14+
15+
it "returns self" do
16+
@numbers.each do |number|
17+
number.conjugate.should.equal?(number)
18+
end
19+
end
620
end

core/numeric/imag_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/imag'
32

43
describe "Numeric#imag" do
5-
it_behaves_like :numeric_imag, :imag
4+
it "is an alias of Numeric#imaginary" do
5+
Numeric.instance_method(:imag).should == Numeric.instance_method(:imaginary)
6+
end
67
end

core/numeric/imaginary_spec.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/imag'
32

43
describe "Numeric#imaginary" do
5-
it_behaves_like :numeric_imag, :imaginary
4+
before :each do
5+
@numbers = [
6+
20, # Integer
7+
398.72, # Float
8+
Rational(3, 4), # Rational
9+
bignum_value, # Bignum
10+
infinity_value,
11+
nan_value
12+
].map{|n| [n,-n]}.flatten
13+
end
14+
15+
it "returns 0" do
16+
@numbers.each do |number|
17+
number.imaginary.should == 0
18+
end
19+
end
20+
21+
it "raises an ArgumentError if given any arguments" do
22+
@numbers.each do |number|
23+
-> { number.imaginary(number) }.should.raise(ArgumentError)
24+
end
25+
end
626
end

core/numeric/magnitude_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative "../../spec_helper"
2-
require_relative 'shared/abs'
32

43
describe "Numeric#magnitude" do
5-
it_behaves_like :numeric_abs, :magnitude
4+
it "is an alias of Numeric#abs" do
5+
Numeric.instance_method(:magnitude).should == Numeric.instance_method(:abs)
6+
end
67
end

core/numeric/phase_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/arg'
32

43
describe "Numeric#phase" do
5-
it_behaves_like :numeric_arg, :phase
4+
it "is an alias of Numeric#arg" do
5+
Numeric.instance_method(:phase).should == Numeric.instance_method(:arg)
6+
end
67
end

core/numeric/rect_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../../spec_helper'
2-
require_relative 'shared/rect'
32

43
describe "Numeric#rect" do
5-
it_behaves_like :numeric_rect, :rect
4+
it "is an alias of Numeric#rectangular" do
5+
Numeric.instance_method(:rect).should == Numeric.instance_method(:rectangular)
6+
end
67
end

0 commit comments

Comments
 (0)