summaryrefslogtreecommitdiff
path: root/scss/tests/functions/test_core.py
blob: d6207f15b4788b90bf4fde74a1224525e482cd4b (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""Tests for the core Sass functions, as defined in the original Sass
documentation:

http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html
"""
from __future__ import division

from scss.expression import Calculator
from scss.functions.core import CORE_LIBRARY
from scss.rule import Namespace
from scss.types import Color, Number, String

import pytest
xfail = pytest.mark.xfail


# TODO many of these tests could also stand to test for failure cases

@pytest.fixture
def calc():
    ns = Namespace(functions=CORE_LIBRARY)
    return Calculator(ns).evaluate_expression


# ------------------------------------------------------------------------------
# RGB functions

def test_rgb(calc):
    assert calc('rgb(128, 192, 224)') == Color.from_rgb(128/255, 192/255, 224/255)
    assert calc('rgb(20%, 40%, 60%)') == Color.from_rgb(0.2, 0.4, 0.6)


def test_rgba(calc):
    # four args (css-style)
    assert calc('rgba(128, 192, 224, 0.5)') == Color.from_rgb(128/255, 192/255, 224/255, 0.5)
    assert calc('rgba(20%, 40%, 60%, 0.7)') == Color.from_rgb(0.2, 0.4, 0.6, 0.7)

    # two args (modify alpha of existing color)
    assert calc('rgba(red, 0.4)') == Color.from_rgb(1., 0., 0., 0.4)


def test_red(calc):
    assert calc('red(orange)') == Number(255)


def test_green(calc):
    assert calc('green(orange)') == Number(165)


def test_blue(calc):
    assert calc('blue(orange)') == Number(0)


@xfail(reason="difference in rounding; ruby floors, we round")
def test_mix(calc):
    # Examples from the Ruby docs
    assert calc('mix(#f00, #00f)') == calc('#7f007f')
    assert calc('mix(#f00, #00f, 25%)') == calc('#3f00bf')
    assert calc('mix(rgba(255, 0, 0, 0.5), #00f)') == calc('rgba(63, 0, 191, 0.75)')


# ------------------------------------------------------------------------------
# HSL functions

def test_hsl(calc):
    # Examples from the CSS 3 color spec, which Sass uses
    # (http://www.w3.org/TR/css3-color/#hsl-color)
    assert calc('hsl(0, 100%, 50%)') == Color.from_rgb(1., 0., 0.)
    assert calc('hsl(120, 100%, 50%)') == Color.from_rgb(0., 1., 0.)
    assert calc('hsl(120, 100%, 25%)') == Color.from_rgb(0., 0.5, 0.)
    assert calc('hsl(120, 100%, 75%)') == Color.from_rgb(0.5, 1., 0.5)
    assert calc('hsl(120, 75%, 75%)') == Color.from_rgb(0.5625, 0.9375, 0.5625)


def test_hsla(calc):
    # Examples from the CSS 3 color spec
    assert calc('hsla(120, 100%, 50%, 1)') == Color.from_rgb(0., 1., 0.,)
    assert calc('hsla(240, 100%, 50%, 0.5)') == Color.from_rgb(0., 0., 1., 0.5)
    assert calc('hsla(30, 100%, 50%, 0.1)') == Color.from_rgb(1., 0.5, 0., 0.1)


def test_hue(calc):
    assert calc('hue(yellow)') == Number(60, unit='deg')


def test_saturation(calc):
    assert calc('saturation(yellow)') == Number(100, unit='%')


def test_lightness(calc):
    assert calc('lightness(yellow)') == Number(50, unit='%')


# HSL manipulation functions

def test_adjust_hue(calc):
    # Examples from the Ruby docs
    assert calc('adjust-hue(hsl(120, 30%, 90%), 60deg)') == calc('hsl(180, 30%, 90%)')
    assert calc('adjust-hue(hsl(120, 30%, 90%), -60deg)') == calc('hsl(60, 30%, 90%)')
    assert calc('adjust-hue(#811, 45deg)') == Color.from_rgb(136/255, 106.25/255, 17/255)


def test_lighten(calc):
    # Examples from the Ruby docs
    assert calc('lighten(hsl(0, 0%, 0%), 30%)') == calc('hsl(0, 0, 30)')
    assert calc('lighten(#800, 20%)') == calc('#e00')


def test_darken(calc):
    # Examples from the Ruby docs
    assert calc('darken(hsl(25, 100%, 80%), 30%)') == calc('hsl(25, 100%, 50%)')
    assert calc('darken(#800, 20%)') == calc('#200')


def test_saturate(calc):
    # Examples from the Ruby docs
    assert calc('saturate(hsl(120, 30%, 90%), 20%)') == calc('hsl(120, 50%, 90%)')
    assert calc('saturate(#855, 20%)') == Color.from_rgb(158.1/255, 62.9/255, 62.9/255)


def test_desaturate(calc):
    # Examples from the Ruby docs
    assert calc('desaturate(hsl(120, 30%, 90%), 20%)') == calc('hsl(120, 10%, 90%)')
    assert calc('desaturate(#855, 20%)') == Color.from_rgb(113.9/255, 107.1/255, 107.1/255)


def test_grayscale(calc):
    assert calc('grayscale(black)') == Color.from_rgb(0., 0., 0.)
    assert calc('grayscale(white)') == Color.from_rgb(1., 1., 1.)
    assert calc('grayscale(yellow)') == Color.from_rgb(0.5, 0.5, 0.5)


@xfail(reason="not implemented, issue #157")
def test_grayscale_css_filter(calc):
    # grayscale(number) is a CSS filter and should be left alone
    assert calc('grayscale(1)') == "grayscale(1)"


def test_complement(calc):
    assert calc('complement(black)') == Color.from_rgb(0., 0., 0.)
    assert calc('complement(white)') == Color.from_rgb(1., 1., 1.)
    assert calc('complement(yellow)') == Color.from_rgb(0., 0., 1.)


def test_invert(calc):
    assert calc('invert(black)') == Color.from_rgb(1., 1., 1.)
    assert calc('invert(white)') == Color.from_rgb(0., 0., 0.)
    assert calc('invert(yellow)') == Color.from_rgb(0., 0., 1.)


# ------------------------------------------------------------------------------
# Opacity functions

def test_alpha_opacity(calc):
    assert calc('alpha(black)') == Number(1.)
    assert calc('alpha(rgba(black, 0.5))') == Number(0.5)
    assert calc('alpha(rgba(black, 0))') == Number(0.)

    # opacity is a synonym
    assert calc('opacity(black)') == Number(1.)
    assert calc('opacity(rgba(black, 0.5))') == Number(0.5)
    assert calc('opacity(rgba(black, 0))') == Number(0.)


@xfail(reason="currently a parse error -- so it still works in practice, but...")
def test_alpha_ie_filter(calc):
    # alpha() is supposed to leave the IE filter syntax alone
    assert calc('alpha(filter=20)') == "alpha(filter=20)"


def test_opacify_fadein(calc):
    # Examples from the Ruby docs
    assert calc('opacify(rgba(0, 0, 0, 0.5), 0.1)') == calc('rgba(0, 0, 0, 0.6)')
    assert calc('opacify(rgba(0, 0, 17, 0.8), 0.2)') == calc('#001')

    # fade-in is a synonym
    assert calc('fade-in(rgba(0, 0, 0, 0.5), 0.1)') == calc('rgba(0, 0, 0, 0.6)')
    assert calc('fade-in(rgba(0, 0, 17, 0.8), 0.2)') == calc('#001')


def test_transparentize_fadeout(calc):
    # Examples from the Ruby docs
    assert calc('transparentize(rgba(0, 0, 0, 0.5), 0.1)') == calc('rgba(0, 0, 0, 0.4)')
    assert calc('transparentize(rgba(0, 0, 0, 0.8), 0.2)') == calc('rgba(0, 0, 0, 0.6)')

    # fade-out is a synonym
    assert calc('fade-out(rgba(0, 0, 0, 0.5), 0.1)') == calc('rgba(0, 0, 0, 0.4)')
    assert calc('fade-out(rgba(0, 0, 0, 0.8), 0.2)') == calc('rgba(0, 0, 0, 0.6)')


# ------------------------------------------------------------------------------
# Other color functions

def test_adjust_color(calc):
    # Examples from the Ruby docs
    assert calc('adjust-color(#102030, $blue: 5)') == calc('#102035')
    assert calc('adjust-color(#102030, $red: -5, $blue: 5)') == calc('#0b2035')
    assert calc('adjust-color(hsl(25, 100%, 80%), $lightness: -30%, $alpha: -0.4)') == calc('hsla(25, 100%, 50%, 0.6)')


def test_scale_color(calc):
    # Examples from the Ruby docs
    assert calc('scale-color(hsl(120, 70, 80), $lightness: 50%)') == calc('hsl(120, 70, 90)')
    assert calc('scale-color(rgb(200, 150, 170), $green: -40%, $blue: 70%)') == calc('rgb(200, 90, 229)')
    assert calc('scale-color(hsl(200, 70, 80), $saturation: -90%, $alpha: -30%)') == calc('hsla(200, 7, 80, 0.7)')


def test_change_color(calc):
    # Examples from the Ruby docs
    assert calc('change-color(#102030, $blue: 5)') == calc('#102005')
    assert calc('change-color(#102030, $red: 120, $blue: 5)') == calc('#782005')
    assert calc('change-color(hsl(25, 100%, 80%), $lightness: 40%, $alpha: 0.8)') == calc('hsla(25, 100%, 40%, 0.8)')


def test_ie_hex_str(calc):
    # Examples from the Ruby docs
    assert calc('ie-hex-str(#abc)') == calc('"#FFAABBCC"')
    assert calc('ie-hex-str(#3322BB)') == calc('"#FF3322BB"')
    assert calc('ie-hex-str(rgba(0, 255, 0, 0.5))') == calc('"#8000FF00"')


# ------------------------------------------------------------------------------
# String functions

def test_unquote(calc):
    # Examples from the Ruby docs
    ret = calc('unquote("foo")')
    assert ret == String('foo')
    assert ret.quotes is None
    ret = calc('unquote(foo)')
    assert ret == String('foo')
    assert ret.quotes is None

    assert calc('unquote((one, two, three))') == String('one, two, three')


def test_quote(calc):
    # Examples from the Ruby docs
    ret = calc('quote("foo")')
    assert ret == String('foo')
    assert ret.quotes == '"'
    ret = calc('quote(foo)')
    assert ret == String('foo')
    assert ret.quotes == '"'


# ------------------------------------------------------------------------------
# Number functions

def test_percentage(calc):
    # Examples from the Ruby docs
    assert calc('percentage(100px / 50px)') == calc('200%')


def test_round(calc):
    # Examples from the Ruby docs
    assert calc('round(10.4px)') == calc('10px')
    assert calc('round(10.6px)') == calc('11px')


def test_ceil(calc):
    # Examples from the Ruby docs
    assert calc('ceil(10.4px)') == calc('11px')
    assert calc('ceil(10.6px)') == calc('11px')


def test_floor(calc):
    # Examples from the Ruby docs
    assert calc('floor(10.4px)') == calc('10px')
    assert calc('floor(10.6px)') == calc('10px')


def test_abs(calc):
    # Examples from the Ruby docs
    assert calc('abs(10px)') == calc('10px')
    assert calc('abs(-10px)') == calc('10px')


def test_min(calc):
    # Examples from the Ruby docs
    assert calc('min(1px, 4px)') == calc('1px')
    assert calc('min(5em, 3em, 4em)') == calc('3em')


def test_max(calc):
    # Examples from the Ruby docs
    assert calc('max(1px, 4px)') == calc('4px')
    assert calc('max(5em, 3em, 4em)') == calc('5em')


# ------------------------------------------------------------------------------
# List functions

def test_length(calc):
    # Examples from the Ruby docs
    assert calc('length(10px)') == calc('1')
    assert calc('length(10px 20px 30px)') == calc('3')


def test_nth(calc):
    # Examples from the Ruby docs
    assert calc('nth(10px 20px 30px, 1)') == calc('10px')
    assert calc('nth((Helvetica, Arial, sans-serif), 3)') == calc('sans-serif')


def test_join(calc):
    # Examples from the Ruby docs
    assert calc('join(10px 20px, 30px 40px)') == calc('10px 20px 30px 40px')
    assert calc('join((blue, red), (#abc, #def))') == calc('blue, red, #abc, #def')
    assert calc('join(10px, 20px)') == calc('10px 20px')
    assert calc('join(10px, 20px, comma)') == calc('10px, 20px')
    assert calc('join((blue, red), (#abc, #def), space)') == calc('blue red #abc #def')


def test_append(calc):
    # Examples from the Ruby docs
    assert calc('append(10px 20px, 30px)') == calc('10px 20px 30px')
    assert calc('append((blue, red), green)') == calc('blue, red, green')
    assert calc('append(10px 20px, 30px 40px)') == calc('10px 20px (30px 40px)')
    assert calc('append(10px, 20px, comma)') == calc('10px, 20px')
    assert calc('append((blue, red), green, space)') == calc('blue red green')


@xfail(reason="not implemented (oops)")
def test_zip(calc):
    # Examples from the Ruby docs
    assert calc('zip(1px 1px 3px, solid dashed solid, red green blue)') == calc('1px solid red, 1px dashed green, 3px solid blue')


def test_index(calc):
    # Examples from the Ruby docs
    assert calc('index(1px solid red, solid)') == calc('2')
    assert calc('index(1px solid red, dashed)') == calc('false')


# ------------------------------------------------------------------------------
# Map functions


# ...


# ------------------------------------------------------------------------------
# Introspection functions

def test_type_of(calc):
    # Examples from the Ruby docs
    assert calc('type-of(100px)') == calc('number')
    assert calc('type-of(asdf)') == calc('string')
    assert calc('type-of("asdf")') == calc('string')
    assert calc('type-of(true)') == calc('bool')
    assert calc('type-of(#fff)') == calc('color')
    assert calc('type-of(blue)') == calc('color')


def test_unit(calc):
    # Examples from the Ruby docs
    assert calc('unit(100)') == calc('""')
    assert calc('unit(100px)') == calc('"px"')
    assert calc('unit(3em)') == calc('"em"')
    assert calc('unit(10px * 5em)') == calc('"em*px"')
    # NOTE: the docs say "em*px/cm*rem", but even Ruby sass doesn't actually
    # return that
    assert calc('unit(10px * 5em / 30cm / 1rem)') == calc('"em/rem"')


def test_unitless(calc):
    # Examples from the Ruby docs
    assert calc('unitless(100)') == calc('true')
    assert calc('unitless(100px)') == calc('false')


def test_comparable(calc):
    # Examples from the Ruby docs
    assert calc('comparable(2px, 1px)') == calc('true')
    assert calc('comparable(100px, 3em)') == calc('false')
    assert calc('comparable(10cm, 3mm)') == calc('true')


# ------------------------------------------------------------------------------
# Miscellaneous functions

def test_if(calc):
    # Examples from the Ruby docs
    assert calc('if(true, 1px, 2px)') == calc('1px')
    assert calc('if(false, 1px, 2px)') == calc('2px')