unit testing, more samples
parent
f8eaf757a2
commit
82afdf1608
|
|
@ -4,7 +4,7 @@ require_relative '../constants'
|
||||||
module Squib
|
module Squib
|
||||||
module Args
|
module Args
|
||||||
module UnitConversion
|
module UnitConversion
|
||||||
module_function def parse(arg, dpi=300, cell_px=75)
|
module_function def parse(arg, dpi=300, cell_px=37.5)
|
||||||
case arg.to_s.rstrip
|
case arg.to_s.rstrip
|
||||||
when /in$/ # ends with "in"
|
when /in$/ # ends with "in"
|
||||||
arg.rstrip[0..-2].to_f * dpi
|
arg.rstrip[0..-2].to_f * dpi
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ Squib::Deck.new(width: '1.5in', height: '1.5in') do
|
||||||
background color: :white
|
background color: :white
|
||||||
|
|
||||||
# Squib has a custom unit, called "cell"
|
# Squib has a custom unit, called "cell"
|
||||||
# A "cell" unit defaults to 75px, which at 300dpi is 1/8in or 3.175mm
|
# A "cell" unit defaults to 37.5px, which at 300dpi is 1/8in or 3.175mm
|
||||||
# This is a very common multiple for layouts.
|
# This is a very common multiple for layouts.
|
||||||
# This helps us lay things out in grids without doing much math in our heads
|
# This helps us lay things out in grids without doing much math in our heads
|
||||||
# Here's an example... with grid!
|
# Here's an example... with grid!
|
||||||
|
|
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 7.3 KiB |
|
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
||||||
require 'squib/args/box'
|
require 'squib/args/box'
|
||||||
|
|
||||||
describe Squib::Args::Box do
|
describe Squib::Args::Box do
|
||||||
let(:deck) { OpenStruct.new(width: 123, height: 456, size: 1, dpi: 300.0) }
|
let(:deck) { OpenStruct.new(width: 123, height: 456, size: 1, dpi: 300.0, cell_px: 37.5) }
|
||||||
let(:expected_defaults) { { x: [0], y: [0], width: [123], height: [456] } }
|
let(:expected_defaults) { { x: [0], y: [0], width: [123], height: [456] } }
|
||||||
|
|
||||||
it 'intitially has no params set' do
|
it 'intitially has no params set' do
|
||||||
|
|
@ -33,7 +33,7 @@ describe Squib::Args::Box do
|
||||||
let(:args) { { x: [1, 2], y: 3 } }
|
let(:args) { { x: [1, 2], y: 3 } }
|
||||||
let(:deck_of_2) { OpenStruct.new(width: 123, height: 456, size: 2) }
|
let(:deck_of_2) { OpenStruct.new(width: 123, height: 456, size: 2) }
|
||||||
let(:box) { Squib::Args.extract_box args, deck_of_2 }
|
let(:box) { Squib::Args.extract_box args, deck_of_2 }
|
||||||
|
|
||||||
it 'expands box' do
|
it 'expands box' do
|
||||||
expect(box).to have_attributes({
|
expect(box).to have_attributes({
|
||||||
x: [1, 2],
|
x: [1, 2],
|
||||||
|
|
@ -55,11 +55,11 @@ describe Squib::Args::Box do
|
||||||
|
|
||||||
context 'layouts' do
|
context 'layouts' do
|
||||||
let(:deck_of_2) do
|
let(:deck_of_2) do
|
||||||
OpenStruct.new(width: 123, height: 456, size: 2, layout: {
|
OpenStruct.new(width: 123, height: 456, size: 2, layout: {
|
||||||
'attack' => { 'x' => 50 },
|
'attack' => { 'x' => 50 },
|
||||||
'defend' => { 'x' => 60 },
|
'defend' => { 'x' => 60 },
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'are used when not specified' do
|
it 'are used when not specified' do
|
||||||
args = { layout: ['attack', 'defend'] }
|
args = { layout: ['attack', 'defend'] }
|
||||||
|
|
@ -113,6 +113,17 @@ describe Squib::Args::Box do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'handles cells' do
|
||||||
|
args = {x: '1c', y: '1c', width: '1c', height: '1c'}
|
||||||
|
box = Squib::Args.extract_box args, deck
|
||||||
|
expect(box).to have_attributes(
|
||||||
|
x: [37.5],
|
||||||
|
width: [37.5],
|
||||||
|
y: [37.5],
|
||||||
|
height: [37.5],
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'validation' do
|
context 'validation' do
|
||||||
|
|
@ -128,25 +139,25 @@ describe Squib::Args::Box do
|
||||||
expect(box).to have_attributes(x_radius: [3], y_radius: [3])
|
expect(box).to have_attributes(x_radius: [3], y_radius: [3])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'listens to middle' do
|
it 'listens to middle' do
|
||||||
args = { width: :middle, height: 'middle' }
|
args = { width: :middle, height: 'middle' }
|
||||||
box = Squib::Args.extract_box args, deck
|
box = Squib::Args.extract_box args, deck
|
||||||
expect(box).to have_attributes(width: [61.5], height: [228.0])
|
expect(box).to have_attributes(width: [61.5], height: [228.0])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'listens to center' do
|
it 'listens to center' do
|
||||||
args = { width: 'center', height: :center }
|
args = { width: 'center', height: :center }
|
||||||
box = Squib::Args.extract_box args, deck
|
box = Squib::Args.extract_box args, deck
|
||||||
expect(box).to have_attributes(width: [61.5], height: [228.0])
|
expect(box).to have_attributes(width: [61.5], height: [228.0])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'listens to height/2' do
|
it 'listens to height/2' do
|
||||||
args = { width: 'height / 2', height: :deck }
|
args = { width: 'height / 2', height: :deck }
|
||||||
box = Squib::Args.extract_box args, deck
|
box = Squib::Args.extract_box args, deck
|
||||||
expect(box).to have_attributes(width: [228.0], height: [456])
|
expect(box).to have_attributes(width: [228.0], height: [456])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'listens to width - 0.5in' do
|
it 'listens to width - 0.5in' do
|
||||||
args = { x: 'width - 0.5in'}
|
args = { x: 'width - 0.5in'}
|
||||||
box = Squib::Args.extract_box args, deck
|
box = Squib::Args.extract_box args, deck
|
||||||
expect(box).to have_attributes(x: [ 123 - 150 ])
|
expect(box).to have_attributes(x: [ 123 - 150 ])
|
||||||
|
|
@ -154,5 +165,24 @@ describe Squib::Args::Box do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'xywh shorthands' do
|
||||||
|
|
||||||
|
it 'handles shorthands' do
|
||||||
|
args = {
|
||||||
|
x: 'middle + 1c',
|
||||||
|
y: 'middle',
|
||||||
|
width: 'width - 2c',
|
||||||
|
height: 'height / 3'
|
||||||
|
}
|
||||||
|
box = Squib::Args.extract_box args, deck
|
||||||
|
expect(box).to have_attributes(
|
||||||
|
x: [99.0],
|
||||||
|
y: [228.0],
|
||||||
|
width: [48.0],
|
||||||
|
height: [152.0]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -34,5 +34,25 @@ describe Squib::Args::UnitConversion do
|
||||||
expect(subject.parse('30deg')).to be_within(0.0001).of(0.523599)
|
expect(subject.parse('30deg')).to be_within(0.0001).of(0.523599)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'does cells' do
|
||||||
|
expect(subject.parse('1c')).to eq(37.5)
|
||||||
|
expect(subject.parse('1cell')).to eq(37.5)
|
||||||
|
expect(subject.parse('1cells')).to eq(37.5)
|
||||||
|
expect(subject.parse('1 cells')).to eq(37.5)
|
||||||
|
expect(subject.parse('1 cells ')).to eq(37.5)
|
||||||
|
expect(subject.parse('2c')).to eq(75)
|
||||||
|
expect(subject.parse(' 0.5c')).to eq(18.75)
|
||||||
|
expect(subject.parse(' -0.5 c ')).to eq(-18.75)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when configured' do
|
||||||
|
it 'does mm @ dpi=100' do
|
||||||
|
expect(subject.parse('3.175mm', 100)).to be_within(0.001).of(12.5)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does cell @ cell_px=75' do
|
||||||
|
expect(subject.parse('1c', 100, 75)).to be_within(0.001).of(75)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
require 'squib/args/xywh_shorthands'
|
||||||
|
|
||||||
|
describe Squib::Args::XYWHShorthands do
|
||||||
|
|
||||||
|
let(:deck) { OpenStruct.new(width: 100, height: 200, size: 1, dpi: 300.0) }
|
||||||
|
|
||||||
|
it 'handles middle' do
|
||||||
|
args = {
|
||||||
|
x: 'middle',
|
||||||
|
y: 'middle + 1in',
|
||||||
|
width: 'width / 2',
|
||||||
|
height: 'height - 1in',
|
||||||
|
}
|
||||||
|
box = Squib::Args.extract_box args, deck
|
||||||
|
expect(box).to have_attributes({
|
||||||
|
x: [50.0],
|
||||||
|
y: [400.0],
|
||||||
|
width: [50.0],
|
||||||
|
height: [-100.0]
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
Loading…
Reference in New Issue