mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
75bfc6440d
commit
1d15d5f080
4370 changed files with 0 additions and 0 deletions
171
spec/ruby/library/English/English_spec.rb
Normal file
171
spec/ruby/library/English/English_spec.rb
Normal file
|
@ -0,0 +1,171 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
require 'English'
|
||||
|
||||
describe "English" do
|
||||
it "aliases $ERROR_INFO to $!" do
|
||||
begin
|
||||
raise "error"
|
||||
rescue
|
||||
$ERROR_INFO.should_not be_nil
|
||||
$ERROR_INFO.should == $!
|
||||
end
|
||||
$ERROR_INFO.should be_nil
|
||||
end
|
||||
|
||||
it "aliases $ERROR_POSITION to $@" do
|
||||
begin
|
||||
raise "error"
|
||||
rescue
|
||||
$ERROR_POSITION.should_not be_nil
|
||||
$ERROR_POSITION.should == $@
|
||||
end
|
||||
$ERROR_POSITION.should be_nil
|
||||
end
|
||||
|
||||
it "aliases $FS to $;" do
|
||||
original = $;
|
||||
$; = ","
|
||||
$FS.should_not be_nil
|
||||
$FS.should == $;
|
||||
$; = original
|
||||
end
|
||||
|
||||
it "aliases $FIELD_SEPARATOR to $;" do
|
||||
original = $;
|
||||
$; = ","
|
||||
$FIELD_SEPARATOR.should_not be_nil
|
||||
$FIELD_SEPARATOR.should == $;
|
||||
$; = original
|
||||
end
|
||||
|
||||
it "aliases $OFS to $," do
|
||||
original = $,
|
||||
$, = "|"
|
||||
$OFS.should_not be_nil
|
||||
$OFS.should == $,
|
||||
$, = original
|
||||
end
|
||||
|
||||
it "aliases $OUTPUT_FIELD_SEPARATOR to $," do
|
||||
original = $,
|
||||
$, = "|"
|
||||
$OUTPUT_FIELD_SEPARATOR.should_not be_nil
|
||||
$OUTPUT_FIELD_SEPARATOR.should == $,
|
||||
$, = original
|
||||
end
|
||||
|
||||
it "aliases $RS to $/" do
|
||||
$RS.should_not be_nil
|
||||
$RS.should == $/
|
||||
end
|
||||
|
||||
it "aliases $INPUT_RECORD_SEPARATOR to $/" do
|
||||
$INPUT_RECORD_SEPARATOR.should_not be_nil
|
||||
$INPUT_RECORD_SEPARATOR.should == $/
|
||||
end
|
||||
|
||||
it "aliases $ORS to $\\" do
|
||||
original = $\
|
||||
$\ = "\t"
|
||||
$ORS.should_not be_nil
|
||||
$ORS.should == $\
|
||||
$\ = original
|
||||
end
|
||||
|
||||
it "aliases $OUTPUT_RECORD_SEPARATOR to $\\" do
|
||||
original = $\
|
||||
$\ = "\t"
|
||||
$OUTPUT_RECORD_SEPARATOR.should_not be_nil
|
||||
$OUTPUT_RECORD_SEPARATOR.should == $\
|
||||
$\ = original
|
||||
end
|
||||
|
||||
it "aliases $INPUT_LINE_NUMBER to $." do
|
||||
$INPUT_LINE_NUMBER.should_not be_nil
|
||||
$INPUT_LINE_NUMBER.should == $.
|
||||
end
|
||||
|
||||
it "aliases $NR to $." do
|
||||
$NR.should_not be_nil
|
||||
$NR.should == $.
|
||||
end
|
||||
|
||||
it "aliases $LAST_READ_LINE to $_ needs to be reviewed for spec completeness"
|
||||
|
||||
it "aliases $DEFAULT_OUTPUT to $>" do
|
||||
$DEFAULT_OUTPUT.should_not be_nil
|
||||
$DEFAULT_OUTPUT.should == $>
|
||||
end
|
||||
|
||||
it "aliases $DEFAULT_INPUT to $<" do
|
||||
$DEFAULT_INPUT.should_not be_nil
|
||||
$DEFAULT_INPUT.should == $<
|
||||
end
|
||||
|
||||
it "aliases $PID to $$" do
|
||||
$PID.should_not be_nil
|
||||
$PID.should == $$
|
||||
end
|
||||
|
||||
it "aliases $PID to $$" do
|
||||
$PID.should_not be_nil
|
||||
$PID.should == $$
|
||||
end
|
||||
|
||||
it "aliases $PROCESS_ID to $$" do
|
||||
$PROCESS_ID.should_not be_nil
|
||||
$PROCESS_ID.should == $$
|
||||
end
|
||||
|
||||
it "aliases $CHILD_STATUS to $?" do
|
||||
ruby_exe('exit 0')
|
||||
$CHILD_STATUS.should_not be_nil
|
||||
$CHILD_STATUS.should == $?
|
||||
end
|
||||
|
||||
it "aliases $LAST_MATCH_INFO to $~" do
|
||||
/c(a)t/ =~ "cat"
|
||||
$LAST_MATCH_INFO.should_not be_nil
|
||||
$LAST_MATCH_INFO.should == $~
|
||||
end
|
||||
|
||||
it "aliases $IGNORECASE to $=" do
|
||||
$VERBOSE, verbose = nil, $VERBOSE
|
||||
begin
|
||||
$IGNORECASE.should_not be_nil
|
||||
$IGNORECASE.should == $=
|
||||
ensure
|
||||
$VERBOSE = verbose
|
||||
end
|
||||
end
|
||||
|
||||
it "aliases $ARGV to $*" do
|
||||
$ARGV.should_not be_nil
|
||||
$ARGV.should == $*
|
||||
end
|
||||
|
||||
it "aliases $MATCH to $&" do
|
||||
/c(a)t/ =~ "cat"
|
||||
$MATCH.should_not be_nil
|
||||
$MATCH.should == $&
|
||||
end
|
||||
|
||||
it "aliases $PREMATCH to $`" do
|
||||
/c(a)t/ =~ "cat"
|
||||
$PREMATCH.should_not be_nil
|
||||
$PREMATCH.should == $`
|
||||
end
|
||||
|
||||
it "aliases $POSTMATCH to $'" do
|
||||
/c(a)t/ =~ "cat"
|
||||
$POSTMATCH.should_not be_nil
|
||||
$POSTMATCH.should == $'
|
||||
end
|
||||
|
||||
it "aliases $LAST_PAREN_MATCH to $+" do
|
||||
/c(a)t/ =~ "cat"
|
||||
$LAST_PAREN_MATCH.should_not be_nil
|
||||
$LAST_PAREN_MATCH.should == $+
|
||||
end
|
||||
end
|
31
spec/ruby/library/abbrev/abbrev_spec.rb
Normal file
31
spec/ruby/library/abbrev/abbrev_spec.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'abbrev'
|
||||
|
||||
#test both Abbrev.abbrev and Array#abbrev in
|
||||
#the same manner, as they're more or less aliases
|
||||
#of one another
|
||||
|
||||
[["Abbrev.abbrev", lambda {|a| Abbrev.abbrev(a)}],
|
||||
["Array#abbrev", lambda {|a| a.abbrev}]
|
||||
].each do |(name, func)|
|
||||
|
||||
describe name do
|
||||
it "returns a hash of all unambiguous abbreviations of the array of strings passed in" do
|
||||
func.call(['ruby', 'rules']).should == {"rub" => "ruby",
|
||||
"ruby" => "ruby",
|
||||
"rul" => "rules",
|
||||
"rule" => "rules",
|
||||
"rules" => "rules"}
|
||||
|
||||
func.call(["car", "cone"]).should == {"ca" => "car",
|
||||
"car" => "car",
|
||||
"co" => "cone",
|
||||
"con" => "cone",
|
||||
"cone" => "cone"}
|
||||
end
|
||||
|
||||
it "returns an empty hash when called on an empty array" do
|
||||
func.call([]).should == {}
|
||||
end
|
||||
end
|
||||
end
|
9
spec/ruby/library/base64/decode64_spec.rb
Normal file
9
spec/ruby/library/base64/decode64_spec.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
require 'base64'
|
||||
|
||||
describe "Base64#decode64" do
|
||||
it "returns the Base64-decoded version of the given string" do
|
||||
Base64.decode64("U2VuZCByZWluZm9yY2VtZW50cw==\n").should == "Send reinforcements"
|
||||
end
|
||||
end
|
14
spec/ruby/library/base64/encode64_spec.rb
Normal file
14
spec/ruby/library/base64/encode64_spec.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
require 'base64'
|
||||
|
||||
describe "Base64#encode64" do
|
||||
it "returns the Base64-encoded version of the given string" do
|
||||
Base64.encode64("Now is the time for all good coders\nto learn Ruby").should ==
|
||||
"Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4g\nUnVieQ==\n"
|
||||
end
|
||||
|
||||
it "returns the Base64-encoded version of the given string" do
|
||||
Base64.encode64('Send reinforcements').should == "U2VuZCByZWluZm9yY2VtZW50cw==\n"
|
||||
end
|
||||
end
|
27
spec/ruby/library/base64/urlsafe_decode64_spec.rb
Normal file
27
spec/ruby/library/base64/urlsafe_decode64_spec.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
require 'base64'
|
||||
|
||||
describe "Base64#urlsafe_decode64" do
|
||||
it "uses '_' instead of '/'" do
|
||||
decoded = Base64.urlsafe_decode64("V2hlcmUgYW0gST8gV2hvIGFtIEk_IEFtIEk_IEk_")
|
||||
decoded.should == 'Where am I? Who am I? Am I? I?'
|
||||
end
|
||||
|
||||
it "uses '-' instead of '+'" do
|
||||
decoded = Base64.urlsafe_decode64('IkJlaW5nIGRpc2ludGVncmF0ZWQgbWFrZXMgbWUgdmUtcnkgYW4tZ3J5ISIgPGh1ZmYsIGh1ZmY-')
|
||||
decoded.should == '"Being disintegrated makes me ve-ry an-gry!" <huff, huff>'
|
||||
end
|
||||
|
||||
ruby_version_is ""..."2.3" do
|
||||
it "requires padding" do
|
||||
lambda { Base64.urlsafe_decode64("MQ") }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is "2.3" do
|
||||
it "does not require padding" do
|
||||
Base64.urlsafe_decode64("MQ").should == "1"
|
||||
end
|
||||
end
|
||||
end
|
22
spec/ruby/library/base64/urlsafe_encode64_spec.rb
Normal file
22
spec/ruby/library/base64/urlsafe_encode64_spec.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
require 'base64'
|
||||
|
||||
describe "Base64#urlsafe_encode64" do
|
||||
it "uses '_' instead of '/'" do
|
||||
encoded = Base64.urlsafe_encode64('Where am I? Who am I? Am I? I?')
|
||||
encoded.should == "V2hlcmUgYW0gST8gV2hvIGFtIEk_IEFtIEk_IEk_"
|
||||
end
|
||||
|
||||
it "uses '-' instead of '+'" do
|
||||
encoded = Base64.urlsafe_encode64('"Being disintegrated makes me ve-ry an-gry!" <huff, huff>')
|
||||
encoded.should == 'IkJlaW5nIGRpc2ludGVncmF0ZWQgbWFrZXMgbWUgdmUtcnkgYW4tZ3J5ISIgPGh1ZmYsIGh1ZmY-'
|
||||
end
|
||||
|
||||
ruby_version_is "2.3" do
|
||||
it "makes padding optional" do
|
||||
Base64.urlsafe_encode64("1", padding: false).should == "MQ"
|
||||
Base64.urlsafe_encode64("1").should == "MQ=="
|
||||
end
|
||||
end
|
||||
end
|
50
spec/ruby/library/bigdecimal/abs_spec.rb
Normal file
50
spec/ruby/library/bigdecimal/abs_spec.rb
Normal file
|
@ -0,0 +1,50 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#abs" do
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@two = BigDecimal("2")
|
||||
@three = BigDecimal("3")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
end
|
||||
|
||||
it "returns the absolute value" do
|
||||
pos_int = BigDecimal("2E5555")
|
||||
neg_int = BigDecimal("-2E5555")
|
||||
pos_frac = BigDecimal("2E-9999")
|
||||
neg_frac = BigDecimal("-2E-9999")
|
||||
|
||||
pos_int.abs.should == pos_int
|
||||
neg_int.abs.should == pos_int
|
||||
pos_frac.abs.should == pos_frac
|
||||
neg_frac.abs.should == pos_frac
|
||||
@one.abs.should == 1
|
||||
@two.abs.should == 2
|
||||
@three.abs.should == 3
|
||||
@mixed.abs.should == @mixed
|
||||
@one_minus.abs.should == @one
|
||||
end
|
||||
|
||||
it "properly handles special values" do
|
||||
@infinity.abs.should == @infinity
|
||||
@infinity_minus.abs.should == @infinity
|
||||
@nan.abs.nan?.should == true # have to do it this way, since == doesn't work on NaN
|
||||
@zero.abs.should == 0
|
||||
@zero.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@zero_pos.abs.should == 0
|
||||
@zero_pos.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@zero_neg.abs.should == 0
|
||||
@zero_neg.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
end
|
||||
|
||||
end
|
179
spec/ruby/library/bigdecimal/add_spec.rb
Normal file
179
spec/ruby/library/bigdecimal/add_spec.rb
Normal file
|
@ -0,0 +1,179 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#add" do
|
||||
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@two = BigDecimal("2")
|
||||
@three = BigDecimal("3")
|
||||
@ten = BigDecimal("10")
|
||||
@eleven = BigDecimal("11")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
@frac_3 = BigDecimal("12345E10")
|
||||
@frac_4 = BigDecimal("98765E10")
|
||||
@dot_ones = BigDecimal("0.1111111111")
|
||||
end
|
||||
|
||||
it "returns a + b with given precision" do
|
||||
# documentation states, that precision ist optional, but it ain't,
|
||||
@two.add(@one, 1).should == @three
|
||||
@one .add(@two, 1).should == @three
|
||||
@one.add(@one_minus, 1).should == @zero
|
||||
@ten.add(@one, 2).should == @eleven
|
||||
@zero.add(@one, 1).should == @one
|
||||
@frac_2.add(@frac_1, 10000).should == BigDecimal("1.9E-99999")
|
||||
@frac_1.add(@frac_1, 10000).should == BigDecimal("2E-99999")
|
||||
@frac_3.add(@frac_4, 0).should == BigDecimal("0.11111E16")
|
||||
@frac_3.add(@frac_4, 1).should == BigDecimal("0.1E16")
|
||||
@frac_3.add(@frac_4, 2).should == BigDecimal("0.11E16")
|
||||
@frac_3.add(@frac_4, 3).should == BigDecimal("0.111E16")
|
||||
@frac_3.add(@frac_4, 4).should == BigDecimal("0.1111E16")
|
||||
@frac_3.add(@frac_4, 5).should == BigDecimal("0.11111E16")
|
||||
@frac_3.add(@frac_4, 6).should == BigDecimal("0.11111E16")
|
||||
end
|
||||
|
||||
it "returns a + [Fixnum value] with given precision" do
|
||||
(1..10).each {|precision|
|
||||
@dot_ones.add(0, precision).should == BigDecimal("0." + "1" * precision)
|
||||
}
|
||||
BigDecimal("0.88").add(0, 1).should == BigDecimal("0.9")
|
||||
end
|
||||
|
||||
it "returns a + [Bignum value] with given precision" do
|
||||
bignum = 10000000000000000000
|
||||
(1..20).each {|precision|
|
||||
@dot_ones.add(bignum, precision).should == BigDecimal("0.1E20")
|
||||
}
|
||||
(21..30).each {|precision|
|
||||
@dot_ones.add(bignum, precision).should == BigDecimal(
|
||||
"0.10000000000000000000" + "1" * (precision - 20) + "E20")
|
||||
}
|
||||
end
|
||||
|
||||
# TODO:
|
||||
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/17374
|
||||
#
|
||||
# This doesn't work on MRI and looks like a bug to me:
|
||||
# one can use BigDecimal + Float, but not Bigdecimal.add(Float)
|
||||
#
|
||||
# it "returns a + [Float value] with given precision" do
|
||||
# (1..10).each {|precision|
|
||||
# @dot_ones.add(0.0, precision).should == BigDecimal("0." + "1" * precision)
|
||||
# }
|
||||
#
|
||||
# BigDecimal("0.88").add(0.0, 1).should == BigDecimal("0.9")
|
||||
# end
|
||||
|
||||
it "favors the precision specified in the second argument over the global limit" do
|
||||
BigDecimalSpecs.with_limit(1) do
|
||||
BigDecimal('0.888').add(@zero, 3).should == BigDecimal('0.888')
|
||||
end
|
||||
|
||||
BigDecimalSpecs.with_limit(2) do
|
||||
BigDecimal('0.888').add(@zero, 1).should == BigDecimal('0.9')
|
||||
end
|
||||
end
|
||||
|
||||
it "uses the current rounding mode if rounding is needed" do
|
||||
BigDecimalSpecs.with_rounding(BigDecimal::ROUND_UP) do
|
||||
BigDecimal('0.111').add(@zero, 1).should == BigDecimal('0.2')
|
||||
BigDecimal('-0.111').add(@zero, 1).should == BigDecimal('-0.2')
|
||||
end
|
||||
BigDecimalSpecs.with_rounding(BigDecimal::ROUND_DOWN) do
|
||||
BigDecimal('0.999').add(@zero, 1).should == BigDecimal('0.9')
|
||||
BigDecimal('-0.999').add(@zero, 1).should == BigDecimal('-0.9')
|
||||
end
|
||||
BigDecimalSpecs.with_rounding(BigDecimal::ROUND_HALF_UP) do
|
||||
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.9')
|
||||
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.9')
|
||||
end
|
||||
BigDecimalSpecs.with_rounding(BigDecimal::ROUND_HALF_DOWN) do
|
||||
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.8')
|
||||
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.8')
|
||||
end
|
||||
BigDecimalSpecs.with_rounding(BigDecimal::ROUND_HALF_EVEN) do
|
||||
BigDecimal('0.75').add(@zero, 1).should == BigDecimal('0.8')
|
||||
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.8')
|
||||
BigDecimal('-0.75').add(@zero, 1).should == BigDecimal('-0.8')
|
||||
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.8')
|
||||
end
|
||||
BigDecimalSpecs.with_rounding(BigDecimal::ROUND_CEILING) do
|
||||
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.9')
|
||||
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.8')
|
||||
end
|
||||
BigDecimalSpecs.with_rounding(BigDecimal::ROUND_FLOOR) do
|
||||
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.8')
|
||||
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.9')
|
||||
end
|
||||
end
|
||||
|
||||
it "uses the default ROUND_HALF_UP rounding if it wasn't explicitly changed" do
|
||||
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.9')
|
||||
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.9')
|
||||
end
|
||||
|
||||
it "returns NaN if NaN is involved" do
|
||||
@one.add(@nan, 10000).nan?.should == true
|
||||
@nan.add(@one, 1).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns Infinity or -Infinity if these are involved" do
|
||||
@zero.add(@infinity, 1).should == @infinity
|
||||
@frac_2.add(@infinity, 1).should == @infinity
|
||||
@one_minus.add(@infinity, 1).should == @infinity
|
||||
@two.add(@infinity, 1).should == @infinity
|
||||
|
||||
@zero.add(@infinity_minus, 1).should == @infinity_minus
|
||||
@frac_2.add(@infinity_minus, 1).should == @infinity_minus
|
||||
@one_minus.add(@infinity_minus, 1).should == @infinity_minus
|
||||
@two.add(@infinity_minus, 1).should == @infinity_minus
|
||||
|
||||
@infinity.add(@zero, 1).should == @infinity
|
||||
@infinity.add(@frac_2, 1).should == @infinity
|
||||
@infinity.add(@one_minus, 1).should == @infinity
|
||||
@infinity.add(@two, 1).should == @infinity
|
||||
|
||||
@infinity_minus.add(@zero, 1).should == @infinity_minus
|
||||
@infinity_minus.add(@frac_2, 1).should == @infinity_minus
|
||||
@infinity_minus.add(@one_minus, 1).should == @infinity_minus
|
||||
@infinity_minus.add(@two, 1).should == @infinity_minus
|
||||
|
||||
@infinity.add(@infinity, 10000).should == @infinity
|
||||
@infinity_minus.add(@infinity_minus, 10000).should == @infinity_minus
|
||||
end
|
||||
|
||||
it "returns NaN if Infinity + (- Infinity)" do
|
||||
@infinity.add(@infinity_minus, 10000).nan?.should == true
|
||||
@infinity_minus.add(@infinity, 10000).nan?.should == true
|
||||
end
|
||||
|
||||
it "raises TypeError when adds nil" do
|
||||
lambda {
|
||||
@one.add(nil, 10)
|
||||
}.should raise_error(TypeError)
|
||||
lambda {
|
||||
@one.add(nil, 0)
|
||||
}.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises TypeError when precision parameter is nil" do
|
||||
lambda {
|
||||
@one.add(@one, nil)
|
||||
}.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises ArgumentError when precision parameter is negative" do
|
||||
lambda {
|
||||
@one.add(@one, -10)
|
||||
}.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
7
spec/ruby/library/bigdecimal/case_compare_spec.rb
Normal file
7
spec/ruby/library/bigdecimal/case_compare_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/eql.rb', __FILE__)
|
||||
|
||||
|
||||
describe "BigDecimal#===" do
|
||||
it_behaves_like(:bigdecimal_eql, :===)
|
||||
end
|
104
spec/ruby/library/bigdecimal/ceil_spec.rb
Normal file
104
spec/ruby/library/bigdecimal/ceil_spec.rb
Normal file
|
@ -0,0 +1,104 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#ceil" do
|
||||
before :each do
|
||||
@zero = BigDecimal("0")
|
||||
@one = BigDecimal("1")
|
||||
@three = BigDecimal("3")
|
||||
@four = BigDecimal("4")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@mixed_big = BigDecimal("1.23456789E100")
|
||||
@pos_int = BigDecimal("2E5555")
|
||||
@neg_int = BigDecimal("-2E5555")
|
||||
@pos_frac = BigDecimal("2E-9999")
|
||||
@neg_frac = BigDecimal("-2E-9999")
|
||||
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_neg = BigDecimal("-Infinity")
|
||||
@nan = BigDecimal("NaN")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
end
|
||||
|
||||
it "returns an Integer, if n is unspecified" do
|
||||
@mixed.ceil.kind_of?(Integer).should == true
|
||||
end
|
||||
|
||||
it "returns a BigDecimal, if n is specified" do
|
||||
@pos_int.ceil(2).kind_of?(BigDecimal).should == true
|
||||
end
|
||||
|
||||
it "returns the smallest integer greater or equal to self, if n is unspecified" do
|
||||
@pos_int.ceil.should == @pos_int
|
||||
@neg_int.ceil.should == @neg_int
|
||||
@pos_frac.ceil.should == BigDecimal("1")
|
||||
@neg_frac.ceil.should == @zero
|
||||
@zero.ceil.should == 0
|
||||
@zero_pos.ceil.should == @zero_pos
|
||||
@zero_neg.ceil.should == @zero_neg
|
||||
|
||||
|
||||
BigDecimal('2.3').ceil.should == 3
|
||||
BigDecimal('2.5').ceil.should == 3
|
||||
BigDecimal('2.9999').ceil.should == 3
|
||||
BigDecimal('-2.3').ceil.should == -2
|
||||
BigDecimal('-2.5').ceil.should == -2
|
||||
BigDecimal('-2.9999').ceil.should == -2
|
||||
end
|
||||
|
||||
it "raise exception, if self is special value" do
|
||||
lambda { @infinity.ceil }.should raise_error(FloatDomainError)
|
||||
lambda { @infinity_neg.ceil }.should raise_error(FloatDomainError)
|
||||
lambda { @nan.ceil }.should raise_error(FloatDomainError)
|
||||
end
|
||||
|
||||
it "returns n digits right of the decimal point if given n > 0" do
|
||||
@mixed.ceil(1).should == BigDecimal("1.3")
|
||||
@mixed.ceil(5).should == BigDecimal("1.23457")
|
||||
|
||||
BigDecimal("-0.03").ceil(1).should == BigDecimal("0")
|
||||
BigDecimal("0.03").ceil(1).should == BigDecimal("0.1")
|
||||
|
||||
BigDecimal("23.45").ceil(0).should == BigDecimal('24')
|
||||
BigDecimal("23.45").ceil(1).should == BigDecimal('23.5')
|
||||
BigDecimal("23.45").ceil(2).should == BigDecimal('23.45')
|
||||
|
||||
BigDecimal("-23.45").ceil(0).should == BigDecimal('-23')
|
||||
BigDecimal("-23.45").ceil(1).should == BigDecimal('-23.4')
|
||||
BigDecimal("-23.45").ceil(2).should == BigDecimal('-23.45')
|
||||
|
||||
BigDecimal("2E-10").ceil(0).should == @one
|
||||
BigDecimal("2E-10").ceil(9).should == BigDecimal('1E-9')
|
||||
BigDecimal("2E-10").ceil(10).should == BigDecimal('2E-10')
|
||||
BigDecimal("2E-10").ceil(11).should == BigDecimal('2E-10')
|
||||
|
||||
(1..10).each do |n|
|
||||
# 0.4, 0.34, 0.334, etc.
|
||||
(@one.div(@three,20)).ceil(n).should == BigDecimal("0.#{'3'*(n-1)}4")
|
||||
# 1.4, 1.34, 1.334, etc.
|
||||
(@four.div(@three,20)).ceil(n).should == BigDecimal("1.#{'3'*(n-1)}4")
|
||||
(BigDecimal('31').div(@three,20)).ceil(n).should == BigDecimal("10.#{'3'*(n-1)}4")
|
||||
end
|
||||
(1..10).each do |n|
|
||||
# -0.4, -0.34, -0.334, etc.
|
||||
(-@one.div(@three,20)).ceil(n).should == BigDecimal("-0.#{'3'* n}")
|
||||
end
|
||||
(1..10).each do |n|
|
||||
(@three.div(@one,20)).ceil(n).should == @three
|
||||
end
|
||||
(1..10).each do |n|
|
||||
(-@three.div(@one,20)).ceil(n).should == -@three
|
||||
end
|
||||
end
|
||||
|
||||
it "sets n digits left of the decimal point to 0, if given n < 0" do
|
||||
BigDecimal("13345.234").ceil(-2).should == BigDecimal("13400.0")
|
||||
@mixed_big.ceil(-99).should == BigDecimal("0.13E101")
|
||||
@mixed_big.ceil(-100).should == BigDecimal("0.2E101")
|
||||
@mixed_big.ceil(-95).should == BigDecimal("0.123457E101")
|
||||
BigDecimal("1E10").ceil(-30).should == BigDecimal('1E30')
|
||||
BigDecimal("-1E10").ceil(-30).should == @zero
|
||||
end
|
||||
|
||||
end
|
26
spec/ruby/library/bigdecimal/coerce_spec.rb
Normal file
26
spec/ruby/library/bigdecimal/coerce_spec.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#coerce" do
|
||||
|
||||
it "returns [other, self] both as BigDecimal" do
|
||||
one = BigDecimal("1.0")
|
||||
five_point_28 = BigDecimal("5.28")
|
||||
zero_minus = BigDecimal("-0.0")
|
||||
some_value = 32434234234234234234
|
||||
|
||||
BigDecimal("1.2").coerce(1).should == [one, BigDecimal("1.2")]
|
||||
five_point_28.coerce(1.0).should == [one, BigDecimal("5.28")]
|
||||
one.coerce(one).should == [one, one]
|
||||
one.coerce(2.5).should == [2.5, one]
|
||||
BigDecimal("1").coerce(3.14).should == [3.14, one]
|
||||
a, b = zero_minus.coerce(some_value)
|
||||
a.should == BigDecimal(some_value.to_s)
|
||||
b.should == zero_minus
|
||||
a, b = one.coerce(some_value)
|
||||
a.should == BigDecimal(some_value.to_s)
|
||||
b.to_f.should be_close(1.0, TOLERANCE) # can we take out the to_f once BigDecimal#- is implemented?
|
||||
b.should == one
|
||||
end
|
||||
|
||||
end
|
81
spec/ruby/library/bigdecimal/comparison_spec.rb
Normal file
81
spec/ruby/library/bigdecimal/comparison_spec.rb
Normal file
|
@ -0,0 +1,81 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#<=>" do
|
||||
before :each do
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@mixed_big = BigDecimal("1.23456789E100")
|
||||
@pos_int = BigDecimal("2E5555")
|
||||
@neg_int = BigDecimal("-2E5555")
|
||||
@pos_frac = BigDecimal("2E-9999")
|
||||
@neg_frac = BigDecimal("-2E-9999")
|
||||
|
||||
@int_mock = mock('123')
|
||||
class << @int_mock
|
||||
def coerce(other)
|
||||
return [other, BigDecimal('123')]
|
||||
end
|
||||
def >= (other)
|
||||
BigDecimal('123') >= other
|
||||
end
|
||||
end
|
||||
|
||||
@values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
|
||||
-2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
|
||||
@zero , 1, 2, 10, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
|
||||
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_neg = BigDecimal("-Infinity")
|
||||
@nan = BigDecimal("NaN")
|
||||
end
|
||||
|
||||
|
||||
it "returns 0 if a == b" do
|
||||
(@pos_int <=> @pos_int).should == 0
|
||||
(@neg_int <=> @neg_int).should == 0
|
||||
(@pos_frac <=> @pos_frac).should == 0
|
||||
(@neg_frac <=> @neg_frac).should == 0
|
||||
(@zero <=> @zero).should == 0
|
||||
(@infinity <=> @infinity).should == 0
|
||||
(@infinity_neg <=> @infinity_neg).should == 0
|
||||
end
|
||||
|
||||
it "returns 1 if a > b" do
|
||||
(@pos_int <=> @neg_int).should == 1
|
||||
(@pos_frac <=> @neg_frac).should == 1
|
||||
(@pos_frac <=> @zero).should == 1
|
||||
@values.each { |val|
|
||||
(@infinity <=> val).should == 1
|
||||
}
|
||||
end
|
||||
|
||||
it "returns -1 if a < b" do
|
||||
(@zero <=> @pos_frac).should == -1
|
||||
(@neg_int <=> @pos_frac).should == -1
|
||||
(@pos_frac <=> @pos_int).should == -1
|
||||
@values.each { |val|
|
||||
(@infinity_neg <=> val).should == -1
|
||||
}
|
||||
end
|
||||
|
||||
it "returns nil if NaN is involved" do
|
||||
@values += [@infinity, @infinity_neg, @nan]
|
||||
@values << nil
|
||||
@values << Object.new
|
||||
@values.each { |val|
|
||||
(@nan <=> val).should == nil
|
||||
}
|
||||
end
|
||||
|
||||
it "returns nil if the argument is nil" do
|
||||
(@zero <=> nil).should == nil
|
||||
(@infinity <=> nil).should == nil
|
||||
(@infinity_neg <=> nil).should == nil
|
||||
(@mixed <=> nil).should == nil
|
||||
(@pos_int <=> nil).should == nil
|
||||
(@neg_frac <=> nil).should == nil
|
||||
end
|
||||
end
|
102
spec/ruby/library/bigdecimal/div_spec.rb
Normal file
102
spec/ruby/library/bigdecimal/div_spec.rb
Normal file
|
@ -0,0 +1,102 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/quo', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#div with precision set to 0" do
|
||||
# TODO: figure out if there is a better way to do these
|
||||
# shared specs rather than sending [0]. See other specs
|
||||
# that share :bigdecimal_quo.
|
||||
it_behaves_like :bigdecimal_quo, :div, [0]
|
||||
end
|
||||
|
||||
describe "BigDecimal#div" do
|
||||
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_plus = BigDecimal("+0")
|
||||
@zero_minus = BigDecimal("-0")
|
||||
@two = BigDecimal("2")
|
||||
@three = BigDecimal("3")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
end
|
||||
|
||||
it "returns a / b with optional precision" do
|
||||
@two.div(@one).should == @two
|
||||
@one.div(@two).should == @zero
|
||||
# ^^ is this really intended for a class with arbitrary precision?
|
||||
@one.div(@two, 1).should == BigDecimal("0.5")
|
||||
@one.div(@one_minus).should == @one_minus
|
||||
@one_minus.div(@one_minus).should == @one
|
||||
@frac_2.div(@frac_1, 1).should == BigDecimal("0.9")
|
||||
@frac_1.div(@frac_1).should == @one
|
||||
|
||||
res = "0." + "3" * 1000
|
||||
(1..100).each { |idx|
|
||||
@one.div(@three, idx).to_s("F").should == "0." + res[2, idx]
|
||||
}
|
||||
end
|
||||
|
||||
it "raises FloatDomainError if NaN is involved" do
|
||||
lambda { @one.div(@nan) }.should raise_error(FloatDomainError)
|
||||
lambda { @nan.div(@one) }.should raise_error(FloatDomainError)
|
||||
lambda { @nan.div(@nan) }.should raise_error(FloatDomainError)
|
||||
end
|
||||
|
||||
it "returns 0 if divided by Infinity and no precision given" do
|
||||
@zero.div(@infinity).should == 0
|
||||
@frac_2.div(@infinity).should == 0
|
||||
end
|
||||
|
||||
it "returns 0 if divided by Infinity with given precision" do
|
||||
@zero.div(@infinity, 0).should == 0
|
||||
@frac_2.div(@infinity, 1).should == 0
|
||||
@zero.div(@infinity, 100000).should == 0
|
||||
@frac_2.div(@infinity, 100000).should == 0
|
||||
end
|
||||
|
||||
it "raises ZeroDivisionError if divided by zero and no precision given" do
|
||||
lambda { @one.div(@zero) }.should raise_error(ZeroDivisionError)
|
||||
lambda { @one.div(@zero_plus) }.should raise_error(ZeroDivisionError)
|
||||
lambda { @one.div(@zero_minus) }.should raise_error(ZeroDivisionError)
|
||||
|
||||
lambda { @zero.div(@zero) }.should raise_error(ZeroDivisionError)
|
||||
lambda { @zero_minus.div(@zero_plus) }.should raise_error(ZeroDivisionError)
|
||||
lambda { @zero_minus.div(@zero_minus) }.should raise_error(ZeroDivisionError)
|
||||
lambda { @zero_plus.div(@zero_minus) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
|
||||
it "returns NaN if zero is divided by zero" do
|
||||
@zero.div(@zero, 0).nan?.should == true
|
||||
@zero_minus.div(@zero_plus, 0).nan?.should == true
|
||||
@zero_plus.div(@zero_minus, 0).nan?.should == true
|
||||
|
||||
@zero.div(@zero, 10).nan?.should == true
|
||||
@zero_minus.div(@zero_plus, 10).nan?.should == true
|
||||
@zero_plus.div(@zero_minus, 10).nan?.should == true
|
||||
end
|
||||
|
||||
it "raises FloatDomainError if (+|-) Infinity divided by 1 and no precision given" do
|
||||
lambda { @infinity_minus.div(@one) }.should raise_error(FloatDomainError)
|
||||
lambda { @infinity.div(@one) }.should raise_error(FloatDomainError)
|
||||
lambda { @infinity_minus.div(@one_minus) }.should raise_error(FloatDomainError)
|
||||
end
|
||||
|
||||
it "returns (+|-)Infinity if (+|-)Infinity by 1 and precision given" do
|
||||
@infinity_minus.div(@one, 0).should == @infinity_minus
|
||||
@infinity.div(@one, 0).should == @infinity
|
||||
@infinity_minus.div(@one_minus, 0).should == @infinity
|
||||
end
|
||||
|
||||
it "returns NaN if Infinity / ((+|-) Infinity)" do
|
||||
@infinity.div(@infinity_minus, 100000).nan?.should == true
|
||||
@infinity_minus.div(@infinity, 1).nan?.should == true
|
||||
end
|
||||
|
||||
|
||||
end
|
7
spec/ruby/library/bigdecimal/divide_spec.rb
Normal file
7
spec/ruby/library/bigdecimal/divide_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/quo', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#/" do
|
||||
it_behaves_like :bigdecimal_quo, :/, []
|
||||
end
|
180
spec/ruby/library/bigdecimal/divmod_spec.rb
Normal file
180
spec/ruby/library/bigdecimal/divmod_spec.rb
Normal file
|
@ -0,0 +1,180 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/modulo', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
module DivmodSpecs
|
||||
def self.check_both_nan(array)
|
||||
array.length.should == 2
|
||||
array[0].nan?.should == true
|
||||
array[1].nan?.should == true
|
||||
end
|
||||
def self.check_both_bigdecimal(array)
|
||||
array.length.should == 2
|
||||
array[0].kind_of?(BigDecimal).should == true
|
||||
array[1].kind_of?(BigDecimal).should == true
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: figure out a way to do the shared specs with helpers instead
|
||||
# of spec'ing a method that does not really exist
|
||||
describe "BigDecimal#mod_part_of_divmod" do
|
||||
# BigDecimal#divmod[1] behaves exactly like #modulo
|
||||
before :all do
|
||||
class BigDecimal
|
||||
def mod_part_of_divmod(arg)
|
||||
divmod(arg)[1]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
after :all do
|
||||
class BigDecimal
|
||||
undef mod_part_of_divmod
|
||||
end
|
||||
end
|
||||
|
||||
it_behaves_like :bigdecimal_modulo, :mod_part_of_divmod
|
||||
|
||||
it "raises ZeroDivisionError if other is zero" do
|
||||
bd5667 = BigDecimal.new("5667.19")
|
||||
|
||||
lambda { bd5667.send(@method, 0) }.should raise_error(ZeroDivisionError)
|
||||
lambda { bd5667.send(@method, BigDecimal("0")) }.should raise_error(ZeroDivisionError)
|
||||
lambda { @zero.send(@method, @zero) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
||||
|
||||
describe "BigDecimal#divmod" do
|
||||
|
||||
before :each do
|
||||
@a = BigDecimal("42.00000000000000000001")
|
||||
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
|
||||
@one = BigDecimal("1")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@pos_int = BigDecimal("2E5555")
|
||||
@neg_int = BigDecimal("-2E5555")
|
||||
@pos_frac = BigDecimal("2E-9999")
|
||||
@neg_frac = BigDecimal("-2E-9999")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
|
||||
@special_vals = [@infinity, @infinity_minus, @nan]
|
||||
@regular_vals = [
|
||||
@one, @mixed, @pos_int, @neg_int, @pos_frac,
|
||||
@neg_frac, @one_minus, @frac_1, @frac_2]
|
||||
@zeroes = [@zero, @zero_pos, @zero_neg]
|
||||
end
|
||||
|
||||
it "divides value, returns an array" do
|
||||
res = @a.divmod(5)
|
||||
res.kind_of?(Array).should == true
|
||||
end
|
||||
|
||||
it "array contains quotient and modulus as BigDecimal" do
|
||||
res = @a.divmod(5)
|
||||
DivmodSpecs.check_both_bigdecimal(res)
|
||||
res[0].should == BigDecimal('0.8E1')
|
||||
res[1].should == BigDecimal('2.00000000000000000001')
|
||||
|
||||
BigDecimal('1').divmod(BigDecimal('2')).should == [0, 1]
|
||||
BigDecimal('2').divmod(BigDecimal('1')).should == [2, 0]
|
||||
|
||||
BigDecimal('1').divmod(BigDecimal('-2')).should == [-1, -1]
|
||||
BigDecimal('2').divmod(BigDecimal('-1')).should == [-2, 0]
|
||||
|
||||
BigDecimal('-1').divmod(BigDecimal('2')).should == [-1, 1]
|
||||
BigDecimal('-2').divmod(BigDecimal('1')).should == [-2, 0]
|
||||
end
|
||||
|
||||
it "can be reversed with * and +" do
|
||||
# Example taken from BigDecimal documentation
|
||||
a = BigDecimal.new("42")
|
||||
b = BigDecimal.new("9")
|
||||
q, m = a.divmod(b)
|
||||
c = q * b + m
|
||||
a.should == c
|
||||
|
||||
values = [@one, @one_minus, BigDecimal('2'), BigDecimal('-2'),
|
||||
BigDecimal('5'), BigDecimal('-5'), BigDecimal('10'), BigDecimal('-10'),
|
||||
BigDecimal('20'), BigDecimal('-20'), BigDecimal('100'), BigDecimal('-100'),
|
||||
BigDecimal('1.23456789E10'), BigDecimal('-1.23456789E10')
|
||||
]
|
||||
|
||||
# TODO: file MRI bug:
|
||||
# BigDecimal('1').divmod(BigDecimal('3E-9'))[0] #=> 0.3E9,
|
||||
# but really should be 0.333333333E9
|
||||
values << BigDecimal('1E-10')
|
||||
values << BigDecimal('-1E-10')
|
||||
values << BigDecimal('2E55')
|
||||
values << BigDecimal('-2E55')
|
||||
values << BigDecimal('2E-5555')
|
||||
values << BigDecimal('-2E-5555')
|
||||
|
||||
|
||||
values_and_zeroes = values + @zeroes
|
||||
values_and_zeroes.each do |val1|
|
||||
values.each do |val2|
|
||||
res = val1.divmod(val2)
|
||||
DivmodSpecs.check_both_bigdecimal(res)
|
||||
res[0].should == ((val1/val2).floor)
|
||||
res[1].should == (val1 - res[0] * val2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "returns an array of two NaNs if NaN is involved" do
|
||||
(@special_vals + @regular_vals + @zeroes).each do |val|
|
||||
DivmodSpecs.check_both_nan(val.divmod(@nan))
|
||||
DivmodSpecs.check_both_nan(@nan.divmod(val))
|
||||
end
|
||||
end
|
||||
|
||||
it "raises ZeroDivisionError if the divisor is zero" do
|
||||
(@special_vals + @regular_vals + @zeroes - [@nan]).each do |val|
|
||||
@zeroes.each do |zero|
|
||||
lambda { val.divmod(zero) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "returns an array of Infinity and NaN if the dividend is Infinity" do
|
||||
@regular_vals.each do |val|
|
||||
array = @infinity.divmod(val)
|
||||
array.length.should == 2
|
||||
array[0].infinite?.should == (val > 0 ? 1 : -1)
|
||||
array[1].nan?.should == true
|
||||
end
|
||||
end
|
||||
|
||||
it "returns an array of zero and the dividend if the divisor is Infinity" do
|
||||
@regular_vals.each do |val|
|
||||
array = val.divmod(@infinity)
|
||||
array.length.should == 2
|
||||
array[0].should == @zero
|
||||
array[1].should == val
|
||||
end
|
||||
end
|
||||
|
||||
it "returns an array of two zero if the diviend is zero" do
|
||||
@zeroes.each do |zero|
|
||||
@regular_vals.each do |val|
|
||||
zero.divmod(val).should == [@zero, @zero]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "raises TypeError if the argument cannot be coerced to BigDecimal" do
|
||||
lambda {
|
||||
@one.divmod('1')
|
||||
}.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
end
|
9
spec/ruby/library/bigdecimal/double_fig_spec.rb
Normal file
9
spec/ruby/library/bigdecimal/double_fig_spec.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal.double_fig" do
|
||||
# The result depends on the CPU and OS
|
||||
it "returns the number of digits a Float number is allowed to have" do
|
||||
BigDecimal.double_fig.should_not == nil
|
||||
end
|
||||
end
|
6
spec/ruby/library/bigdecimal/eql_spec.rb
Normal file
6
spec/ruby/library/bigdecimal/eql_spec.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/eql.rb', __FILE__)
|
||||
|
||||
describe "BigDecimal#eql?" do
|
||||
it_behaves_like(:bigdecimal_eql, :eql?)
|
||||
end
|
7
spec/ruby/library/bigdecimal/equal_value_spec.rb
Normal file
7
spec/ruby/library/bigdecimal/equal_value_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/eql.rb', __FILE__)
|
||||
|
||||
|
||||
describe "BigDecimal#==" do
|
||||
it_behaves_like(:bigdecimal_eql, :==)
|
||||
end
|
38
spec/ruby/library/bigdecimal/exponent_spec.rb
Normal file
38
spec/ruby/library/bigdecimal/exponent_spec.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/power', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#**" do
|
||||
it_behaves_like(:bigdecimal_power, :**)
|
||||
end
|
||||
|
||||
describe "BigDecimal#exponent" do
|
||||
|
||||
it "returns an Integer" do
|
||||
BigDecimal("2E100000000").exponent.kind_of?(Integer).should == true
|
||||
BigDecimal("2E-999").exponent.kind_of?(Integer).should == true
|
||||
end
|
||||
|
||||
it "is n if number can be represented as 0.xxx*10**n" do
|
||||
BigDecimal("2E1000").exponent.should == 1001
|
||||
BigDecimal("1234567E10").exponent.should == 17
|
||||
end
|
||||
|
||||
# commenting this spec out after discussion with Defiler, since it seems to be an MRI bug, not a real feature
|
||||
=begin
|
||||
platform_is wordsize: 32 do
|
||||
# TODO: write specs for both 32 and 64 bit
|
||||
it "returns 0 if exponent can't be represented as Fixnum" do
|
||||
BigDecimal("2E1000000000000000").exponent.should == 0
|
||||
BigDecimal("-5E-999999999999999").exponent.should == 0
|
||||
end
|
||||
end
|
||||
=end
|
||||
|
||||
it "returns 0 if self is 0" do
|
||||
BigDecimal("0").exponent.should == 0
|
||||
BigDecimal("+0").exponent.should == 0
|
||||
BigDecimal("-0").exponent.should == 0
|
||||
end
|
||||
|
||||
end
|
35
spec/ruby/library/bigdecimal/finite_spec.rb
Normal file
35
spec/ruby/library/bigdecimal/finite_spec.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#finite?" do
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@two = BigDecimal("2")
|
||||
@three = BigDecimal("3")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
@big = BigDecimal("2E40001")
|
||||
@finite_vals = [@one, @zero, @zero_pos, @zero_neg, @two,
|
||||
@three, @frac_1, @frac_2, @big, @one_minus]
|
||||
end
|
||||
|
||||
it "is false if Infinity or NaN" do
|
||||
@infinity.finite?.should == false
|
||||
@infinity_minus.finite?.should == false
|
||||
@nan.finite?.should == false
|
||||
end
|
||||
|
||||
it "returns true for finite values" do
|
||||
@finite_vals.each do |val|
|
||||
val.finite?.should == true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
57
spec/ruby/library/bigdecimal/fix_spec.rb
Normal file
57
spec/ruby/library/bigdecimal/fix_spec.rb
Normal file
|
@ -0,0 +1,57 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#fix" do
|
||||
before :each do
|
||||
@zero = BigDecimal("0")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@pos_int = BigDecimal("2E5555")
|
||||
@neg_int = BigDecimal("-2E5555")
|
||||
@pos_frac = BigDecimal("2E-9999")
|
||||
@neg_frac = BigDecimal("-2E-9999")
|
||||
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_neg = BigDecimal("-Infinity")
|
||||
@nan = BigDecimal("NaN")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
end
|
||||
|
||||
it "returns a BigDecimal" do
|
||||
BigDecimal("2E100000000").fix.kind_of?(BigDecimal).should == true
|
||||
BigDecimal("2E-999").kind_of?(BigDecimal).should == true
|
||||
end
|
||||
|
||||
it "returns the integer part of the absolute value" do
|
||||
a = BigDecimal("2E1000")
|
||||
a.fix.should == a
|
||||
b = BigDecimal("-2E1000")
|
||||
b.fix.should == b
|
||||
BigDecimal("0.123456789E5").fix.should == BigDecimal("0.12345E5")
|
||||
BigDecimal("-0.123456789E5").fix.should == BigDecimal("-0.12345E5")
|
||||
end
|
||||
|
||||
it "correctly handles special values" do
|
||||
@infinity.fix.should == @infinity
|
||||
@infinity_neg.fix.should == @infinity_neg
|
||||
@nan.fix.nan?.should == true
|
||||
end
|
||||
|
||||
it "returns 0 if the absolute value is < 1" do
|
||||
BigDecimal("0.99999").fix.should == 0
|
||||
BigDecimal("-0.99999").fix.should == 0
|
||||
BigDecimal("0.000000001").fix.should == 0
|
||||
BigDecimal("-0.00000001").fix.should == 0
|
||||
BigDecimal("-1000000").fix.should_not == 0
|
||||
@zero.fix.should == 0
|
||||
@zero_pos.fix.should == @zero_pos
|
||||
@zero_neg.fix.should == @zero_neg
|
||||
end
|
||||
|
||||
it "does not allow any arguments" do
|
||||
lambda {
|
||||
@mixed.fix(10)
|
||||
}.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
end
|
17
spec/ruby/library/bigdecimal/fixtures/classes.rb
Normal file
17
spec/ruby/library/bigdecimal/fixtures/classes.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
module BigDecimalSpecs
|
||||
# helper method to sure that the global limit is reset back
|
||||
def self.with_limit(l)
|
||||
old = BigDecimal.limit(l)
|
||||
yield
|
||||
ensure
|
||||
BigDecimal.limit(old)
|
||||
end
|
||||
|
||||
def self.with_rounding(r)
|
||||
old = BigDecimal.mode(BigDecimal::ROUND_MODE)
|
||||
BigDecimal.mode(BigDecimal::ROUND_MODE, r)
|
||||
yield
|
||||
ensure
|
||||
BigDecimal.mode(BigDecimal::ROUND_MODE, old)
|
||||
end
|
||||
end
|
100
spec/ruby/library/bigdecimal/floor_spec.rb
Normal file
100
spec/ruby/library/bigdecimal/floor_spec.rb
Normal file
|
@ -0,0 +1,100 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#floor" do
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@three = BigDecimal("3")
|
||||
@four = BigDecimal("4")
|
||||
@zero = BigDecimal("0")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@mixed_big = BigDecimal("1.23456789E100")
|
||||
@pos_int = BigDecimal("2E5555")
|
||||
@neg_int = BigDecimal("-2E5555")
|
||||
@pos_frac = BigDecimal("2E-9999")
|
||||
@neg_frac = BigDecimal("-2E-9999")
|
||||
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_neg = BigDecimal("-Infinity")
|
||||
@nan = BigDecimal("NaN")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
end
|
||||
|
||||
it "returns the greatest integer smaller or equal to self" do
|
||||
@pos_int.floor.should == @pos_int
|
||||
@neg_int.floor.should == @neg_int
|
||||
@pos_frac.floor.should == @zero
|
||||
@neg_frac.floor.should == BigDecimal("-1")
|
||||
@zero.floor.should == 0
|
||||
@zero_pos.floor.should == @zero_pos
|
||||
@zero_neg.floor.should == @zero_neg
|
||||
|
||||
BigDecimal('2.3').floor.should == 2
|
||||
BigDecimal('2.5').floor.should == 2
|
||||
BigDecimal('2.9999').floor.should == 2
|
||||
BigDecimal('-2.3').floor.should == -3
|
||||
BigDecimal('-2.5').floor.should == -3
|
||||
BigDecimal('-2.9999').floor.should == -3
|
||||
BigDecimal('0.8').floor.should == 0
|
||||
BigDecimal('-0.8').floor.should == -1
|
||||
end
|
||||
|
||||
it "raise exception, if self is special value" do
|
||||
lambda { @infinity.floor }.should raise_error(FloatDomainError)
|
||||
lambda { @infinity_neg.floor }.should raise_error(FloatDomainError)
|
||||
lambda { @nan.floor }.should raise_error(FloatDomainError)
|
||||
end
|
||||
|
||||
it "returns n digits right of the decimal point if given n > 0" do
|
||||
@mixed.floor(1).should == BigDecimal("1.2")
|
||||
@mixed.floor(5).should == BigDecimal("1.23456")
|
||||
|
||||
BigDecimal("-0.03").floor(1).should == BigDecimal("-0.1")
|
||||
BigDecimal("0.03").floor(1).should == BigDecimal("0")
|
||||
|
||||
BigDecimal("23.45").floor(0).should == BigDecimal('23')
|
||||
BigDecimal("23.45").floor(1).should == BigDecimal('23.4')
|
||||
BigDecimal("23.45").floor(2).should == BigDecimal('23.45')
|
||||
|
||||
BigDecimal("-23.45").floor(0).should == BigDecimal('-24')
|
||||
BigDecimal("-23.45").floor(1).should == BigDecimal('-23.5')
|
||||
BigDecimal("-23.45").floor(2).should == BigDecimal('-23.45')
|
||||
|
||||
BigDecimal("2E-10").floor(0).should == @zero
|
||||
BigDecimal("2E-10").floor(9).should == @zero
|
||||
BigDecimal("2E-10").floor(10).should == BigDecimal('2E-10')
|
||||
BigDecimal("2E-10").floor(11).should == BigDecimal('2E-10')
|
||||
|
||||
(1..10).each do |n|
|
||||
# 0.3, 0.33, 0.333, etc.
|
||||
(@one.div(@three,20)).floor(n).should == BigDecimal("0.#{'3'*n}")
|
||||
# 1.3, 1.33, 1.333, etc.
|
||||
(@four.div(@three,20)).floor(n).should == BigDecimal("1.#{'3'*n}")
|
||||
(BigDecimal('31').div(@three,20)).floor(n).should == BigDecimal("10.#{'3'*n}")
|
||||
end
|
||||
(1..10).each do |n|
|
||||
# -0.4, -0.34, -0.334, etc.
|
||||
(-@one.div(@three,20)).floor(n).should == BigDecimal("-0.#{'3'*(n-1)}4")
|
||||
end
|
||||
(1..10).each do |n|
|
||||
(@three.div(@one,20)).floor(n).should == @three
|
||||
end
|
||||
(1..10).each do |n|
|
||||
(-@three.div(@one,20)).floor(n).should == -@three
|
||||
end
|
||||
end
|
||||
|
||||
it "sets n digits left of the decimal point to 0, if given n < 0" do
|
||||
BigDecimal("13345.234").floor(-2).should == BigDecimal("13300.0")
|
||||
@mixed_big.floor(-99).should == BigDecimal("0.12E101")
|
||||
@mixed_big.floor(-100).should == BigDecimal("0.1E101")
|
||||
@mixed_big.floor(-95).should == BigDecimal("0.123456E101")
|
||||
(1..10).each do |n|
|
||||
BigDecimal('1.8').floor(-n).should == @zero
|
||||
end
|
||||
BigDecimal("1E10").floor(-30).should == @zero
|
||||
BigDecimal("-1E10").floor(-30).should == BigDecimal('-1E30')
|
||||
end
|
||||
|
||||
end
|
48
spec/ruby/library/bigdecimal/frac_spec.rb
Normal file
48
spec/ruby/library/bigdecimal/frac_spec.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#frac" do
|
||||
before :each do
|
||||
@zero = BigDecimal("0")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@pos_int = BigDecimal("2E5555")
|
||||
@neg_int = BigDecimal("-2E5555")
|
||||
@pos_frac = BigDecimal("2E-9999")
|
||||
@neg_frac = BigDecimal("-2E-9999")
|
||||
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_neg = BigDecimal("-Infinity")
|
||||
@nan = BigDecimal("NaN")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
end
|
||||
|
||||
it "returns a BigDecimal" do
|
||||
@pos_int.frac.kind_of?(BigDecimal).should == true
|
||||
@neg_int.frac.kind_of?(BigDecimal).should == true
|
||||
@pos_frac.kind_of?(BigDecimal).should == true
|
||||
@neg_frac.kind_of?(BigDecimal).should == true
|
||||
end
|
||||
|
||||
it "returns the fractional part of the absolute value" do
|
||||
@mixed.frac.should == BigDecimal("0.23456789")
|
||||
@pos_frac.frac.should == @pos_frac
|
||||
@neg_frac.frac.should == @neg_frac
|
||||
end
|
||||
|
||||
it "returns 0 if the value is 0" do
|
||||
@zero.frac.should == @zero
|
||||
end
|
||||
|
||||
it "returns 0 if the value is an integer" do
|
||||
@pos_int.frac.should == @zero
|
||||
@neg_int.frac.should == @zero
|
||||
end
|
||||
|
||||
it "correctly handles special values" do
|
||||
@infinity.frac.should == @infinity
|
||||
@infinity_neg.frac.should == @infinity_neg
|
||||
@nan.frac.nan?.should == true
|
||||
end
|
||||
|
||||
end
|
98
spec/ruby/library/bigdecimal/gt_spec.rb
Normal file
98
spec/ruby/library/bigdecimal/gt_spec.rb
Normal file
|
@ -0,0 +1,98 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#>" do
|
||||
before :each do
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@pos_int = BigDecimal("2E5555")
|
||||
@neg_int = BigDecimal("-2E5555")
|
||||
@pos_frac = BigDecimal("2E-9999")
|
||||
@neg_frac = BigDecimal("-2E-9999")
|
||||
|
||||
@int_mock = mock('123')
|
||||
class << @int_mock
|
||||
def coerce(other)
|
||||
return [other, BigDecimal('123')]
|
||||
end
|
||||
def > (other)
|
||||
BigDecimal('123') > other
|
||||
end
|
||||
end
|
||||
|
||||
@values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
|
||||
-2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
|
||||
@zero , 1, 2, 10, 10.5, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
|
||||
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_neg = BigDecimal("-Infinity")
|
||||
|
||||
@float_infinity = Float::INFINITY
|
||||
@float_infinity_neg = -Float::INFINITY
|
||||
|
||||
@nan = BigDecimal("NaN")
|
||||
end
|
||||
|
||||
it "returns true if a > b" do
|
||||
one = BigDecimal("1")
|
||||
two = BigDecimal("2")
|
||||
|
||||
frac_1 = BigDecimal("1E-99999")
|
||||
frac_2 = BigDecimal("0.9E-99999")
|
||||
(@zero > one).should == false
|
||||
(two > @zero).should == true
|
||||
(frac_2 > frac_1).should == false
|
||||
|
||||
(@neg_int > @pos_int).should == false
|
||||
(@pos_int > @neg_int).should == true
|
||||
(@neg_int > @pos_frac).should == false
|
||||
(@pos_frac > @neg_int).should == true
|
||||
(@zero > @zero_pos).should == false
|
||||
(@zero > @zero_neg).should == false
|
||||
(@zero_neg > @zero_pos).should == false
|
||||
(@zero_pos > @zero_neg).should == false
|
||||
end
|
||||
|
||||
it "properly handles infinity values" do
|
||||
@values.each { |val|
|
||||
(val > @infinity).should == false
|
||||
(@infinity > val).should == true
|
||||
(val > @infinity_neg).should == true
|
||||
(@infinity_neg > val).should == false
|
||||
}
|
||||
(@infinity > @infinity).should == false
|
||||
(@infinity_neg > @infinity_neg).should == false
|
||||
(@infinity > @infinity_neg).should == true
|
||||
(@infinity_neg > @infinity).should == false
|
||||
end
|
||||
|
||||
ruby_bug "#13674", ""..."2.4" do
|
||||
it "properly handles Float infinity values" do
|
||||
@values.each { |val|
|
||||
(val > @float_infinity).should == false
|
||||
(@float_infinity > val).should == true
|
||||
(val > @float_infinity_neg).should == true
|
||||
(@float_infinity_neg > val).should == false
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
it "properly handles NaN values" do
|
||||
@values += [@infinity, @infinity_neg, @nan]
|
||||
@values.each { |val|
|
||||
(@nan > val).should == false
|
||||
(val > @nan).should == false
|
||||
}
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if the argument can't be coerced into a BigDecimal" do
|
||||
lambda {@zero > nil }.should raise_error(ArgumentError)
|
||||
lambda {@infinity > nil }.should raise_error(ArgumentError)
|
||||
lambda {@infinity_neg > nil }.should raise_error(ArgumentError)
|
||||
lambda {@mixed > nil }.should raise_error(ArgumentError)
|
||||
lambda {@pos_int > nil }.should raise_error(ArgumentError)
|
||||
lambda {@neg_frac > nil }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
102
spec/ruby/library/bigdecimal/gte_spec.rb
Normal file
102
spec/ruby/library/bigdecimal/gte_spec.rb
Normal file
|
@ -0,0 +1,102 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#>=" do
|
||||
before :each do
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@pos_int = BigDecimal("2E5555")
|
||||
@neg_int = BigDecimal("-2E5555")
|
||||
@pos_frac = BigDecimal("2E-9999")
|
||||
@neg_frac = BigDecimal("-2E-9999")
|
||||
|
||||
@int_mock = mock('123')
|
||||
class << @int_mock
|
||||
def coerce(other)
|
||||
return [other, BigDecimal('123')]
|
||||
end
|
||||
def >= (other)
|
||||
BigDecimal('123') >= other
|
||||
end
|
||||
end
|
||||
|
||||
@values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
|
||||
-2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
|
||||
@zero , 1, 2, 10, 10.5, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
|
||||
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_neg = BigDecimal("-Infinity")
|
||||
|
||||
@float_infinity = Float::INFINITY
|
||||
@float_infinity_neg = -Float::INFINITY
|
||||
|
||||
@nan = BigDecimal("NaN")
|
||||
end
|
||||
|
||||
it "returns true if a >= b" do
|
||||
one = BigDecimal("1")
|
||||
two = BigDecimal("2")
|
||||
|
||||
frac_1 = BigDecimal("1E-99999")
|
||||
frac_2 = BigDecimal("0.9E-99999")
|
||||
|
||||
(@zero >= one).should == false
|
||||
(two >= @zero).should == true
|
||||
|
||||
(frac_2 >= frac_1).should == false
|
||||
(two >= two).should == true
|
||||
(frac_1 >= frac_1).should == true
|
||||
|
||||
(@neg_int >= @pos_int).should == false
|
||||
(@pos_int >= @neg_int).should == true
|
||||
(@neg_int >= @pos_frac).should == false
|
||||
(@pos_frac >= @neg_int).should == true
|
||||
(@zero >= @zero_pos).should == true
|
||||
(@zero >= @zero_neg).should == true
|
||||
(@zero_neg >= @zero_pos).should == true
|
||||
(@zero_pos >= @zero_neg).should == true
|
||||
end
|
||||
|
||||
it "properly handles infinity values" do
|
||||
@values.each { |val|
|
||||
(val >= @infinity).should == false
|
||||
(@infinity >= val).should == true
|
||||
(val >= @infinity_neg).should == true
|
||||
(@infinity_neg >= val).should == false
|
||||
}
|
||||
(@infinity >= @infinity).should == true
|
||||
(@infinity_neg >= @infinity_neg).should == true
|
||||
(@infinity >= @infinity_neg).should == true
|
||||
(@infinity_neg >= @infinity).should == false
|
||||
end
|
||||
|
||||
ruby_bug "#13674", ""..."2.4" do
|
||||
it "properly handles Float infinity values" do
|
||||
@values.each { |val|
|
||||
(val >= @float_infinity).should == false
|
||||
(@float_infinity >= val).should == true
|
||||
(val >= @float_infinity_neg).should == true
|
||||
(@float_infinity_neg >= val).should == false
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
it "properly handles NaN values" do
|
||||
@values += [@infinity, @infinity_neg, @nan]
|
||||
@values.each { |val|
|
||||
(@nan >= val).should == false
|
||||
(val >= @nan).should == false
|
||||
}
|
||||
end
|
||||
|
||||
it "returns nil if the argument is nil" do
|
||||
lambda {@zero >= nil }.should raise_error(ArgumentError)
|
||||
lambda {@infinity >= nil }.should raise_error(ArgumentError)
|
||||
lambda {@infinity_neg >= nil }.should raise_error(ArgumentError)
|
||||
lambda {@mixed >= nil }.should raise_error(ArgumentError)
|
||||
lambda {@pos_int >= nil }.should raise_error(ArgumentError)
|
||||
lambda {@neg_frac >= nil }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
32
spec/ruby/library/bigdecimal/infinite_spec.rb
Normal file
32
spec/ruby/library/bigdecimal/infinite_spec.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#infinite?" do
|
||||
|
||||
it "returns 1 if self is Infinity" do
|
||||
BigDecimal("Infinity").infinite?.should == 1
|
||||
end
|
||||
|
||||
it "returns -1 if self is -Infinity" do
|
||||
BigDecimal("-Infinity").infinite?.should == -1
|
||||
end
|
||||
|
||||
it "returns not true otherwise" do
|
||||
e2_plus = BigDecimal("2E40001")
|
||||
e3_minus = BigDecimal("3E-20001")
|
||||
really_small_zero = BigDecimal("0E-200000000")
|
||||
really_big_zero = BigDecimal("0E200000000000")
|
||||
e3_minus.infinite?.should == nil
|
||||
e2_plus.infinite?.should == nil
|
||||
really_small_zero.infinite?.should == nil
|
||||
really_big_zero.infinite?.should == nil
|
||||
BigDecimal("0.000000000000000000000000").infinite?.should == nil
|
||||
end
|
||||
|
||||
it "returns not true if self is NaN" do
|
||||
# NaN is a special value which is neither finite nor infinite.
|
||||
nan = BigDecimal("NaN")
|
||||
nan.infinite?.should == nil
|
||||
end
|
||||
|
||||
end
|
47
spec/ruby/library/bigdecimal/inspect_spec.rb
Normal file
47
spec/ruby/library/bigdecimal/inspect_spec.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#inspect" do
|
||||
|
||||
before :each do
|
||||
@bigdec = BigDecimal.new("1234.5678")
|
||||
end
|
||||
|
||||
it "returns String" do
|
||||
@bigdec.inspect.kind_of?(String).should == true
|
||||
end
|
||||
|
||||
ruby_version_is ""..."2.4" do
|
||||
it "returns String starting with #" do
|
||||
@bigdec.inspect[0].should == ?#
|
||||
end
|
||||
|
||||
it "encloses information in angle brackets" do
|
||||
@bigdec.inspect.should =~ /^.<.*>$/
|
||||
end
|
||||
|
||||
it "is comma separated list of three items" do
|
||||
@bigdec.inspect.should =~ /...*,.*,.*/
|
||||
end
|
||||
|
||||
it "value after first comma is value as string" do
|
||||
@bigdec.inspect.split(",")[1].should == "\'0.12345678E4\'"
|
||||
end
|
||||
|
||||
it "last part is number of significant digits" do
|
||||
signific_string = "#{@bigdec.precs[0]}(#{@bigdec.precs[1]})"
|
||||
@bigdec.inspect.split(",")[2].should == signific_string + ">"
|
||||
end
|
||||
|
||||
it "looks like this" do
|
||||
regex = /^\#\<BigDecimal\:.*,'0\.12345678E4',[0-9]+\([0-9]+\)>$/
|
||||
@bigdec.inspect.should =~ regex
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is "2.4" do
|
||||
it "looks like this" do
|
||||
@bigdec.inspect.should == "0.12345678e4"
|
||||
end
|
||||
end
|
||||
end
|
30
spec/ruby/library/bigdecimal/limit_spec.rb
Normal file
30
spec/ruby/library/bigdecimal/limit_spec.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal.limit" do
|
||||
it "returns the value before set if the passed argument is nil or is not specified" do
|
||||
old = BigDecimal.limit
|
||||
BigDecimal.limit.should == 0
|
||||
BigDecimal.limit(10).should == 0
|
||||
BigDecimal.limit.should == 10
|
||||
BigDecimal.limit(old)
|
||||
end
|
||||
|
||||
it "use the global limit if no precision is specified" do
|
||||
BigDecimalSpecs.with_limit(0) do
|
||||
(BigDecimal('0.888') + BigDecimal('0')).should == BigDecimal('0.888')
|
||||
(BigDecimal('0.888') * BigDecimal('3')).should == BigDecimal('2.664')
|
||||
end
|
||||
|
||||
BigDecimalSpecs.with_limit(1) do
|
||||
(BigDecimal('0.888') + BigDecimal('0')).should == BigDecimal('0.9')
|
||||
(BigDecimal('0.888') * BigDecimal('3')).should == BigDecimal('3')
|
||||
end
|
||||
|
||||
BigDecimalSpecs.with_limit(2) do
|
||||
(BigDecimal('0.888') + BigDecimal('0')).should == BigDecimal('0.89')
|
||||
(BigDecimal('0.888') * BigDecimal('3')).should == BigDecimal('2.7')
|
||||
end
|
||||
end
|
||||
end
|
96
spec/ruby/library/bigdecimal/lt_spec.rb
Normal file
96
spec/ruby/library/bigdecimal/lt_spec.rb
Normal file
|
@ -0,0 +1,96 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#<" do
|
||||
before :each do
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@pos_int = BigDecimal("2E5555")
|
||||
@neg_int = BigDecimal("-2E5555")
|
||||
@pos_frac = BigDecimal("2E-9999")
|
||||
@neg_frac = BigDecimal("-2E-9999")
|
||||
|
||||
@int_mock = mock('123')
|
||||
class << @int_mock
|
||||
def coerce(other)
|
||||
return [other, BigDecimal('123')]
|
||||
end
|
||||
def < (other)
|
||||
BigDecimal('123') < other
|
||||
end
|
||||
end
|
||||
|
||||
@values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
|
||||
-2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
|
||||
@zero , 1, 2, 10, 10.5, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
|
||||
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_neg = BigDecimal("-Infinity")
|
||||
|
||||
@float_infinity = Float::INFINITY
|
||||
@float_infinity_neg = -Float::INFINITY
|
||||
|
||||
@nan = BigDecimal("NaN")
|
||||
end
|
||||
|
||||
it "returns true if a < b" do
|
||||
one = BigDecimal("1")
|
||||
two = BigDecimal("2")
|
||||
frac_1 = BigDecimal("1E-99999")
|
||||
frac_2 = BigDecimal("0.9E-99999")
|
||||
(@zero < one).should == true
|
||||
(two < @zero).should == false
|
||||
(frac_2 < frac_1).should == true
|
||||
(@neg_int < @pos_int).should == true
|
||||
(@pos_int < @neg_int).should == false
|
||||
(@neg_int < @pos_frac).should == true
|
||||
(@pos_frac < @neg_int).should == false
|
||||
(@zero < @zero_pos).should == false
|
||||
(@zero < @zero_neg).should == false
|
||||
(@zero_neg < @zero_pos).should == false
|
||||
(@zero_pos < @zero_neg).should == false
|
||||
end
|
||||
|
||||
it "properly handles infinity values" do
|
||||
@values.each { |val|
|
||||
(val < @infinity).should == true
|
||||
(@infinity < val).should == false
|
||||
(val < @infinity_neg).should == false
|
||||
(@infinity_neg < val).should == true
|
||||
}
|
||||
(@infinity < @infinity).should == false
|
||||
(@infinity_neg < @infinity_neg).should == false
|
||||
(@infinity < @infinity_neg).should == false
|
||||
(@infinity_neg < @infinity).should == true
|
||||
end
|
||||
|
||||
ruby_bug "#13674", ""..."2.4" do
|
||||
it "properly handles Float infinity values" do
|
||||
@values.each { |val|
|
||||
(val < @float_infinity).should == true
|
||||
(@float_infinity < val).should == false
|
||||
(val < @float_infinity_neg).should == false
|
||||
(@float_infinity_neg < val).should == true
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
it "properly handles NaN values" do
|
||||
@values += [@infinity, @infinity_neg, @nan]
|
||||
@values.each { |val|
|
||||
(@nan < val).should == false
|
||||
(val < @nan).should == false
|
||||
}
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if the argument can't be coerced into a BigDecimal" do
|
||||
lambda {@zero < nil }.should raise_error(ArgumentError)
|
||||
lambda {@infinity < nil }.should raise_error(ArgumentError)
|
||||
lambda {@infinity_neg < nil }.should raise_error(ArgumentError)
|
||||
lambda {@mixed < nil }.should raise_error(ArgumentError)
|
||||
lambda {@pos_int < nil }.should raise_error(ArgumentError)
|
||||
lambda {@neg_frac < nil }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
102
spec/ruby/library/bigdecimal/lte_spec.rb
Normal file
102
spec/ruby/library/bigdecimal/lte_spec.rb
Normal file
|
@ -0,0 +1,102 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#<=" do
|
||||
before :each do
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@pos_int = BigDecimal("2E5555")
|
||||
@neg_int = BigDecimal("-2E5555")
|
||||
@pos_frac = BigDecimal("2E-9999")
|
||||
@neg_frac = BigDecimal("-2E-9999")
|
||||
|
||||
@int_mock = mock('123')
|
||||
class << @int_mock
|
||||
def coerce(other)
|
||||
return [other, BigDecimal('123')]
|
||||
end
|
||||
def <= (other)
|
||||
BigDecimal('123') <= other
|
||||
end
|
||||
end
|
||||
|
||||
@values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
|
||||
-2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
|
||||
@zero , 1, 2, 10, 10.5, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
|
||||
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_neg = BigDecimal("-Infinity")
|
||||
|
||||
@float_infinity = Float::INFINITY
|
||||
@float_infinity_neg = -Float::INFINITY
|
||||
|
||||
@nan = BigDecimal("NaN")
|
||||
end
|
||||
|
||||
it "returns true if a <= b" do
|
||||
one = BigDecimal("1")
|
||||
two = BigDecimal("2")
|
||||
|
||||
frac_1 = BigDecimal("1E-99999")
|
||||
frac_2 = BigDecimal("0.9E-99999")
|
||||
|
||||
(@zero <= one).should == true
|
||||
(two <= @zero).should == false
|
||||
|
||||
(frac_2 <= frac_1).should == true
|
||||
(two <= two).should == true
|
||||
(frac_1 <= frac_1).should == true
|
||||
|
||||
(@neg_int <= @pos_int).should == true
|
||||
(@pos_int <= @neg_int).should == false
|
||||
(@neg_int <= @pos_frac).should == true
|
||||
(@pos_frac <= @neg_int).should == false
|
||||
(@zero <= @zero_pos).should == true
|
||||
(@zero <= @zero_neg).should == true
|
||||
(@zero_neg <= @zero_pos).should == true
|
||||
(@zero_pos <= @zero_neg).should == true
|
||||
end
|
||||
|
||||
it "properly handles infinity values" do
|
||||
@values.each { |val|
|
||||
(val <= @infinity).should == true
|
||||
(@infinity <= val).should == false
|
||||
(val <= @infinity_neg).should == false
|
||||
(@infinity_neg <= val).should == true
|
||||
}
|
||||
(@infinity <= @infinity).should == true
|
||||
(@infinity_neg <= @infinity_neg).should == true
|
||||
(@infinity <= @infinity_neg).should == false
|
||||
(@infinity_neg <= @infinity).should == true
|
||||
end
|
||||
|
||||
ruby_bug "#13674", ""..."2.4" do
|
||||
it "properly handles Float infinity values" do
|
||||
@values.each { |val|
|
||||
(val <= @float_infinity).should == true
|
||||
(@float_infinity <= val).should == false
|
||||
(val <= @float_infinity_neg).should == false
|
||||
(@float_infinity_neg <= val).should == true
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
it "properly handles NaN values" do
|
||||
@values += [@infinity, @infinity_neg, @nan]
|
||||
@values.each { |val|
|
||||
(@nan <= val).should == false
|
||||
(val <= @nan).should == false
|
||||
}
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if the argument can't be coerced into a BigDecimal" do
|
||||
lambda {@zero <= nil }.should raise_error(ArgumentError)
|
||||
lambda {@infinity <= nil }.should raise_error(ArgumentError)
|
||||
lambda {@infinity_neg <= nil }.should raise_error(ArgumentError)
|
||||
lambda {@mixed <= nil }.should raise_error(ArgumentError)
|
||||
lambda {@pos_int <= nil }.should raise_error(ArgumentError)
|
||||
lambda {@neg_frac <= nil }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
58
spec/ruby/library/bigdecimal/minus_spec.rb
Normal file
58
spec/ruby/library/bigdecimal/minus_spec.rb
Normal file
|
@ -0,0 +1,58 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#-" do
|
||||
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@two = BigDecimal("2")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
end
|
||||
|
||||
it "returns a - b" do
|
||||
(@two - @one).should == @one
|
||||
(@one - @two).should == @one_minus
|
||||
(@one - @one_minus).should == @two
|
||||
(@frac_2 - @frac_1).should == BigDecimal("-0.1E-99999")
|
||||
(@two - @two).should == @zero
|
||||
(@frac_1 - @frac_1).should == @zero
|
||||
(BigDecimal('1.23456789') - BigDecimal('1.2')).should == BigDecimal("0.03456789")
|
||||
end
|
||||
|
||||
it "returns NaN if NaN is involved" do
|
||||
(@one - @nan).nan?.should == true
|
||||
(@nan - @one).nan?.should == true
|
||||
(@nan - @nan).nan?.should == true
|
||||
(@nan - @infinity).nan?.should == true
|
||||
(@nan - @infinity_minus).nan?.should == true
|
||||
(@infinity - @nan).nan?.should == true
|
||||
(@infinity_minus - @nan).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns NaN both operands are infinite with the same sign" do
|
||||
(@infinity - @infinity).nan?.should == true
|
||||
(@infinity_minus - @infinity_minus).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns Infinity or -Infinity if these are involved" do
|
||||
(@infinity - @infinity_minus).should == @infinity
|
||||
(@infinity_minus - @infinity).should == @infinity_minus
|
||||
|
||||
(@infinity - @zero).should == @infinity
|
||||
(@infinity - @frac_2).should == @infinity
|
||||
(@infinity - @two).should == @infinity
|
||||
(@infinity - @one_minus).should == @infinity
|
||||
|
||||
(@zero - @infinity).should == @infinity_minus
|
||||
(@frac_2 - @infinity).should == @infinity_minus
|
||||
(@two - @infinity).should == @infinity_minus
|
||||
(@one_minus - @infinity).should == @infinity_minus
|
||||
end
|
||||
|
||||
end
|
36
spec/ruby/library/bigdecimal/mode_spec.rb
Normal file
36
spec/ruby/library/bigdecimal/mode_spec.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal.mode" do
|
||||
#the default value of BigDecimal exception constants is false
|
||||
after :each do
|
||||
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
|
||||
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
|
||||
BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, false)
|
||||
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
|
||||
BigDecimal.mode(BigDecimal::EXCEPTION_ZERODIVIDE, false)
|
||||
end
|
||||
|
||||
it "returns the appropriate value and continue the computation if the flag is false" do
|
||||
BigDecimal("NaN").add(BigDecimal("1"),0).nan?.should == true
|
||||
BigDecimal("0").add(BigDecimal("Infinity"),0).should == BigDecimal("Infinity")
|
||||
BigDecimal("1").quo(BigDecimal("0")).should == BigDecimal("Infinity")
|
||||
end
|
||||
|
||||
it "returns Infinity when too big" do
|
||||
BigDecimal("1E11111111111111111111").should == BigDecimal("Infinity")
|
||||
(BigDecimal("1E1000000000000000000")**10).should == BigDecimal("Infinity")
|
||||
end
|
||||
|
||||
it "raise an exception if the flag is true" do
|
||||
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, true)
|
||||
lambda { BigDecimal("NaN").add(BigDecimal("1"),0) }.should raise_error(FloatDomainError)
|
||||
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, true)
|
||||
lambda { BigDecimal("0").add(BigDecimal("Infinity"),0) }.should raise_error(FloatDomainError)
|
||||
BigDecimal.mode(BigDecimal::EXCEPTION_ZERODIVIDE, true)
|
||||
lambda { BigDecimal("1").quo(BigDecimal("0")) }.should raise_error(FloatDomainError)
|
||||
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, true)
|
||||
lambda { BigDecimal("1E11111111111111111111") }.should raise_error(FloatDomainError)
|
||||
lambda { (BigDecimal("1E1000000000000000000")**10) }.should raise_error(FloatDomainError)
|
||||
end
|
||||
end
|
12
spec/ruby/library/bigdecimal/modulo_spec.rb
Normal file
12
spec/ruby/library/bigdecimal/modulo_spec.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/modulo', __FILE__)
|
||||
|
||||
describe "BigDecimal#%" do
|
||||
it_behaves_like(:bigdecimal_modulo, :%)
|
||||
it_behaves_like(:bigdecimal_modulo_zerodivisionerror, :%)
|
||||
end
|
||||
|
||||
describe "BigDecimal#modulo" do
|
||||
it_behaves_like(:bigdecimal_modulo, :modulo)
|
||||
it_behaves_like(:bigdecimal_modulo_zerodivisionerror, :modulo)
|
||||
end
|
24
spec/ruby/library/bigdecimal/mult_spec.rb
Normal file
24
spec/ruby/library/bigdecimal/mult_spec.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/mult', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#mult" do
|
||||
it_behaves_like :bigdecimal_mult, :mult, [10]
|
||||
end
|
||||
|
||||
describe "BigDecimal#mult" do
|
||||
before :each do
|
||||
@one = BigDecimal "1"
|
||||
@e3_minus = BigDecimal "3E-20001"
|
||||
@e = BigDecimal "1.00000000000000000000123456789"
|
||||
@tolerance = @e.sub @one, 1000
|
||||
@tolerance2 = BigDecimal "30001E-20005"
|
||||
|
||||
end
|
||||
|
||||
it "multiply self with other with (optional) precision" do
|
||||
@e.mult(@one, 1).should be_close(@one, @tolerance)
|
||||
@e3_minus.mult(@one, 1).should be_close(0, @tolerance2)
|
||||
end
|
||||
|
||||
end
|
26
spec/ruby/library/bigdecimal/multiply_spec.rb
Normal file
26
spec/ruby/library/bigdecimal/multiply_spec.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/mult', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#*" do
|
||||
it_behaves_like :bigdecimal_mult, :*, []
|
||||
end
|
||||
|
||||
describe "BigDecimal#*" do
|
||||
before :each do
|
||||
@e3_minus = BigDecimal("3E-20001")
|
||||
@e3_plus = BigDecimal("3E20001")
|
||||
@e = BigDecimal("1.00000000000000000000123456789")
|
||||
@one = BigDecimal("1")
|
||||
end
|
||||
|
||||
it "multiply self with other" do
|
||||
(@one * @one).should == @one
|
||||
(@e3_minus * @e3_plus).should == BigDecimal("9")
|
||||
# Can't do this till we implement **
|
||||
# (@e3_minus * @e3_minus).should == @e3_minus ** 2
|
||||
# So let's rewrite it as:
|
||||
(@e3_minus * @e3_minus).should == BigDecimal("9E-40002")
|
||||
(@e * @one).should == @e
|
||||
end
|
||||
end
|
23
spec/ruby/library/bigdecimal/nan_spec.rb
Normal file
23
spec/ruby/library/bigdecimal/nan_spec.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#nan?" do
|
||||
|
||||
it "returns true if self is not a number" do
|
||||
BigDecimal("NaN").nan?.should == true
|
||||
end
|
||||
|
||||
it "returns false if self is not a NaN" do
|
||||
BigDecimal("Infinity").nan?.should == false
|
||||
BigDecimal("-Infinity").nan?.should == false
|
||||
BigDecimal("0").nan?.should == false
|
||||
BigDecimal("+0").nan?.should == false
|
||||
BigDecimal("-0").nan?.should == false
|
||||
BigDecimal("2E40001").nan?.should == false
|
||||
BigDecimal("3E-20001").nan?.should == false
|
||||
BigDecimal("0E-200000000").nan?.should == false
|
||||
BigDecimal("0E200000000000").nan?.should == false
|
||||
BigDecimal("0.000000000000000000000000").nan?.should == false
|
||||
end
|
||||
|
||||
end
|
109
spec/ruby/library/bigdecimal/new_spec.rb
Normal file
109
spec/ruby/library/bigdecimal/new_spec.rb
Normal file
|
@ -0,0 +1,109 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal.new" do
|
||||
|
||||
it "creates a new object of class BigDecimal" do
|
||||
BigDecimal.new("3.14159").should be_kind_of(BigDecimal)
|
||||
(0..9).each {|i|
|
||||
BigDecimal.new("1#{i}").should == 10 + i
|
||||
BigDecimal.new("-1#{i}").should == -10 - i
|
||||
BigDecimal.new("1E#{i}").should == 10**i
|
||||
BigDecimal.new("1000000E-#{i}").should == 10**(6-i).to_f
|
||||
# ^ to_f to avoid Rational type
|
||||
}
|
||||
(1..9).each {|i|
|
||||
BigDecimal.new("100.#{i}").to_s.should =~ /\A0\.100#{i}E3\z/i
|
||||
BigDecimal.new("-100.#{i}").to_s.should =~ /\A-0\.100#{i}E3\z/i
|
||||
}
|
||||
end
|
||||
|
||||
it "accepts significant digits >= given precision" do
|
||||
BigDecimal.new("3.1415923", 10).precs[1].should >= 10
|
||||
end
|
||||
|
||||
it "determines precision from initial value" do
|
||||
pi_string = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043"
|
||||
BigDecimal.new(pi_string).precs[1].should >= pi_string.size-1
|
||||
end
|
||||
|
||||
it "ignores leading whitespace" do
|
||||
BigDecimal.new(" \t\n \r1234").should == BigDecimal.new("1234")
|
||||
BigDecimal.new(" \t\n \rNaN \n").nan?.should == true
|
||||
BigDecimal.new(" \t\n \rInfinity \n").infinite?.should == 1
|
||||
BigDecimal.new(" \t\n \r-Infinity \n").infinite?.should == -1
|
||||
end
|
||||
|
||||
it "ignores trailing garbage" do
|
||||
BigDecimal.new("123E45ruby").should == BigDecimal.new("123E45")
|
||||
BigDecimal.new("123x45").should == BigDecimal.new("123")
|
||||
BigDecimal.new("123.4%E5").should == BigDecimal.new("123.4")
|
||||
BigDecimal.new("1E2E3E4E5E").should == BigDecimal.new("100")
|
||||
end
|
||||
|
||||
ruby_version_is ""..."2.4" do
|
||||
it "treats invalid strings as 0.0" do
|
||||
BigDecimal.new("ruby").should == BigDecimal.new("0.0")
|
||||
BigDecimal.new(" \t\n \r-\t\t\tInfinity \n").should == BigDecimal.new("0.0")
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is "2.4" do
|
||||
it "raises ArgumentError for invalid strings" do
|
||||
lambda { BigDecimal.new("ruby") }.should raise_error(ArgumentError)
|
||||
lambda { BigDecimal.new(" \t\n \r-\t\t\tInfinity \n") }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
it "allows omitting the integer part" do
|
||||
BigDecimal.new(".123").should == BigDecimal.new("0.123")
|
||||
end
|
||||
|
||||
it "allows for underscores in all parts" do
|
||||
reference = BigDecimal.new("12345.67E89")
|
||||
|
||||
BigDecimal.new("12_345.67E89").should == reference
|
||||
BigDecimal.new("1_2_3_4_5_._6____7_E89").should == reference
|
||||
BigDecimal.new("12345_.67E_8__9_").should == reference
|
||||
end
|
||||
|
||||
it "accepts NaN and [+-]Infinity" do
|
||||
BigDecimal.new("NaN").nan?.should == true
|
||||
|
||||
pos_inf = BigDecimal.new("Infinity")
|
||||
pos_inf.finite?.should == false
|
||||
pos_inf.should > 0
|
||||
pos_inf.should == BigDecimal.new("+Infinity")
|
||||
|
||||
neg_inf = BigDecimal.new("-Infinity")
|
||||
neg_inf.finite?.should == false
|
||||
neg_inf.should < 0
|
||||
end
|
||||
|
||||
it "allows for [eEdD] as exponent separator" do
|
||||
reference = BigDecimal.new("12345.67E89")
|
||||
|
||||
BigDecimal.new("12345.67e89").should == reference
|
||||
BigDecimal.new("12345.67E89").should == reference
|
||||
BigDecimal.new("12345.67d89").should == reference
|
||||
BigDecimal.new("12345.67D89").should == reference
|
||||
end
|
||||
|
||||
it "allows for varying signs" do
|
||||
reference = BigDecimal.new("123.456E1")
|
||||
|
||||
BigDecimal.new("+123.456E1").should == reference
|
||||
BigDecimal.new("-123.456E1").should == -reference
|
||||
BigDecimal.new("123.456E+1").should == reference
|
||||
BigDecimal.new("12345.6E-1").should == reference
|
||||
BigDecimal.new("+123.456E+1").should == reference
|
||||
BigDecimal.new("+12345.6E-1").should == reference
|
||||
BigDecimal.new("-123.456E+1").should == -reference
|
||||
BigDecimal.new("-12345.6E-1").should == -reference
|
||||
end
|
||||
|
||||
it 'raises ArgumentError when Float is used without precision' do
|
||||
lambda { BigDecimal(1.0) }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
end
|
29
spec/ruby/library/bigdecimal/nonzero_spec.rb
Normal file
29
spec/ruby/library/bigdecimal/nonzero_spec.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#nonzero?" do
|
||||
|
||||
it "returns self if self doesn't equal zero" do
|
||||
# documentation says, it returns true. (04/10/08)
|
||||
e2_plus = BigDecimal("2E40001")
|
||||
e3_minus = BigDecimal("3E-20001")
|
||||
infinity = BigDecimal("Infinity")
|
||||
infinity_minus = BigDecimal("-Infinity")
|
||||
nan = BigDecimal("NaN")
|
||||
infinity.nonzero?.should equal(infinity)
|
||||
infinity_minus.nonzero?.should equal(infinity_minus)
|
||||
nan.nonzero?.should equal(nan)
|
||||
e3_minus.nonzero?.should equal(e3_minus)
|
||||
e2_plus.nonzero?.should equal(e2_plus)
|
||||
end
|
||||
|
||||
it "returns nil otherwise" do
|
||||
# documentation states, it should return false. (04/10/08)
|
||||
really_small_zero = BigDecimal("0E-200000000")
|
||||
really_big_zero = BigDecimal("0E200000000000")
|
||||
really_small_zero.nonzero?.should == nil
|
||||
really_big_zero.nonzero?.should == nil
|
||||
BigDecimal("0.000000000000000000000000").nonzero?.should == nil
|
||||
end
|
||||
|
||||
end
|
47
spec/ruby/library/bigdecimal/plus_spec.rb
Normal file
47
spec/ruby/library/bigdecimal/plus_spec.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#+" do
|
||||
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@two = BigDecimal("2")
|
||||
@three = BigDecimal("3")
|
||||
@ten = BigDecimal("10")
|
||||
@eleven = BigDecimal("11")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
end
|
||||
|
||||
it "returns a + b" do
|
||||
(@two + @one).should == @three
|
||||
(@one + @two).should == @three
|
||||
(@one + @one_minus).should == @zero
|
||||
(@zero + @one).should == @one
|
||||
(@ten + @one).should == @eleven
|
||||
(@frac_1 + @frac_2).should == BigDecimal("1.9E-99999")
|
||||
(@frac_2 + @frac_1).should == BigDecimal("1.9E-99999")
|
||||
(@frac_1 + @frac_1).should == BigDecimal("2E-99999")
|
||||
end
|
||||
|
||||
it "returns NaN if NaN is involved" do
|
||||
(@one + @nan).nan?.should == true
|
||||
(@nan + @one).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns Infinity or -Infinity if these are involved" do
|
||||
(@zero + @infinity).should == @infinity
|
||||
(@frac_2 + @infinity).should == @infinity
|
||||
(@two + @infinity_minus).should == @infinity_minus
|
||||
end
|
||||
|
||||
it "returns NaN if Infinity + (- Infinity)" do
|
||||
(@infinity + @infinity_minus).nan?.should == true
|
||||
end
|
||||
|
||||
end
|
6
spec/ruby/library/bigdecimal/power_spec.rb
Normal file
6
spec/ruby/library/bigdecimal/power_spec.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/power', __FILE__)
|
||||
|
||||
describe "BigDecimal#power" do
|
||||
it_behaves_like(:bigdecimal_power, :power)
|
||||
end
|
49
spec/ruby/library/bigdecimal/precs_spec.rb
Normal file
49
spec/ruby/library/bigdecimal/precs_spec.rb
Normal file
|
@ -0,0 +1,49 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#precs" do
|
||||
|
||||
before :each do
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_neg = BigDecimal("-Infinity")
|
||||
@nan = BigDecimal("NaN")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
|
||||
@arr = [BigDecimal("2E40001"), BigDecimal("3E-20001"),\
|
||||
@infinity, @infinity_neg, @nan, @zero, @zero_neg]
|
||||
@precision = BigDecimal::BASE.to_s.length - 1
|
||||
end
|
||||
|
||||
it "returns array of two values" do
|
||||
@arr.each do |x|
|
||||
x.precs.kind_of?(Array).should == true
|
||||
x.precs.size.should == 2
|
||||
end
|
||||
end
|
||||
|
||||
it "returns Integers as array values" do
|
||||
@arr.each do |x|
|
||||
x.precs[0].kind_of?(Integer).should == true
|
||||
x.precs[1].kind_of?(Integer).should == true
|
||||
end
|
||||
end
|
||||
|
||||
it "returns the current value of significant digits as the first value" do
|
||||
BigDecimal("3.14159").precs[0].should >= 6
|
||||
BigDecimal('1').precs[0].should == BigDecimal('1' + '0' * 100).precs[0]
|
||||
[@infinity, @infinity_neg, @nan, @zero, @zero_neg].each do |value|
|
||||
value.precs[0].should <= @precision
|
||||
end
|
||||
end
|
||||
|
||||
it "returns the maximum number of significant digits as the second value" do
|
||||
BigDecimal("3.14159").precs[1].should >= 6
|
||||
BigDecimal('1').precs[1].should >= 1
|
||||
BigDecimal('1' + '0' * 100).precs[1] >= 101
|
||||
[@infinity, @infinity_neg, @nan, @zero, @zero_neg].each do |value|
|
||||
value.precs[1].should >= 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
13
spec/ruby/library/bigdecimal/quo_spec.rb
Normal file
13
spec/ruby/library/bigdecimal/quo_spec.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/quo', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#quo" do
|
||||
it_behaves_like :bigdecimal_quo, :quo, []
|
||||
|
||||
it "returns NaN if NaN is involved" do
|
||||
BigDecimal("1").quo(BigDecimal("NaN")).nan?.should == true
|
||||
BigDecimal("NaN").quo(BigDecimal("1")).nan?.should == true
|
||||
end
|
||||
end
|
||||
|
84
spec/ruby/library/bigdecimal/remainder_spec.rb
Normal file
84
spec/ruby/library/bigdecimal/remainder_spec.rb
Normal file
|
@ -0,0 +1,84 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#remainder" do
|
||||
|
||||
before :each do
|
||||
@zero = BigDecimal("0")
|
||||
@one = BigDecimal("0")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@pos_int = BigDecimal("2E5555")
|
||||
@neg_int = BigDecimal("-2E5555")
|
||||
@pos_frac = BigDecimal("2E-9999")
|
||||
@neg_frac = BigDecimal("-2E-9999")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
end
|
||||
|
||||
it "it equals modulo, if both values are of same sign" do
|
||||
BigDecimal('1234567890123456789012345679').remainder(BigDecimal('1')).should == @zero
|
||||
BigDecimal('123456789').remainder(BigDecimal('333333333333333333333333333E-50')).should == BigDecimal('0.12233333333333333333345679E-24')
|
||||
|
||||
@mixed.remainder(@pos_frac).should == @mixed % @pos_frac
|
||||
@pos_int.remainder(@pos_frac).should == @pos_int % @pos_frac
|
||||
@neg_frac.remainder(@neg_int).should == @neg_frac % @neg_int
|
||||
@neg_int.remainder(@neg_frac).should == @neg_int % @neg_frac
|
||||
end
|
||||
|
||||
it "means self-arg*(self/arg).truncate" do
|
||||
@mixed.remainder(@neg_frac).should == @mixed - @neg_frac * (@mixed / @neg_frac).truncate
|
||||
@pos_int.remainder(@neg_frac).should == @pos_int - @neg_frac * (@pos_int / @neg_frac).truncate
|
||||
@neg_frac.remainder(@pos_int).should == @neg_frac - @pos_int * (@neg_frac / @pos_int).truncate
|
||||
@neg_int.remainder(@pos_frac).should == @neg_int - @pos_frac * (@neg_int / @pos_frac).truncate
|
||||
end
|
||||
|
||||
it "returns NaN used with zero" do
|
||||
@mixed.remainder(@zero).nan?.should == true
|
||||
@zero.remainder(@zero).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns zero if used on zero" do
|
||||
@zero.remainder(@mixed).should == @zero
|
||||
end
|
||||
|
||||
it "returns NaN if NaN is involved" do
|
||||
@nan.remainder(@nan).nan?.should == true
|
||||
@nan.remainder(@one).nan?.should == true
|
||||
@one.remainder(@nan).nan?.should == true
|
||||
@infinity.remainder(@nan).nan?.should == true
|
||||
@nan.remainder(@infinity).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns NaN if Infinity is involved" do
|
||||
@infinity.remainder(@infinity).nan?.should == true
|
||||
@infinity.remainder(@one).nan?.should == true
|
||||
@infinity.remainder(@mixed).nan?.should == true
|
||||
@infinity.remainder(@one_minus).nan?.should == true
|
||||
@infinity.remainder(@frac_1).nan?.should == true
|
||||
@one.remainder(@infinity).nan?.should == true
|
||||
|
||||
@infinity_minus.remainder(@infinity_minus).nan?.should == true
|
||||
@infinity_minus.remainder(@one).nan?.should == true
|
||||
@one.remainder(@infinity_minus).nan?.should == true
|
||||
@frac_2.remainder(@infinity_minus).nan?.should == true
|
||||
|
||||
@infinity.remainder(@infinity_minus).nan?.should == true
|
||||
@infinity_minus.remainder(@infinity).nan?.should == true
|
||||
end
|
||||
|
||||
it "coerces arguments to BigDecimal if possible" do
|
||||
@one.remainder(2).should == @one
|
||||
end
|
||||
|
||||
|
||||
it "raises TypeError if the argument cannot be coerced to BigDecimal" do
|
||||
lambda {
|
||||
@one.remainder('2')
|
||||
}.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
end
|
202
spec/ruby/library/bigdecimal/round_spec.rb
Normal file
202
spec/ruby/library/bigdecimal/round_spec.rb
Normal file
|
@ -0,0 +1,202 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#round" do
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@two = BigDecimal("2")
|
||||
@three = BigDecimal("3")
|
||||
|
||||
@neg_one = BigDecimal("-1")
|
||||
@neg_two = BigDecimal("-2")
|
||||
@neg_three = BigDecimal("-3")
|
||||
|
||||
@p1_50 = BigDecimal("1.50")
|
||||
@p1_51 = BigDecimal("1.51")
|
||||
@p1_49 = BigDecimal("1.49")
|
||||
@n1_50 = BigDecimal("-1.50")
|
||||
@n1_51 = BigDecimal("-1.51")
|
||||
@n1_49 = BigDecimal("-1.49")
|
||||
|
||||
@p2_50 = BigDecimal("2.50")
|
||||
@p2_51 = BigDecimal("2.51")
|
||||
@p2_49 = BigDecimal("2.49")
|
||||
@n2_50 = BigDecimal("-2.50")
|
||||
@n2_51 = BigDecimal("-2.51")
|
||||
@n2_49 = BigDecimal("-2.49")
|
||||
end
|
||||
|
||||
after :each do
|
||||
BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_UP)
|
||||
end
|
||||
|
||||
it "uses default rounding method unless given" do
|
||||
@p1_50.round(0).should == @two
|
||||
@p1_51.round(0).should == @two
|
||||
@p1_49.round(0).should == @one
|
||||
@n1_50.round(0).should == @neg_two
|
||||
@n1_51.round(0).should == @neg_two
|
||||
@n1_49.round(0).should == @neg_one
|
||||
|
||||
@p2_50.round(0).should == @three
|
||||
@p2_51.round(0).should == @three
|
||||
@p2_49.round(0).should == @two
|
||||
@n2_50.round(0).should == @neg_three
|
||||
@n2_51.round(0).should == @neg_three
|
||||
@n2_49.round(0).should == @neg_two
|
||||
|
||||
BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_DOWN)
|
||||
|
||||
@p1_50.round(0).should == @one
|
||||
@p1_51.round(0).should == @one
|
||||
@p1_49.round(0).should == @one
|
||||
@n1_50.round(0).should == @neg_one
|
||||
@n1_51.round(0).should == @neg_one
|
||||
@n1_49.round(0).should == @neg_one
|
||||
|
||||
@p2_50.round(0).should == @two
|
||||
@p2_51.round(0).should == @two
|
||||
@p2_49.round(0).should == @two
|
||||
@n2_50.round(0).should == @neg_two
|
||||
@n2_51.round(0).should == @neg_two
|
||||
@n2_49.round(0).should == @neg_two
|
||||
end
|
||||
|
||||
describe "BigDecimal::ROUND_UP" do
|
||||
it "rounds values away from zero" do
|
||||
@p1_50.round(0, BigDecimal::ROUND_UP).should == @two
|
||||
@p1_51.round(0, BigDecimal::ROUND_UP).should == @two
|
||||
@p1_49.round(0, BigDecimal::ROUND_UP).should == @two
|
||||
@n1_50.round(0, BigDecimal::ROUND_UP).should == @neg_two
|
||||
@n1_51.round(0, BigDecimal::ROUND_UP).should == @neg_two
|
||||
@n1_49.round(0, BigDecimal::ROUND_UP).should == @neg_two
|
||||
|
||||
@p2_50.round(0, BigDecimal::ROUND_UP).should == @three
|
||||
@p2_51.round(0, BigDecimal::ROUND_UP).should == @three
|
||||
@p2_49.round(0, BigDecimal::ROUND_UP).should == @three
|
||||
@n2_50.round(0, BigDecimal::ROUND_UP).should == @neg_three
|
||||
@n2_51.round(0, BigDecimal::ROUND_UP).should == @neg_three
|
||||
@n2_49.round(0, BigDecimal::ROUND_UP).should == @neg_three
|
||||
end
|
||||
end
|
||||
|
||||
describe "BigDecimal::ROUND_DOWN" do
|
||||
it "rounds values towards zero" do
|
||||
@p1_50.round(0, BigDecimal::ROUND_DOWN).should == @one
|
||||
@p1_51.round(0, BigDecimal::ROUND_DOWN).should == @one
|
||||
@p1_49.round(0, BigDecimal::ROUND_DOWN).should == @one
|
||||
@n1_50.round(0, BigDecimal::ROUND_DOWN).should == @neg_one
|
||||
@n1_51.round(0, BigDecimal::ROUND_DOWN).should == @neg_one
|
||||
@n1_49.round(0, BigDecimal::ROUND_DOWN).should == @neg_one
|
||||
|
||||
@p2_50.round(0, BigDecimal::ROUND_DOWN).should == @two
|
||||
@p2_51.round(0, BigDecimal::ROUND_DOWN).should == @two
|
||||
@p2_49.round(0, BigDecimal::ROUND_DOWN).should == @two
|
||||
@n2_50.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
|
||||
@n2_51.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
|
||||
@n2_49.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
|
||||
end
|
||||
end
|
||||
|
||||
describe "BigDecimal::ROUND_HALF_UP" do
|
||||
it "rounds values >= 5 up, otherwise down" do
|
||||
@p1_50.round(0, BigDecimal::ROUND_HALF_UP).should == @two
|
||||
@p1_51.round(0, BigDecimal::ROUND_HALF_UP).should == @two
|
||||
@p1_49.round(0, BigDecimal::ROUND_HALF_UP).should == @one
|
||||
@n1_50.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
|
||||
@n1_51.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
|
||||
@n1_49.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_one
|
||||
|
||||
@p2_50.round(0, BigDecimal::ROUND_HALF_UP).should == @three
|
||||
@p2_51.round(0, BigDecimal::ROUND_HALF_UP).should == @three
|
||||
@p2_49.round(0, BigDecimal::ROUND_HALF_UP).should == @two
|
||||
@n2_50.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_three
|
||||
@n2_51.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_three
|
||||
@n2_49.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
|
||||
end
|
||||
end
|
||||
|
||||
describe "BigDecimal::ROUND_HALF_DOWN" do
|
||||
it "rounds values > 5 up, otherwise down" do
|
||||
@p1_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @one
|
||||
@p1_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
|
||||
@p1_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @one
|
||||
@n1_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_one
|
||||
@n1_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
|
||||
@n1_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_one
|
||||
|
||||
@p2_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
|
||||
@p2_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @three
|
||||
@p2_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
|
||||
@n2_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
|
||||
@n2_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_three
|
||||
@n2_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
|
||||
end
|
||||
end
|
||||
|
||||
describe "BigDecimal::ROUND_CEILING" do
|
||||
it "rounds values towards +infinity" do
|
||||
@p1_50.round(0, BigDecimal::ROUND_CEILING).should == @two
|
||||
@p1_51.round(0, BigDecimal::ROUND_CEILING).should == @two
|
||||
@p1_49.round(0, BigDecimal::ROUND_CEILING).should == @two
|
||||
@n1_50.round(0, BigDecimal::ROUND_CEILING).should == @neg_one
|
||||
@n1_51.round(0, BigDecimal::ROUND_CEILING).should == @neg_one
|
||||
@n1_49.round(0, BigDecimal::ROUND_CEILING).should == @neg_one
|
||||
|
||||
@p2_50.round(0, BigDecimal::ROUND_CEILING).should == @three
|
||||
@p2_51.round(0, BigDecimal::ROUND_CEILING).should == @three
|
||||
@p2_49.round(0, BigDecimal::ROUND_CEILING).should == @three
|
||||
@n2_50.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
|
||||
@n2_51.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
|
||||
@n2_49.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
|
||||
end
|
||||
end
|
||||
|
||||
describe "BigDecimal::ROUND_FLOOR" do
|
||||
it "rounds values towards -infinity" do
|
||||
@p1_50.round(0, BigDecimal::ROUND_FLOOR).should == @one
|
||||
@p1_51.round(0, BigDecimal::ROUND_FLOOR).should == @one
|
||||
@p1_49.round(0, BigDecimal::ROUND_FLOOR).should == @one
|
||||
@n1_50.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two
|
||||
@n1_51.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two
|
||||
@n1_49.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two
|
||||
|
||||
@p2_50.round(0, BigDecimal::ROUND_FLOOR).should == @two
|
||||
@p2_51.round(0, BigDecimal::ROUND_FLOOR).should == @two
|
||||
@p2_49.round(0, BigDecimal::ROUND_FLOOR).should == @two
|
||||
@n2_50.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
|
||||
@n2_51.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
|
||||
@n2_49.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
|
||||
end
|
||||
end
|
||||
|
||||
describe "BigDecimal::ROUND_HALF_EVEN" do
|
||||
it "rounds values > 5 up, < 5 down and == 5 towards even neighbor" do
|
||||
@p1_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
|
||||
@p1_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
|
||||
@p1_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @one
|
||||
@n1_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
|
||||
@n1_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
|
||||
@n1_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_one
|
||||
|
||||
@p2_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
|
||||
@p2_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @three
|
||||
@p2_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
|
||||
@n2_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
|
||||
@n2_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_three
|
||||
@n2_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
|
||||
end
|
||||
end
|
||||
|
||||
it 'raise exception, if self is special value' do
|
||||
lambda { BigDecimal('NaN').round }.should raise_error(FloatDomainError)
|
||||
lambda { BigDecimal('Infinity').round }.should raise_error(FloatDomainError)
|
||||
lambda { BigDecimal('-Infinity').round }.should raise_error(FloatDomainError)
|
||||
end
|
||||
|
||||
it 'do not raise exception, if self is special value and precision is given' do
|
||||
lambda { BigDecimal('NaN').round(2) }.should_not raise_error(FloatDomainError)
|
||||
lambda { BigDecimal('Infinity').round(2) }.should_not raise_error(FloatDomainError)
|
||||
lambda { BigDecimal('-Infinity').round(2) }.should_not raise_error(FloatDomainError)
|
||||
end
|
||||
end
|
61
spec/ruby/library/bigdecimal/shared/eql.rb
Normal file
61
spec/ruby/library/bigdecimal/shared/eql.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_eql, shared: true do
|
||||
before :each do
|
||||
@bg6543_21 = BigDecimal.new("6543.21")
|
||||
@bg5667_19 = BigDecimal.new("5667.19")
|
||||
@a = BigDecimal("1.0000000000000000000000000000000000000000005")
|
||||
@b = BigDecimal("1.00000000000000000000000000000000000000000005")
|
||||
@bigint = BigDecimal("1000.0")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
end
|
||||
|
||||
it "tests for equality" do
|
||||
@bg6543_21.send(@method, @bg6543_21).should == true
|
||||
@a.send(@method, @a).should == true
|
||||
@a.send(@method, @b).should == false
|
||||
@bg6543_21.send(@method, @a).should == false
|
||||
@bigint.send(@method, 1000).should == true
|
||||
end
|
||||
|
||||
it "returns false for NaN as it is never equal to any number" do
|
||||
@nan.send(@method, @nan).should == false
|
||||
@a.send(@method, @nan).should == false
|
||||
@nan.send(@method, @a).should == false
|
||||
@nan.send(@method, @infinity).should == false
|
||||
@nan.send(@method, @infinity_minus).should == false
|
||||
@infinity.send(@method, @nan).should == false
|
||||
@infinity_minus.send(@method, @nan).should == false
|
||||
end
|
||||
|
||||
it "returns true for infinity values with the same sign" do
|
||||
@infinity.send(@method, @infinity).should == true
|
||||
@infinity.send(@method, BigDecimal("Infinity")).should == true
|
||||
BigDecimal("Infinity").send(@method, @infinity).should == true
|
||||
|
||||
@infinity_minus.send(@method, @infinity_minus).should == true
|
||||
@infinity_minus.send(@method, BigDecimal("-Infinity")).should == true
|
||||
BigDecimal("-Infinity").send(@method, @infinity_minus).should == true
|
||||
end
|
||||
|
||||
it "returns false for infinity values with different signs" do
|
||||
@infinity.send(@method, @infinity_minus).should == false
|
||||
@infinity_minus.send(@method, @infinity).should == false
|
||||
end
|
||||
|
||||
it "returns false when infinite value compared to finite one" do
|
||||
@infinity.send(@method, @a).should == false
|
||||
@infinity_minus.send(@method, @a).should == false
|
||||
|
||||
@a.send(@method, @infinity).should == false
|
||||
@a.send(@method, @infinity_minus).should == false
|
||||
end
|
||||
|
||||
it "returns false when compared objects that can not be coerced into BigDecimal" do
|
||||
@infinity.send(@method, nil).should == false
|
||||
@bigint.send(@method, nil).should == false
|
||||
@nan.send(@method, nil).should == false
|
||||
end
|
||||
end
|
116
spec/ruby/library/bigdecimal/shared/modulo.rb
Normal file
116
spec/ruby/library/bigdecimal/shared/modulo.rb
Normal file
|
@ -0,0 +1,116 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_modulo, shared: true do
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@two = BigDecimal("2")
|
||||
@three = BigDecimal("3")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-9999")
|
||||
@frac_2 = BigDecimal("0.9E-9999")
|
||||
end
|
||||
|
||||
it "returns self modulo other" do
|
||||
bd6543 = BigDecimal.new("6543.21")
|
||||
bd5667 = BigDecimal.new("5667.19")
|
||||
a = BigDecimal("1.0000000000000000000000000000000000000000005")
|
||||
b = BigDecimal("1.00000000000000000000000000000000000000000005")
|
||||
|
||||
bd6543.send(@method, 137).should == BigDecimal("104.21")
|
||||
bd5667.send(@method, bignum_value).should == 5667.19
|
||||
bd6543.send(@method, BigDecimal("137.24")).should == BigDecimal("92.93")
|
||||
bd6543.send(@method, 137).should be_close(6543.21.%(137), TOLERANCE)
|
||||
bd6543.send(@method, 137).should == bd6543 % 137
|
||||
bd5667.send(@method, bignum_value).should be_close(5667.19.%(0xffffffff), TOLERANCE)
|
||||
bd5667.send(@method, bignum_value).should == bd5667.%(0xffffffff)
|
||||
bd6543.send(@method, 137.24).should be_close(6543.21.%(137.24), TOLERANCE)
|
||||
a.send(@method, b).should == BigDecimal("0.45E-42")
|
||||
@zero.send(@method, @one).should == @zero
|
||||
@zero.send(@method, @one_minus).should == @zero
|
||||
@two.send(@method, @one).should == @zero
|
||||
@one.send(@method, @two).should == @one
|
||||
@frac_1.send(@method, @one).should == @frac_1
|
||||
@frac_2.send(@method, @one).should == @frac_2
|
||||
@one_minus.send(@method, @one_minus).should == @zero
|
||||
@one_minus.send(@method, @one).should == @zero
|
||||
@one_minus.send(@method, @two).should == @one
|
||||
@one.send(@method, -@two).should == -@one
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.9')).should == BigDecimal('0.8')
|
||||
@one.modulo(BigDecimal('-0.9')).should == BigDecimal('-0.8')
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.8')).should == BigDecimal('0.6')
|
||||
@one.modulo(BigDecimal('-0.8')).should == BigDecimal('-0.6')
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.6')).should == BigDecimal('0.2')
|
||||
@one.modulo(BigDecimal('-0.6')).should == BigDecimal('-0.2')
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.5')).should == @zero
|
||||
@one.modulo(BigDecimal('-0.5')).should == @zero
|
||||
@one_minus.modulo(BigDecimal('-0.5')).should == @zero
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.4')).should == BigDecimal('0.2')
|
||||
@one.modulo(BigDecimal('-0.4')).should == BigDecimal('-0.2')
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.3')).should == BigDecimal('0.2')
|
||||
@one_minus.modulo(BigDecimal('0.2')).should == @zero
|
||||
end
|
||||
|
||||
it "returns a [Float value] when the argument is Float" do
|
||||
@two.send(@method, 2.0).should == 0.0
|
||||
@one.send(@method, 2.0).should == 1.0
|
||||
res = @two.send(@method, 5.0)
|
||||
res.kind_of?(BigDecimal).should == true
|
||||
end
|
||||
|
||||
it "returns NaN if NaN is involved" do
|
||||
@nan.send(@method, @nan).nan?.should == true
|
||||
@nan.send(@method, @one).nan?.should == true
|
||||
@one.send(@method, @nan).nan?.should == true
|
||||
@infinity.send(@method, @nan).nan?.should == true
|
||||
@nan.send(@method, @infinity).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns NaN if the dividend is Infinity" do
|
||||
@infinity.send(@method, @infinity).nan?.should == true
|
||||
@infinity.send(@method, @one).nan?.should == true
|
||||
@infinity.send(@method, @mixed).nan?.should == true
|
||||
@infinity.send(@method, @one_minus).nan?.should == true
|
||||
@infinity.send(@method, @frac_1).nan?.should == true
|
||||
|
||||
@infinity_minus.send(@method, @infinity_minus).nan?.should == true
|
||||
@infinity_minus.send(@method, @one).nan?.should == true
|
||||
|
||||
@infinity.send(@method, @infinity_minus).nan?.should == true
|
||||
@infinity_minus.send(@method, @infinity).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns the dividend if the divisor is Infinity" do
|
||||
@one.send(@method, @infinity).should == @one
|
||||
@one.send(@method, @infinity_minus).should == @one
|
||||
@frac_2.send(@method, @infinity_minus).should == @frac_2
|
||||
end
|
||||
|
||||
it "raises TypeError if the argument cannot be coerced to BigDecimal" do
|
||||
lambda {
|
||||
@one.send(@method, '2')
|
||||
}.should raise_error(TypeError)
|
||||
end
|
||||
end
|
||||
|
||||
describe :bigdecimal_modulo_zerodivisionerror, shared: true do
|
||||
it "raises ZeroDivisionError if other is zero" do
|
||||
bd5667 = BigDecimal.new("5667.19")
|
||||
|
||||
lambda { bd5667.send(@method, 0) }.should raise_error(ZeroDivisionError)
|
||||
lambda { bd5667.send(@method, BigDecimal("0")) }.should raise_error(ZeroDivisionError)
|
||||
lambda { @zero.send(@method, @zero) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
97
spec/ruby/library/bigdecimal/shared/mult.rb
Normal file
97
spec/ruby/library/bigdecimal/shared/mult.rb
Normal file
|
@ -0,0 +1,97 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_mult, shared: true do
|
||||
before :each do
|
||||
@zero = BigDecimal "0"
|
||||
@zero_pos = BigDecimal "+0"
|
||||
@zero_neg = BigDecimal "-0"
|
||||
|
||||
@one = BigDecimal "1"
|
||||
@mixed = BigDecimal "1.23456789"
|
||||
@pos_int = BigDecimal "2E5555"
|
||||
@neg_int = BigDecimal "-2E5555"
|
||||
@pos_frac = BigDecimal "2E-9999"
|
||||
@neg_frac = BigDecimal "-2E-9999"
|
||||
@nan = BigDecimal "NaN"
|
||||
@infinity = BigDecimal "Infinity"
|
||||
@infinity_minus = BigDecimal "-Infinity"
|
||||
@one_minus = BigDecimal "-1"
|
||||
@frac_1 = BigDecimal "1E-99999"
|
||||
@frac_2 = BigDecimal "0.9E-99999"
|
||||
|
||||
@e3_minus = BigDecimal "3E-20001"
|
||||
@e = BigDecimal "1.00000000000000000000123456789"
|
||||
@tolerance = @e.sub @one, 1000
|
||||
@tolerance2 = BigDecimal "30001E-20005"
|
||||
|
||||
@special_vals = [@infinity, @infinity_minus, @nan]
|
||||
@regular_vals = [ @one, @mixed, @pos_int, @neg_int,
|
||||
@pos_frac, @neg_frac, @one_minus,
|
||||
@frac_1, @frac_2
|
||||
]
|
||||
@zeroes = [@zero, @zero_pos, @zero_neg]
|
||||
end
|
||||
|
||||
it "returns zero of appropriate sign if self or argument is zero" do
|
||||
@zero.send(@method, @zero, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@zero_neg.send(@method, @zero_neg, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@zero.send(@method, @zero_neg, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
@zero_neg.send(@method, @zero, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
|
||||
@one.send(@method, @zero, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@one.send(@method, @zero_neg, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
|
||||
@zero.send(@method, @one, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@zero.send(@method, @one_minus, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
@zero_neg.send(@method, @one_minus, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@zero_neg.send(@method, @one, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
end
|
||||
|
||||
it "returns NaN if NaN is involved" do
|
||||
values = @regular_vals + @zeroes
|
||||
|
||||
values.each do |val|
|
||||
@nan.send(@method, val, *@object).nan?.should == true
|
||||
val.send(@method, @nan, *@object).nan?.should == true
|
||||
end
|
||||
end
|
||||
|
||||
it "returns zero if self or argument is zero" do
|
||||
values = @regular_vals + @zeroes
|
||||
|
||||
values.each do |val|
|
||||
@zeroes.each do |zero|
|
||||
zero.send(@method, val, *@object).should == 0
|
||||
zero.send(@method, val, *@object).zero?.should == true
|
||||
val.send(@method, zero, *@object).should == 0
|
||||
val.send(@method, zero, *@object).zero?.should == true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "returns infinite value if self or argument is infinite" do
|
||||
values = @regular_vals
|
||||
infs = [@infinity, @infinity_minus]
|
||||
|
||||
values.each do |val|
|
||||
infs.each do |inf|
|
||||
inf.send(@method, val, *@object).finite?.should == false
|
||||
val.send(@method, inf, *@object).finite?.should == false
|
||||
end
|
||||
end
|
||||
|
||||
@infinity.send(@method, @infinity, *@object).infinite?.should == 1
|
||||
@infinity_minus.send(@method, @infinity_minus, *@object).infinite?.should == 1
|
||||
@infinity.send(@method, @infinity_minus, *@object).infinite?.should == -1
|
||||
@infinity_minus.send(@method, @infinity, *@object).infinite?.should == -1
|
||||
@infinity.send(@method, @one, *@object).infinite?.should == 1
|
||||
@infinity_minus.send(@method, @one, *@object).infinite?.should == -1
|
||||
end
|
||||
|
||||
it "returns NaN if the result is undefined" do
|
||||
@zero.send(@method, @infinity, *@object).nan?.should == true
|
||||
@zero.send(@method, @infinity_minus, *@object).nan?.should == true
|
||||
@infinity.send(@method, @zero, *@object).nan?.should == true
|
||||
@infinity_minus.send(@method, @zero, *@object).nan?.should == true
|
||||
end
|
||||
end
|
72
spec/ruby/library/bigdecimal/shared/power.rb
Normal file
72
spec/ruby/library/bigdecimal/shared/power.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_power, shared: true do
|
||||
it "powers of self" do
|
||||
e3_minus = BigDecimal("3E-20001")
|
||||
e3_minus_power_2 = BigDecimal("9E-40002")
|
||||
e3_plus = BigDecimal("3E20001")
|
||||
e2_plus = BigDecimal("2E40001")
|
||||
e5_minus = BigDecimal("5E-40002")
|
||||
e = BigDecimal("1.00000000000000000000123456789")
|
||||
one = BigDecimal("1")
|
||||
ten = BigDecimal("10")
|
||||
# The tolerance is dependent upon the size of BASE_FIG
|
||||
tolerance = BigDecimal("1E-70")
|
||||
ten_powers = BigDecimal("1E10000")
|
||||
pi = BigDecimal("3.14159265358979")
|
||||
e3_minus.send(@method, 2).should == e3_minus_power_2
|
||||
e3_plus.send(@method, 0).should == 1
|
||||
e3_minus.send(@method, 1).should == e3_minus
|
||||
e2_plus.send(@method, -1).should == e5_minus
|
||||
e2_plus.send(@method, -1).should == e5_minus.power(1)
|
||||
(e2_plus.send(@method, -1) * e5_minus.send(@method, -1)).should == 1
|
||||
e.send(@method, 2).should == e * e
|
||||
e.send(@method, -1).should be_close(one.div(e, 120), tolerance)
|
||||
ten.send(@method, 10000).should == ten_powers
|
||||
pi.send(@method, 10).should be_close(Math::PI ** 10, TOLERANCE)
|
||||
end
|
||||
|
||||
it "powers of 1 equal 1" do
|
||||
one = BigDecimal("1")
|
||||
one.send(@method, 0).should == 1
|
||||
one.send(@method, 1).should == 1
|
||||
one.send(@method, 10).should == 1
|
||||
one.send(@method, -10).should == 1
|
||||
end
|
||||
|
||||
it "0 to power of 0 is 1" do
|
||||
zero = BigDecimal("0")
|
||||
zero.send(@method, 0).should == 1
|
||||
end
|
||||
|
||||
it "0 to powers < 0 is Infinity" do
|
||||
zero = BigDecimal("0")
|
||||
infinity = BigDecimal("Infinity")
|
||||
zero.send(@method, -10).should == infinity
|
||||
zero.send(@method, -1).should == infinity
|
||||
end
|
||||
|
||||
it "other powers of 0 are 0" do
|
||||
zero = BigDecimal("0")
|
||||
zero.send(@method, 1).should == 0
|
||||
zero.send(@method, 10).should == 0
|
||||
end
|
||||
|
||||
it "returns NaN if self is NaN" do
|
||||
BigDecimal("NaN").send(@method, -5).nan?.should == true
|
||||
BigDecimal("NaN").send(@method, 5).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns 0.0 if self is infinite and argument is negative" do
|
||||
BigDecimal("Infinity").send(@method, -5).should == 0
|
||||
BigDecimal("-Infinity").send(@method, -5).should == 0
|
||||
end
|
||||
|
||||
it "returns infinite if self is infinite and argument is positive" do
|
||||
infinity = BigDecimal("Infinity")
|
||||
BigDecimal("Infinity").send(@method, 4).should == infinity
|
||||
BigDecimal("-Infinity").send(@method, 4).should == infinity
|
||||
BigDecimal("Infinity").send(@method, 5).should == infinity
|
||||
BigDecimal("-Infinity").send(@method, 5).should == -infinity
|
||||
end
|
||||
end
|
59
spec/ruby/library/bigdecimal/shared/quo.rb
Normal file
59
spec/ruby/library/bigdecimal/shared/quo.rb
Normal file
|
@ -0,0 +1,59 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_quo, shared: true do
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_plus = BigDecimal("+0")
|
||||
@zero_minus = BigDecimal("-0")
|
||||
@two = BigDecimal("2")
|
||||
@three = BigDecimal("3")
|
||||
@eleven = BigDecimal("11")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
end
|
||||
|
||||
it "returns a / b" do
|
||||
@two.send(@method, @one, *@object).should == @two
|
||||
@one.send(@method, @two, *@object).should == BigDecimal("0.5")
|
||||
@eleven.send(@method, @three, *@object).should be_close(@three + (@two / @three), TOLERANCE)
|
||||
@one.send(@method, @one_minus, *@object).should == @one_minus
|
||||
@one_minus.send(@method, @one_minus, *@object).should == @one
|
||||
@frac_2.send(@method, @frac_1, *@object).should == BigDecimal("0.9")
|
||||
@frac_1.send(@method, @frac_1, *@object).should == @one
|
||||
@one.send(@method, BigDecimal('-2E5555'), *@object).should == BigDecimal('-0.5E-5555')
|
||||
@one.send(@method, BigDecimal('2E-5555'), *@object).should == BigDecimal('0.5E5555')
|
||||
end
|
||||
|
||||
it "returns 0 if divided by Infinity" do
|
||||
@zero.send(@method, @infinity, *@object).should == 0
|
||||
@frac_2.send(@method, @infinity, *@object).should == 0
|
||||
end
|
||||
|
||||
it "returns (+|-) Infinity if (+|-) Infinity divided by one" do
|
||||
@infinity_minus.send(@method, @one, *@object).should == @infinity_minus
|
||||
@infinity.send(@method, @one, *@object).should == @infinity
|
||||
@infinity_minus.send(@method, @one_minus, *@object).should == @infinity
|
||||
end
|
||||
|
||||
it "returns NaN if Infinity / ((+|-) Infinity)" do
|
||||
@infinity.send(@method, @infinity_minus, *@object).nan?.should == true
|
||||
@infinity_minus.send(@method, @infinity, *@object).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns (+|-) Infinity if divided by zero" do
|
||||
@one.send(@method, @zero, *@object).should == @infinity
|
||||
@one.send(@method, @zero_plus, *@object).should == @infinity
|
||||
@one.send(@method, @zero_minus, *@object).should == @infinity_minus
|
||||
end
|
||||
|
||||
it "returns NaN if zero is divided by zero" do
|
||||
@zero.send(@method, @zero, *@object).nan?.should == true
|
||||
@zero_minus.send(@method, @zero_plus, *@object).nan?.should == true
|
||||
@zero_plus.send(@method, @zero_minus, *@object).nan?.should == true
|
||||
end
|
||||
end
|
16
spec/ruby/library/bigdecimal/shared/to_int.rb
Normal file
16
spec/ruby/library/bigdecimal/shared/to_int.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_to_int , shared: true do
|
||||
it "raises FloatDomainError if BigDecimal is infinity or NaN" do
|
||||
lambda { BigDecimal("Infinity").send(@method) }.should raise_error(FloatDomainError)
|
||||
lambda { BigDecimal("NaN").send(@method) }.should raise_error(FloatDomainError)
|
||||
end
|
||||
|
||||
it "returns Integer or Bignum otherwise" do
|
||||
BigDecimal("3E-20001").send(@method).should == 0
|
||||
BigDecimal("2E4000").send(@method).should == 2 * 10 ** 4000
|
||||
BigDecimal("2").send(@method).should == 2
|
||||
BigDecimal("2E10").send(@method).should == 20000000000
|
||||
BigDecimal("3.14159").send(@method).should == 3
|
||||
end
|
||||
end
|
47
spec/ruby/library/bigdecimal/sign_spec.rb
Normal file
47
spec/ruby/library/bigdecimal/sign_spec.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#sign" do
|
||||
|
||||
it "defines several constants for signs" do
|
||||
# are these really correct?
|
||||
BigDecimal::SIGN_POSITIVE_INFINITE.should == 3
|
||||
BigDecimal::SIGN_NEGATIVE_INFINITE.should == -3
|
||||
BigDecimal::SIGN_POSITIVE_ZERO.should == 1
|
||||
BigDecimal::SIGN_NEGATIVE_ZERO.should == -1
|
||||
BigDecimal::SIGN_POSITIVE_FINITE.should == 2
|
||||
BigDecimal::SIGN_NEGATIVE_FINITE.should == -2
|
||||
end
|
||||
|
||||
it "returns positive value if BigDecimal greater than 0" do
|
||||
BigDecimal("1").sign.should == BigDecimal::SIGN_POSITIVE_FINITE
|
||||
BigDecimal("1E-20000000").sign.should == BigDecimal::SIGN_POSITIVE_FINITE
|
||||
BigDecimal("1E200000000").sign.should == BigDecimal::SIGN_POSITIVE_FINITE
|
||||
BigDecimal("Infinity").sign.should == BigDecimal::SIGN_POSITIVE_INFINITE
|
||||
end
|
||||
|
||||
it "returns negative value if BigDecimal less than 0" do
|
||||
BigDecimal("-1").sign.should == BigDecimal::SIGN_NEGATIVE_FINITE
|
||||
BigDecimal("-1E-9990000").sign.should == BigDecimal::SIGN_NEGATIVE_FINITE
|
||||
BigDecimal("-1E20000000").sign.should == BigDecimal::SIGN_NEGATIVE_FINITE
|
||||
BigDecimal("-Infinity").sign.should == BigDecimal::SIGN_NEGATIVE_INFINITE
|
||||
end
|
||||
|
||||
it "returns positive zero if BigDecimal equals positve zero" do
|
||||
BigDecimal("0").sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
BigDecimal("0E-200000000").sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
BigDecimal("0E200000000").sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
end
|
||||
|
||||
it "returns negative zero if BigDecimal equals negative zero" do
|
||||
BigDecimal("-0").sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
BigDecimal("-0E-200000000").sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
BigDecimal("-0E200000000").sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
end
|
||||
|
||||
it "returns BigDecimal::SIGN_NaN if BigDecimal is NaN" do
|
||||
BigDecimal("NaN").sign.should == BigDecimal::SIGN_NaN
|
||||
end
|
||||
|
||||
end
|
||||
|
88
spec/ruby/library/bigdecimal/split_spec.rb
Normal file
88
spec/ruby/library/bigdecimal/split_spec.rb
Normal file
|
@ -0,0 +1,88 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#split" do
|
||||
|
||||
before :each do
|
||||
@arr = BigDecimal("0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043E1").split
|
||||
@arr_neg = BigDecimal("-0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043E1").split
|
||||
@digits = "922337203685477580810101333333333333333333333333333"
|
||||
@arr_big = BigDecimal("00#{@digits}000").split
|
||||
@arr_big_neg = BigDecimal("-00#{@digits}000").split
|
||||
@huge = BigDecimal('100000000000000000000000000000000000000000001E90000000').split
|
||||
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_neg = BigDecimal("-Infinity")
|
||||
@nan = BigDecimal("NaN")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
end
|
||||
|
||||
it "splits BigDecimal in an array with four values" do
|
||||
@arr.size.should == 4
|
||||
end
|
||||
|
||||
it "first value: 1 for numbers > 0" do
|
||||
@arr[0].should == 1
|
||||
@arr_big[0].should == 1
|
||||
@zero.split[0].should == 1
|
||||
@huge[0].should == 1
|
||||
BigDecimal("+0").split[0].should == 1
|
||||
BigDecimal("1E400").split[0].should == 1
|
||||
@infinity.split[0].should == 1
|
||||
end
|
||||
|
||||
it "first value: -1 for numbers < 0" do
|
||||
@arr_neg[0].should == -1
|
||||
@arr_big_neg[0].should == -1
|
||||
@zero_neg.split[0].should == -1
|
||||
BigDecimal("-1E400").split[0].should == -1
|
||||
@infinity_neg.split[0].should == -1
|
||||
end
|
||||
|
||||
it "first value: 0 if BigDecimal is NaN" do
|
||||
BigDecimal("NaN").split[0].should == 0
|
||||
end
|
||||
|
||||
it "second value: a string with the significant digits" do
|
||||
string = "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043"
|
||||
@arr[1].should == string
|
||||
@arr_big[1].should == @digits
|
||||
@arr_big_neg[1].should == @digits
|
||||
@huge[1].should == "100000000000000000000000000000000000000000001"
|
||||
@infinity.split[1].should == @infinity.to_s
|
||||
@nan.split[1].should == @nan.to_s
|
||||
@infinity_neg.split[1].should == @infinity.to_s
|
||||
@zero.split[1].should == "0"
|
||||
BigDecimal("-0").split[1].should == "0"
|
||||
end
|
||||
|
||||
it "third value: the base (currently always ten)" do
|
||||
@arr[2].should == 10
|
||||
@arr_neg[2].should == 10
|
||||
@arr_big[2].should == 10
|
||||
@arr_big_neg[2].should == 10
|
||||
@huge[2].should == 10
|
||||
@infinity.split[2].should == 10
|
||||
@nan.split[2].should == 10
|
||||
@infinity_neg.split[2].should == 10
|
||||
@zero.split[2].should == 10
|
||||
@zero_neg.split[2].should == 10
|
||||
end
|
||||
|
||||
it "fourth value: the exponent" do
|
||||
@arr[3].should == 1
|
||||
@arr_neg[3].should == 1
|
||||
@arr_big[3].should == 54
|
||||
@arr_big_neg[3].should == 54
|
||||
@huge[3].should == 90000045
|
||||
@infinity.split[3].should == 0
|
||||
@nan.split[3].should == 0
|
||||
@infinity_neg.split[3].should == 0
|
||||
@zero.split[3].should == 0
|
||||
@zero_neg.split[3].should == 0
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
112
spec/ruby/library/bigdecimal/sqrt_spec.rb
Normal file
112
spec/ruby/library/bigdecimal/sqrt_spec.rb
Normal file
|
@ -0,0 +1,112 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/classes', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#sqrt" do
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@two = BigDecimal("2.0")
|
||||
@three = BigDecimal("3.0")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
end
|
||||
|
||||
it "returns square root of 2 with desired precision" do
|
||||
string = "1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157"
|
||||
(1..99).each { |idx|
|
||||
@two.sqrt(idx).should be_close(BigDecimal(string), BigDecimal("1E-#{idx-1}"))
|
||||
}
|
||||
end
|
||||
|
||||
it "returns square root of 3 with desired precision" do
|
||||
sqrt_3 = "1.732050807568877293527446341505872366942805253810380628055806979451933016908800037081146186757248575"
|
||||
(1..99).each { |idx|
|
||||
@three.sqrt(idx).should be_close(BigDecimal(sqrt_3), BigDecimal("1E-#{idx-1}"))
|
||||
}
|
||||
end
|
||||
|
||||
it "returns square root of 121 with desired precision" do
|
||||
BigDecimal('121').sqrt(5).should be_close(11, 0.00001)
|
||||
end
|
||||
|
||||
it "returns square root of 0.9E-99999 with desired precision" do
|
||||
@frac_2.sqrt(1).to_s.should =~ /\A0\.3E-49999\z/i
|
||||
end
|
||||
|
||||
it "raises ArgumentError when no argument is given" do
|
||||
lambda {
|
||||
@one.sqrt
|
||||
}.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "raises ArgumentError if a negative number is given" do
|
||||
lambda {
|
||||
@one.sqrt(-1)
|
||||
}.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "raises ArgumentError if 2 arguments are given" do
|
||||
lambda {
|
||||
@one.sqrt(1, 1)
|
||||
}.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "raises TypeError if nil is given" do
|
||||
lambda {
|
||||
@one.sqrt(nil)
|
||||
}.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises TypeError if a string is given" do
|
||||
lambda {
|
||||
@one.sqrt("stuff")
|
||||
}.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises TypeError if a plain Object is given" do
|
||||
lambda {
|
||||
@one.sqrt(Object.new)
|
||||
}.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "returns 1 if precision is 0 or 1" do
|
||||
@one.sqrt(1).should == 1
|
||||
@one.sqrt(0).should == 1
|
||||
end
|
||||
|
||||
it "raises FloatDomainError on negative values" do
|
||||
lambda {
|
||||
BigDecimal('-1').sqrt(10)
|
||||
}.should raise_error(FloatDomainError)
|
||||
end
|
||||
|
||||
it "returns positive infitinity for infinity" do
|
||||
@infinity.sqrt(1).should == @infinity
|
||||
end
|
||||
|
||||
it "raises FloatDomainError for negative infinity" do
|
||||
lambda {
|
||||
@infinity_minus.sqrt(1)
|
||||
}.should raise_error(FloatDomainError)
|
||||
end
|
||||
|
||||
it "raises FloatDomainError for NaN" do
|
||||
lambda {
|
||||
@nan.sqrt(1)
|
||||
}.should raise_error(FloatDomainError)
|
||||
end
|
||||
|
||||
it "returns 0 for 0, +0.0 and -0.0" do
|
||||
@zero.sqrt(1).should == 0
|
||||
@zero_pos.sqrt(1).should == 0
|
||||
@zero_neg.sqrt(1).should == 0
|
||||
end
|
||||
|
||||
end
|
53
spec/ruby/library/bigdecimal/sub_spec.rb
Normal file
53
spec/ruby/library/bigdecimal/sub_spec.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#sub" do
|
||||
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@two = BigDecimal("2")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
end
|
||||
|
||||
it "returns a - b with given precision" do
|
||||
# documentation states, that precision is optional
|
||||
# but implementation raises ArgumentError if not given.
|
||||
|
||||
@two.sub(@one, 1).should == @one
|
||||
@one.sub(@two, 1).should == @one_minus
|
||||
@one.sub(@one_minus, 1).should == @two
|
||||
@frac_2.sub(@frac_1, 1000000).should == BigDecimal("-0.1E-99999")
|
||||
@frac_2.sub(@frac_1, 1).should == BigDecimal("-0.1E-99999")
|
||||
# the above two examples puzzle me.
|
||||
in_arow_one = BigDecimal("1.23456789")
|
||||
in_arow_two = BigDecimal("1.2345678")
|
||||
in_arow_one.sub(in_arow_two, 10).should == BigDecimal("0.9E-7")
|
||||
@two.sub(@two,1).should == @zero
|
||||
@frac_1.sub(@frac_1, 1000000).should == @zero
|
||||
end
|
||||
|
||||
it "returns NaN if NaN is involved" do
|
||||
@one.sub(@nan, 1).nan?.should == true
|
||||
@nan.sub(@one, 1).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns NaN if both values are infinite with the same signs" do
|
||||
@infinity.sub(@infinity, 1).nan?.should == true
|
||||
@infinity_minus.sub(@infinity_minus, 1).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns Infinity or -Infinity if these are involved" do
|
||||
@infinity.sub(@infinity_minus, 1).should == @infinity
|
||||
@infinity_minus.sub(@infinity, 1).should == @infinity_minus
|
||||
@zero.sub(@infinity, 1).should == @infinity_minus
|
||||
@frac_2.sub( @infinity, 1).should == @infinity_minus
|
||||
@two.sub(@infinity, 1).should == @infinity_minus
|
||||
end
|
||||
|
||||
end
|
55
spec/ruby/library/bigdecimal/to_f_spec.rb
Normal file
55
spec/ruby/library/bigdecimal/to_f_spec.rb
Normal file
|
@ -0,0 +1,55 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#to_f" do
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@two = BigDecimal("2")
|
||||
@three = BigDecimal("3")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
@vals = [@one, @zero, @two, @three, @frac_1, @frac_2]
|
||||
@spec_vals = [@zero_pos, @zero_neg, @nan, @infinity, @infinity_minus]
|
||||
end
|
||||
|
||||
it "returns number of type float" do
|
||||
BigDecimal("3.14159").to_f.should be_kind_of(Float)
|
||||
@vals.each { |val| val.to_f.should be_kind_of(Float) }
|
||||
@spec_vals.each { |val| val.to_f.should be_kind_of(Float) }
|
||||
end
|
||||
|
||||
it "rounds correctly to Float precision" do
|
||||
bigdec = BigDecimal("3.141592653589793238462643383279502884197169399375")
|
||||
bigdec.to_f.should be_close(3.14159265358979, TOLERANCE)
|
||||
@one.to_f.should == 1.0
|
||||
@two.to_f.should == 2.0
|
||||
@three.to_f.should be_close(3.0, TOLERANCE)
|
||||
@one_minus.to_f.should == -1.0
|
||||
|
||||
# regression test for [ruby-talk:338957]
|
||||
BigDecimal("10.03").to_f.should == 10.03
|
||||
end
|
||||
|
||||
it "properly handles special values" do
|
||||
@zero.to_f.should == 0
|
||||
@zero.to_f.to_s.should == "0.0"
|
||||
|
||||
@nan.to_f.nan?.should == true
|
||||
|
||||
@infinity.to_f.infinite?.should == 1
|
||||
@infinity_minus.to_f.infinite?.should == -1
|
||||
end
|
||||
|
||||
it "remembers negative zero when converted to float" do
|
||||
@zero_neg.to_f.should == 0
|
||||
@zero_neg.to_f.to_s.should == "-0.0"
|
||||
end
|
||||
end
|
||||
|
7
spec/ruby/library/bigdecimal/to_i_spec.rb
Normal file
7
spec/ruby/library/bigdecimal/to_i_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/to_int', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#to_i" do
|
||||
it_behaves_like(:bigdecimal_to_int, :to_i)
|
||||
end
|
8
spec/ruby/library/bigdecimal/to_int_spec.rb
Normal file
8
spec/ruby/library/bigdecimal/to_int_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/to_int', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
|
||||
describe "BigDecimal#to_int" do
|
||||
it_behaves_like(:bigdecimal_to_int, :to_int)
|
||||
end
|
16
spec/ruby/library/bigdecimal/to_r_spec.rb
Normal file
16
spec/ruby/library/bigdecimal/to_r_spec.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#to_r" do
|
||||
|
||||
it "returns a Rational" do
|
||||
BigDecimal("3.14159").to_r.should be_kind_of(Rational)
|
||||
end
|
||||
|
||||
it "returns a Rational with bignum values" do
|
||||
r = BigDecimal.new("3.141592653589793238462643").to_r
|
||||
r.numerator.should eql(3141592653589793238462643)
|
||||
r.denominator.should eql(1000000000000000000000000)
|
||||
end
|
||||
|
||||
end
|
73
spec/ruby/library/bigdecimal/to_s_spec.rb
Normal file
73
spec/ruby/library/bigdecimal/to_s_spec.rb
Normal file
|
@ -0,0 +1,73 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#to_s" do
|
||||
|
||||
before :each do
|
||||
@bigdec_str = "3.14159265358979323846264338327950288419716939937"
|
||||
@bigneg_str = "-3.1415926535897932384626433832795028841971693993"
|
||||
@bigdec = BigDecimal(@bigdec_str)
|
||||
@bigneg = BigDecimal(@bigneg_str)
|
||||
end
|
||||
|
||||
it "return type is of class String" do
|
||||
@bigdec.to_s.kind_of?(String).should == true
|
||||
@bigneg.to_s.kind_of?(String).should == true
|
||||
end
|
||||
|
||||
it "the default format looks like 0.xxxxEnn" do
|
||||
@bigdec.to_s.should =~ /^0\.[0-9]*E[0-9]*$/i
|
||||
end
|
||||
|
||||
it "takes an optional argument" do
|
||||
lambda {@bigdec.to_s("F")}.should_not raise_error()
|
||||
end
|
||||
|
||||
it "starts with + if + is supplied and value is positive" do
|
||||
@bigdec.to_s("+").should =~ /^\+.*/
|
||||
@bigneg.to_s("+").should_not =~ /^\+.*/
|
||||
end
|
||||
|
||||
it "inserts a space every n chars, if integer n is supplied" do
|
||||
re =\
|
||||
/\A0\.314 159 265 358 979 323 846 264 338 327 950 288 419 716 939 937E1\z/i
|
||||
@bigdec.to_s(3).should =~ re
|
||||
|
||||
str1 = '-123.45678 90123 45678 9'
|
||||
BigDecimal.new("-123.45678901234567890").to_s('5F').should == str1
|
||||
# trailing zeroes removed
|
||||
BigDecimal.new("1.00000000000").to_s('1F').should == "1.0"
|
||||
# 0 is treated as no spaces
|
||||
BigDecimal.new("1.2345").to_s('0F').should == "1.2345"
|
||||
end
|
||||
|
||||
it "can return a leading space for values > 0" do
|
||||
@bigdec.to_s(" F").should =~ /\ .*/
|
||||
@bigneg.to_s(" F").should_not =~ /\ .*/
|
||||
end
|
||||
|
||||
it "removes trailing spaces in floating point notation" do
|
||||
BigDecimal.new('-123.45678901234567890').to_s('F').should == "-123.4567890123456789"
|
||||
BigDecimal.new('1.2500').to_s('F').should == "1.25"
|
||||
BigDecimal.new('0000.00000').to_s('F').should == "0.0"
|
||||
BigDecimal.new('-00.000010000').to_s('F').should == "-0.00001"
|
||||
BigDecimal.new("5.00000E-2").to_s("F").should == "0.05"
|
||||
|
||||
BigDecimal.new("500000").to_s("F").should == "500000.0"
|
||||
BigDecimal.new("5E2").to_s("F").should == "500.0"
|
||||
BigDecimal.new("-5E100").to_s("F").should == "-5" + "0" * 100 + ".0"
|
||||
end
|
||||
|
||||
it "can use engineering notation" do
|
||||
@bigdec.to_s("E").should =~ /^0\.[0-9]*E[0-9]*$/i
|
||||
end
|
||||
|
||||
it "can use conventional floating point notation" do
|
||||
@bigdec.to_s("F").should == @bigdec_str
|
||||
@bigneg.to_s("F").should == @bigneg_str
|
||||
str2 = "+123.45678901 23456789"
|
||||
BigDecimal.new('123.45678901234567890').to_s('+8F').should == str2
|
||||
end
|
||||
|
||||
end
|
||||
|
81
spec/ruby/library/bigdecimal/truncate_spec.rb
Normal file
81
spec/ruby/library/bigdecimal/truncate_spec.rb
Normal file
|
@ -0,0 +1,81 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#truncate" do
|
||||
|
||||
before :each do
|
||||
@arr = ['3.14159', '8.7', "0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043E1"]
|
||||
@big = BigDecimal("123456.789")
|
||||
@nan = BigDecimal('NaN')
|
||||
@infinity = BigDecimal('Infinity')
|
||||
@infinity_negative = BigDecimal('-Infinity')
|
||||
end
|
||||
|
||||
it "returns value of type Integer." do
|
||||
@arr.each do |x|
|
||||
BigDecimal(x).truncate.kind_of?(Integer).should == true
|
||||
end
|
||||
end
|
||||
|
||||
it "returns the integer part as a BigDecimal if no precision given" do
|
||||
BigDecimal(@arr[0]).truncate.should == 3
|
||||
BigDecimal(@arr[1]).truncate.should == 8
|
||||
BigDecimal(@arr[2]).truncate.should == 3
|
||||
BigDecimal('0').truncate.should == 0
|
||||
BigDecimal('0.1').truncate.should == 0
|
||||
BigDecimal('-0.1').truncate.should == 0
|
||||
BigDecimal('1.5').truncate.should == 1
|
||||
BigDecimal('-1.5').truncate.should == -1
|
||||
BigDecimal('1E10').truncate.should == BigDecimal('1E10')
|
||||
BigDecimal('-1E10').truncate.should == BigDecimal('-1E10')
|
||||
BigDecimal('1.8888E10').truncate.should == BigDecimal('1.8888E10')
|
||||
BigDecimal('-1E-1').truncate.should == 0
|
||||
end
|
||||
|
||||
it "returns value of given precision otherwise" do
|
||||
BigDecimal('-1.55').truncate(1).should == BigDecimal('-1.5')
|
||||
BigDecimal('1.55').truncate(1).should == BigDecimal('1.5')
|
||||
BigDecimal(@arr[0]).truncate(2).should == BigDecimal("3.14")
|
||||
BigDecimal('123.456').truncate(2).should == BigDecimal("123.45")
|
||||
BigDecimal('123.456789').truncate(4).should == BigDecimal("123.4567")
|
||||
BigDecimal('0.456789').truncate(10).should == BigDecimal("0.456789")
|
||||
BigDecimal('-1E-1').truncate(1).should == BigDecimal('-0.1')
|
||||
BigDecimal('-1E-1').truncate(2).should == BigDecimal('-0.1E0')
|
||||
BigDecimal('-1E-1').truncate.should == BigDecimal('0')
|
||||
BigDecimal('-1E-1').truncate(0).should == BigDecimal('0')
|
||||
BigDecimal('-1E-1').truncate(-1).should == BigDecimal('0')
|
||||
BigDecimal('-1E-1').truncate(-2).should == BigDecimal('0')
|
||||
|
||||
BigDecimal(@arr[1]).truncate(1).should == BigDecimal("8.7")
|
||||
BigDecimal(@arr[2]).truncate(100).should == BigDecimal(\
|
||||
"3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679")
|
||||
end
|
||||
|
||||
it "sets n digits left of the decimal point to 0, if given n < 0" do
|
||||
@big.truncate(-1).should == BigDecimal("123450.0")
|
||||
@big.truncate(-2).should == BigDecimal("123400.0")
|
||||
BigDecimal(@arr[2]).truncate(-1).should == 0
|
||||
end
|
||||
|
||||
it "returns NaN if self is NaN" do
|
||||
@nan.truncate(-1).nan?.should == true
|
||||
@nan.truncate(+1).nan?.should == true
|
||||
@nan.truncate(0).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns Infinity if self is infinite" do
|
||||
@infinity.truncate(-1).should == @infinity
|
||||
@infinity.truncate(+1).should == @infinity
|
||||
@infinity.truncate(0).should == @infinity
|
||||
|
||||
@infinity_negative.truncate(-1).should == @infinity_negative
|
||||
@infinity_negative.truncate(+1).should == @infinity_negative
|
||||
@infinity_negative.truncate(0).should == @infinity_negative
|
||||
end
|
||||
|
||||
it "returns the same value if self is special value" do
|
||||
lambda { @nan.truncate }.should raise_error(FloatDomainError)
|
||||
lambda { @infinity.truncate }.should raise_error(FloatDomainError)
|
||||
lambda { @infinity_negative.truncate }.should raise_error(FloatDomainError)
|
||||
end
|
||||
end
|
58
spec/ruby/library/bigdecimal/uminus_spec.rb
Normal file
58
spec/ruby/library/bigdecimal/uminus_spec.rb
Normal file
|
@ -0,0 +1,58 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#-@" do
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
@big = BigDecimal("333E99999")
|
||||
@big_neg = BigDecimal("-333E99999")
|
||||
@values = [@one, @zero, @zero_pos, @zero_neg, @infinity,
|
||||
@infinity_minus, @one_minus, @frac_1, @frac_2, @big, @big_neg]
|
||||
end
|
||||
|
||||
it "negates self" do
|
||||
@one.send(:-@).should == @one_minus
|
||||
@one_minus.send(:-@).should == @one
|
||||
@frac_1.send(:-@).should == BigDecimal("-1E-99999")
|
||||
@frac_2.send(:-@).should == BigDecimal("-0.9E-99999")
|
||||
@big.send(:-@).should == @big_neg
|
||||
@big_neg.send(:-@).should == @big
|
||||
BigDecimal("2.221").send(:-@).should == BigDecimal("-2.221")
|
||||
BigDecimal("2E10000").send(:-@).should == BigDecimal("-2E10000")
|
||||
some_number = BigDecimal("2455999221.5512")
|
||||
some_number_neg = BigDecimal("-2455999221.5512")
|
||||
some_number.send(:-@).should == some_number_neg
|
||||
(-BigDecimal("-5.5")).should == BigDecimal("5.5")
|
||||
another_number = BigDecimal("-8.551551551551551551")
|
||||
another_number_pos = BigDecimal("8.551551551551551551")
|
||||
another_number.send(:-@).should == another_number_pos
|
||||
@values.each do |val|
|
||||
(val.send(:-@).send(:-@)).should == val
|
||||
end
|
||||
end
|
||||
|
||||
it "properly handles special values" do
|
||||
@infinity.send(:-@).should == @infinity_minus
|
||||
@infinity_minus.send(:-@).should == @infinity
|
||||
@infinity.send(:-@).infinite?.should == -1
|
||||
@infinity_minus.send(:-@).infinite?.should == 1
|
||||
|
||||
@zero.send(:-@).should == @zero
|
||||
@zero.send(:-@).sign.should == -1
|
||||
@zero_pos.send(:-@).should == @zero
|
||||
@zero_pos.send(:-@).sign.should == -1
|
||||
@zero_neg.send(:-@).should == @zero
|
||||
@zero_neg.send(:-@).sign.should == 1
|
||||
|
||||
@nan.send(:-@).nan?.should == true
|
||||
end
|
||||
end
|
20
spec/ruby/library/bigdecimal/uplus_spec.rb
Normal file
20
spec/ruby/library/bigdecimal/uplus_spec.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#+@" do
|
||||
it "returns the same value with same sign (twos complement)" do
|
||||
first = BigDecimal("34.56")
|
||||
first.send(:+@).should == first
|
||||
second = BigDecimal("-34.56")
|
||||
second.send(:+@).should == second
|
||||
third = BigDecimal("0.0")
|
||||
third.send(:+@).should == third
|
||||
fourth = BigDecimal("2E1000000")
|
||||
fourth.send(:+@).should == fourth
|
||||
fifth = BigDecimal("123456789E-1000000")
|
||||
fifth.send(:+@).should == fifth
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
11
spec/ruby/library/bigdecimal/ver_spec.rb
Normal file
11
spec/ruby/library/bigdecimal/ver_spec.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal.ver" do
|
||||
|
||||
it "returns the Version number" do
|
||||
lambda {BigDecimal.ver }.should_not raise_error()
|
||||
BigDecimal.ver.should_not == nil
|
||||
end
|
||||
|
||||
end
|
28
spec/ruby/library/bigdecimal/zero_spec.rb
Normal file
28
spec/ruby/library/bigdecimal/zero_spec.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#zero?" do
|
||||
|
||||
it "returns true if self does equal zero" do
|
||||
really_small_zero = BigDecimal("0E-200000000")
|
||||
really_big_zero = BigDecimal("0E200000000000")
|
||||
really_small_zero.zero?.should == true
|
||||
really_big_zero.zero?.should == true
|
||||
BigDecimal("0.000000000000000000000000").zero?.should == true
|
||||
BigDecimal("0").zero?.should == true
|
||||
BigDecimal("0E0").zero?.should == true
|
||||
BigDecimal("+0").zero?.should == true
|
||||
BigDecimal("-0").zero?.should == true
|
||||
end
|
||||
|
||||
it "returns false otherwise" do
|
||||
BigDecimal("0000000001").zero?.should == false
|
||||
BigDecimal("2E40001").zero?.should == false
|
||||
BigDecimal("3E-20001").zero?.should == false
|
||||
BigDecimal("Infinity").zero?.should == false
|
||||
BigDecimal("-Infinity").zero?.should == false
|
||||
BigDecimal("NaN").zero?.should == false
|
||||
end
|
||||
|
||||
end
|
||||
|
10
spec/ruby/library/bigmath/log_spec.rb
Normal file
10
spec/ruby/library/bigmath/log_spec.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'bigdecimal'
|
||||
|
||||
describe "BigDecimal#log" do
|
||||
it "handles high-precision Rational arguments" do
|
||||
result = BigDecimal('0.22314354220170971436137296411949880462556361100856391620766259404746040597133837784E0')
|
||||
r = Rational(1_234_567_890, 987_654_321)
|
||||
BigMath.log(r, 50).should == result
|
||||
end
|
||||
end
|
23
spec/ruby/library/cgi/cookie/domain_spec.rb
Normal file
23
spec/ruby/library/cgi/cookie/domain_spec.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI::Cookie#domain" do
|
||||
it "returns self's domain" do
|
||||
cookie = CGI::Cookie.new("test-cookie")
|
||||
cookie.domain.should be_nil
|
||||
|
||||
cookie = CGI::Cookie.new("name" => "test-cookie", "domain" => "example.com")
|
||||
cookie.domain.should == "example.com"
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::Cookie#domain=" do
|
||||
it "sets self's domain" do
|
||||
cookie = CGI::Cookie.new("test-cookie")
|
||||
cookie.domain = "test.com"
|
||||
cookie.domain.should == "test.com"
|
||||
|
||||
cookie.domain = "example.com"
|
||||
cookie.domain.should == "example.com"
|
||||
end
|
||||
end
|
23
spec/ruby/library/cgi/cookie/expires_spec.rb
Normal file
23
spec/ruby/library/cgi/cookie/expires_spec.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI::Cookie#expires" do
|
||||
it "returns self's expiration date" do
|
||||
cookie = CGI::Cookie.new("test-cookie")
|
||||
cookie.expires.should be_nil
|
||||
|
||||
cookie = CGI::Cookie.new("name" => "test-cookie", "expires" => Time.at(1196524602))
|
||||
cookie.expires.should == Time.at(1196524602)
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::Cookie#expires=" do
|
||||
it "sets self's expiration date" do
|
||||
cookie = CGI::Cookie.new("test-cookie")
|
||||
cookie.expires = Time.at(1196524602)
|
||||
cookie.expires.should == Time.at(1196524602)
|
||||
|
||||
cookie.expires = Time.at(1196525000)
|
||||
cookie.expires.should == Time.at(1196525000)
|
||||
end
|
||||
end
|
147
spec/ruby/library/cgi/cookie/initialize_spec.rb
Normal file
147
spec/ruby/library/cgi/cookie/initialize_spec.rb
Normal file
|
@ -0,0 +1,147 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI::Cookie#initialize when passed String" do
|
||||
before :each do
|
||||
@cookie = CGI::Cookie.allocate
|
||||
end
|
||||
|
||||
it "sets the self's name to the passed String" do
|
||||
@cookie.send(:initialize, "test-cookie")
|
||||
@cookie.name.should == "test-cookie"
|
||||
end
|
||||
|
||||
it "sets the self's value to an empty Array" do
|
||||
@cookie.send(:initialize, "test-cookie")
|
||||
@cookie.value.should == []
|
||||
end
|
||||
|
||||
it "sets self to a non-secure cookie" do
|
||||
@cookie.send(:initialize, "test")
|
||||
@cookie.secure.should be_false
|
||||
end
|
||||
|
||||
it "does set self's path to an empty String when ENV[\"SCRIPT_NAME\"] is not set" do
|
||||
@cookie.send(:initialize, "test-cookie")
|
||||
@cookie.path.should == ""
|
||||
end
|
||||
|
||||
it "does set self's path based on ENV[\"SCRIPT_NAME\"] when ENV[\"SCRIPT_NAME\"] is set" do
|
||||
old_script_name = ENV["SCRIPT_NAME"]
|
||||
|
||||
begin
|
||||
ENV["SCRIPT_NAME"] = "some/path/script.rb"
|
||||
@cookie.send(:initialize, "test-cookie")
|
||||
@cookie.path.should == "some/path/"
|
||||
|
||||
ENV["SCRIPT_NAME"] = "script.rb"
|
||||
@cookie.send(:initialize, "test-cookie")
|
||||
@cookie.path.should == ""
|
||||
|
||||
ENV["SCRIPT_NAME"] = nil
|
||||
@cookie.send(:initialize, "test-cookie")
|
||||
@cookie.path.should == ""
|
||||
ensure
|
||||
ENV["SCRIPT_NAME"] = old_script_name
|
||||
end
|
||||
end
|
||||
|
||||
it "does not set self's expiration date" do
|
||||
@cookie.expires.should be_nil
|
||||
end
|
||||
|
||||
it "does not set self's domain" do
|
||||
@cookie.domain.should be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::Cookie#initialize when passed Hash" do
|
||||
before :each do
|
||||
@cookie = CGI::Cookie.allocate
|
||||
end
|
||||
|
||||
it "sets self's contents based on the passed Hash" do
|
||||
@cookie.send(:initialize,
|
||||
'name' => 'test-cookie',
|
||||
'value' => ["one", "two", "three"],
|
||||
'path' => 'some/path/',
|
||||
'domain' => 'example.com',
|
||||
'expires' => Time.at(1196524602),
|
||||
'secure' => true)
|
||||
|
||||
@cookie.name.should == "test-cookie"
|
||||
@cookie.value.should == ["one", "two", "three"]
|
||||
@cookie.path.should == "some/path/"
|
||||
@cookie.domain.should == "example.com"
|
||||
@cookie.expires.should == Time.at(1196524602)
|
||||
@cookie.secure.should be_true
|
||||
end
|
||||
|
||||
it "does set self's path based on ENV[\"SCRIPT_NAME\"] when the Hash has no 'path' entry" do
|
||||
old_script_name = ENV["SCRIPT_NAME"]
|
||||
|
||||
begin
|
||||
ENV["SCRIPT_NAME"] = "some/path/script.rb"
|
||||
@cookie.send(:initialize, 'name' => 'test-cookie')
|
||||
@cookie.path.should == "some/path/"
|
||||
|
||||
ENV["SCRIPT_NAME"] = "script.rb"
|
||||
@cookie.send(:initialize, 'name' => 'test-cookie')
|
||||
@cookie.path.should == ""
|
||||
|
||||
ENV["SCRIPT_NAME"] = nil
|
||||
@cookie.send(:initialize, 'name' => 'test-cookie')
|
||||
@cookie.path.should == ""
|
||||
ensure
|
||||
ENV["SCRIPT_NAME"] = old_script_name
|
||||
end
|
||||
end
|
||||
|
||||
it "tries to convert the Hash's 'value' to an Array using #Array" do
|
||||
obj = mock("Converted To Array")
|
||||
obj.should_receive(:to_ary).and_return(["1", "2", "3"])
|
||||
@cookie.send(:initialize,
|
||||
'name' => 'test-cookie',
|
||||
'value' => obj)
|
||||
@cookie.value.should == [ "1", "2", "3" ]
|
||||
|
||||
obj = mock("Converted To Array")
|
||||
obj.should_receive(:to_a).and_return(["one", "two", "three"])
|
||||
@cookie.send(:initialize,
|
||||
'name' => 'test-cookie',
|
||||
'value' => obj)
|
||||
@cookie.value.should == [ "one", "two", "three" ]
|
||||
|
||||
obj = mock("Put into an Array")
|
||||
@cookie.send(:initialize,
|
||||
'name' => 'test-cookie',
|
||||
'value' => obj)
|
||||
@cookie.value.should == [ obj ]
|
||||
end
|
||||
|
||||
it "raises a ArgumentError when the passed Hash has no 'name' entry" do
|
||||
lambda { @cookie.send(:initialize, {}) }.should raise_error(ArgumentError)
|
||||
lambda { @cookie.send(:initialize, "value" => "test") }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::Cookie#initialize when passed String, values ..." do
|
||||
before :each do
|
||||
@cookie = CGI::Cookie.allocate
|
||||
end
|
||||
|
||||
it "sets the self's name to the passed String" do
|
||||
@cookie.send(:initialize, "test-cookie", "one", "two", "three")
|
||||
@cookie.name.should == "test-cookie"
|
||||
end
|
||||
|
||||
it "sets the self's value to an Array containing all passed values" do
|
||||
@cookie.send(:initialize, "test-cookie", "one", "two", "three")
|
||||
@cookie.value.should == ["one", "two", "three"]
|
||||
end
|
||||
|
||||
it "sets self to a non-secure cookie" do
|
||||
@cookie.send(:initialize, "test", "one", "two", "three")
|
||||
@cookie.secure.should be_false
|
||||
end
|
||||
end
|
23
spec/ruby/library/cgi/cookie/name_spec.rb
Normal file
23
spec/ruby/library/cgi/cookie/name_spec.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI::Cookie#name" do
|
||||
it "returns self's name" do
|
||||
cookie = CGI::Cookie.new("test-cookie")
|
||||
cookie.name.should == "test-cookie"
|
||||
|
||||
cookie = CGI::Cookie.new("name" => "another cookie")
|
||||
cookie.name.should == "another cookie"
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::Cookie#name=" do
|
||||
it "sets self's expiration date" do
|
||||
cookie = CGI::Cookie.new("test-cookie")
|
||||
cookie.name = "another name"
|
||||
cookie.name.should == "another name"
|
||||
|
||||
cookie.name = "and one more"
|
||||
cookie.name.should == "and one more"
|
||||
end
|
||||
end
|
18
spec/ruby/library/cgi/cookie/parse_spec.rb
Normal file
18
spec/ruby/library/cgi/cookie/parse_spec.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI::Cookie.parse" do
|
||||
it "parses a raw cookie string into a hash of Cookies" do
|
||||
expected = { "test-cookie" => ["one", "two", "three"] }
|
||||
CGI::Cookie.parse("test-cookie=one&two&three").should == expected
|
||||
|
||||
expected = { "second cookie" => ["three", "four"], "first cookie" => ["one", "two"] }
|
||||
CGI::Cookie.parse("first cookie=one&two;second cookie=three&four").should == expected
|
||||
end
|
||||
|
||||
it "unescapes the Cookie values" do
|
||||
cookie = "test-cookie=+%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E"
|
||||
expected = { "test-cookie" => [ " !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ] }
|
||||
CGI::Cookie.parse(cookie).should == expected
|
||||
end
|
||||
end
|
23
spec/ruby/library/cgi/cookie/path_spec.rb
Normal file
23
spec/ruby/library/cgi/cookie/path_spec.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI::Cookie#path" do
|
||||
it "returns self's path" do
|
||||
cookie = CGI::Cookie.new("test-cookie")
|
||||
cookie.path.should == ""
|
||||
|
||||
cookie = CGI::Cookie.new("name" => "test-cookie", "path" => "/some/path/")
|
||||
cookie.path.should == "/some/path/"
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::Cookie#path=" do
|
||||
it "sets self's path" do
|
||||
cookie = CGI::Cookie.new("test-cookie")
|
||||
cookie.path = "/some/path/"
|
||||
cookie.path.should == "/some/path/"
|
||||
|
||||
cookie.path = "/another/path/"
|
||||
cookie.path.should == "/another/path/"
|
||||
end
|
||||
end
|
70
spec/ruby/library/cgi/cookie/secure_spec.rb
Normal file
70
spec/ruby/library/cgi/cookie/secure_spec.rb
Normal file
|
@ -0,0 +1,70 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI::Cookie#secure" do
|
||||
before :each do
|
||||
@cookie = CGI::Cookie.new("test-cookie")
|
||||
end
|
||||
|
||||
it "returns whether self is a secure cookie or not" do
|
||||
@cookie.secure = true
|
||||
@cookie.secure.should be_true
|
||||
|
||||
@cookie.secure = false
|
||||
@cookie.secure.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::Cookie#secure= when passed true" do
|
||||
before :each do
|
||||
@cookie = CGI::Cookie.new("test-cookie")
|
||||
end
|
||||
|
||||
it "returns true" do
|
||||
(@cookie.secure = true).should be_true
|
||||
end
|
||||
|
||||
it "sets self to a secure cookie" do
|
||||
@cookie.secure = true
|
||||
@cookie.secure.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::Cookie#secure= when passed false" do
|
||||
before :each do
|
||||
@cookie = CGI::Cookie.new("test-cookie")
|
||||
end
|
||||
|
||||
it "returns false" do
|
||||
(@cookie.secure = false).should be_false
|
||||
end
|
||||
|
||||
it "sets self to a non-secure cookie" do
|
||||
@cookie.secure = false
|
||||
@cookie.secure.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::Cookie#secure= when passed Object" do
|
||||
before :each do
|
||||
@cookie = CGI::Cookie.new("test-cookie")
|
||||
end
|
||||
|
||||
it "does not change self's secure value" do
|
||||
@cookie.secure = false
|
||||
|
||||
@cookie.secure = Object.new
|
||||
@cookie.secure.should be_false
|
||||
|
||||
@cookie.secure = "Test"
|
||||
@cookie.secure.should be_false
|
||||
|
||||
@cookie.secure = true
|
||||
|
||||
@cookie.secure = Object.new
|
||||
@cookie.secure.should be_true
|
||||
|
||||
@cookie.secure = "Test"
|
||||
@cookie.secure.should be_true
|
||||
end
|
||||
end
|
42
spec/ruby/library/cgi/cookie/to_s_spec.rb
Normal file
42
spec/ruby/library/cgi/cookie/to_s_spec.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI::Cookie#to_s" do
|
||||
it "returns a String representation of self" do
|
||||
cookie = CGI::Cookie.new("test-cookie")
|
||||
cookie.to_s.should == "test-cookie=; path="
|
||||
|
||||
cookie = CGI::Cookie.new("test-cookie", "value")
|
||||
cookie.to_s.should == "test-cookie=value; path="
|
||||
|
||||
cookie = CGI::Cookie.new("test-cookie", "one", "two", "three")
|
||||
cookie.to_s.should == "test-cookie=one&two&three; path="
|
||||
|
||||
cookie = CGI::Cookie.new(
|
||||
'name' => 'test-cookie',
|
||||
'value' => ["one", "two", "three"],
|
||||
'path' => 'some/path/',
|
||||
'domain' => 'example.com',
|
||||
'expires' => Time.at(1196524602),
|
||||
'secure' => true)
|
||||
cookie.to_s.should == "test-cookie=one&two&three; domain=example.com; path=some/path/; expires=Sat, 01 Dec 2007 15:56:42 GMT; secure"
|
||||
end
|
||||
|
||||
it "escapes the self's values" do
|
||||
cookie = CGI::Cookie.new("test-cookie", " !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}")
|
||||
cookie.to_s.should == "test-cookie=+%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D; path="
|
||||
end
|
||||
|
||||
ruby_version_is ""..."2.5" do
|
||||
it "escapes tilde" do
|
||||
cookie = CGI::Cookie.new("test-cookie", "~").to_s.should == "test-cookie=%7E; path="
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is "2.5" do
|
||||
it "does not escape tilde" do
|
||||
cookie = CGI::Cookie.new("test-cookie", "~").to_s.should == "test-cookie=~; path="
|
||||
end
|
||||
end
|
||||
|
||||
end
|
76
spec/ruby/library/cgi/cookie/value_spec.rb
Normal file
76
spec/ruby/library/cgi/cookie/value_spec.rb
Normal file
|
@ -0,0 +1,76 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI::Cookie#value" do
|
||||
it "returns self's value" do
|
||||
cookie = CGI::Cookie.new("test-cookie")
|
||||
cookie.value.should == []
|
||||
|
||||
cookie = CGI::Cookie.new("test-cookie", "one")
|
||||
cookie.value.should == ["one"]
|
||||
|
||||
cookie = CGI::Cookie.new("test-cookie", "one", "two", "three")
|
||||
cookie.value.should == ["one", "two", "three"]
|
||||
|
||||
cookie = CGI::Cookie.new("name" => "test-cookie", "value" => ["one", "two", "three"])
|
||||
cookie.value.should == ["one", "two", "three"]
|
||||
end
|
||||
|
||||
it "is in synch with self" do
|
||||
fail = []
|
||||
[
|
||||
:pop,
|
||||
:shift,
|
||||
[:<<, "Hello"],
|
||||
[:push, "Hello"],
|
||||
[:unshift, "World"],
|
||||
[:replace, ["A", "B"]],
|
||||
[:[]=, 1, "Set"],
|
||||
[:delete, "first"],
|
||||
[:delete_at, 0],
|
||||
].each do |method, *args|
|
||||
cookie1 = CGI::Cookie.new("test-cookie", "first", "second")
|
||||
cookie2 = CGI::Cookie.new("test-cookie", "first", "second")
|
||||
cookie1.send(method, *args)
|
||||
cookie2.value.send(method, *args)
|
||||
fail << method unless cookie1.value == cookie2.value
|
||||
end
|
||||
fail.should be_empty
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::Cookie#value=" do
|
||||
before :each do
|
||||
@cookie = CGI::Cookie.new("test-cookie")
|
||||
end
|
||||
|
||||
it "sets self's value" do
|
||||
@cookie.value = ["one"]
|
||||
@cookie.value.should == ["one"]
|
||||
|
||||
@cookie.value = ["one", "two", "three"]
|
||||
@cookie.value.should == ["one", "two", "three"]
|
||||
end
|
||||
|
||||
it "automatically converts the passed Object to an Array using #Array" do
|
||||
@cookie.value = "test"
|
||||
@cookie.value.should == ["test"]
|
||||
|
||||
obj = mock("to_a")
|
||||
obj.should_receive(:to_a).and_return(["1", "2"])
|
||||
@cookie.value = obj
|
||||
@cookie.value.should == ["1", "2"]
|
||||
|
||||
obj = mock("to_ary")
|
||||
obj.should_receive(:to_ary).and_return(["1", "2"])
|
||||
@cookie.value = obj
|
||||
@cookie.value.should == ["1", "2"]
|
||||
end
|
||||
|
||||
it "does keep self and the values in sync" do
|
||||
@cookie.value = ["one", "two", "three"]
|
||||
@cookie[0].should == "one"
|
||||
@cookie[1].should == "two"
|
||||
@cookie[2].should == "three"
|
||||
end
|
||||
end
|
20
spec/ruby/library/cgi/escapeElement_spec.rb
Normal file
20
spec/ruby/library/cgi/escapeElement_spec.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI.escapeElement when passed String, elements, ..." do
|
||||
it "escapes only the tags of the passed elements in the passed String" do
|
||||
res = CGI.escapeElement('<BR><A HREF="url"></A>', "A", "IMG")
|
||||
res.should == "<BR><A HREF="url"></A>"
|
||||
|
||||
res = CGI.escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"])
|
||||
res.should == "<BR><A HREF="url"></A>"
|
||||
end
|
||||
|
||||
it "is case-insensitive" do
|
||||
res = CGI.escapeElement('<BR><A HREF="url"></A>', "a", "img")
|
||||
res.should == '<BR><A HREF="url"></A>'
|
||||
|
||||
res = CGI.escapeElement('<br><a href="url"></a>', "A", "IMG")
|
||||
res.should == '<br><a href="url"></a>'
|
||||
end
|
||||
end
|
13
spec/ruby/library/cgi/escapeHTML_spec.rb
Normal file
13
spec/ruby/library/cgi/escapeHTML_spec.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI.escapeHTML" do
|
||||
it "escapes special HTML characters (&\"<>') in the passed argument" do
|
||||
CGI.escapeHTML(%[& < > " ']).should == '& < > " ''
|
||||
end
|
||||
|
||||
it "does not escape any other characters" do
|
||||
chars = " !\#$%()*+,-./0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
||||
CGI.escapeHTML(chars).should == chars
|
||||
end
|
||||
end
|
26
spec/ruby/library/cgi/escape_spec.rb
Normal file
26
spec/ruby/library/cgi/escape_spec.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI.escape" do
|
||||
it "url-encodes the passed argument" do
|
||||
input = " !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}"
|
||||
expected = "+%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D"
|
||||
CGI.escape(input).should == expected
|
||||
|
||||
input = "http://ja.wikipedia.org/wiki/\343\203\255\343\203\240\343\202\271\343\202\253\343\203\273\343\203\221\343\203\255\343\203\273\343\202\246\343\203\253\343\203\273\343\203\251\343\203\224\343\203\245\343\202\277"
|
||||
expected = 'http%3A%2F%2Fja.wikipedia.org%2Fwiki%2F%E3%83%AD%E3%83%A0%E3%82%B9%E3%82%AB%E3%83%BB%E3%83%91%E3%83%AD%E3%83%BB%E3%82%A6%E3%83%AB%E3%83%BB%E3%83%A9%E3%83%94%E3%83%A5%E3%82%BF'
|
||||
CGI.escape(input).should == expected
|
||||
end
|
||||
|
||||
ruby_version_is ""..."2.5" do
|
||||
it "escapes tilde" do
|
||||
CGI.escape("~").should == "%7E"
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is "2.5" do
|
||||
it "does not escape tilde" do
|
||||
CGI.escape("~").should == "~"
|
||||
end
|
||||
end
|
||||
end
|
49
spec/ruby/library/cgi/htmlextension/a_spec.rb
Normal file
49
spec/ruby/library/cgi/htmlextension/a_spec.rb
Normal file
|
@ -0,0 +1,49 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#a" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed a String" do
|
||||
it "returns an 'a'-element, using the passed String as the 'href'-attribute" do
|
||||
output = @html.a("http://www.example.com")
|
||||
output.should equal_element("A", "HREF" => "http://www.example.com")
|
||||
end
|
||||
|
||||
it "includes the passed block's return value when passed a block" do
|
||||
output = @html.a("http://www.example.com") { "Example" }
|
||||
output.should equal_element("A", { "HREF" => "http://www.example.com" }, "Example")
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed a Hash" do
|
||||
it "returns an 'a'-element, using the passed Hash for attributes" do
|
||||
attributes = {"HREF" => "http://www.example.com", "TARGET" => "_top"}
|
||||
@html.a(attributes).should equal_element("A", attributes)
|
||||
end
|
||||
|
||||
it "includes the passed block's return value when passed a block" do
|
||||
attributes = {"HREF" => "http://www.example.com", "TARGET" => "_top"}
|
||||
@html.a(attributes) { "Example" }.should equal_element("A", attributes, "Example")
|
||||
end
|
||||
end
|
||||
|
||||
describe "when each HTML generation" do
|
||||
it "returns the doctype declaration for HTML3" do
|
||||
CGISpecs.cgi_new("html3").a.should == %(<A HREF=""></A>)
|
||||
CGISpecs.cgi_new("html3").a { "link text" }.should == %(<A HREF="">link text</A>)
|
||||
end
|
||||
|
||||
it "returns the doctype declaration for HTML4" do
|
||||
CGISpecs.cgi_new("html4").a.should == %(<A HREF=""></A>)
|
||||
CGISpecs.cgi_new("html4").a { "link text" }.should == %(<A HREF="">link text</A>)
|
||||
end
|
||||
it "returns the doctype declaration for the Transitional version of HTML4" do
|
||||
CGISpecs.cgi_new("html4Tr").a.should == %(<A HREF=""></A>)
|
||||
CGISpecs.cgi_new("html4Tr").a { "link text" }.should == %(<A HREF="">link text</A>)
|
||||
end
|
||||
end
|
||||
end
|
33
spec/ruby/library/cgi/htmlextension/base_spec.rb
Normal file
33
spec/ruby/library/cgi/htmlextension/base_spec.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#base" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when bassed a String" do
|
||||
it "returns a 'base'-element, using the passed String as the 'href'-attribute" do
|
||||
output = @html.base("http://www.example.com")
|
||||
output.should equal_element("BASE", {"HREF" => "http://www.example.com"}, nil, not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.base("http://www.example.com") { "Example" }
|
||||
output.should equal_element("BASE", {"HREF" => "http://www.example.com"}, nil, not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed a Hash" do
|
||||
it "returns a 'base'-element, using the passed Hash for attributes" do
|
||||
output = @html.base("HREF" => "http://www.example.com", "ID" => "test")
|
||||
output.should equal_element("BASE", {"HREF" => "http://www.example.com", "ID" => "test"}, nil, not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.base("HREF" => "http://www.example.com", "ID" => "test") { "Example" }
|
||||
output.should equal_element("BASE", {"HREF" => "http://www.example.com", "ID" => "test"}, nil, not_closed: true)
|
||||
end
|
||||
end
|
||||
end
|
33
spec/ruby/library/cgi/htmlextension/blockquote_spec.rb
Normal file
33
spec/ruby/library/cgi/htmlextension/blockquote_spec.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#blockquote" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed a String" do
|
||||
it "returns a 'blockquote'-element, using the passed String for the 'cite'-attribute" do
|
||||
output = @html.blockquote("http://www.example.com/quotes/foo.html")
|
||||
output.should equal_element("BLOCKQUOTE", "CITE" => "http://www.example.com/quotes/foo.html")
|
||||
end
|
||||
|
||||
it "includes the passed block's return value when passed a block" do
|
||||
output = @html.blockquote("http://www.example.com/quotes/foo.html") { "Foo!" }
|
||||
output.should equal_element("BLOCKQUOTE", { "CITE" => "http://www.example.com/quotes/foo.html" }, "Foo!")
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed a Hash" do
|
||||
it "returns a 'blockquote'-element, using the passed Hash for attributes" do
|
||||
output = @html.blockquote("CITE" => "http://www.example.com/quotes/foo.html", "ID" => "test")
|
||||
output.should equal_element("BLOCKQUOTE", "CITE" => "http://www.example.com/quotes/foo.html", "ID" => "test")
|
||||
end
|
||||
|
||||
it "includes the passed block's return value when passed a block" do
|
||||
output = @html.blockquote("CITE" => "http://www.example.com/quotes/foo.html", "ID" => "test") { "Foo!" }
|
||||
output.should equal_element("BLOCKQUOTE", {"CITE" => "http://www.example.com/quotes/foo.html", "ID" => "test"}, "Foo!")
|
||||
end
|
||||
end
|
||||
end
|
22
spec/ruby/library/cgi/htmlextension/br_spec.rb
Normal file
22
spec/ruby/library/cgi/htmlextension/br_spec.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#br" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when each HTML generation" do
|
||||
it "returns the doctype declaration for HTML3" do
|
||||
CGISpecs.cgi_new("html3").br.should == "<BR>"
|
||||
end
|
||||
|
||||
it "returns the doctype declaration for HTML4" do
|
||||
CGISpecs.cgi_new("html4").br.should == "<BR>"
|
||||
end
|
||||
it "returns the doctype declaration for the Transitional version of HTML4" do
|
||||
CGISpecs.cgi_new("html4Tr").br.should == "<BR>"
|
||||
end
|
||||
end
|
||||
end
|
33
spec/ruby/library/cgi/htmlextension/caption_spec.rb
Normal file
33
spec/ruby/library/cgi/htmlextension/caption_spec.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#caption" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed a String" do
|
||||
it "returns a 'caption'-element, using the passed String for the 'align'-attribute" do
|
||||
output = @html.caption("left")
|
||||
output.should equal_element("CAPTION", "ALIGN" => "left")
|
||||
end
|
||||
|
||||
it "includes the passed block's return value when passed a block" do
|
||||
output = @html.caption("left") { "Capital Cities" }
|
||||
output.should equal_element("CAPTION", {"ALIGN" => "left"}, "Capital Cities")
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed a Hash" do
|
||||
it "returns a 'caption'-element, using the passed Hash for attributes" do
|
||||
output = @html.caption("ALIGN" => "left", "ID" => "test")
|
||||
output.should equal_element("CAPTION", "ALIGN" => "left", "ID" => "test")
|
||||
end
|
||||
|
||||
it "includes the passed block's return value when passed a block" do
|
||||
output = @html.caption("ALIGN" => "left", "ID" => "test") { "Capital Cities" }
|
||||
output.should equal_element("CAPTION", {"ALIGN" => "left", "ID" => "test"}, "Capital Cities")
|
||||
end
|
||||
end
|
||||
end
|
76
spec/ruby/library/cgi/htmlextension/checkbox_group_spec.rb
Normal file
76
spec/ruby/library/cgi/htmlextension/checkbox_group_spec.rb
Normal file
|
@ -0,0 +1,76 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#checkbox_group" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed name, values ..." do
|
||||
it "returns a sequence of 'checkbox'-elements with the passed name and the passed values" do
|
||||
output = CGISpecs.split(@html.checkbox_group("test", "foo", "bar", "baz"))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
end
|
||||
|
||||
it "allows passing a value inside an Array" do
|
||||
output = CGISpecs.split(@html.checkbox_group("test", ["foo"], "bar", ["baz"]))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
end
|
||||
|
||||
it "allows passing a value as an Array containing the value and the checked state or a label" do
|
||||
output = CGISpecs.split(@html.checkbox_group("test", ["foo"], ["bar", true], ["baz", "label for baz"]))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"CHECKED" => true, "NAME" => "test", "TYPE" => "checkbox", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "baz"}, "label for baz", not_closed: true)
|
||||
end
|
||||
|
||||
it "allows passing a value as an Array containing the value, a label and the checked state" do
|
||||
output = CGISpecs.split(@html.checkbox_group("test", ["foo", "label for foo", true], ["bar", "label for bar", false], ["baz", "label for baz", true]))
|
||||
output[0].should equal_element("INPUT", {"CHECKED" => true, "NAME" => "test", "TYPE" => "checkbox", "VALUE" => "foo"}, "label for foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "bar"}, "label for bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"CHECKED" => true, "NAME" => "test", "TYPE" => "checkbox", "VALUE" => "baz"}, "label for baz", not_closed: true)
|
||||
end
|
||||
|
||||
it "returns an empty String when passed no values" do
|
||||
@html.checkbox_group("test").should == ""
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = CGISpecs.split(@html.checkbox_group("test", "foo", "bar", "baz") { "test" })
|
||||
output[0].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Hash" do
|
||||
it "uses the passed Hash to generate the checkbox sequence" do
|
||||
output = CGISpecs.split(@html.checkbox_group("NAME" => "name", "VALUES" => ["foo", "bar", "baz"]))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "checkbox", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "checkbox", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "checkbox", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
|
||||
output = CGISpecs.split(@html.checkbox_group("NAME" => "name", "VALUES" => [["foo"], ["bar", true], "baz"]))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "checkbox", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"CHECKED" => true, "NAME" => "name", "TYPE" => "checkbox", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "checkbox", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
|
||||
output = CGISpecs.split(@html.checkbox_group("NAME" => "name", "VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"]))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "checkbox", "VALUE" => "1"}, "Foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"CHECKED" => true, "NAME" => "name", "TYPE" => "checkbox", "VALUE" => "2"}, "Bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "checkbox", "VALUE" => "Baz"}, "Baz", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = CGISpecs.split(@html.checkbox_group("NAME" => "name", "VALUES" => ["foo", "bar", "baz"]) { "test" })
|
||||
output[0].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "checkbox", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "checkbox", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "checkbox", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
end
|
||||
end
|
||||
end
|
77
spec/ruby/library/cgi/htmlextension/checkbox_spec.rb
Normal file
77
spec/ruby/library/cgi/htmlextension/checkbox_spec.rb
Normal file
|
@ -0,0 +1,77 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#checkbox" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed no arguments" do
|
||||
it "returns a checkbox-'input'-element without a name" do
|
||||
output = @html.checkbox
|
||||
output.should equal_element("INPUT", {"NAME" => "", "TYPE" => "checkbox"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.checkbox { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "", "TYPE" => "checkbox"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name" do
|
||||
it "returns a checkbox-'input'-element with the passed name" do
|
||||
output = @html.checkbox("test")
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.checkbox("test") { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::HtmlExtension#checkbox when passed name, value" do
|
||||
it "returns a checkbox-'input'-element with the passed name and value" do
|
||||
output = @html.checkbox("test", "test-value")
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.checkbox("test", "test-value") { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name, value, checked" do
|
||||
it "returns a checked checkbox-'input'-element with the passed name and value when checked is true" do
|
||||
output = @html.checkbox("test", "test-value", true)
|
||||
output.should equal_element("INPUT", {"CHECKED" => true, "NAME" => "test", "TYPE" => "checkbox", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
|
||||
output = @html.checkbox("test", "test-value", false)
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
|
||||
output = @html.checkbox("test", "test-value", nil)
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.checkbox("test", "test-value", nil) { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "checkbox", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Hash" do
|
||||
it "returns a checkbox-'input'-element using the passed Hash for attributes" do
|
||||
attributes = {"NAME" => "test", "VALUE" => "test-value", "CHECKED" => true}
|
||||
output = @html.checkbox(attributes)
|
||||
output.should equal_element("INPUT", attributes, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
attributes = {"NAME" => "test", "VALUE" => "test-value", "CHECKED" => true}
|
||||
output = @html.checkbox(attributes) { "test" }
|
||||
output.should equal_element("INPUT", attributes, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
end
|
27
spec/ruby/library/cgi/htmlextension/doctype_spec.rb
Normal file
27
spec/ruby/library/cgi/htmlextension/doctype_spec.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#doctype" do
|
||||
describe "when each HTML generation" do
|
||||
it "returns the doctype declaration for HTML3" do
|
||||
expect = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'
|
||||
CGISpecs.cgi_new("html3").doctype.should == expect
|
||||
end
|
||||
|
||||
it "returns the doctype declaration for HTML4" do
|
||||
expect = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
|
||||
CGISpecs.cgi_new("html4").doctype.should == expect
|
||||
end
|
||||
|
||||
it "returns the doctype declaration for the Frameset version of HTML4" do
|
||||
expect = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'
|
||||
CGISpecs.cgi_new("html4Fr").doctype.should == expect
|
||||
end
|
||||
|
||||
it "returns the doctype declaration for the Transitional version of HTML4" do
|
||||
expect = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
|
||||
CGISpecs.cgi_new("html4Tr").doctype.should == expect
|
||||
end
|
||||
end
|
||||
end
|
72
spec/ruby/library/cgi/htmlextension/file_field_spec.rb
Normal file
72
spec/ruby/library/cgi/htmlextension/file_field_spec.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#file_field" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed no arguments" do
|
||||
it "returns a file-'input'-element without a name and a size of 20" do
|
||||
output = @html.file_field
|
||||
output.should equal_element("INPUT", {"SIZE" => 20, "NAME" => "", "TYPE" => "file"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.file_field { "test" }
|
||||
output.should equal_element("INPUT", {"SIZE" => 20, "NAME" => "", "TYPE" => "file"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name" do
|
||||
it "returns a checkbox-'input'-element with the passed name" do
|
||||
output = @html.file_field("Example")
|
||||
output.should equal_element("INPUT", {"SIZE" => 20, "NAME" => "Example", "TYPE" => "file"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.file_field("Example") { "test" }
|
||||
output.should equal_element("INPUT", {"SIZE" => 20, "NAME" => "Example", "TYPE" => "file"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name, size" do
|
||||
it "returns a checkbox-'input'-element with the passed name and size" do
|
||||
output = @html.file_field("Example", 40)
|
||||
output.should equal_element("INPUT", {"SIZE" => 40, "NAME" => "Example", "TYPE" => "file"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.file_field("Example", 40) { "test" }
|
||||
output.should equal_element("INPUT", {"SIZE" => 40, "NAME" => "Example", "TYPE" => "file"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name, size, maxlength" do
|
||||
it "returns a checkbox-'input'-element with the passed name, size and maxlength" do
|
||||
output = @html.file_field("Example", 40, 100)
|
||||
output.should equal_element("INPUT", {"SIZE" => 40, "NAME" => "Example", "TYPE" => "file", "MAXLENGTH" => 100}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.file_field("Example", 40, 100) { "test" }
|
||||
output.should equal_element("INPUT", {"SIZE" => 40, "NAME" => "Example", "TYPE" => "file", "MAXLENGTH" => 100}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed a Hash" do
|
||||
it "returns a file-'input'-element using the passed Hash for attributes" do
|
||||
output = @html.file_field("NAME" => "test", "SIZE" => 40)
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "SIZE" => 40}, "", not_closed: true)
|
||||
|
||||
output = @html.file_field("NAME" => "test", "MAXLENGTH" => 100)
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "MAXLENGTH" => 100}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.file_field("NAME" => "test", "SIZE" => 40) { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "SIZE" => 40}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
end
|
15
spec/ruby/library/cgi/htmlextension/fixtures/common.rb
Normal file
15
spec/ruby/library/cgi/htmlextension/fixtures/common.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module CGISpecs
|
||||
def self.cgi_new(html = "html4")
|
||||
old_request_method = ENV['REQUEST_METHOD']
|
||||
ENV['REQUEST_METHOD'] = "GET"
|
||||
begin
|
||||
CGI.new(tag_maker: html)
|
||||
ensure
|
||||
ENV['REQUEST_METHOD'] = old_request_method
|
||||
end
|
||||
end
|
||||
|
||||
def self.split(string)
|
||||
string.split("<").reject { |x| x.empty? }.map { |x| "<#{x}" }
|
||||
end
|
||||
end
|
58
spec/ruby/library/cgi/htmlextension/form_spec.rb
Normal file
58
spec/ruby/library/cgi/htmlextension/form_spec.rb
Normal file
|
@ -0,0 +1,58 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#form" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
@html.stub!(:script_name).and_return("/path/to/some/script")
|
||||
end
|
||||
|
||||
describe "when passed no arguments" do
|
||||
it "returns a 'form'-element" do
|
||||
output = @html.form
|
||||
output.should equal_element("FORM", {"ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "post", "ACTION" => "/path/to/some/script"}, "")
|
||||
end
|
||||
|
||||
it "includes the return value of the passed block when passed a block" do
|
||||
output = @html.form { "test" }
|
||||
output.should equal_element("FORM", {"ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "post", "ACTION" => "/path/to/some/script"}, "test")
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed method" do
|
||||
it "returns a 'form'-element with the passed method" do
|
||||
output = @html.form("get")
|
||||
output.should equal_element("FORM", {"ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "get", "ACTION" => "/path/to/some/script"}, "")
|
||||
end
|
||||
|
||||
it "includes the return value of the passed block when passed a block" do
|
||||
output = @html.form("get") { "test" }
|
||||
output.should equal_element("FORM", {"ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "get", "ACTION" => "/path/to/some/script"}, "test")
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed method, action" do
|
||||
it "returns a 'form'-element with the passed method and the passed action" do
|
||||
output = @html.form("get", "/some/other/script")
|
||||
output.should equal_element("FORM", {"ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "get", "ACTION" => "/some/other/script"}, "")
|
||||
end
|
||||
|
||||
it "includes the return value of the passed block when passed a block" do
|
||||
output = @html.form("get", "/some/other/script") { "test" }
|
||||
output.should equal_element("FORM", {"ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "get", "ACTION" => "/some/other/script"}, "test")
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed method, action, enctype" do
|
||||
it "returns a 'form'-element with the passed method, action and enctype" do
|
||||
output = @html.form("get", "/some/other/script", "multipart/form-data")
|
||||
output.should equal_element("FORM", {"ENCTYPE" => "multipart/form-data", "METHOD" => "get", "ACTION" => "/some/other/script"}, "")
|
||||
end
|
||||
|
||||
it "includes the return value of the passed block when passed a block" do
|
||||
output = @html.form("get", "/some/other/script", "multipart/form-data") { "test" }
|
||||
output.should equal_element("FORM", {"ENCTYPE" => "multipart/form-data", "METHOD" => "get", "ACTION" => "/some/other/script"}, "test")
|
||||
end
|
||||
end
|
||||
end
|
14
spec/ruby/library/cgi/htmlextension/frame_spec.rb
Normal file
14
spec/ruby/library/cgi/htmlextension/frame_spec.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI::HtmlExtension#frame" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new("html4Fr")
|
||||
end
|
||||
|
||||
it "initializes the HTML Generation methods for the Frameset version of HTML4" do
|
||||
@html.frameset.should == "<FRAMESET></FRAMESET>"
|
||||
@html.frameset { "link text" }.should == "<FRAMESET>link text</FRAMESET>"
|
||||
end
|
||||
end
|
14
spec/ruby/library/cgi/htmlextension/frameset_spec.rb
Normal file
14
spec/ruby/library/cgi/htmlextension/frameset_spec.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
require 'cgi'
|
||||
|
||||
describe "CGI::HtmlExtension#frameset" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new("html4Fr")
|
||||
end
|
||||
|
||||
it "initializes the HTML Generation methods for the Frameset version of HTML4" do
|
||||
@html.frameset.should == "<FRAMESET></FRAMESET>"
|
||||
@html.frameset { "link text" }.should == "<FRAMESET>link text</FRAMESET>"
|
||||
end
|
||||
end
|
59
spec/ruby/library/cgi/htmlextension/hidden_spec.rb
Normal file
59
spec/ruby/library/cgi/htmlextension/hidden_spec.rb
Normal file
|
@ -0,0 +1,59 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#hidden" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed no arguments" do
|
||||
it "returns an hidden-'input'-element without a name" do
|
||||
output = @html.hidden
|
||||
output.should equal_element("INPUT", {"NAME" => "", "TYPE" => "hidden"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.hidden { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "", "TYPE" => "hidden"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name" do
|
||||
it "returns an hidden-'input'-element with the passed name" do
|
||||
output = @html.hidden("test")
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "hidden"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.hidden("test") { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "hidden"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name, value" do
|
||||
it "returns an hidden-'input'-element with the passed name and value" do
|
||||
output = @html.hidden("test", "some value")
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "hidden", "VALUE" => "some value"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.hidden("test", "some value") { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "hidden", "VALUE" => "some value"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Hash" do
|
||||
it "returns a checkbox-'input'-element using the passed Hash for attributes" do
|
||||
attributes = { "NAME" => "test", "VALUE" => "some value" }
|
||||
output = @html.hidden("test", "some value")
|
||||
output.should equal_element("INPUT", attributes, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
attributes = { "NAME" => "test", "VALUE" => "some value" }
|
||||
output = @html.hidden("test", "some value") { "test" }
|
||||
output.should equal_element("INPUT", attributes, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
end
|
66
spec/ruby/library/cgi/htmlextension/html_spec.rb
Normal file
66
spec/ruby/library/cgi/htmlextension/html_spec.rb
Normal file
|
@ -0,0 +1,66 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#html" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
@html.stub!(:doctype).and_return("<!DOCTYPE SUPA-FUNKAY-RUBYSPEC-DOCTYPE>")
|
||||
end
|
||||
|
||||
describe "when passed no arguments" do
|
||||
it "returns a self's doctype and an 'html'-element" do
|
||||
expected = '<!DOCTYPE SUPA-FUNKAY-RUBYSPEC-DOCTYPE><HTML>'
|
||||
@html.html.should == expected
|
||||
end
|
||||
|
||||
it "includes the passed block when passed a block" do
|
||||
expected = '<!DOCTYPE SUPA-FUNKAY-RUBYSPEC-DOCTYPE><HTML>test</HTML>'
|
||||
@html.html { "test" }.should == expected
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed 'PRETTY'" do
|
||||
it "returns pretty output when the passed String is 'PRETTY" do
|
||||
expected = "<!DOCTYPE SUPA-FUNKAY-RUBYSPEC-DOCTYPE>\n<HTML>\n"
|
||||
@html.html("PRETTY").should == expected
|
||||
end
|
||||
|
||||
it "includes the passed block when passed a block" do
|
||||
expected = "<!DOCTYPE SUPA-FUNKAY-RUBYSPEC-DOCTYPE>\n<HTML>\n test\n</HTML>\n"
|
||||
@html.html("PRETTY") { "test" }.should == expected
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed a Hash" do
|
||||
it "returns an 'html'-element using the passed Hash for attributes" do
|
||||
expected = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML BLA="TEST">'
|
||||
@html.html("DOCTYPE" => '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">', "BLA" => "TEST").should == expected
|
||||
end
|
||||
|
||||
it "omits the doctype when the Hash contains a 'DOCTYPE' entry that's false or nil" do
|
||||
@html.html("DOCTYPE" => false).should == "<HTML>"
|
||||
@html.html("DOCTYPE" => nil).should == "<HTML>"
|
||||
end
|
||||
end
|
||||
|
||||
describe "when each HTML generation" do
|
||||
it "returns the doctype declaration for HTML3" do
|
||||
expect = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'
|
||||
CGISpecs.cgi_new("html3").html.should == expect + "<HTML>"
|
||||
CGISpecs.cgi_new("html3").html { "html body" }.should == expect + "<HTML>html body</HTML>"
|
||||
end
|
||||
|
||||
it "returns the doctype declaration for HTML4" do
|
||||
expect = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
|
||||
CGISpecs.cgi_new("html4").html.should == expect + "<HTML>"
|
||||
CGISpecs.cgi_new("html4").html { "html body" }.should == expect + "<HTML>html body</HTML>"
|
||||
end
|
||||
|
||||
it "returns the doctype declaration for the Transitional version of HTML4" do
|
||||
expect = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
|
||||
CGISpecs.cgi_new("html4Tr").html.should == expect + "<HTML>"
|
||||
CGISpecs.cgi_new("html4Tr").html { "html body" }.should == expect + "<HTML>html body</HTML>"
|
||||
end
|
||||
end
|
||||
end
|
69
spec/ruby/library/cgi/htmlextension/image_button_spec.rb
Normal file
69
spec/ruby/library/cgi/htmlextension/image_button_spec.rb
Normal file
|
@ -0,0 +1,69 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#image_button" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed no arguments" do
|
||||
it "returns an image-'input'-element without a source image" do
|
||||
output = @html.image_button
|
||||
output.should equal_element("INPUT", {"SRC" => "", "TYPE" => "image"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.image_button { "test" }
|
||||
output.should equal_element("INPUT", {"SRC" => "", "TYPE" => "image"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed src" do
|
||||
it "returns an image-'input'-element with the passed src" do
|
||||
output = @html.image_button("/path/to/image.png")
|
||||
output.should equal_element("INPUT", {"SRC" => "/path/to/image.png", "TYPE" => "image"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.image_button("/path/to/image.png") { "test" }
|
||||
output.should equal_element("INPUT", {"SRC" => "/path/to/image.png", "TYPE" => "image"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed src, name" do
|
||||
it "returns an image-'input'-element with the passed src and name" do
|
||||
output = @html.image_button("/path/to/image.png", "test")
|
||||
output.should equal_element("INPUT", {"SRC" => "/path/to/image.png", "TYPE" => "image", "NAME" => "test"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.image_button("/path/to/image.png", "test") { "test" }
|
||||
output.should equal_element("INPUT", {"SRC" => "/path/to/image.png", "TYPE" => "image", "NAME" => "test"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed src, name, alt" do
|
||||
it "returns an image-'input'-element with the passed src, name and alt" do
|
||||
output = @html.image_button("/path/to/image.png", "test", "alternative")
|
||||
output.should equal_element("INPUT", {"SRC" => "/path/to/image.png", "TYPE" => "image", "NAME" => "test", "ALT" => "alternative"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.image_button("/path/to/image.png", "test", "alternative") { "test" }
|
||||
output.should equal_element("INPUT", {"SRC" => "/path/to/image.png", "TYPE" => "image", "NAME" => "test", "ALT" => "alternative"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Hash" do
|
||||
it "returns a image-'input'-element using the passed Hash for attributes" do
|
||||
output = @html.image_button("NAME" => "test", "VALUE" => "test-value")
|
||||
output.should equal_element("INPUT", {"SRC" => "", "TYPE" => "image", "NAME" => "test", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.image_button("NAME" => "test", "VALUE" => "test-value") { "test" }
|
||||
output.should equal_element("INPUT", {"SRC" => "", "TYPE" => "image", "NAME" => "test", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
end
|
83
spec/ruby/library/cgi/htmlextension/img_spec.rb
Normal file
83
spec/ruby/library/cgi/htmlextension/img_spec.rb
Normal file
|
@ -0,0 +1,83 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#img" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed no arguments" do
|
||||
it "returns an 'img'-element without an src-url or alt-text" do
|
||||
output = @html.img
|
||||
output.should equal_element("IMG", { "SRC" => "", "ALT" => "" }, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.img { "test" }
|
||||
output.should equal_element("IMG", { "SRC" => "", "ALT" => "" }, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed src" do
|
||||
it "returns an 'img'-element with the passed src-url" do
|
||||
output = @html.img("/path/to/some/image.png")
|
||||
output.should equal_element("IMG", { "SRC" => "/path/to/some/image.png", "ALT" => "" }, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.img("/path/to/some/image.png")
|
||||
output.should equal_element("IMG", { "SRC" => "/path/to/some/image.png", "ALT" => "" }, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed src, alt" do
|
||||
it "returns an 'img'-element with the passed src-url and the passed alt-text" do
|
||||
output = @html.img("/path/to/some/image.png", "Alternative")
|
||||
output.should equal_element("IMG", { "SRC" => "/path/to/some/image.png", "ALT" => "Alternative" }, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.img("/path/to/some/image.png", "Alternative") { "test" }
|
||||
output.should equal_element("IMG", { "SRC" => "/path/to/some/image.png", "ALT" => "Alternative" }, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed src, alt, width" do
|
||||
it "returns an 'img'-element with the passed src-url, the passed alt-text and the passed width" do
|
||||
output = @html.img("/path/to/some/image.png", "Alternative", 40)
|
||||
output.should equal_element("IMG", { "SRC" => "/path/to/some/image.png", "ALT" => "Alternative", "WIDTH" => "40" }, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.img("/path/to/some/image.png", "Alternative", 40) { "test" }
|
||||
output.should equal_element("IMG", { "SRC" => "/path/to/some/image.png", "ALT" => "Alternative", "WIDTH" => "40" }, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed src, alt, width, height" do
|
||||
it "returns an 'img'-element with the passed src-url, the passed alt-text, the passed width and the passed height" do
|
||||
output = @html.img("/path/to/some/image.png", "Alternative", 40, 60)
|
||||
output.should equal_element("IMG", { "SRC" => "/path/to/some/image.png", "ALT" => "Alternative", "WIDTH" => "40", "HEIGHT" => "60" }, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.img { "test" }
|
||||
output.should equal_element("IMG", { "SRC" => "", "ALT" => "" }, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Hash" do
|
||||
it "returns an 'img'-element with the passed Hash as attributes" do
|
||||
attributes = { "SRC" => "src", "ALT" => "alt", "WIDTH" => 100, "HEIGHT" => 50 }
|
||||
output = @html.img(attributes)
|
||||
output.should equal_element("IMG", attributes, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
attributes = { "SRC" => "src", "ALT" => "alt", "WIDTH" => 100, "HEIGHT" => 50 }
|
||||
output = @html.img(attributes) { "test" }
|
||||
output.should equal_element("IMG", attributes, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
end
|
64
spec/ruby/library/cgi/htmlextension/multipart_form_spec.rb
Normal file
64
spec/ruby/library/cgi/htmlextension/multipart_form_spec.rb
Normal file
|
@ -0,0 +1,64 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#multipart_form" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
@html.stub!(:script_name).and_return("/path/to/some/script.rb")
|
||||
end
|
||||
|
||||
describe "when passed no arguments" do
|
||||
it "returns a 'form'-element with it's enctype set to multipart" do
|
||||
output = @html.multipart_form
|
||||
output.should equal_element("FORM", { "ENCTYPE" => "multipart/form-data", "METHOD" => "post" }, "")
|
||||
end
|
||||
|
||||
it "includes the return value of the passed block when passed a block" do
|
||||
output = @html.multipart_form { "test" }
|
||||
output.should equal_element("FORM", { "ENCTYPE" => "multipart/form-data", "METHOD" => "post" }, "test")
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed action" do
|
||||
it "returns a 'form'-element with the passed action" do
|
||||
output = @html.multipart_form("/some/other/script.rb")
|
||||
output.should equal_element("FORM", { "ENCTYPE" => "multipart/form-data", "METHOD" => "post", "ACTION" => "/some/other/script.rb" }, "")
|
||||
end
|
||||
|
||||
it "includes the return value of the passed block when passed a block" do
|
||||
output = @html.multipart_form("/some/other/script.rb") { "test" }
|
||||
output.should equal_element("FORM", { "ENCTYPE" => "multipart/form-data", "METHOD" => "post", "ACTION" => "/some/other/script.rb" }, "test")
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed action, enctype" do
|
||||
it "returns a 'form'-element with the passed action and enctype" do
|
||||
output = @html.multipart_form("/some/other/script.rb", "application/x-www-form-urlencoded")
|
||||
output.should equal_element("FORM", { "ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "post", "ACTION" => "/some/other/script.rb" }, "")
|
||||
end
|
||||
|
||||
it "includes the return value of the passed block when passed a block" do
|
||||
output = @html.multipart_form("/some/other/script.rb", "application/x-www-form-urlencoded") { "test" }
|
||||
output.should equal_element("FORM", { "ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "post", "ACTION" => "/some/other/script.rb" }, "test")
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Hash" do
|
||||
it "returns a 'form'-element with the passed Hash as attributes" do
|
||||
output = @html.multipart_form("ID" => "test")
|
||||
output.should equal_element("FORM", { "ENCTYPE" => "multipart/form-data", "METHOD" => "post", "ID" => "test" }, "")
|
||||
|
||||
output = @html.multipart_form("ID" => "test", "ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "get")
|
||||
output.should equal_element("FORM", { "ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "get", "ID" => "test" }, "")
|
||||
end
|
||||
|
||||
it "includes the return value of the passed block when passed a block" do
|
||||
output = @html.multipart_form("ID" => "test") { "test" }
|
||||
output.should equal_element("FORM", { "ENCTYPE" => "multipart/form-data", "METHOD" => "post", "ID" => "test" }, "test")
|
||||
|
||||
output = @html.multipart_form("ID" => "test", "ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "get") { "test" }
|
||||
output.should equal_element("FORM", { "ENCTYPE" => "application/x-www-form-urlencoded", "METHOD" => "get", "ID" => "test" }, "test")
|
||||
end
|
||||
end
|
||||
end
|
84
spec/ruby/library/cgi/htmlextension/password_field_spec.rb
Normal file
84
spec/ruby/library/cgi/htmlextension/password_field_spec.rb
Normal file
|
@ -0,0 +1,84 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#password_field" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed no arguments" do
|
||||
it "returns an password-'input'-element without a name" do
|
||||
output = @html.password_field
|
||||
output.should equal_element("INPUT", {"NAME" => "", "TYPE" => "password", "SIZE" => "40"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.password_field { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "", "TYPE" => "password", "SIZE" => "40"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name" do
|
||||
it "returns an password-'input'-element with the passed name" do
|
||||
output = @html.password_field("test")
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "password", "SIZE" => "40"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.password_field("test") { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "password", "SIZE" => "40"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name, value" do
|
||||
it "returns an password-'input'-element with the passed name and value" do
|
||||
output = @html.password_field("test", "some value")
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "password", "VALUE" => "some value", "SIZE" => "40"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.password_field("test", "some value") { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "password", "VALUE" => "some value", "SIZE" => "40"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name, value, size" do
|
||||
it "returns an password-'input'-element with the passed name, value and size" do
|
||||
output = @html.password_field("test", "some value", 60)
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "password", "VALUE" => "some value", "SIZE" => "60"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.password_field("test", "some value", 60) { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "password", "VALUE" => "some value", "SIZE" => "60"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name, value, size, maxlength" do
|
||||
it "returns an password-'input'-element with the passed name, value, size and maxlength" do
|
||||
output = @html.password_field("test", "some value", 60, 12)
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "password", "VALUE" => "some value", "SIZE" => "60", "MAXLENGTH" => 12}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.password_field("test", "some value", 60, 12) { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "password", "VALUE" => "some value", "SIZE" => "60", "MAXLENGTH" => 12}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Hash" do
|
||||
it "returns a checkbox-'input'-element using the passed Hash for attributes" do
|
||||
output = @html.password_field("NAME" => "test", "VALUE" => "some value")
|
||||
output.should equal_element("INPUT", { "NAME" => "test", "VALUE" => "some value", "TYPE" => "password" }, "", not_closed: true)
|
||||
|
||||
output = @html.password_field("TYPE" => "hidden")
|
||||
output.should equal_element("INPUT", {"TYPE" => "password"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.password_field("NAME" => "test", "VALUE" => "some value") { "test" }
|
||||
output.should equal_element("INPUT", { "NAME" => "test", "VALUE" => "some value", "TYPE" => "password" }, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
end
|
8
spec/ruby/library/cgi/htmlextension/popup_menu_spec.rb
Normal file
8
spec/ruby/library/cgi/htmlextension/popup_menu_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
require File.expand_path('../shared/popup_menu', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#popup_menu" do
|
||||
it_behaves_like :cgi_htmlextension_popup_menu, :popup_menu
|
||||
end
|
77
spec/ruby/library/cgi/htmlextension/radio_button_spec.rb
Normal file
77
spec/ruby/library/cgi/htmlextension/radio_button_spec.rb
Normal file
|
@ -0,0 +1,77 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#radio_button" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed no arguments" do
|
||||
it "returns a radio-'input'-element without a name" do
|
||||
output = @html.radio_button
|
||||
output.should equal_element("INPUT", {"NAME" => "", "TYPE" => "radio"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.radio_button { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "", "TYPE" => "radio"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name" do
|
||||
it "returns a radio-'input'-element with the passed name" do
|
||||
output = @html.radio_button("test")
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.radio_button("test") { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "CGI::HtmlExtension#checkbox when passed name, value" do
|
||||
it "returns a radio-'input'-element with the passed name and value" do
|
||||
output = @html.radio_button("test", "test-value")
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.radio_button("test", "test-value") { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed name, value, checked" do
|
||||
it "returns a checked radio-'input'-element with the passed name and value when checked is true" do
|
||||
output = @html.radio_button("test", "test-value", true)
|
||||
output.should equal_element("INPUT", {"CHECKED" => true, "NAME" => "test", "TYPE" => "radio", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
|
||||
output = @html.radio_button("test", "test-value", false)
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
|
||||
output = @html.radio_button("test", "test-value", nil)
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = @html.radio_button("test", "test-value", nil) { "test" }
|
||||
output.should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "test-value"}, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Hash" do
|
||||
it "returns a radio-'input'-element using the passed Hash for attributes" do
|
||||
attributes = {"NAME" => "test", "VALUE" => "test-value", "CHECKED" => true}
|
||||
output = @html.radio_button(attributes)
|
||||
output.should equal_element("INPUT", attributes, "", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
attributes = {"NAME" => "test", "VALUE" => "test-value", "CHECKED" => true}
|
||||
output = @html.radio_button(attributes) { "test" }
|
||||
output.should equal_element("INPUT", attributes, "", not_closed: true)
|
||||
end
|
||||
end
|
||||
end
|
77
spec/ruby/library/cgi/htmlextension/radio_group_spec.rb
Normal file
77
spec/ruby/library/cgi/htmlextension/radio_group_spec.rb
Normal file
|
@ -0,0 +1,77 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require 'cgi'
|
||||
require File.expand_path('../fixtures/common', __FILE__)
|
||||
|
||||
describe "CGI::HtmlExtension#radio_group" do
|
||||
before :each do
|
||||
@html = CGISpecs.cgi_new
|
||||
end
|
||||
|
||||
describe "when passed name, values ..." do
|
||||
it "returns a sequence of 'radio'-elements with the passed name and the passed values" do
|
||||
output = CGISpecs.split(@html.radio_group("test", "foo", "bar", "baz"))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
end
|
||||
|
||||
it "allows passing a value inside an Array" do
|
||||
output = CGISpecs.split(@html.radio_group("test", ["foo"], "bar", ["baz"]))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
end
|
||||
|
||||
it "allows passing a value as an Array containing the value and the checked state or a label" do
|
||||
output = CGISpecs.split(@html.radio_group("test", ["foo"], ["bar", true], ["baz", "label for baz"]))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"CHECKED" => true, "NAME" => "test", "TYPE" => "radio", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "baz"}, "label for baz", not_closed: true)
|
||||
end
|
||||
|
||||
# TODO: CGI does not like passing false instead of true.
|
||||
it "allows passing a value as an Array containing the value, a label and the checked state" do
|
||||
output = CGISpecs.split(@html.radio_group("test", ["foo", "label for foo", true], ["bar", "label for bar", false], ["baz", "label for baz", true]))
|
||||
output[0].should equal_element("INPUT", {"CHECKED" => true, "NAME" => "test", "TYPE" => "radio", "VALUE" => "foo"}, "label for foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "bar"}, "label for bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"CHECKED" => true, "NAME" => "test", "TYPE" => "radio", "VALUE" => "baz"}, "label for baz", not_closed: true)
|
||||
end
|
||||
|
||||
it "returns an empty String when passed no values" do
|
||||
@html.radio_group("test").should == ""
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = CGISpecs.split(@html.radio_group("test", "foo", "bar", "baz") { "test" })
|
||||
output[0].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "test", "TYPE" => "radio", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Hash" do
|
||||
it "uses the passed Hash to generate the radio sequence" do
|
||||
output = CGISpecs.split(@html.radio_group("NAME" => "name", "VALUES" => ["foo", "bar", "baz"]))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "radio", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "radio", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "radio", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
|
||||
output = CGISpecs.split(@html.radio_group("NAME" => "name", "VALUES" => [["foo"], ["bar", true], "baz"]))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "radio", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"CHECKED" => true, "NAME" => "name", "TYPE" => "radio", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "radio", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
|
||||
output = CGISpecs.split(@html.radio_group("NAME" => "name", "VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"]))
|
||||
output[0].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "radio", "VALUE" => "1"}, "Foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"CHECKED" => true, "NAME" => "name", "TYPE" => "radio", "VALUE" => "2"}, "Bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "radio", "VALUE" => "Baz"}, "Baz", not_closed: true)
|
||||
end
|
||||
|
||||
it "ignores a passed block" do
|
||||
output = CGISpecs.split(@html.radio_group("NAME" => "name", "VALUES" => ["foo", "bar", "baz"]) { "test" })
|
||||
output[0].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "radio", "VALUE" => "foo"}, "foo", not_closed: true)
|
||||
output[1].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "radio", "VALUE" => "bar"}, "bar", not_closed: true)
|
||||
output[2].should equal_element("INPUT", {"NAME" => "name", "TYPE" => "radio", "VALUE" => "baz"}, "baz", not_closed: true)
|
||||
end
|
||||
end
|
||||
end
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue