File: test_value.rb

package info (click to toggle)
ruby-css-parser 1.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: ruby: 3,093; makefile: 6
file content (168 lines) | stat: -rw-r--r-- 5,900 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# frozen_string_literal: true

require_relative '../../test_helper'
require 'minitest/spec'
require 'ostruct'

class RuleSetProperyTest < Minitest::Test
  describe '.new' do
    describe 'with invalid value' do
      it 'raises an error when empty' do
        exception = assert_raises(ArgumentError) { CssParser::RuleSet::Declarations::Value.new('  ') }
        assert_equal 'value is empty', exception.message
      end

      it 'raises an error when nil' do
        exception = assert_raises(ArgumentError) { CssParser::RuleSet::Declarations::Value.new(nil) }
        assert_equal 'value is empty', exception.message
      end

      it 'raises an error when contains only important declaration' do
        exception = assert_raises(ArgumentError) { CssParser::RuleSet::Declarations::Value.new(' !important; ') }
        assert_equal 'value is empty', exception.message
      end
    end

    describe 'with valid value' do
      it 'remove semicolon at the end' do
        assert_equal 'value', CssParser::RuleSet::Declarations::Value.new('value;').value
      end

      it 'removes important declarations' do
        assert_equal 'value', CssParser::RuleSet::Declarations::Value.new('value !important').value
      end

      it 'strips value' do
        assert_equal 'value', CssParser::RuleSet::Declarations::Value.new('  value ').value
      end

      it 'does everything above' do
        assert_equal "value\t another one",
          CssParser::RuleSet::Declarations::Value.new("  \tvalue\t another one  \t!important  \t  ;  ").value
      end

      it 'freezes the string' do
        assert_equal true, CssParser::RuleSet::Declarations::Value.new('value').value.frozen?
      end
    end

    describe 'important' do
      describe 'when not set' do
        it 'is not important if value is not important' do
          assert_equal false, CssParser::RuleSet::Declarations::Value.new('value').important
        end

        it 'is important if value is not important' do
          assert_equal true, CssParser::RuleSet::Declarations::Value.new('value !important;').important
        end
      end

      describe 'when set' do
        it 'overrides value importance' do
          assert_equal false, CssParser::RuleSet::Declarations::Value.new('value !important;', important: false).important
          assert_equal true, CssParser::RuleSet::Declarations::Value.new('value', important: true).important
        end
      end
    end
  end

  describe 'important=' do
    it 'sets importance' do
      property = CssParser::RuleSet::Declarations::Value.new('value')
      assert_equal false, property.important

      property.important = true
      assert_equal true, property.important
    end
  end

  describe 'value=' do
    it 'sets normalized value' do
      property = CssParser::RuleSet::Declarations::Value.new('foo')
      assert_equal 'foo', property.value

      property.value = "  \tvalue\t another one  \t!important  \t  ;  "
      assert_equal "value\t another one", property.value
    end

    it 'sets importance' do
      property = CssParser::RuleSet::Declarations::Value.new('foo')
      assert_equal 'foo', property.value
      assert_equal false, property.important

      property.value = 'bar !important'

      assert_equal 'bar', property.value
      assert_equal true, property.important
    end

    it 'freezes the string' do
      property = CssParser::RuleSet::Declarations::Value.new('foo')
      property.value = 'bar'

      assert_equal true, CssParser::RuleSet::Declarations::Value.new('value').value.frozen?
    end

    it 'raises an exception when the value is empty' do
      assert_raises ArgumentError do
        CssParser::RuleSet::Declarations::Value.new
      end
    end
  end

  describe '#to_s' do
    it 'returns value if not important' do
      assert_equal 'value', CssParser::RuleSet::Declarations::Value.new('value').to_s
    end

    it 'returns value with important annotation if important' do
      assert_equal 'value !important', CssParser::RuleSet::Declarations::Value.new('value', important: true).to_s
    end
  end

  describe '#==' do
    it 'returns true if value & importance are the same' do
      property = CssParser::RuleSet::Declarations::Value.new('value', important: true)
      other = CssParser::RuleSet::Declarations::Value.new('value', important: true)

      assert_equal property, other
    end

    it 'returns false if value is not a Declarations::Value' do
      property = CssParser::RuleSet::Declarations::Value.new('value', important: true)
      other = OpenStruct.new(value: 'value', important: true)

      refute_equal other, property
    end

    it 'returns true if value is a Declarations::Value subclass and value are equal' do
      property = CssParser::RuleSet::Declarations::Value.new('value', important: true)
      other_class = Class.new(CssParser::RuleSet::Declarations::Value)
      other = other_class.new('value', important: true)

      assert_equal property, other
    end

    it 'returns false if value is a Declarations::Value subclass and value are not equal' do
      property = CssParser::RuleSet::Declarations::Value.new('value', important: true)
      other_class = Class.new(CssParser::RuleSet::Declarations::Value)
      other = other_class.new('other value', important: true)

      refute_equal other, property
    end

    it 'returns false if value is different' do
      property = CssParser::RuleSet::Declarations::Value.new('value', important: true)
      other = CssParser::RuleSet::Declarations::Value.new('other value', important: true)

      refute_equal property, other
    end

    it 'returns false if importance is different' do
      property = CssParser::RuleSet::Declarations::Value.new('value', important: true)
      other = CssParser::RuleSet::Declarations::Value.new('value', important: false)

      refute_equal property, other
    end
  end
end