test/arithmetic_test.rb in measured-1.6.0 vs test/arithmetic_test.rb in measured-2.0.0.pre1

- old
+ new

@@ -10,13 +10,13 @@ test "#+ should add together same units" do assert_equal Magic.new(5, :magic_missile), @two + @three assert_equal Magic.new(5, :magic_missile), @three + @two end - test "#+ should add a number to the value" do - assert_equal Magic.new(5, :magic_missile), @two + 3 - assert_equal Magic.new(5, :magic_missile), 2 + @three + test "#+ shouldn't add with a Integer" do + assert_raises(TypeError) { @two + 3 } + assert_raises(TypeError) { 2 + @three } end test "#+ should raise if different unit system" do assert_raises TypeError do OtherFakeSystem.new(1, :other_fake_base) + @two @@ -40,13 +40,13 @@ test "#- should subtract same units" do assert_equal Magic.new(-1, :magic_missile), @two - @three assert_equal Magic.new(1, :magic_missile), @three - @two end - test "#- should subtract a number from the value" do - assert_equal Magic.new(-1, :magic_missile), @two - 3 - assert_equal Magic.new(1, :magic_missile), 2 - @three + test "#- shouldn't subtract with a Integer" do + assert_raises(TypeError) { @two - 3 } + assert_raises(TypeError) { 2 - @three } end test "#- should raise if different unit system" do assert_raises TypeError do OtherFakeSystem.new(1, :other_fake_base) - @two @@ -65,69 +65,32 @@ assert_raises NoMethodError do "thing" - @two end end - test "#* should multiply together same units" do - assert_equal Magic.new(6, :magic_missile), @two * @three - assert_equal Magic.new(6, :magic_missile), @three * @two + test "#-@ returns the negative version" do + assert_equal Magic.new(-2, :magic_missile), -@two end - test "#* should multiply a number to the value" do - assert_equal Magic.new(6, :magic_missile), @two * 3 - assert_equal Magic.new(6, :magic_missile), 2 * @three + test "#scale should multiply the value by a given scalar" do + assert_equal Magic.new(-2, :magic_missile), @two.scale(-1) + assert_equal Magic.new(5, :magic_missile), @two.scale(2.5) end - test "#* should raise if different unit system" do - assert_raises TypeError do - OtherFakeSystem.new(1, :other_fake_base) * @two - end + test "arithmetic operations favours unit of left" do + left = Magic.new(1, :arcane) + right = Magic.new(1, :magic_missile) - assert_raises TypeError do - @two * OtherFakeSystem.new(1, :other_fake_base) - end + assert_equal "arcane", (left + right).unit + assert_equal "arcane", (left - right).unit end - test "#* should raise if multiplying something nonsense" do - assert_raises TypeError do - @two * "thing" - end - - assert_raises TypeError do - "thing" * @two - end + test "#coerce should return other as-is when same class" do + assert_equal [@two, @three], @three.coerce(@two) end - test "#/ should divide together same units" do - assert_equal Magic.new("0.5", :magic_missile), @two / @four - assert_equal Magic.new(2, :magic_missile), @four / @two - end - - test "#/ should divide a number to the value" do - assert_equal Magic.new("0.5", :magic_missile), @two / 4 - assert_equal Magic.new(2, :magic_missile), 2 / @four - end - - test "#/ should raise if different unit system" do - assert_raises TypeError do - OtherFakeSystem.new(1, :other_fake_base) / @two - end - - assert_raises TypeError do - @two / OtherFakeSystem.new(1, :other_fake_base) - end - end - - test "#/ should raise if dividing something nonsense" do - assert_raises TypeError do - @two / "thing" - end - - assert_raises NoMethodError do - "thing" / @two - end - end - - test "#-@ returns the negative version" do - assert_equal Magic.new(-2, :magic_missile), -@two + test "#coerce should raise TypeError when other cannot be coerced" do + assert_raises(TypeError) { @two.coerce(2) } + assert_raises(TypeError) { @three.coerce(5.2) } + assert_raises(TypeError) { @four.coerce(Object.new) } end end