File: value_helpers_test.rb

package info (click to toggle)
ruby-sass 3.7.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,396 kB
  • sloc: ruby: 32,443; sh: 26; makefile: 25
file content (178 lines) | stat: -rwxr-xr-x 4,711 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
169
170
171
172
173
174
175
176
177
178
require File.dirname(__FILE__) + '/../test_helper'

class ValueHelpersTest < Minitest::Test
  include Sass::Script
  include Sass::Script::Value::Helpers

  def test_bool
    assert_same Value::Bool::TRUE, bool(true)
    assert_same Value::Bool::FALSE, bool(false)
    assert_same Value::Bool::FALSE, bool(nil)
    assert_same Value::Bool::TRUE, bool(Object.new)
  end

  def test_hex_color_with_three_digits
    color = hex_color("F07")
    assert_equal 255, color.red
    assert_equal 0, color.green
    assert_equal 119, color.blue
    assert_equal 1, color.alpha
  end

  def test_hex_color_without_hash
    color_without_hash = hex_color("FF007F")
    assert_equal 255, color_without_hash.red
    assert_equal 0, color_without_hash.green
    assert_equal 127, color_without_hash.blue
    assert_equal 1, color_without_hash.alpha
  end

  def test_hex_color_with_hash
    color_with_hash = hex_color("#FF007F")
    assert_equal 255, color_with_hash.red
    assert_equal 0, color_with_hash.green
    assert_equal 127, color_with_hash.blue
    assert_equal 1, color_with_hash.alpha
  end

  def test_malformed_hex_color
    assert_raises ArgumentError do
      hex_color("green")
    end
    assert_raises ArgumentError do
      hex_color("#abcde")
    end
  end


  def test_hex_color_with_alpha
    color_with_alpha = hex_color("FF007F", 0.5)
    assert_equal 0.5, color_with_alpha.alpha
  end

  def test_hex_color_alpha_clamps_0_to_1
    assert_equal 1, hex_color("FF007F", 50).alpha
  end

  def test_hsl_color_without_alpha
    no_alpha = hsl_color(1, 0.5, 1)
    assert_equal 1, no_alpha.hue
    assert_equal 0.5, no_alpha.saturation
    assert_equal 1, no_alpha.lightness
    assert_equal 1, no_alpha.alpha
  end

  def test_hsl_color_with_alpha
    has_alpha = hsl_color(1, 0.5, 1, 0.5)
    assert_equal 1, has_alpha.hue
    assert_equal 0.5, has_alpha.saturation
    assert_equal 1, has_alpha.lightness
    assert_equal 0.5, has_alpha.alpha
  end

  def test_rgb_color_without_alpha
    no_alpha = rgb_color(255, 0, 0)
    assert_equal 255, no_alpha.red
    assert_equal 0, no_alpha.green
    assert_equal 0, no_alpha.blue
    assert_equal 1, no_alpha.alpha
  end

  def test_rgb_color_with_alpha
    has_alpha = rgb_color(255, 255, 255, 0.5)
    assert_equal 255, has_alpha.red
    assert_equal 255, has_alpha.green
    assert_equal 255, has_alpha.blue
    assert_equal 0.5, has_alpha.alpha
  end

  def test_number
    n = number(1)
    assert_equal 1, n.value
    assert_equal "1", n.to_sass
  end

  def test_number_with_single_unit
    n = number(1, "px")
    assert_equal 1, n.value
    assert_equal "1px", n.to_sass
  end

  def test_number_with_singal_numerator_and_denominator
    ratio = number(1, "px/em")
    assert_equal "1px/em", ratio.to_sass
  end

  def test_number_with_many_numerator_and_denominator_units
    complex = number(1, "px*in/em*%")
    assert_equal "1in*px/%*em", complex.to_sass
  end

  def test_number_with_many_numerator_and_denominator_units_with_spaces
    complex = number(1, "px * in / em * %")
    assert_equal "1in*px/%*em", complex.to_sass
  end

  def test_number_with_malformed_units
    assert_raises ArgumentError do
      number(1, "px/em/%")
    end
    assert_raises ArgumentError do
      number(1, "/")
    end
    assert_raises ArgumentError do
      number(1, "px/")
    end
  end

  def test_space_list
    l = list(number(1, "px"), hex_color("#f71"), :space)
    l.options = {}
    assert_kind_of Sass::Script::Value::List, l
    assert_equal "1px #f71", l.to_sass
  end

  def test_comma_list
    l = list(number(1, "px"), hex_color("#f71"), :comma)
    l.options = {}
    assert_kind_of Sass::Script::Value::List, l
    assert_equal "1px, #f71", l.to_sass
  end

  def test_missing_list_type
    assert_raises ArgumentError do
      list(number(1, "px"), hex_color("#f71"))
    end
  end

  def test_null
    assert_kind_of Sass::Script::Value::Null, null
  end

  def test_quoted_string
    s = quoted_string("sassy string")
    s.options = {}
    assert_kind_of Sass::Script::Value::String, s
    assert_equal "sassy string", s.value
    assert_equal :string, s.type
    assert_equal '"sassy string"', s.to_sass
  end

  def test_identifier
    s = identifier("a-sass-ident")
    s.options = {}
    assert_kind_of Sass::Script::Value::String, s
    assert_equal "a-sass-ident", s.value
    assert_equal :identifier, s.type
    assert_equal "a-sass-ident", s.to_sass
  end

  def test_unquoted_string
    s = unquoted_string("a-sass-ident")
    s.options = {}
    assert_kind_of Sass::Script::Value::String, s
    assert_equal "a-sass-ident", s.value
    assert_equal :identifier, s.type
    assert_equal "a-sass-ident", s.to_sass
  end
end