Browse Source

unit testing, more samples

dev
Andy Meneely 5 years ago
parent
commit
82afdf1608
  1. 2
      lib/squib/args/unit_conversion.rb
  2. 2
      samples/units/_cells.rb
  3. BIN
      samples/units/cells_00_expected.png
  4. 46
      spec/args/box_spec.rb
  5. 20
      spec/args/unit_conversion_spec.rb
  6. 26
      spec/args/xywh_shorthands_spec.rb

2
lib/squib/args/unit_conversion.rb

@ -4,7 +4,7 @@ require_relative '../constants'
module Squib
module Args
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
when /in$/ # ends with "in"
arg.rstrip[0..-2].to_f * dpi

2
samples/units/_cells.rb

@ -6,7 +6,7 @@ Squib::Deck.new(width: '1.5in', height: '1.5in') do
background color: :white
# 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 helps us lay things out in grids without doing much math in our heads
# Here's an example... with grid!

BIN
samples/units/cells_00_expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

46
spec/args/box_spec.rb

@ -2,7 +2,7 @@ require 'spec_helper'
require 'squib/args/box'
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] } }
it 'intitially has no params set' do
@ -33,7 +33,7 @@ describe Squib::Args::Box do
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],
@ -55,11 +55,11 @@ describe Squib::Args::Box do
context 'layouts' 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 },
'defend' => { 'x' => 60 },
})
end
end
it 'are used when not specified' do
args = { layout: ['attack', 'defend'] }
@ -113,6 +113,17 @@ describe Squib::Args::Box do
)
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
context 'validation' do
@ -128,25 +139,25 @@ describe Squib::Args::Box do
expect(box).to have_attributes(x_radius: [3], y_radius: [3])
end
it 'listens to middle' do
it 'listens to middle' do
args = { width: :middle, height: 'middle' }
box = Squib::Args.extract_box args, deck
expect(box).to have_attributes(width: [61.5], height: [228.0])
end
it 'listens to center' do
it 'listens to center' do
args = { width: 'center', height: :center }
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
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
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 ])
@ -154,5 +165,24 @@ describe Squib::Args::Box do
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

20
spec/args/unit_conversion_spec.rb

@ -34,5 +34,25 @@ describe Squib::Args::UnitConversion do
expect(subject.parse('30deg')).to be_within(0.0001).of(0.523599)
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

26
spec/args/xywh_shorthands_spec.rb

@ -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…
Cancel
Save