First foray into args class refactoring: rect

So far, I really like this approach. It allows us to push validation logic out of the Card class and into its own set of classes that can be more easily tested.

Fixes #75, but we've got a lot more work to do in getting this full scale
This commit is contained in:
Andy Meneely
2015-06-08 13:36:23 -04:00
parent 6c60f21bb0
commit c1e410fe72
8 changed files with 341 additions and 14 deletions
+108
View File
@@ -0,0 +1,108 @@
require 'spec_helper'
require 'squib/args/box'
describe Squib::Args::Box do
subject(:box) { Squib::Args::Box.new }
let(:expected_defaults) { {x: [0], y: [0], width: [:native], height: [:native] } }
it 'intitially has no params set' do
expect(box).not_to respond_to(:x, :y, :width, :height)
end
it 'extracts the defaults from Box on an empty hash' do
box.load!({})
expect(box).to have_attributes(expected_defaults)
end
it 'extracts what is specified and fills in defaults from Box' do
box.load!(x: 4, width: 40)
expect(box).to have_attributes(x: [4], width: [40], y: [0], height: [:native])
end
it 'extracts the defaults from Box on an empty hash' do
box.load!({foo: :bar})
expect(box).to have_attributes(expected_defaults)
expect(box).not_to respond_to(:foo)
end
context 'single expansion' do
let(:args) { {x: [1, 2], y: 3} }
before(:each) { box.load!(args, expand_by: 2) }
it 'expands box' do
expect(box).to have_attributes({
x: [1, 2],
y: [3, 3],
height: [:native, :native],
width: [:native, :native]
})
end
it 'gives access to each card too' do
expect(box[0]).to have_attributes({
x: 1,
y: 3,
height: :native,
width: :native
})
end
end
context 'layouts' do
let(:layout) do
{ 'attack' => { x: 50 },
'defend' => { x: 60 } }
end
it 'are used when not specified' do
args = { layout: ['attack', 'defend'] }
box.load!(args, expand_by: 2, layout: layout)
expect(box).to have_attributes(
x: [50, 60], # set by layout
y: [0, 0], # Box default
)
end
it 'handle single expansion' do
args = { layout: 'attack' }
box.load!(args, expand_by: 2, layout: layout)
expect(box).to have_attributes(
x: [50, 50], # set by layout
y: [0, 0], # Box default
)
end
end
context 'unit conversion' do
it 'converts units on all args' do
args = {x: ['1in', '2in'], y: 300, width: '1in', height: '1in'}
box.load!(args, expand_by: 2)
expect(box).to have_attributes(
x: [300, 600],
y: [300, 300],
width: [300, 300],
height: [300, 300],
)
end
end
context 'validation' do
it 'replaces with deck width and height' do
args = {width: :native, height: :native}
deck = OpenStruct.new(width: 123, height: 456)
box = Squib::Args::Box.new(deck)
box.load!(args, expand_by: 1)
expect(box).to have_attributes(width: [123], height: [456])
end
it 'has radius override x_radius and y_radius' do
args = {x_radius: 1, y_radius: 2, radius: 3}
box.load!(args, expand_by: 2)
expect(box).to have_attributes(x_radius: [3, 3], y_radius: [3, 3])
end
end
end
+4 -3
View File
@@ -32,9 +32,10 @@ describe Squib::Card do
expect(cxt).to receive(:restore).once
card = Squib::Card.new(deck, 100, 150)
# rect(x, y, width, height, x_radius, y_radius,
# fill_color, stroke_color, stroke_width)
card.rect(37, 38, 50, 100, 10, 15, '#fff', '#f00', 2.0)
# rect(Args::Box, x_radius, y_radius, Args::Draw)
box = OpenStruct.new(x: 37, y: 38, width: 50, height: 100, x_radius: 10, y_radius: 15)
draw = OpenStruct.new(fill_color: '#fff', stroke_color: '#f00', stroke_width: 2.0)
card.rect(box, draw)
end
end