File: test_merging.rb

package info (click to toggle)
ruby-css-parser 1.3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 268 kB
  • ctags: 199
  • sloc: ruby: 1,742; makefile: 3
file content (127 lines) | stat: -rw-r--r-- 4,012 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require File.expand_path(File.dirname(__FILE__) + '/test_helper')

class MergingTests < Test::Unit::TestCase
  include CssParser

  def setup
    @cp = CssParser::Parser.new
  end

  def test_simple_merge
    rs1 = RuleSet.new(nil, 'color: black;')
    rs2 = RuleSet.new(nil, 'margin: 0px;')
    merged = CssParser.merge(rs1, rs2)
    assert_equal '0px;', merged['margin']
    assert_equal 'black;', merged['color']
  end

  def test_merging_array
    rs1 = RuleSet.new(nil, 'color: black;')
    rs2 = RuleSet.new(nil, 'margin: 0px;')
    merged = CssParser.merge([rs1, rs2])
    assert_equal '0px;', merged['margin']
    assert_equal 'black;', merged['color']
  end

  def test_merging_with_compound_selectors
    @cp.add_block! "body { margin: 0; }"
    @cp.add_block! "h2   { margin: 5px; }"

    rules = @cp.find_rule_sets([ "body", "h2" ])
    assert_equal "margin: 5px;", CssParser.merge(rules).declarations_to_s

    @cp = CssParser::Parser.new
    @cp.add_block! "body { margin: 0; }"
    @cp.add_block! "h2,h1 { margin: 5px; }"

    rules = @cp.find_rule_sets([ "body", "h2" ])
    assert_equal "margin: 5px;", CssParser.merge(rules).declarations_to_s
  end

  def test_merging_multiple
    rs1 = RuleSet.new(nil, 'color: black;')
    rs2 = RuleSet.new(nil, 'margin: 0px;')
    rs3 = RuleSet.new(nil, 'margin: 5px;')
    merged = CssParser.merge(rs1, rs2, rs3)
    assert_equal '5px;', merged['margin']
  end

  def test_multiple_selectors_should_have_proper_specificity
    rs1 = RuleSet.new('p, a[rel="external"]', 'color: black;')
    rs2 = RuleSet.new('a', 'color: blue;')
    merged = CssParser.merge(rs1, rs2)
    assert_equal 'black;', merged['color']
  end

  def test_setting_specificity
    rs1 = RuleSet.new(nil, 'color: red;', 20)
    rs2 = RuleSet.new(nil, 'color: blue;', 10)
    merged = CssParser.merge(rs1, rs2)
    assert_equal 'red;', merged['color']
  end

  def test_properties_should_be_case_insensitive
    rs1 = RuleSet.new(nil, ' CoLor   : red  ;', 20)
    rs2 = RuleSet.new(nil, 'color: blue;', 10)
    merged = CssParser.merge(rs1, rs2)
    assert_equal 'red;', merged['color']
  end

  def test_merging_backgrounds
    rs1 = RuleSet.new(nil, 'background-color: black;')
    rs2 = RuleSet.new(nil, 'background-image: none;')
    merged = CssParser.merge(rs1, rs2)
    assert_equal 'black none;', merged['background']
  end

  def test_merging_dimensions
    rs1 = RuleSet.new(nil, 'margin: 3em;')
    rs2 = RuleSet.new(nil, 'margin-left: 1em;')
    merged = CssParser.merge(rs1, rs2)
    assert_equal '3em 3em 3em 1em;', merged['margin']
  end

  def test_merging_fonts
    rs1 = RuleSet.new(nil, 'font: 11px Arial;')
    rs2 = RuleSet.new(nil, 'font-weight: bold;')
    merged = CssParser.merge(rs1, rs2)
    assert_equal 'bold 11px Arial;', merged['font']
  end

  def test_raising_error_on_bad_type
    assert_raise ArgumentError do
      CssParser.merge([1,2,3])
    end
  end

  def test_returning_early_with_only_one_params
    rs = RuleSet.new(nil, 'font-weight: bold;')
    merged = CssParser.merge(rs)
    assert_equal rs.object_id, merged.object_id
  end

  def test_merging_important
    rs1 = RuleSet.new(nil, 'color: black !important;')
    rs2 = RuleSet.new(nil, 'color: red;')
    merged = CssParser.merge(rs1, rs2)
    assert_equal 'black !important;', merged['color']
  end

  def test_merging_multiple_important
    rs1 = RuleSet.new(nil, 'color: black !important;', 1000)
    rs2 = RuleSet.new(nil, 'color: red !important;', 1)
    merged = CssParser.merge(rs1, rs2)
    assert_equal 'black !important;', merged['color']

    rs3 = RuleSet.new(nil, 'color: blue !important;', 1000)
    merged = CssParser.merge(rs1, rs2, rs3)
    assert_equal 'blue !important;', merged['color']
  end

  def test_merging_shorthand_important
    rs1 = RuleSet.new(nil, 'background: black none !important;')
    rs2 = RuleSet.new(nil, 'background-color: red;')
    merged = CssParser.merge(rs1, rs2)
    assert_equal 'black !important;', merged['background-color']
  end
end