styles 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/.gitignore +18 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +725 -0
  5. data/Rakefile +9 -0
  6. data/bin/styles +11 -0
  7. data/lib/styles.rb +18 -0
  8. data/lib/styles/application.rb +190 -0
  9. data/lib/styles/colors.rb +289 -0
  10. data/lib/styles/core_ext.rb +12 -0
  11. data/lib/styles/engine.rb +73 -0
  12. data/lib/styles/line.rb +55 -0
  13. data/lib/styles/properties.rb +34 -0
  14. data/lib/styles/properties/background_color.rb +15 -0
  15. data/lib/styles/properties/base.rb +68 -0
  16. data/lib/styles/properties/border.rb +147 -0
  17. data/lib/styles/properties/color.rb +16 -0
  18. data/lib/styles/properties/display.rb +10 -0
  19. data/lib/styles/properties/font_weight.rb +13 -0
  20. data/lib/styles/properties/function.rb +7 -0
  21. data/lib/styles/properties/margin.rb +83 -0
  22. data/lib/styles/properties/match_background_color.rb +28 -0
  23. data/lib/styles/properties/match_color.rb +21 -0
  24. data/lib/styles/properties/match_font_weight.rb +23 -0
  25. data/lib/styles/properties/match_text_decoration.rb +36 -0
  26. data/lib/styles/properties/padding.rb +81 -0
  27. data/lib/styles/properties/text_align.rb +10 -0
  28. data/lib/styles/properties/text_decoration.rb +20 -0
  29. data/lib/styles/properties/width.rb +11 -0
  30. data/lib/styles/rule.rb +67 -0
  31. data/lib/styles/stylesheet.rb +103 -0
  32. data/lib/styles/sub_engines.rb +4 -0
  33. data/lib/styles/sub_engines/base.rb +16 -0
  34. data/lib/styles/sub_engines/color.rb +115 -0
  35. data/lib/styles/sub_engines/layout.rb +158 -0
  36. data/lib/styles/sub_engines/pre_processor.rb +19 -0
  37. data/lib/styles/version.rb +3 -0
  38. data/styles.gemspec +26 -0
  39. data/test/application_test.rb +92 -0
  40. data/test/colors_test.rb +162 -0
  41. data/test/engine_test.rb +59 -0
  42. data/test/integration_test.rb +136 -0
  43. data/test/line_test.rb +24 -0
  44. data/test/properties/background_color_test.rb +36 -0
  45. data/test/properties/base_test.rb +11 -0
  46. data/test/properties/border_test.rb +154 -0
  47. data/test/properties/color_test.rb +28 -0
  48. data/test/properties/display_test.rb +26 -0
  49. data/test/properties/font_weight_test.rb +24 -0
  50. data/test/properties/function_test.rb +28 -0
  51. data/test/properties/margin_test.rb +98 -0
  52. data/test/properties/match_background_color_test.rb +71 -0
  53. data/test/properties/match_color_test.rb +79 -0
  54. data/test/properties/match_font_weight_test.rb +34 -0
  55. data/test/properties/match_text_decoration_test.rb +38 -0
  56. data/test/properties/padding_test.rb +87 -0
  57. data/test/properties/text_align_test.rb +107 -0
  58. data/test/properties/text_decoration_test.rb +25 -0
  59. data/test/properties/width_test.rb +41 -0
  60. data/test/rule_test.rb +39 -0
  61. data/test/stylesheet_test.rb +245 -0
  62. data/test/sub_engines/color_test.rb +144 -0
  63. data/test/sub_engines/layout_test.rb +110 -0
  64. data/test/test_helper.rb +5 -0
  65. metadata +184 -0
@@ -0,0 +1,26 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ class DisplayTest < MiniTest::Unit::TestCase
4
+ def test_line_passes_through_with_show_values
5
+ test_line = 'this is a test line'
6
+
7
+ assert_equal test_line, process('test', :block, test_line)
8
+ assert_equal test_line, process('test', true, test_line)
9
+ end
10
+
11
+ def test_line_hidden_with_hide_values
12
+ test_line = 'this is a test line'
13
+
14
+ assert_nil process('test', :none, test_line)
15
+ assert_nil process('test', false, test_line)
16
+ end
17
+
18
+ private
19
+
20
+ def process(selector, value, line)
21
+ sub_engine = ::Styles::SubEngines::Layout.new
22
+ line = ::Styles::Line.new(line, [::Styles::Properties::Display.new(selector, :display, value)])
23
+ result = sub_engine.process(line)
24
+ result.text
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ require 'term/ansicolor'
3
+
4
+ class FontWeightTest < MiniTest::Unit::TestCase
5
+ def test_can_embolden_a_line
6
+ test_line = 'this is a test line'
7
+
8
+ assert_equal "#{color.bold}#{test_line}#{color.reset}", process('test', :bold, test_line)
9
+ assert_equal test_line, process('test', :normal, test_line)
10
+ assert_equal test_line, process('test', :invalid, test_line)
11
+ end
12
+
13
+ private
14
+
15
+ def process(selector, value, line)
16
+ sub_engine = ::Styles::SubEngines::Color.new
17
+ line = ::Styles::Line.new(line, [::Styles::Properties::FontWeight.new(selector, :font_weight, value)])
18
+ sub_engine.process(line).to_s
19
+ end
20
+
21
+ def color
22
+ ::Term::ANSIColor
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ class FunctionTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @sub_engine = ::Styles::SubEngines::PreProcessor.new
6
+ end
7
+
8
+ def test_all_callables_work
9
+ test_line = 'this is a line'
10
+ assert_equal test_line, process('line', :function, ->(line) { line }, test_line)
11
+ assert_equal test_line, process('line', :function, Proc.new {|line| line }, test_line)
12
+ assert_equal test_line, process('line', :function, proc {|line| line }, test_line)
13
+ end
14
+
15
+ def test_arbitrary_functions
16
+ test_line = 'this is a line'
17
+ assert_equal nil, process('line', :function, ->(line) { line.include?('this') ? nil : line }, test_line)
18
+ assert_equal test_line.upcase, process('line', :function, ->(line) { line.upcase }, test_line)
19
+ end
20
+
21
+ private
22
+
23
+ def process(selector, property_name, value, line)
24
+ line = ::Styles::Line.new(line, [::Styles::Properties::Function.new(selector, property_name, value)])
25
+ result = @sub_engine.process(line)
26
+ result.text
27
+ end
28
+ end
@@ -0,0 +1,98 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ class MarginTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @sub_engine = ::Styles::SubEngines::Layout.new
6
+ end
7
+
8
+ def test_can_use_all_property_names
9
+ [:margin, :margin_left, :margin_right, :margin_top, :margin_bottom].each do |prop_name|
10
+ assert_equal ::Styles::Properties::Margin, ::Styles::Properties.find_class_by_property_name(prop_name)
11
+ end
12
+ end
13
+
14
+ def test_gives_correct_margin_values
15
+ ml = prop(:margin_left, 2)
16
+ assert_equal 0, ml.top
17
+ assert_equal 0, ml.right
18
+ assert_equal 0, ml.bottom
19
+ assert_equal 2, ml.left
20
+ assert_equal [0, 0, 0, 2], ml.all_margins
21
+
22
+ assert_equal [4, 4, 4, 4], prop(:margin, 4).all_margins
23
+ assert_equal [2, 2, 2, 2], prop(:margin, 2).all_margins
24
+ assert_equal [5, 0, 0, 0], prop(:margin_top, 5).all_margins
25
+ assert_equal [0, 3, 0, 0], prop(:margin_right, 3).all_margins
26
+ assert_equal [0, 0, 10, 0], prop(:margin_bottom, 10).all_margins
27
+
28
+ assert_equal [0, 0, 0, 0], prop(:margin, :none).all_margins
29
+ assert_equal [0, 0, 0, 0], prop(:margin, :wat).all_margins
30
+ assert_equal [0, 0, 0, 0], prop(:margin_right, :none).all_margins
31
+
32
+ assert_equal [:auto, :auto, :auto, :auto], prop(:margin, :auto).all_margins
33
+ end
34
+
35
+ def test_can_configure_with_side_values
36
+ assert_equal [1, 2, 3, 4], prop(:margin, '1 2 3 4').all_margins
37
+ assert_equal [1, 2, 3, 0], prop(:margin, '1 2 3').all_margins
38
+ assert_equal [1, 2, 0, 0], prop(:margin, '1 2').all_margins
39
+ assert_equal [1, 0, 0, 0], prop(:margin, '1').all_margins
40
+
41
+ assert_equal [1, :auto, 0, 0], prop(:margin, '1 auto').all_margins
42
+ assert_equal [1, :auto, 3, :auto], prop(:margin, '1 auto 3 auto').all_margins
43
+ assert_equal [1, 0, 3, 0], prop(:margin, '1 wat 3 huh').all_margins
44
+ end
45
+
46
+ def test_can_combine_margin_properties
47
+ assert_equal [0, 3, 0, 2], combine([prop(:margin_left, 2), prop(:margin_right, 3)]).all_margins
48
+ assert_equal [0, 0, 0, 2], combine([prop(:margin_left, 2)]).all_margins
49
+ assert_equal [5, 2, 2, 2], combine([prop(:margin, 2), prop(:margin_top, 5)]).all_margins
50
+ assert_equal [5, 4, 2, 2], combine([prop(:margin, 2), prop(:margin_top, 5), prop(:margin_right, 4)]).all_margins
51
+ assert_equal [2, 2, 2, 2], combine([prop(:margin_top, 5), prop(:margin, 2)]).all_margins
52
+ assert_equal [0, 2, 2, 2], combine([prop(:margin, 2), prop(:margin_top, 0)]).all_margins
53
+
54
+ assert_equal [4, 0, 0, 0], combine([prop(:margin, :none), prop(:margin_top, 4)]).all_margins
55
+ assert_equal [0, 4, 4, 4], combine([prop(:margin, 4), prop(:margin_top, :none)]).all_margins
56
+
57
+ no_top = combine([prop(:margin, 3), prop(:margin_top, 0)])
58
+ left_and_right = combine([prop(:margin_left, 2), prop(:margin_right, 5)])
59
+ assert_equal [0, 5, 3, 2], combine([no_top, left_and_right]).all_margins
60
+ end
61
+
62
+ def test_margin_left
63
+ test_line = 'line needs margin'
64
+ stub_term_width(80)
65
+ assert_equal " #{test_line}", process('line', :margin_left, 2, test_line)
66
+ assert_equal " #{test_line}", process('line', :margin_left, 10, test_line)
67
+ end
68
+
69
+ def test_auto
70
+ test_line = 'the line'
71
+ stub_term_width(20)
72
+ assert_equal " #{test_line} ", process('line', :margin, :auto, test_line)
73
+ assert_equal " #{test_line} ", process('line', :margin_left, :auto, test_line)
74
+ assert_equal " #{test_line} ", process('line', :margin_right, :auto, test_line)
75
+ assert_equal " #{test_line} ", process('line', :margin, '0 auto', test_line)
76
+ assert_equal " #{test_line} ", process('line', :margin, '0 auto 0 auto', test_line)
77
+ end
78
+
79
+ private
80
+
81
+ def process(selector, property_name, value, line)
82
+ line = ::Styles::Line.new(line, [::Styles::Properties::Margin.new(selector, property_name, value)])
83
+ result = @sub_engine.process(line)
84
+ result.to_s
85
+ end
86
+
87
+ def prop(name, value, selector='test')
88
+ ::Styles::Properties::Margin.new(selector, name, value)
89
+ end
90
+
91
+ def combine(*props)
92
+ ::Styles::Properties::Margin.new(props.flatten)
93
+ end
94
+
95
+ def stub_term_width(width)
96
+ @sub_engine.stubs(:terminal_width).returns(width)
97
+ end
98
+ end
@@ -0,0 +1,71 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ require 'term/ansicolor'
3
+
4
+ class MatchBackgroundColorTest < MiniTest::Unit::TestCase
5
+ def test_applies_correct_colors_to_string_selector_matches
6
+ test_line = 'this is a test line'
7
+
8
+ assert_equal "this is a #{color.on_red}test#{color.reset} line",
9
+ process('test', :red, test_line)
10
+ assert_equal "#{color.on_blue}this is#{color.reset} a test line",
11
+ process('this is', :blue, test_line)
12
+ end
13
+
14
+ def test_applies_correct_colors_to_multiple_string_selector_matches
15
+ assert_equal "#{color.on_green}the#{color.reset} word #{color.on_green}the#{color.reset} is repeated",
16
+ process('the', :green, 'the word the is repeated')
17
+ end
18
+
19
+ def test_applies_correct_colors_to_regex_matches
20
+ assert_equal "the number #{color.on_green}89#{color.reset} is in this line",
21
+ process(/\d\d/, :green, 'the number 89 is in this line')
22
+ end
23
+
24
+ def test_applies_correct_colors_to_multiple_regex_matches
25
+ assert_equal "the numbers #{color.on_blue}89#{color.reset} and #{color.on_blue}22#{color.reset} are in this line",
26
+ process(/\d\d/, :blue, 'the numbers 89 and 22 are in this line')
27
+ end
28
+
29
+ def test_can_use_multiple_colors_to_match_groups
30
+ assert_equal "this has some color: #{color.on_red}this#{color.reset}",
31
+ process(/color: (\w+)/, [:red], 'this has some color: this')
32
+ assert_equal "numbers #{color.on_green}one#{color.reset} and #{color.on_yellow}two#{color.reset}",
33
+ process(/(one)[\s\w]+(two)/, [:green, :yellow], 'numbers one and two')
34
+ assert_equal "#{color.on_cyan}1#{color.reset} #{color.on_magenta}2#{color.reset} #{color.on_black}3#{color.reset}",
35
+ process(/(\d) (\d) (\d)/, [:cyan, :magenta, :black], '1 2 3')
36
+ end
37
+
38
+ def test_applies_colors_correctly_with_different_numbers_of_colors_and_match_groups
39
+ assert_equal "numbers #{color.on_green}one#{color.reset} and two",
40
+ process(/(one)[\s\w]+two/, [:green, :yellow], 'numbers one and two')
41
+ assert_equal "numbers #{color.on_green}one#{color.reset} and two",
42
+ process(/(one)[\s\w]+(two)/, [:green], 'numbers one and two')
43
+ assert_equal "numbers #{color.on_green}one#{color.reset} and #{color.on_red}two#{color.reset} and three",
44
+ process(/(one)[\s\w]+(two)[\s\w]+three/, [:green, :red], 'numbers one and two and three')
45
+ end
46
+
47
+ def test_can_specify_no_color_for_match
48
+ assert_equal 'no color here', process('color', :none, 'no color here')
49
+ assert_equal 'no color here', process(/color/, :none, 'no color here')
50
+ end
51
+
52
+ def test_can_specify_no_color_for_match_groups
53
+ assert_equal 'no color here', process(/(color)/, [:none], 'no color here')
54
+ assert_equal "the first and #{color.on_red}second#{color.reset} matches",
55
+ process(/(first)[\s\w]+(second)/, [:none, :red], 'the first and second matches')
56
+ assert_equal "the #{color.on_red}first#{color.reset} and second matches",
57
+ process(/(first)[\s\w]+(second)/, [:red, :none], 'the first and second matches')
58
+ end
59
+
60
+ private
61
+
62
+ def process(selector, value, line)
63
+ sub_engine = ::Styles::SubEngines::Color.new
64
+ line = ::Styles::Line.new(line, [::Styles::Properties::MatchBackgroundColor.new(selector, :match_background_color, value)])
65
+ sub_engine.process(line).to_s
66
+ end
67
+
68
+ def color
69
+ ::Term::ANSIColor
70
+ end
71
+ end
@@ -0,0 +1,79 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ require 'term/ansicolor'
3
+
4
+ class MatchColorTest < MiniTest::Unit::TestCase
5
+ def test_applies_correct_colors_to_string_selector_matches
6
+ test_line = 'this is a test line, to be colored'
7
+
8
+ assert_equal "this is a #{color.red}test#{color.reset} line, to be colored",
9
+ process('test', :red, test_line)
10
+ assert_equal "#{color.blue}this is#{color.reset} a test line, to be colored",
11
+ process('this is', :blue, test_line)
12
+ end
13
+
14
+ def test_invalid_values_are_ignored
15
+ test_line = 'this is a test line'
16
+ assert_equal 'this is a test line', process('test', :invalid, test_line)
17
+ assert_equal 'this is a test line', process(/test/, :invalid, test_line)
18
+ assert_equal 'this is a test line', process(/(test)/, :invalid, test_line)
19
+ assert_equal 'this is a test line', process(/(test) (line)/, [:invalid, :bogus], test_line)
20
+ end
21
+
22
+ def test_applies_correct_colors_to_multiple_string_selector_matches
23
+ assert_equal "#{color.green}the#{color.reset} word #{color.green}the#{color.reset} is repeated",
24
+ process('the', :green, 'the word the is repeated')
25
+ end
26
+
27
+ def test_applies_correct_colors_to_regex_matches
28
+ assert_equal "the number #{color.green}89#{color.reset} is in this line",
29
+ process(/\d\d/, :green, 'the number 89 is in this line')
30
+ end
31
+
32
+ def test_applies_correct_colors_to_multiple_regex_matches
33
+ assert_equal "the numbers #{color.blue}89#{color.reset} and #{color.blue}22#{color.reset} are in this line",
34
+ process(/\d\d/, :blue, 'the numbers 89 and 22 are in this line')
35
+ end
36
+
37
+ def test_can_use_multiple_colors_to_match_groups
38
+ assert_equal "this has some color: #{color.red}this#{color.reset}",
39
+ process(/color: (\w+)/, [:red], 'this has some color: this')
40
+ assert_equal "numbers #{color.green}one#{color.reset} and #{color.yellow}two#{color.reset}",
41
+ process(/(one)[\s\w]+(two)/, [:green, :yellow], 'numbers one and two')
42
+ assert_equal "#{color.cyan}1#{color.reset} #{color.magenta}2#{color.reset} #{color.black}3#{color.reset}",
43
+ process(/(\d) (\d) (\d)/, [:cyan, :magenta, :black], '1 2 3')
44
+ end
45
+
46
+ def test_applies_colors_correctly_with_different_numbers_of_colors_and_match_groups
47
+ assert_equal "numbers #{color.green}one#{color.reset} and two",
48
+ process(/(one)[\s\w]+two/, [:green, :yellow], 'numbers one and two')
49
+ assert_equal "numbers #{color.green}one#{color.reset} and two",
50
+ process(/(one)[\s\w]+(two)/, [:green], 'numbers one and two')
51
+ assert_equal "numbers #{color.green}one#{color.reset} and #{color.red}two#{color.reset} and three",
52
+ process(/(one)[\s\w]+(two)[\s\w]+three/, [:green, :red], 'numbers one and two and three')
53
+ end
54
+
55
+ def test_can_specify_no_color_for_match
56
+ assert_equal 'no color here', process('color', :none, 'no color here')
57
+ assert_equal 'no color here', process(/color/, :none, 'no color here')
58
+ end
59
+
60
+ def test_can_specify_no_color_for_match_groups
61
+ assert_equal 'no color here', process(/(color)/, [:none], 'no color here')
62
+ assert_equal "the first and #{color.red}second#{color.reset} matches",
63
+ process(/(first)[\s\w]+(second)/, [:none, :red], 'the first and second matches')
64
+ assert_equal "the #{color.red}first#{color.reset} and second matches",
65
+ process(/(first)[\s\w]+(second)/, [:red, :none], 'the first and second matches')
66
+ end
67
+
68
+ private
69
+
70
+ def process(selector, value, line)
71
+ sub_engine = ::Styles::SubEngines::Color.new
72
+ line = ::Styles::Line.new(line, [::Styles::Properties::MatchColor.new(selector, :match_color, value)])
73
+ sub_engine.process(line).to_s
74
+ end
75
+
76
+ def color
77
+ ::Term::ANSIColor
78
+ end
79
+ end
@@ -0,0 +1,34 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ require 'term/ansicolor'
3
+
4
+ class MatchFontWeightTest < MiniTest::Unit::TestCase
5
+ def test_can_embolden_a_match
6
+ test_line = 'this is a test line'
7
+
8
+ assert_equal "this is a #{color.bold}test#{color.reset} line", process('test', :bold, test_line)
9
+ assert_equal "this is a #{color.bold}test#{color.reset} line", process(/test/, :bold, test_line)
10
+ assert_equal test_line, process('test', :normal, test_line)
11
+ assert_equal test_line, process(/test/, :normal, test_line)
12
+ assert_equal test_line, process('test', :invalid, test_line)
13
+ end
14
+
15
+ def test_can_embolden_multiple_matches
16
+ test_line = 'this is another test line'
17
+ assert_equal "this is #{color.bold}another#{color.reset} #{color.bold}test#{color.reset} line",
18
+ process(/(another) (test)/, [:bold, :bold], test_line)
19
+ assert_equal "this is #{color.bold}another#{color.reset} test line",
20
+ process(/(another) (test)/, [:bold, :normal], test_line)
21
+ end
22
+
23
+ private
24
+
25
+ def process(selector, value, line)
26
+ sub_engine = ::Styles::SubEngines::Color.new
27
+ line = ::Styles::Line.new(line, [::Styles::Properties::MatchFontWeight.new(selector, :match_font_weight, value)])
28
+ sub_engine.process(line).to_s
29
+ end
30
+
31
+ def color
32
+ ::Term::ANSIColor
33
+ end
34
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ require 'term/ansicolor'
3
+
4
+ class MatchTextDecorationTest < MiniTest::Unit::TestCase
5
+ def test_decorate_match_text
6
+ test_line = 'this is a test line'
7
+
8
+ assert_equal "this is a #{color.underline}test#{color.reset} line", process('test', :underline, test_line)
9
+ assert_equal "this is a #{color.strikethrough}test#{color.reset} line", process('test', :strikethrough, test_line)
10
+ assert_equal "this is a #{color.strikethrough}test#{color.reset} line", process('test', :line_through, test_line)
11
+ assert_equal "this is a #{color.blink}test#{color.reset} line", process('test', :blink, test_line)
12
+ assert_equal "this is a #{color.underline}test#{color.reset} line", process(/test/, :underline, test_line)
13
+ assert_equal "this is a #{color.underline}test#{color.reset} line", process(/(test)/, :underline, test_line)
14
+ assert_equal test_line, process('test', :none, test_line)
15
+ assert_equal test_line, process('test', :invalid, test_line)
16
+ end
17
+
18
+ def test_decorate_multiple_matches
19
+ test_line = 'this is a another test line'
20
+
21
+ assert_equal "this is a #{color.underline}another#{color.reset} #{color.strikethrough}test#{color.reset} line",
22
+ process(/(another) (test)/, [:underline, :strikethrough], test_line)
23
+ assert_equal "this is a #{color.underline}another#{color.reset} test line",
24
+ process(/(another) (test)/, [:underline, :none], test_line)
25
+ end
26
+
27
+ private
28
+
29
+ def process(selector, value, line)
30
+ sub_engine = ::Styles::SubEngines::Color.new
31
+ line = ::Styles::Line.new(line, [::Styles::Properties::MatchTextDecoration.new(selector, :match_text_decoration, value)])
32
+ sub_engine.process(line).to_s
33
+ end
34
+
35
+ def color
36
+ ::Term::ANSIColor
37
+ end
38
+ end
@@ -0,0 +1,87 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ class PaddingTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @sub_engine = ::Styles::SubEngines::Layout.new
6
+ end
7
+
8
+ def test_can_use_all_property_names
9
+ [:padding, :padding_left, :padding_right, :padding_top, :padding_bottom].each do |prop_name|
10
+ assert_equal ::Styles::Properties::Padding, ::Styles::Properties.find_class_by_property_name(prop_name)
11
+ end
12
+ end
13
+
14
+ def test_gives_correct_padding_values
15
+ p = prop(:padding_left, 2)
16
+ assert_equal 0, p.top
17
+ assert_equal 0, p.right
18
+ assert_equal 0, p.bottom
19
+ assert_equal 2, p.left
20
+ assert_equal [0, 0, 0, 2], p.all_padding
21
+
22
+ assert_equal [4, 4, 4, 4], prop(:padding, 4).all_padding
23
+ assert_equal [2, 2, 2, 2], prop(:padding, 2).all_padding
24
+ assert_equal [5, 0, 0, 0], prop(:padding_top, 5).all_padding
25
+ assert_equal [0, 3, 0, 0], prop(:padding_right, 3).all_padding
26
+ assert_equal [0, 0, 10, 0], prop(:padding_bottom, 10).all_padding
27
+
28
+ assert_equal [0, 0, 0, 0], prop(:padding, :none).all_padding
29
+ assert_equal [0, 0, 0, 0], prop(:padding_right, :none).all_padding
30
+ end
31
+
32
+ def test_can_combine_padding_properties
33
+ assert_equal [0, 3, 0, 2], combine([prop(:padding_left, 2), prop(:padding_right, 3)]).all_padding
34
+ assert_equal [0, 0, 0, 2], combine([prop(:padding_left, 2)]).all_padding
35
+ assert_equal [5, 2, 2, 2], combine([prop(:padding, 2), prop(:padding_top, 5)]).all_padding
36
+ assert_equal [5, 4, 2, 2], combine([prop(:padding, 2), prop(:padding_top, 5), prop(:padding_right, 4)]).all_padding
37
+ assert_equal [2, 2, 2, 2], combine([prop(:padding_top, 5), prop(:padding, 2)]).all_padding
38
+ assert_equal [0, 2, 2, 2], combine([prop(:padding, 2), prop(:padding_top, 0)]).all_padding
39
+
40
+ assert_equal [4, 0, 0, 0], combine([prop(:padding, :none), prop(:padding_top, 4)]).all_padding
41
+ assert_equal [0, 4, 4, 4], combine([prop(:padding, 4), prop(:padding_top, :none)]).all_padding
42
+
43
+ no_top = combine([prop(:padding, 3), prop(:padding_top, 0)])
44
+ left_and_right = combine([prop(:padding_left, 2), prop(:padding_right, 5)])
45
+ assert_equal [0, 5, 3, 2], combine([no_top, left_and_right]).all_padding
46
+ end
47
+
48
+ def test_can_configure_with_side_values
49
+ assert_equal [1, 2, 3, 4], prop(:padding, '1 2 3 4').all_padding
50
+ assert_equal [1, 2, 3, 0], prop(:padding, '1 2 3').all_padding
51
+ assert_equal [1, 2, 0, 0], prop(:padding, '1 2').all_padding
52
+ assert_equal [1, 0, 0, 0], prop(:padding, '1').all_padding
53
+ end
54
+
55
+ def test_padding_left
56
+ test_line = 'line tests'
57
+ stub_term_width(80)
58
+ assert_equal " #{test_line}", process('line', :padding_left, 2, test_line)
59
+ assert_equal " #{test_line}", process('line', :padding_left, 10, test_line)
60
+ end
61
+
62
+ def test_all_padding
63
+ test_line = 'line tests'
64
+ stub_term_width(80)
65
+ assert_equal "#{' ' * 12}\n line tests \n#{' ' * 12}\n", process('line', :padding, 1, test_line)
66
+ end
67
+
68
+ private
69
+
70
+ def process(selector, property_name, value, line)
71
+ line = ::Styles::Line.new(line, [::Styles::Properties::Padding.new(selector, property_name, value)])
72
+ result = @sub_engine.process(line)
73
+ result.to_s
74
+ end
75
+
76
+ def prop(name, value, selector='test')
77
+ ::Styles::Properties::Padding.new(selector, name, value)
78
+ end
79
+
80
+ def combine(*props)
81
+ ::Styles::Properties::Padding.new(props.flatten)
82
+ end
83
+
84
+ def stub_term_width(width)
85
+ @sub_engine.stubs(:terminal_width).returns(width)
86
+ end
87
+ end