Permalink
Browse files

add Rect with basic implementation

  • Loading branch information...
moredip committed Nov 23, 2012
1 parent f3f991e commit 8b85b56472636b7bb2ba48f2a26faf39c4d6aba4
Showing with 49 additions and 0 deletions.
  1. +24 −0 gem/lib/frank-cucumber/rect.rb
  2. +25 −0 gem/test/rect_test.rb
@@ -0,0 +1,24 @@
+module Frank module Cucumber
+
+ class Rect
+ attr_reader :x, :y, :width, :height
+
+ def self.from_api_repr( hash )
+ x,y = hash["origin"]["x"], hash["origin"]["y"]
+ width,height = hash["size"]["width"],hash["size"]["height"]
+ self.new( x, y, width, height )
+ end
+
+ def initialize(x,y,width,height)
+ @x,@y,@width,@height = x,y,width,height
+ end
+
+ def center
+ OpenStruct.new(
+ x: @x.to_f + (@width.to_f/2),
+ y: @y.to_f + (@height.to_f/2)
+ )
+ end
+ end
+
+end end
View
@@ -0,0 +1,25 @@
+require_relative 'test_helper.rb'
+
+require_relative '../lib/frank-cucumber/rect'
+
+module Frank module Cucumber
+
+describe Rect do
+ it 'parsing from the api hash representation correctly' do
+ api_repr = { "origin" => {"x" => 1.1, "y" => 2.2}, "size" => { "height" => 11.1, "width" => 22.2 } }
+ rect = Rect.from_api_repr( api_repr )
+
+ rect.x.must_equal 1.1
+ rect.y.must_equal 2.2
+ rect.width.must_equal 22.2
+ rect.height.must_equal 11.1
+ end
+
+ it 'calculates the center correctly' do
+ rect = Rect.new( 100, 200, 11, 21 )
+ rect.center.x.must_equal 105.5
+ rect.center.y.must_equal 210.5
+ end
+end
+
+end end

0 comments on commit 8b85b56

Please sign in to comment.