Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/css-parsing-tests
211 changes: 201 additions & 10 deletions tests/test_tinycss2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from tinycss2.color3 import parse_color as parse_color3 # isort:skip
from tinycss2.color4 import Color # isort:skip
from tinycss2.color4 import parse_color as parse_color4 # isort:skip
from tinycss2.color5 import parse_color as parse_color5 # isort:skip
from tinycss2.nth import parse_nth # isort:skip


Expand Down Expand Up @@ -159,6 +160,19 @@ def _number(value):
return str(int(value) if value.is_integer() else value)


def _build_color(color):
if color is None:
return
(*coordinates, alpha) = color
result = f'color({color.space}'
for coordinate in coordinates:
result += f' {_number(coordinate)}'
if alpha != 1:
result += f' / {_number(alpha)}'
result += ')'
return result


def test_color_currentcolor_3():
for value in ('currentcolor', 'currentColor', 'CURRENTCOLOR'):
assert parse_color3(value) == 'currentColor'
Expand All @@ -169,23 +183,48 @@ def test_color_currentcolor_4():
assert parse_color4(value) == 'currentcolor'


def test_color_currentcolor_5():
for value in ('currentcolor', 'currentColor', 'CURRENTCOLOR'):
assert parse_color5(value) == 'currentcolor'


@json_test()
def test_color_function_4(input):
if not (color := parse_color4(input)):
return _build_color(parse_color4(input))


@json_test(filename='color_function_4.json')
def test_color_function_4_with_5(input):
return _build_color(parse_color5(input))


@json_test()
def test_color_functions_5(input):
if input.startswith('light-dark'):
result = []
result.append(_build_color(parse_color5(input, ('light',))))
result.append(_build_color(parse_color5(input, ('dark',))))
else:
result = _build_color(parse_color5(input))
return result


@json_test()
def test_color_hexadecimal_3(input):
if not (color := parse_color3(input)):
return None
(*coordinates, alpha) = color
result = f'color({color.space}'
for coordinate in coordinates:
result += f' {_number(coordinate)}'
result = f'rgb{"a" if alpha != 1 else ""}('
result += f'{", ".join(_number(coordinate * 255) for coordinate in coordinates)}'
if alpha != 1:
result += f' / {_number(alpha)}'
result += f', {_number(alpha)}'
result += ')'
return result


@json_test()
def test_color_hexadecimal_3(input):
if not (color := parse_color3(input)):
@json_test(filename='color_hexadecimal_3.json')
def test_color_hexadecimal_3_with_4(input):
if not (color := parse_color4(input)):
return None
(*coordinates, alpha) = color
result = f'rgb{"a" if alpha != 1 else ""}('
Expand All @@ -210,9 +249,23 @@ def test_color_hexadecimal_4(input):
return result


@json_test(filename='color_hexadecimal_4.json')
def test_color_hexadecimal_4_with_5(input):
if not (color := parse_color5(input)):
return None
assert color.space == 'srgb'
(*coordinates, alpha) = color
result = f'rgb{"a" if alpha != 1 else ""}('
result += f'{", ".join(_number(coordinate * 255) for coordinate in coordinates)}'
if alpha != 1:
result += f', {_number(alpha)}'
result += ')'
return result


@json_test(filename='color_hexadecimal_3.json')
def test_color_hexadecimal_3_with_4(input):
if not (color := parse_color4(input)):
def test_color_hexadecimal_3_with_5(input):
if not (color := parse_color5(input)):
return None
assert color.space == 'srgb'
(*coordinates, alpha) = color
Expand Down Expand Up @@ -251,6 +304,20 @@ def test_color_hsl_3_with_4(input):
return result


@json_test(filename='color_hsl_3.json')
def test_color_hsl_3_with_5(input):
if not (color := parse_color5(input)):
return None
assert color.space == 'hsl'
(*coordinates, alpha) = color.to('srgb')
result = f'rgb{"a" if alpha != 1 else ""}('
result += f'{", ".join(_number(coordinate * 255) for coordinate in coordinates)}'
if alpha != 1:
result += f', {_number(alpha)}'
result += ')'
return result


@json_test()
def test_color_hsl_4(input):
if not (color := parse_color4(input)):
Expand All @@ -265,6 +332,20 @@ def test_color_hsl_4(input):
return result


@json_test(filename='color_hsl_4.json')
def test_color_hsl_4_with_5(input):
if not (color := parse_color5(input)):
return None
assert color.space == 'hsl'
(*coordinates, alpha) = color.to('srgb')
result = f'rgb{"a" if alpha != 1 else ""}('
result += f'{", ".join(_number(coordinate * 255) for coordinate in coordinates)}'
if alpha != 1:
result += f', {_number(alpha)}'
result += ')'
return result


@json_test()
def test_color_hwb_4(input):
if not (color := parse_color4(input)):
Expand All @@ -279,6 +360,20 @@ def test_color_hwb_4(input):
return result


@json_test(filename='color_hwb_4.json')
def test_color_hwb_4_with_5(input):
if not (color := parse_color5(input)):
return None
assert color.space == 'hwb'
(*coordinates, alpha) = color.to('srgb')
result = f'rgb{"a" if alpha != 1 else ""}('
result += f'{", ".join(_number(coordinate * 255) for coordinate in coordinates)}'
if alpha != 1:
result += f', {_number(alpha)}'
result += ')'
return result


@json_test()
def test_color_keywords_3(input):
if not (color := parse_color3(input)):
Expand Down Expand Up @@ -310,6 +405,22 @@ def test_color_keywords_3_with_4(input):
return result


@json_test(filename='color_keywords_3.json')
def test_color_keywords_3_with_5(input):
if not (color := parse_color5(input)):
return None
elif isinstance(color, str):
return color
assert color.space == 'srgb'
(*coordinates, alpha) = color
result = f'rgb{"a" if alpha != 1 else ""}('
result += f'{", ".join(_number(coordinate * 255) for coordinate in coordinates)}'
if alpha != 1:
result += f', {_number(alpha)}'
result += ')'
return result


@json_test()
def test_color_keywords_4(input):
if not (color := parse_color4(input)):
Expand All @@ -326,6 +437,22 @@ def test_color_keywords_4(input):
return result


@json_test(filename='color_keywords_4.json')
def test_color_keywords_4_with_5(input):
if not (color := parse_color5(input)):
return None
elif isinstance(color, str):
return color
assert color.space == 'srgb'
(*coordinates, alpha) = color
result = f'rgb{"a" if alpha != 1 else ""}('
result += f'{", ".join(_number(coordinate * 255) for coordinate in coordinates)}'
if alpha != 1:
result += f', {_number(alpha)}'
result += ')'
return result


@json_test()
def test_color_lab_4(input):
if not (color := parse_color4(input)):
Expand All @@ -342,6 +469,22 @@ def test_color_lab_4(input):
return result


@json_test(filename='color_lab_4.json')
def test_color_lab_4_with_5(input):
if not (color := parse_color5(input)):
return None
elif isinstance(color, str):
return color
assert color.space == 'lab'
(*coordinates, alpha) = color
result = f'{color.space}('
result += f'{" ".join(_number(coordinate) for coordinate in coordinates)}'
if alpha != 1:
result += f' / {_number(alpha)}'
result += ')'
return result


@json_test()
def test_color_oklab_4(input):
if not (color := parse_color4(input)):
Expand All @@ -358,6 +501,22 @@ def test_color_oklab_4(input):
return result


@json_test(filename='color_oklab_4.json')
def test_color_oklab_4_with_5(input):
if not (color := parse_color5(input)):
return None
elif isinstance(color, str):
return color
assert color.space == 'oklab'
(*coordinates, alpha) = color
result = f'{color.space}('
result += f'{" ".join(_number(coordinate) for coordinate in coordinates)}'
if alpha != 1:
result += f' / {_number(alpha)}'
result += ')'
return result


@json_test()
def test_color_lch_4(input):
if not (color := parse_color4(input)):
Expand All @@ -374,6 +533,22 @@ def test_color_lch_4(input):
return result


@json_test(filename='color_lch_4.json')
def test_color_lch_4_with_5(input):
if not (color := parse_color5(input)):
return None
elif isinstance(color, str):
return color
assert color.space == 'lch'
(*coordinates, alpha) = color
result = f'{color.space}('
result += f'{" ".join(_number(coordinate) for coordinate in coordinates)}'
if alpha != 1:
result += f' / {_number(alpha)}'
result += ')'
return result


@json_test()
def test_color_oklch_4(input):
if not (color := parse_color4(input)):
Expand All @@ -390,6 +565,22 @@ def test_color_oklch_4(input):
return result


@json_test(filename='color_oklch_4.json')
def test_color_oklch_4_with_5(input):
if not (color := parse_color5(input)):
return None
elif isinstance(color, str):
return color
assert color.space == 'oklch'
(*coordinates, alpha) = color
result = f'{color.space}('
result += f'{" ".join(_number(coordinate) for coordinate in coordinates)}'
if alpha != 1:
result += f' / {_number(alpha)}'
result += ')'
return result


@json_test()
def test_stylesheet_bytes(kwargs):
kwargs['css_bytes'] = kwargs['css_bytes'].encode('latin1')
Expand Down
5 changes: 4 additions & 1 deletion tinycss2/color4.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ class Color:
to [0, 1]. Coordinates can also be set to ``None`` when undefined.

"""
COLOR_SPACES = COLOR_SPACES

def __init__(self, space, coordinates, alpha):
assert space in COLOR_SPACES, f"{space} is not a supported color space"
if self.COLOR_SPACES:
assert space in self.COLOR_SPACES, f"{space} is not a supported color space"
self.space = space
self.coordinates = tuple(
None if coordinate is None else float(coordinate)
Expand Down
Loading
Loading