test/geometry/point.rb in geometry-6 vs test/geometry/point.rb in geometry-6.1

- old
+ new

@@ -1,9 +1,21 @@ require 'minitest/autorun' require 'geometry/point' describe Geometry::Point do + PointZero = Geometry::PointZero + + describe "class methods" do + it "must generate a PointZero" do + Point.zero.must_be_instance_of(PointZero) + end + + it "must generate a Point full of zeros" do + Point.zero(3).must_equal Point[0,0,0] + end + end + describe "constructor" do it "must return the Point when constructed from a Point" do original_point = Point[3,4] point = Geometry::Point[original_point] point.must_be_same_as original_point @@ -17,16 +29,10 @@ point = Geometry::Point[original_point] point.must_be_same_as original_point end end - it "create a Point object using list syntax" do - point = Geometry::Point[2,1] - assert_equal(2, point.size) - assert_equal(2, point.x) - assert_equal(1, point.y) - end it "create a Point object from an array" do point = Geometry::Point[[3,4]] assert_equal(2, point.size) assert_equal(3, point.x) assert_equal(4, point.y) @@ -42,16 +48,10 @@ assert_equal(2, point.size) assert_equal(3, point.x) assert_equal(4, point.y) end - it "create a Point object from a Vector using list syntax" do - point = Geometry::Point[Vector[3,4]] - assert_equal(2, point.size) - assert_equal(3, point.x) - assert_equal(4, point.y) - end it "create a Point object from a Point using list syntax" do point = Geometry::Point[Geometry::Point[13,14]] assert_equal(2, point.size) assert_equal(13, point.x) assert_equal(14, point.y) @@ -83,10 +83,20 @@ Point[1,2][0].must_equal 1 Point[1,2][1].must_equal 2 Point[1,2][2].must_equal nil end + it "must clone" do + Point[1,2].clone.must_be_instance_of(Point) + Point[1,2].clone.must_equal Point[1,2] + end + + it "must duplicate" do + Point[1,2].dup.must_be_instance_of(Point) + Point[1,2].dup.must_equal Point[1,2] + end + describe "arithmetic" do let(:left) { Point[1,2] } let(:right) { Point[3,4] } it "must have +@" do @@ -120,10 +130,17 @@ it "must return a Point when adding a Vector" do (left + Vector[5,6]).must_equal Point[6,8] (Vector[5,6] + right).must_equal Vector[8,10] end + it "must return self when adding a PointZero" do + (left + Point.zero).must_equal left + end + + it "must return self when adding a NilClass" do + (left + nil).must_equal left + end end describe "when subtracting" do it "return a Point when subtracting two Points" do assert_kind_of(Point, left-right) @@ -139,19 +156,43 @@ end it "must raise an exception when subtracting mismatched sizes" do lambda { left - [1,2,3,4] }.must_raise Geometry::DimensionMismatch end + + it "must return self when subtracting a PointZero" do + (left - Point.zero).must_equal left + end + + it "must return self when subtracting a NilClass" do + (left - nil).must_equal left + end end + + describe "when multiplying" do + it "must return a Point when multiplied by a Matrix" do + (Matrix[[1,2],[3,4]]*Point[5,6]).must_equal Point[17, 39] + end + end end describe "coercion" do + subject { Point[1,2] } + it "must coerce Arrays into Points" do - Point[1,2].coerce([3,4]).must_equal [Point[3,4], Point[1,2]] + subject.coerce([3,4]).must_equal [Point[3,4], subject] end it "must coerce Vectors into Points" do - Point[1,2].coerce(Vector[3,4]).must_equal [Point[3,4], Point[1,2]] + subject.coerce(Vector[3,4]).must_equal [Point[3,4], subject] + end + + it "must coerce a Numeric into a Point" do + subject.coerce(42).must_equal [Point[42,42], subject] + end + + it "must reject anything that can't be coerced" do + -> { subject.coerce(NilClass) }.must_raise TypeError end end describe "comparison" do let(:point) { Point[1,2] }