Skip to content

Commit

Permalink
Bug fixes and TEST ALL THE THINGS
Browse files Browse the repository at this point in the history
  • Loading branch information
clayallsopp committed Oct 27, 2012
1 parent 0c9c2c1 commit 633e116
Show file tree
Hide file tree
Showing 6 changed files with 514 additions and 37 deletions.
10 changes: 7 additions & 3 deletions lib/geomotion/cg_point.rb
@@ -1,7 +1,7 @@
class CGPoint class CGPoint
# CGPoint.make(x: 10, y: 30) # CGPoint.make(x: 10, y: 30)
def self.make(options = {}) def self.make(options = {})
CGPoint.new([options[:x] || 0, options[:y] || 0]) CGPoint.new(options[:x] || 0, options[:y] || 0)
end end


# size = CGSize.make width: 100, height: 100 # size = CGSize.make width: 100, height: 100
Expand All @@ -21,12 +21,16 @@ def +(other)
end end
end end


def intersects?(rect) def round
CGPoint.new(self.x.round, self.y.round)
end

def inside?(rect)
CGRectContainsPoint(rect, self) CGRectContainsPoint(rect, self)
end end


def ==(point) def ==(point)
point.is_a? CGPoint && CGPointEqualToPoint(self, point) point.is_a?(CGPoint) && CGPointEqualToPoint(self, point)
end end


end end
25 changes: 12 additions & 13 deletions lib/geomotion/cg_rect.rb
Expand Up @@ -14,7 +14,8 @@ def self.make(options = {})
end end


def self.empty def self.empty
CGRectZero # Don't return CGRectZero; can be mutated
CGRect.make
end end


def self.null def self.null
Expand Down Expand Up @@ -156,23 +157,23 @@ def centered_in(rect, relative = false)
def +(other) def +(other)
case other case other
when CGRect when CGRect
return self.union(other) return self.union_with(other)
when CGSize when CGSize
return CGRect.new(self.x, self.y, self.width + other.width, self.height + other.height) return CGRect.new([self.x, self.y], [self.width + other.width, self.height + other.height])
when CGPoint when CGPoint
return CGRectOffset(self, other.x, other.y) return CGRectOffset(self, other.x, other.y)
when UIOffset when UIOffset
CGRectOffset(self, rect.horizontal, rect.vertical) CGRectOffset(self, other.horizontal, other.vertical)
when UIEdgeInsets when UIEdgeInsets
UIEdgeInsetsInsetRect(self, rect) UIEdgeInsetsInsetRect(self, other)
end end
end end


def intersection(rect) def intersection_with(rect)
CGRectIntersection(self, rect) CGRectIntersection(self, rect)
end end


def union(rect) def union_with(rect)
CGRectUnion(self, rect) CGRectUnion(self, rect)
end end


Expand Down Expand Up @@ -202,12 +203,10 @@ def null?
CGRectIsNull(self) CGRectIsNull(self)
end end


def intersects?(rect_or_point) def intersects?(rect)
case rect_or_point case rect
when CGPoint
CGRectContainsPoint(self, rect_or_point)
when CGRect when CGRect
CGRectIntersectsRect(self, rect_or_point) CGRectIntersectsRect(self, rect)
else else
super super
end end
Expand All @@ -225,6 +224,6 @@ def contains?(rect_or_point)
end end


def ==(rect) def ==(rect)
rect.is_a? CGRect && CGRectEqualToRect(self, rect) rect.is_a?(CGRect) && CGRectEqualToRect(self, rect)
end end
end end
10 changes: 7 additions & 3 deletions lib/geomotion/cg_size.rb
@@ -1,12 +1,12 @@
class CGSize class CGSize
# CGSize.make(width: 10, height: 30) # CGSize.make(width: 10, height: 30)
def self.make(options = {}) def self.make(options = {})
CGSize.new([options[:width] || 0, options[:height] || 0]) CGSize.new(options[:width] || 0, options[:height] || 0)
end end


def self.infinite def self.infinite
infinity = CGRect.null[0][0] infinity = CGRect.null[0][0]
CGSize(infinity, infinity) CGSizeMake(infinity, infinity)
end end


# size = CGSize.make width: 100, height: 100 # size = CGSize.make width: 100, height: 100
Expand All @@ -31,7 +31,11 @@ def infinite?
self.width == infinity or self.height == infinity self.width == infinity or self.height == infinity
end end


def empty?
self == CGSizeZero
end

def ==(size) def ==(size)
size.is_a? CGSize && CGSizeEqualToSize(self, size) size.is_a?(CGSize) && CGSizeEqualToSize(self, size)
end end
end end
65 changes: 65 additions & 0 deletions spec/cg_point_spec.rb
@@ -0,0 +1,65 @@
describe "CGPoint" do
before do
@point = CGPoint.make(x: 10, y: 20)
end

describe ".make" do
it "should work with options" do
CGPointEqualToPoint(@point, CGPointMake(10, 20)).should == true
end

it "should work with no options" do
CGPointEqualToPoint(CGPoint.make, CGPointMake(0, 0)).should == true
end
end

describe "#rect_of_size" do
it "should work" do
size = CGSizeMake(20, 30)
@point.rect_of_size(size).should == CGRectMake(10, 20, 20, 30)
end
end

describe "#+" do
it "should work with CGSize" do
size = CGSizeMake(20, 30)
(@point + size).should == CGRectMake(10, 20, 20, 30)
end

it "should work with CGPoint" do
point = CGPoint.make(x: 100, y: 200)
(@point + point).should == CGPointMake(110, 220)
end
end

describe "#inside?" do
it "should return true" do
rect = CGRectMake(0, 0, 100, 100)
@point.inside?(rect).should == true
end

it "should return false" do
rect = CGRectMake(0, 0, 5, 5)
@point.inside?(rect).should == false
end
end

describe "#round" do
it "should work" do
point = CGPointMake(10.4, 11.5).round
CGPointEqualToPoint(point, CGPointMake(10, 12)).should == true
end
end

describe "#==?" do
it "should return true" do
point = CGPointMake(10, 20)
@point.should == point
end

it "should return false" do
point = CGPointMake(20, 10)
@point.should != point
end
end
end

0 comments on commit 633e116

Please sign in to comment.