shorthands for xywh! initial implementation
This commit is contained in:
+50
-33
@@ -2,38 +2,44 @@ 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: [:deck], height: [:deck] } }
|
||||
let(:deck) { OpenStruct.new(width: 123, height: 456, size: 1, dpi: 300.0) }
|
||||
let(:expected_defaults) { { x: [0], y: [0], width: [123], height: [456] } }
|
||||
|
||||
it 'intitially has no params set' do
|
||||
box = Squib::Args::Box.new
|
||||
expect(box).not_to respond_to(:x, :y, :width, :height)
|
||||
end
|
||||
|
||||
it 'extracts the defaults from Box on an empty hash' do
|
||||
box.load!({})
|
||||
args = {}
|
||||
box = Squib::Args.extract_box args, deck
|
||||
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: [:deck])
|
||||
args = {x: 4, width: 40}
|
||||
box = Squib::Args.extract_box args, deck
|
||||
expect(box).to have_attributes(x: [4], width: [40], y: [0], height: [456])
|
||||
end
|
||||
|
||||
it 'extracts the defaults from Box on an empty hash' do
|
||||
box.load!({ foo: :bar })
|
||||
args = { foo: :bar }
|
||||
box = Squib::Args.extract_box args, deck
|
||||
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) }
|
||||
let(:args) { { x: [1, 2], y: 3 } }
|
||||
let(:deck_of_2) { OpenStruct.new(width: 123, height: 456, size: 2) }
|
||||
let(:box) { Squib::Args.extract_box args, deck_of_2 }
|
||||
|
||||
it 'expands box' do
|
||||
expect(box).to have_attributes({
|
||||
x: [1, 2],
|
||||
y: [3, 3],
|
||||
height: [:deck, :deck],
|
||||
width: [:deck, :deck]
|
||||
width: [123, 123],
|
||||
height: [456, 456],
|
||||
})
|
||||
end
|
||||
|
||||
@@ -41,21 +47,23 @@ describe Squib::Args::Box do
|
||||
expect(box[0]).to have_attributes({
|
||||
x: 1,
|
||||
y: 3,
|
||||
height: :deck,
|
||||
width: :deck
|
||||
width: 123,
|
||||
height: 456,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
context 'layouts' do
|
||||
let(:layout) do
|
||||
{ 'attack' => { 'x' => 50 },
|
||||
'defend' => { 'x' => 60 } }
|
||||
end
|
||||
let(:deck_of_2) do
|
||||
OpenStruct.new(width: 123, height: 456, size: 2, layout: {
|
||||
'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)
|
||||
box = Squib::Args.extract_box args, deck_of_2
|
||||
expect(box).to have_attributes(
|
||||
x: [50, 60], # set by layout
|
||||
y: [0, 0], # Box default
|
||||
@@ -64,7 +72,7 @@ describe Squib::Args::Box do
|
||||
|
||||
it 'handle single expansion' do
|
||||
args = { layout: 'attack' }
|
||||
box.load!(args, expand_by: 2, layout: layout)
|
||||
box = Squib::Args.extract_box args, deck_of_2
|
||||
expect(box).to have_attributes(
|
||||
x: [50, 50], # set by layout
|
||||
y: [0, 0], # Box default
|
||||
@@ -73,7 +81,7 @@ describe Squib::Args::Box do
|
||||
|
||||
it 'handles symbols' do
|
||||
args = { layout: :attack }
|
||||
box.load!(args, expand_by: 2, layout: layout)
|
||||
box = Squib::Args.extract_box args, deck_of_2
|
||||
expect(box).to have_attributes(
|
||||
x: [50, 50], # set by layout
|
||||
y: [0, 0], # Box default
|
||||
@@ -83,7 +91,7 @@ describe Squib::Args::Box do
|
||||
it 'warns on non-existent layouts' do
|
||||
args = { layout: :heal }
|
||||
expect(Squib.logger).to receive(:warn).with('Layout "heal" does not exist in layout file - using default instead').at_least(:once)
|
||||
box.load!(args, expand_by: 2, layout: layout)
|
||||
box = Squib::Args.extract_box args, deck_of_2
|
||||
expect(box).to have_attributes(
|
||||
x: [0, 0], # Box default
|
||||
y: [0, 0], # Box default
|
||||
@@ -92,49 +100,58 @@ describe Squib::Args::Box do
|
||||
end
|
||||
|
||||
context 'unit conversion' do
|
||||
let(:deck_of_2) { OpenStruct.new(width: 123, height: 456, size: 2, dpi: 300) }
|
||||
|
||||
it 'converts units on all args' do
|
||||
args = { x: ['1in', '2in'], y: 300, width: '1in', height: '1in' }
|
||||
box.load!(args, expand_by: 2)
|
||||
box = Squib::Args.extract_box args, deck_of_2
|
||||
expect(box).to have_attributes(
|
||||
x: [300, 600],
|
||||
x: [300.0, 600.0],
|
||||
y: [300, 300],
|
||||
width: [300, 300],
|
||||
height: [300, 300],
|
||||
width: [300.0, 300.0],
|
||||
height: [300.0, 300.0],
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'validation' do
|
||||
let(:deck) { OpenStruct.new(width: 123, height: 456, size: 1) }
|
||||
|
||||
it 'replaces with deck width and height' do
|
||||
args = { width: :deck, height: :deck }
|
||||
box = Squib::Args::Box.new
|
||||
box.extract! args, deck
|
||||
box = Squib::Args.extract_box args, deck
|
||||
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.extract! args, deck
|
||||
box = Squib::Args.extract_box args, deck
|
||||
expect(box).to have_attributes(x_radius: [3], y_radius: [3])
|
||||
end
|
||||
|
||||
it 'listens to middle' do
|
||||
args = { width: :middle, height: 'middle' }
|
||||
box = Squib::Args::Box.new
|
||||
box.extract! args, deck
|
||||
box = Squib::Args.extract_box args, deck
|
||||
expect(box).to have_attributes(width: [61.5], height: [228.0])
|
||||
end
|
||||
|
||||
it 'listens to center' do
|
||||
args = { width: 'center', height: :center }
|
||||
box = Squib::Args::Box.new
|
||||
box.extract! args, deck
|
||||
box = Squib::Args.extract_box args, deck
|
||||
expect(box).to have_attributes(width: [61.5], height: [228.0])
|
||||
end
|
||||
|
||||
it 'listens to height/2' do
|
||||
args = { width: 'height / 2', height: :deck }
|
||||
box = Squib::Args.extract_box args, deck
|
||||
expect(box).to have_attributes(width: [228.0], height: [456])
|
||||
end
|
||||
|
||||
it 'listens to width - 0.5in' do
|
||||
args = { x: 'width - 0.5in'}
|
||||
box = Squib::Args.extract_box args, deck
|
||||
expect(box).to have_attributes(x: [ 123 - 150 ])
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
+11
-12
@@ -2,12 +2,13 @@ require 'spec_helper'
|
||||
require 'squib/args/scale_box'
|
||||
|
||||
describe Squib::Args::ScaleBox do
|
||||
subject(:box) { Squib::Args::ScaleBox.new }
|
||||
let(:deck) { OpenStruct.new(width: 123, height: 456, size: 1, dpi: 300.0) }
|
||||
|
||||
context 'unit conversion' do
|
||||
it 'converts units on all args' do
|
||||
deck_w_2 = OpenStruct.new(width: 123, height: 456, size: 2, dpi: 300.0)
|
||||
args = { x: ['1in', '2in'], y: 300, width: '1in', height: '1in' }
|
||||
box.load!(args, expand_by: 2)
|
||||
box = Squib::Args.extract_scale_box args, deck_w_2
|
||||
expect(box).to have_attributes(
|
||||
x: [300, 600],
|
||||
y: [300, 300],
|
||||
@@ -20,49 +21,47 @@ describe Squib::Args::ScaleBox do
|
||||
context 'validation' do
|
||||
it 'replaces with deck width and height' do
|
||||
args = { width: :deck, height: :deck }
|
||||
deck = OpenStruct.new(width: 123, height: 456)
|
||||
box = Squib::Args::Box.new(deck)
|
||||
box.load!(args, expand_by: 1)
|
||||
box = Squib::Args.extract_scale_box args, deck
|
||||
expect(box).to have_attributes(width: [123], height: [456])
|
||||
end
|
||||
|
||||
it 'allows :native' do
|
||||
args = { width: :native, height: :native }
|
||||
box.load!(args, expand_by: 1)
|
||||
box = Squib::Args.extract_scale_box args, deck
|
||||
expect(box).to have_attributes(width: [:native], height: [:native])
|
||||
end
|
||||
|
||||
it 'allows native to be a string' do
|
||||
args = { width: 'native' }
|
||||
box.load!(args, expand_by: 1)
|
||||
box = Squib::Args.extract_scale_box args, deck
|
||||
expect(box).to have_attributes(width: [:native], height: [:native])
|
||||
end
|
||||
|
||||
it 'allows :scale on width if height has to_f' do
|
||||
args = { width: :scale, height: 75 }
|
||||
box.load!(args, expand_by: 1)
|
||||
box = Squib::Args.extract_scale_box args, deck
|
||||
expect(box).to have_attributes(width: [:scale], height: [75])
|
||||
end
|
||||
|
||||
it 'allows :scale on width if height has to_f' do
|
||||
args = { width: 75, height: :scale }
|
||||
box.load!(args, expand_by: 1)
|
||||
box = Squib::Args.extract_scale_box args, deck
|
||||
expect(box).to have_attributes(width: [75], height: [:scale])
|
||||
end
|
||||
|
||||
it 'disallows both :scale' do
|
||||
args = { width: :scale, height: :scale }
|
||||
expect { box.load!(args, expand_by: 1) }.to raise_error('if width is :scale, height must be a number')
|
||||
expect { Squib::Args.extract_scale_box args, deck }.to raise_error('if width is :scale, height must be a number')
|
||||
end
|
||||
|
||||
it 'disallows non-to_f on width' do
|
||||
args = { width: :foo }
|
||||
expect { box.load!(args, expand_by: 1) }.to raise_error('width must be a number, :scale, :native, or :deck')
|
||||
expect { Squib::Args.extract_scale_box args, deck }.to raise_error('width must be a number, :scale, :native, or :deck')
|
||||
end
|
||||
|
||||
it 'disallows non-to_f on height' do
|
||||
args = { height: :foo }
|
||||
expect { box.load!(args, expand_by: 1) }.to raise_error('height must be a number, :scale, :native, or :deck')
|
||||
expect { Squib::Args.extract_scale_box args, deck }.to raise_error('height must be a number, :scale, :native, or :deck')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user