spec/cg_rect_spec.rb in geomotion-0.5 vs spec/cg_rect_spec.rb in geomotion-0.7.0

- old
+ new

@@ -140,10 +140,15 @@ describe "#x" do it "returns value when no args" do @rect.x.should == 10 end + it "returns min_x when width is negative" do + rect = CGRect.make(x: 110, y: 10, width: -100, height: 100) + rect.x.should == 10 + rect.origin.x.should == 110 + end it "returns copy when has args" do rect = @rect.x(20) rect.is_a?(CGRect).should == true CGRectEqualToRect(rect, CGRectMake(20, 100, 50, 20)).should == true @@ -160,10 +165,15 @@ describe "#y" do it "returns value when no args" do @rect.y.should == 100 end + it "returns min_y when height is negative" do + rect = CGRect.make(x: 10, y: 110, width: 100, height: -100) + rect.y.should == 10 + rect.origin.y.should == 110 + end it "returns copy when has args" do rect = @rect.y(20) rect.is_a?(CGRect).should == true CGRectEqualToRect(rect, CGRectMake(10, 20, 50, 20)).should == true @@ -180,11 +190,14 @@ describe "#width" do it "returns value when no args" do @rect.width.should == 50 end - + it "always returns positive width" do + rect = CGRect.make(x: 10, y: 110, width: -100, height: -100) + rect.width.should == 100 + end it "returns copy when has args" do rect = @rect.width(20) rect.is_a?(CGRect).should == true CGRectEqualToRect(rect, CGRectMake(10, 100, 20, 20)).should == true end @@ -200,11 +213,14 @@ describe "#height" do it "returns value when no args" do @rect.height.should == 20 end - + it "always returns positive height" do + rect = CGRect.make(x: 10, y: 110, width: -100, height: -100) + rect.height.should == 100 + end it "returns copy when has args" do rect = @rect.height(50) rect.is_a?(CGRect).should == true CGRectEqualToRect(rect, CGRectMake(10, 100, 50, 50)).should == true end @@ -216,10 +232,55 @@ rect.height = 50 rect.size.height.should == 50 end end + describe "#min_x, #mid_x, #max_x, #min_y, #mid_y, #max_y" do + before do + @min_rect = CGRect.make(x: 10, y: 10, width: 100, height: 100) + @min_rect_negative = CGRect.make(x: 110, y: 110, width: -100, height: -100) + end + + it "#min_x works" do + @min_rect.min_x.should == 10 + end + it "#mid_x works" do + @min_rect.mid_x.should == 60 + end + it "#max_x works" do + @min_rect.max_x.should == 110 + end + it "#min_x works with negative width" do + @min_rect_negative.min_x.should == 10 + end + it "#mid_x works with negative width" do + @min_rect_negative.mid_x.should == 60 + end + it "#max_x works with negative width" do + @min_rect_negative.max_x.should == 110 + end + + it "#min_y works" do + @min_rect.min_y.should == 10 + end + it "#mid_y works" do + @min_rect.mid_y.should == 60 + end + it "#max_y works" do + @min_rect.max_y.should == 110 + end + it "#min_y works with negative height" do + @min_rect_negative.min_y.should == 10 + end + it "#mid_y works with negative height" do + @min_rect_negative.mid_y.should == 60 + end + it "#max_y works with negative height" do + @min_rect_negative.max_y.should == 110 + end + end + describe "#left" do it "works" do rect = CGRect.empty rect = rect.left(20) rect.origin.x.should == -20 @@ -331,41 +392,133 @@ describe "#top_left" do it "works" do rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) point = rect.top_left point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(0, 0)).should == true + end + + it "works as relative" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.top_left(true) + point.is_a?(CGPoint).should == true CGPointEqualToPoint(point, CGPointMake(10, 20)).should == true end end - describe "#bottom_left" do + describe "#top_center" do it "works" do rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) - point = rect.bottom_left + point = rect.top_center point.is_a?(CGPoint).should == true - CGPointEqualToPoint(point, CGPointMake(10, 220)).should == true + CGPointEqualToPoint(point, CGPointMake(50, 0)).should == true end + + it "works as relative" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.top_center(true) + point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(60, 20)).should == true + end end describe "#top_right" do it "works" do rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) point = rect.top_right point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(100, 0)).should == true + end + + it "works as relative" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.top_right(true) + point.is_a?(CGPoint).should == true CGPointEqualToPoint(point, CGPointMake(110, 20)).should == true end end + describe "#center_right" do + it "works" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.center_right + point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(100, 100)).should == true + end + + it "works as relative" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.center_right(true) + point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(110, 120)).should == true + end + end + describe "#bottom_right" do it "works" do rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) point = rect.bottom_right point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(100, 200)).should == true + end + + it "works as relative" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.bottom_right(true) + point.is_a?(CGPoint).should == true CGPointEqualToPoint(point, CGPointMake(110, 220)).should == true end end + describe "#bottom_center" do + it "works" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.bottom_center + point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(50, 200)).should == true + end + + it "works as relative" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.bottom_center(true) + point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(60, 220)).should == true + end + end + + describe "#bottom_left" do + it "works" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.bottom_left + point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(0, 200)).should == true + end + + it "works as relative" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.bottom_left(true) + point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(10, 220)).should == true + end + end + + describe "#center_left" do + it "works" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.center_left + point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(0, 100)).should == true + end + + it "works as relative" do + rect = CGRect.make(x: 10, y: 20, width: 100, height: 200) + point = rect.center_left(true) + point.is_a?(CGPoint).should == true + CGPointEqualToPoint(point, CGPointMake(10, 120)).should == true + end + end + describe "#center" do it "works" do point = CGRect.make(width: 100, height: 100).center point.is_a?(CGPoint).should == true CGPointEqualToPoint(point, CGPointMake(50, 50)).should == true @@ -441,16 +594,61 @@ rect = (@rect + offset) CGRectEqualToRect(rect, CGRectMake(60, 120, 50, 20)).should == true end it "works with UIEdgeInsets" do - inset = UIEdgeInsetsMake(10, 10, 10, 5) - rect = (@rect + inset) + insets = UIEdgeInsetsMake(10, 10, 10, 5) + rect = (@rect + insets) CGRectEqualToRect(rect, CGRectMake(20, 110, 35, 0)).should == true end end + describe "offset" do + it "works with x, y" do + rect = @rect.offset(50, 20) + CGRectEqualToRect(rect, CGRectMake(60, 120, 50, 20)).should == true + end + + it "works with CGPoint" do + point = CGPointMake(50, 20) + rect = @rect.offset(point) + CGRectEqualToRect(rect, CGRectMake(60, 120, 50, 20)).should == true + end + + it "works with UIOffset" do + offset = UIOffsetMake(50, 20) + rect = @rect.offset(offset) + CGRectEqualToRect(rect, CGRectMake(60, 120, 50, 20)).should == true + end + end + + describe "inset" do + it "works with UIEdgeInsets" do + insets = UIEdgeInsetsMake(10, 10, 10, 5) + rect = @rect.inset(insets) + CGRectEqualToRect(rect, CGRectMake(20, 110, 35, 0)).should == true + end + end + + describe "#*" do + it "should work with Numeric" do + rect = CGRectMake(0, 0, 12, 24) + bigger = rect * 3 + bigger.size.width.should == 36 + bigger.size.height.should == 72 + end + end + + describe "#/" do + it "should work with Numeric" do + rect = CGRectMake(0, 0, 12, 24) + smaller = rect / 3 + smaller.size.width.should == 4 + smaller.size.height.should == 8 + end + end + describe "#intersection_with" do it "should work" do lower_rect = CGRectMake(0, 0, 100, 100) upper_rect = CGRectMake(10, 10, 100, 100) rect = lower_rect.intersection_with(upper_rect) @@ -487,7 +685,12 @@ it "should work with CGSize" do rect = @rect.shrink(CGSizeMake(20, 10)) CGRectEqualToRect(rect, CGRectMake(30, 110, 10, 0)).should == true end + + it "should work with Array" do + rect = @rect.shrink([20, 10]) + CGRectEqualToRect(rect, CGRectMake(30, 110, 10, 0)).should == true + end end -end \ No newline at end of file +end