Browse Source

change x= to *=, add unit tests and improve existing tests

dev
Clarence "Sparr" Risher 9 years ago committed by Andy Meneely
parent
commit
8509ab5728
  1. 4
      docs/layouts.rst
  2. 4
      lib/squib/layout_parser.rb
  3. 8
      spec/data/layouts/extends-units-mixed.yml
  4. 9
      spec/layout_parser_spec.rb

4
docs/layouts.rst

@ -117,7 +117,7 @@ You can also **modify** data as they get passed through extends::
The following operators are supported within evaluating ``extends``
* ``+=`` will add the giuven number to the inherited number
* ``-=`` will subtract the given number from the inherited number
* ``x=`` will multiply the inherited number by the given number
* ``*=`` will multiply the inherited number by the given number
* ``/=`` will divide the inherited number by the given number
``+=`` and ``-=`` also support :doc:`/units`
@ -154,7 +154,7 @@ As you might expect, ``extends`` can be composed multiple times::
x: += 10 # evaluates to 110
aristotle:
extends: plato
x: x= 2 # evaluates to 220
x: "*= 2" # evaluates to 220, note that YAML requires quotes here
Yes, ``extends`` has Multiple Inheritance
-----------------------------------------

4
lib/squib/layout_parser.rb

@ -53,7 +53,7 @@ module Squib
add_parent_child(parent_val, child_val)
elsif child_val.to_s.strip.start_with?('-=')
sub_parent_child(parent_val, child_val)
elsif child_val.to_s.strip.start_with?('x=')
elsif child_val.to_s.strip.start_with?('*=')
mul_parent_child(parent_val, child_val)
elsif child_val.to_s.strip.start_with?('/=')
div_parent_child(parent_val, child_val)
@ -82,7 +82,7 @@ module Squib
def mul_parent_child(parent, child)
parent_pixels = Args::UnitConversion.parse(parent, @dpi).to_f
child_float = child.sub('x=', '').to_f
child_float = child.sub('*=', '').to_f
parent_pixels * child_float
end

8
spec/data/layouts/extends-units-mixed.yml

@ -1,8 +1,12 @@
parent:
x: 0.5in
x: 100
y: 1in
width: 200
height: 1cm
child:
extends: parent
x: += 1in
y: -= 0.5in
y: -= 1cm
width: "*= 1.5"
height: /= 4

9
spec/layout_parser_spec.rb

@ -206,8 +206,13 @@ describe Squib::LayoutParser do
it 'does mixed unit conversion when extending' do
layout = subject.load_layout(layout_file('extends-units-mixed.yml'))
expect(layout).to eq({
'parent' => { 'x' => '0.5in', 'y' => '1in'},
'child' => { 'x' => 450.0, 'y' => 150.0, 'extends' => 'parent' },
'parent' => {
'x' => 100, 'y' => '1in',
'width' => 200, 'height' => '1cm' },
'child' => {
'x' => 400.0, 'y' => 181.8897639,
'width' => 300.0, 'height' => 29.527559025,
'extends' => 'parent' },
})
end

Loading…
Cancel
Save