Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

convert many tests around unit math to specs

  • Loading branch information...
commit 1853ef20ffd35e67eb69380c84f53a7ebab050d4 1 parent 8b7b691
@olbrich authored
Showing with 25 additions and 135 deletions.
  1. +24 −0 spec/ruby-units/unit_spec.rb
  2. +1 −135 test/test_ruby-units.rb
View
24 spec/ruby-units/unit_spec.rb
@@ -836,6 +836,7 @@
context "between incompatible units" do
specify { expect {Unit("10 kg") + Unit("10 m")}.to raise_error(ArgumentError)}
specify { expect {Unit("10 m") + Unit("10 kg")}.to raise_error(ArgumentError)}
+ specify { expect {Unit("10 m") + nil}.to raise_error(ArgumentError)}
end
context "a number from a unit" do
@@ -843,6 +844,11 @@
specify { expect { 10 + Unit("10 kg") }.to raise_error(ArgumentError)}
end
+ context "between a unit and coerceable types" do
+ specify { (Unit('10 kg') + %w{1 kg}).should == Unit('11 kg') }
+ specify { (Unit('10 kg') + "1 kg").should == Unit('11 kg') }
+ end
+
context "between two temperatures" do
specify { expect {(Unit("100 tempK") + Unit("100 tempK"))}.to raise_error(ArgumentError,"Cannot add two temperatures") }
end
@@ -871,6 +877,12 @@
context "incompatible units" do
specify { expect {Unit("10 kg") - Unit("10 m")}.to raise_error(ArgumentError)}
specify { expect {Unit("10 m") - Unit("10 kg")}.to raise_error(ArgumentError)}
+ specify { expect {Unit("10 m") - nil}.to raise_error(ArgumentError)}
+ end
+
+ context "between a unit and coerceable types" do
+ specify { (Unit('10 kg') - %w{1 kg}).should == Unit('9 kg') }
+ specify { (Unit('10 kg') - "1 kg").should == Unit('9 kg') }
end
context "a number from a unit" do
@@ -901,6 +913,12 @@
context "between incompatible units" do
specify { (Unit("0 m") * Unit("10 kg")).should == Unit("0 kg*m")}
specify { (Unit("5 m") * Unit("10 kg")).should == Unit("50 kg*m")}
+ specify { expect {Unit("10 m") * nil}.to raise_error(ArgumentError)}
+ end
+
+ context "between a unit and coerceable types" do
+ specify { (Unit('10 kg') * %w{1 kg}).should == Unit('10 kg^2') }
+ specify { (Unit('10 kg') * "1 kg").should == Unit('10 kg^2') }
end
context "by a temperature" do
@@ -922,8 +940,14 @@
context "incompatible units" do
specify { (Unit("0 m") / Unit("10 kg")).should == Unit("0 m/kg")}
specify { (Unit("5 m") / Unit("10 kg")).should == Unit("1/2 m/kg")}
+ specify { expect {Unit("10 m") / nil}.to raise_error(ArgumentError)}
end
+ context "between a unit and coerceable types" do
+ specify { (Unit('10 kg^2') / %w{1 kg}).should == Unit('10 kg') }
+ specify { (Unit('10 kg^2') / "1 kg").should == Unit('10 kg') }
+ end
+
context "by a temperature" do
specify { expect { Unit("5 kg") / Unit("100 tempF")}.to raise_exception(ArgumentError) }
end
View
136 test/test_ruby-units.rb
@@ -105,146 +105,12 @@ def test_create_from_array
assert_equal ['<milli>','<meter>','<milli>','<meter>'], unit1.numerator
assert_equal ['<micro>','<liter>','<micro>','<liter>'], unit1.denominator
end
-
- def test_matched_units_using_string
- unit1 = Unit.new("1 m*kg/s")
- assert unit1 =~ "in*pound/min"
- end
-
- def test_comparison_like_units
- unit1 = Unit.new("1 in")
- unit2 = Unit.new("1 mm")
- assert_nothing_raised {
- assert unit1 > unit2
- }
- end
-
- def test_comparison_unlike_units
- unit1 = Unit.new("1 kg")
- unit2 = Unit.new("1 mm")
- assert_raise(ArgumentError) { unit1 > unit2 }
- end
-
- def test_add_like_units
- unit1 = Unit.new("1 mm")
- unit2 = Unit.new("2 mm")
- assert_nothing_raised {
- assert_equal 3.0, (unit1+unit2).scalar
- }
- end
-
- def test_add_similar_units
- unit1 = Unit.new("1 cm")
- unit2 = Unit.new("1 in")
- assert_nothing_raised {
- unit3 = unit1 + unit2
- assert_in_delta 3.54, unit3.scalar, 0.01
- }
- end
-
- def test_subtract_similar_units
- unit1 = Unit.new("1 cm")
- unit2 = Unit.new("1 in")
- assert_nothing_raised {
- unit3 = unit1 - unit2
- assert_in_delta(-1.54, unit3.scalar, 0.01)
- }
- end
-
- def test_add_unlike_units
- unit1 = Unit.new("1 mm")
- unit2 = Unit.new("2 ml")
- assert_raise(ArgumentError) {(unit1+unit2).scalar}
- end
-
- def test_add_coerce
- unit1 = "1 mm".unit
- assert_nothing_raised {
- unit2 = unit1 + "1 mm"
- assert_equal "2 mm".unit, unit2
- }
- assert_nothing_raised {
- unit2 = unit1 + [1,"mm"]
- assert_equal "2 mm".unit, unit2
- }
- assert_nothing_raised {
- unit2 = "1".unit + 1
- assert_equal "2".unit, unit2
- }
- assert_raises(ArgumentError) {
- "1".unit + nil
- }
- end
- def test_subtract_coerce
- unit1 = "1 mm".unit
- assert_nothing_raised {
- unit2 = unit1 - "1 mm"
- assert_equal "0 mm".unit, unit2
- }
- end
- def test_multiply_coerce
- unit1 = "1 mm".unit
- assert_nothing_raised {
- unit2 = unit1 * "1 mm"
- assert_equal "1 mm^2".unit, unit2
- }
- end
- def test_divide_coerce
- unit1 = "1 mm".unit
- assert_nothing_raised {
- unit2 = unit1 / "1 mm"
- assert_equal "1".unit, unit2
- }
- end
-
def test_signature #"1 m s deg K kg A mol cd byte rad
unit1 = Unit.new("1 m*s*degK*kg*A*mol*cd*byte*rad*dollar")
assert_equal unit1.signature, (0..9).inject(0) {|product, n| product + 20**n}
end
-
- def test_subtract_like_units
- unit1 = Unit.new("1 mm")
- unit2 = Unit.new("2 mm")
- assert_nothing_raised {
- assert_equal(-1, (unit1-unit2).scalar)
- }
- end
-
- def test_subtract_unlike_units
- unit1 = Unit.new("1 mm")
- unit2 = Unit.new("2 ml")
- assert_raise(ArgumentError) {(unit1-unit2).scalar}
- end
-
- def test_multiply
- unit1 = Unit.new("1 m/ms")
- unit2 = Unit.new("1 m/ms")
- assert_nothing_raised {
- unit3 = unit1 * unit2
- assert_equal Unit.new("1 m^2/ms^2"), unit3
- }
- assert_equal unit1 * 0, '0 m/ms'.unit
- end
-
- def test_divide
- unit1 = Unit.new("200 M*g/mol")
- unit2 = Unit.new("200 g/mole")
- assert_nothing_raised {
- unit3 = unit1 / unit2
- assert_equal Unit.new("1 M"), unit3
- }
- assert_equal unit2 / 1, unit2
- unit3 = '0 s'.unit
- assert_raises(ZeroDivisionError) {
- unit1 / unit3
- }
-
- assert_raises(ZeroDivisionError) {
- unit1 / 0
- }
- end
-
+
def test_inverse
unit1 = Unit.new("1 m")
unit2 = Unit.new("1 1/m")
Please sign in to comment.
Something went wrong with that request. Please try again.