1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
require File.dirname(__FILE__) + '/../../spec_helper'
require 'complex'
require "rational"
describe "Complex#coerce" do
before(:each) do
@one = Complex.new!(1)
end
it "returns an array containing other and self as Complex when other is an Integer" do
result = @one.coerce(2)
result.should == [2, 1]
result.first.should be_kind_of(Complex)
result.last.should be_kind_of(Complex)
end
it "returns an array containing other and self as Complex when other is a Float" do
result = @one.coerce(20.5)
result.should == [20.5, 1]
result.first.should be_kind_of(Complex)
result.last.should be_kind_of(Complex)
end
it "returns an array containing other and self as Complex when other is a Bignum" do
result = @one.coerce(4294967296)
result.should == [4294967296, 1]
result.first.should be_kind_of(Complex)
result.last.should be_kind_of(Complex)
end
it "returns an array containing other and self as Complex when other is a Rational" do
result = @one.coerce(Rational(5,6))
result.should == [Rational(5,6), 1]
result.first.should be_kind_of(Complex)
result.last.should be_kind_of(Complex)
end
it "raises a TypeError when other is a String" do
lambda { @one.coerce("20") }.should raise_error(TypeError)
lambda { @one.coerce(nil) }.should raise_error(TypeError)
lambda { @one.coerce(false) }.should raise_error(TypeError)
end
end
|