aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorThomas Touhey <thomas@touhey.fr>2021-09-11 21:09:06 +0200
committerThomas Touhey <thomas@touhey.fr>2021-12-31 21:01:37 +0100
commit542a4bcb3ca5fc0c867c07c399dd16e8642f68d9 (patch)
treed6fd255efc366ed7843bf37fba17f8246a52482d /tests
parentb3edd31df8c96035d70c0216e444d6dd9951699f (diff)
Reworking thcolor.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/__init__.py6
-rwxr-xr-xtests/test_angles.py27
-rwxr-xr-xtests/test_colors.py38
-rwxr-xr-xtests/test_decoders.py209
-rwxr-xr-xtests/test_text.py73
5 files changed, 277 insertions, 76 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
index 47268e6..f8d9bfb 100755
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -1,8 +1,8 @@
#!/usr/bin/env python3
-#******************************************************************************
-# Copyright (C) 2019 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
+# *****************************************************************************
+# Copyright (C) 2019-2021 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the thcolor project, which is MIT-licensed.
-#******************************************************************************
+# *****************************************************************************
""" Unit tests for the `thcolor` Python module. """
# This file is only there to indicate that the folder is a module.
diff --git a/tests/test_angles.py b/tests/test_angles.py
new file mode 100755
index 0000000..d5358a4
--- /dev/null
+++ b/tests/test_angles.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+# *****************************************************************************
+# Copyright (C) 2019-2021 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
+# This file is part of the thcolor project, which is MIT-licensed.
+# *****************************************************************************
+""" Unit tests for the thcolor color decoding and management module. """
+
+import pytest
+from thcolor.angles import (
+ Angle, DegreesAngle, GradiansAngle, RadiansAngle, TurnsAngle,
+)
+
+
+@pytest.mark.parametrize('test_input,expected', (
+ ('120deg', DegreesAngle(120)),
+ ('5rad', RadiansAngle(5)),
+ ('3grad', GradiansAngle(3)),
+ ('6.turns', TurnsAngle(6)),
+ ('355', DegreesAngle(355)),
+))
+def test_angles(test_input, expected):
+ angle = Angle.fromtext(test_input)
+ assert isinstance(angle, type(expected))
+ assert angle == expected
+
+
+# End of file.
diff --git a/tests/test_colors.py b/tests/test_colors.py
new file mode 100755
index 0000000..58988a5
--- /dev/null
+++ b/tests/test_colors.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+# *****************************************************************************
+# Copyright (C) 2019-2021 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
+# This file is part of the thcolor project, which is MIT-licensed.
+# *****************************************************************************
+""" Unit tests for the thcolor color decoding and management module. """
+
+import pytest
+from thcolor.angles import DegreesAngle
+from thcolor.colors import SRGBColor, HSLColor, HWBColor
+
+
+@pytest.mark.parametrize('color,expected', (
+ (SRGBColor.frombytes(0, 0, 255), (
+ '#0000FF',
+ )),
+ (SRGBColor.frombytes(1, 22, 242, alpha=.5), (
+ '#0116F2',
+ 'rgba(1, 22, 242, 50%)',
+ )),
+ (HSLColor(DegreesAngle(0), 1, .4), (
+ '#CC0000',
+ 'hsl(0deg, 100%, 40%)',
+ )),
+ (HSLColor(DegreesAngle(0), .5, 1, alpha=.2), (
+ '#FFFFFF',
+ 'rgba(255, 255, 255, 20%)',
+ 'hsla(0deg, 50%, 100%, 20%)',
+ )),
+ (HWBColor(DegreesAngle(127), blackness=.5), (
+ '#00800F',
+ 'hwb(127deg, 0%, 50%)',
+ )),
+))
+def test_css(color, expected):
+ assert color.css() == expected
+
+# End of file.
diff --git a/tests/test_decoders.py b/tests/test_decoders.py
new file mode 100755
index 0000000..8dd91eb
--- /dev/null
+++ b/tests/test_decoders.py
@@ -0,0 +1,209 @@
+#!/usr/bin/env python3
+# *****************************************************************************
+# Copyright (C) 2019-2021 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
+# This file is part of the thcolor project, which is MIT-licensed.
+# *****************************************************************************
+""" Unit tests for the thcolor color decoding and management module. """
+
+import pytest
+from thcolor.angles import * # NOQA
+from thcolor.colors import * # NOQA
+from thcolor.decoders import * # NOQA
+from thcolor.errors import * # NOQA
+
+
+class TestBaseDecoder:
+ @pytest.fixture
+ def decoder(self):
+ class StrictDecoder(MetaColorDecoder):
+ __ncol_support__ = False
+ __defaults_to_netscape_color__ = False
+
+ B20 = 12.34
+ heLLo = 56.78
+
+ return StrictDecoder()
+
+ @pytest.mark.parametrize('test_input,expected', (
+ ('1 / 2 , 3', (
+ 1, 2, 3.0,
+ )),
+ ('20deg 4turn 3,, 3. 3.0 .2 50%', (
+ DegreesAngle(20), TurnsAngle(4), 3, None, 3.0, 3.0, 0.2, 0.5,
+ )),
+ ('#12345F', (
+ SRGBColor.frombytes(18, 52, 95, 1.00),
+ )),
+ ))
+ def test_syntax(self, decoder, test_input, expected):
+ """ Test basic syntax.
+
+ Tests syntaxes that are always available, with separators.
+ """
+
+ result = decoder.decode(test_input)
+ assert result == expected
+
+ @pytest.mark.parametrize('test_input,expected', (
+ ('20.5, 20 / 19.5deg', (
+ DegreesAngle(20.5), DegreesAngle(20), DegreesAngle(19.5),
+ )),
+ ))
+ def test_coersce_into_angles(self, decoder, test_input, expected):
+ """ Test the decoding while indicating that we want angles. """
+
+ result = decoder.decode(test_input, prefer_angles=True)
+ assert result == expected
+
+ def test_existing_variable(self, decoder):
+ """ Test getting an existing variable. """
+
+ result = decoder.decode('hello')
+ assert result == (56.78,)
+
+ def test_non_existing_variable(self, decoder):
+ """ Test getting a non-existing variable. """
+
+ with pytest.raises(ColorExpressionSyntaxError, match=r'unknown value'):
+ decoder.decode('nonexisting')
+
+ def test_existing_variable_ncol_format(self, decoder):
+ """ Test decoding an expression using an NCol-like variable.
+
+ We want to avoid the NCol subsytem from shadowing
+ existing variables when NCol support is disabled.
+ """
+
+ result = decoder.decode('B20')
+ assert result == (12.34,)
+
+ def test_non_existing_variable_ncol_format(self, decoder):
+ """ Test decoding an expression using an NCol-like variable.
+
+ We want to avoid the NCol subsystem intercepting errors
+ in case of non-existing variables.
+ """
+
+ with pytest.raises(ColorExpressionSyntaxError, match=r'unknown value'):
+ decoder.decode('Y40')
+
+
+class TestNColDecoder:
+ """ Test base decoder with ncol support. """
+
+ @pytest.fixture
+ def decoder(self):
+ class Decoder(MetaColorDecoder):
+ __ncol_support__ = True
+ __defaults_to_netscape_color__ = False
+
+ return Decoder()
+
+ @pytest.mark.parametrize('test_input,expected', (
+ (
+ 'B20 / 50% 32%',
+ HWBColor(DegreesAngle(252), .5, .32, 1.00),
+ ),
+ (
+ 'Y40, 33%, 55%',
+ HWBColor(DegreesAngle(84), 0.33, 0.55, 1.00),
+ ),
+ ))
+ def test_ncol(self, decoder, test_input, expected):
+ """ Test natural colors. """
+
+ result = decoder.decode(test_input)
+ assert result == (expected,)
+
+
+class TestNetscapeDecoder:
+ """ Test base decoder with netscape color support. """
+
+ @pytest.fixture
+ def decoder(self):
+ class Decoder(MetaColorDecoder):
+ __ncol_support__ = False
+ __defaults_to_netscape_color__ = True
+
+ return Decoder()
+
+ @pytest.mark.parametrize('test_input,expected', (
+ ('chucknorris', SRGBColor.frombytes(192, 0, 0, 1.00)),
+ ))
+ def test_netscape_colors(self, decoder, test_input, expected):
+ """ Test decoding using Netscape colors. """
+
+ result = decoder.decode(test_input, prefer_colors=True)
+ assert result == (expected,)
+
+ @pytest.mark.parametrize('test_input,expected', (
+ ('#123', SRGBColor.frombytes(17, 34, 51, 1.00)),
+ ('123', SRGBColor.frombytes(1, 2, 3, 1.00)),
+ ('123.0', SRGBColor.frombytes(18, 48, 0, 1.00)),
+ ))
+ def test_coersce_into_colors(self, decoder, test_input, expected):
+ """ Test the decoding while indicating that we want colors.
+
+ It is expected from the decoder to use Netscape color name
+ style decoding in this scenario.
+ """
+
+ result = decoder.decode(test_input, prefer_colors=True)
+ assert result == (expected,)
+
+
+class TestDefaultDecoder:
+ """ Test the default decoder with all features. """
+
+ @pytest.fixture
+ def decoder(self):
+ return DefaultColorDecoder()
+
+ @pytest.mark.parametrize('test_input,expected', (
+ ('blue', SRGBColor.frombytes(0, 0, 255, 1.00)),
+ ('rgb(1, 22,242)', SRGBColor.frombytes(1, 22, 242, 1.00)),
+ (' rgb (1,22, 242 , 50.0% )', SRGBColor.frombytes(1, 22, 242, 0.50)),
+ (' rgb (1 22/ 242,50.0%,/)', SRGBColor.frombytes(1, 22, 242, 0.50)),
+ ('rgba(1,22,242,0.500)', SRGBColor.frombytes(1, 22, 242, 0.50)),
+ ('rbga(5, 7)', SRGBColor.frombytes(5, 0, 7, 1.00)),
+ ('hsl(0, 1,50.0%)', HSLColor(DegreesAngle(0.0), 1.0, 0.5, 1.00)),
+ (
+ 'hls(0 / 1 0.5 , 0.2)',
+ HSLColor(DegreesAngle(0.0), 0.5, 1.0, 0.20),
+ ),
+ ('hwb(0 0% 0)', HWBColor(DegreesAngle(0), 0.0, 0.0, 1.00)),
+ ('hbw(127 .5)', HWBColor(DegreesAngle(127), 0.0, 0.5)),
+ ('gray(100)', SRGBColor.frombytes(100, 100, 100, 1.00)),
+ ('gray(100 / 55 %)', SRGBColor.frombytes(100, 100, 100, 0.55)),
+ ('gray(red( #123456 )/0.2/)', SRGBColor.frombytes(18, 18, 18, 0.20)),
+ ('B20 50% 32%', HWBColor(DegreesAngle(252), .5, .32, 1.00)),
+ ('ncol(B20 / 50% 32%)', HWBColor(DegreesAngle(252), .5, .32, 1.00)),
+ ('cmyk(0% 37% 0.13 .78)', CMYKColor(0, .37, .13, .78, 1.00)),
+ (
+ 'darker(10%, hsl(0, 1, 50.0%))',
+ HSLColor(DegreesAngle(0), 1.00, 0.40, 1.00),
+ ),
+ (
+ 'lighter(50%, hsl(0, 1, 60.0%))',
+ HSLColor(DegreesAngle(0), 1.00, 1.00, 1.00),
+ ),
+ (
+ 'saturate(10%, hls(0, 1, 85.0%))',
+ HSLColor(DegreesAngle(0), 0.95, 1.00, 1.00),
+ ),
+ (
+ 'desaturate(10%, hls(0turn, 1, 5%, 0.2))',
+ HSLColor(DegreesAngle(0), 0.00, 1.00, 0.20),
+ ),
+ ('rgba(255, 0, 0, 20 %)', HSLColor(DegreesAngle(0), 1.00, 0.50, 0.20)),
+ (
+ 'Y40, 33%, 55%',
+ HWBColor(DegreesAngle(84), 0.33, 0.55, 1.00),
+ ),
+ ('cmyk(0% 37% 0.13 .78)', CMYKColor(0.00, 0.37, 0.13, 0.78, 1.00)),
+ ))
+ def test_decoding_colors(self, decoder, test_input, expected):
+ result = decoder.decode(test_input, prefer_colors=True)
+ assert result[0] == expected
+
+# End of file.
diff --git a/tests/test_text.py b/tests/test_text.py
deleted file mode 100755
index 069ef9c..0000000
--- a/tests/test_text.py
+++ /dev/null
@@ -1,73 +0,0 @@
-#!/usr/bin/env python3
-#******************************************************************************
-# Copyright (C) 2019 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
-# This file is part of the thcolor project, which is MIT-licensed.
-#******************************************************************************
-""" Unit tests for the thcolor color decoding and management module. """
-
-import pytest
-from thcolor import Color, Angle
-
-def _deg(value):
- return Angle(Angle.Type.DEG, value)
-
-@pytest.mark.parametrize('test_input,expected', (
- ('blue', ( 0, 0, 255, 1.00)),
- ('#12345F', ( 18, 52, 95, 1.00)),
- ('#123', ( 17, 34, 51, 1.00)),
- ('123', ( 1, 2, 3, 1.00)),
- ('123.0', ( 18, 48, 0, 1.00)),
- ('chucknorris', (192, 0, 0, 1.00)),
- ('rgb(1, 22,242)', ( 1, 22, 242, 1.00)),
- (' rgb (1,22, 242 , 50.0% )', ( 1, 22, 242, 0.50)),
- (' rgb (1 22/ /242,50.0%,/)', ( 1, 22, 242, 0.50)),
- ('rgba(1,22,242,0.500)', ( 1, 22, 242, 0.50)),
- ('rbga(5, 7)', ( 5, 0, 7, 1.00)),
- ('hsl(0, 1,50.0%)', (255, 0, 0, 1.00)),
- ('hls(0 / 1 0.5 , 0.2)', (255, 255, 255, 0.20)),
- ('hwb(0 0% 0)', (255, 0, 0, 1.00)),
- ('hbw(127 .5)', ( 0, 128, 15, 1.00)),
- ('gray(100)', (100, 100, 100, 1.00)),
- ('gray(100 / 55 %)', (100, 100, 100, 0.55)),
- ('gray(red( #123456 )/0.2/)', ( 18, 18, 18, 0.20)),
- ('B20 50% 32%', (137, 128, 173, 1.00)),
- ('ncol(B20 / 50% 32%)', (137, 128, 173, 1.00)),
- ('cmyk(0% 37% 0.13 .78)', ( 56, 35, 49, 1.00)),
- ('lab(50 50 0)', (193, 78, 121, 1.00)),
-))
-def test_rgba(test_input, expected):
- assert Color.from_text(test_input).rgba() == expected
-
-@pytest.mark.parametrize('test_input,expected', (
- ('darker(10%, hsl(0, 1, 50.0%))', (_deg( 0 ), 1.00, 0.40, 1.00)),
- ('lighter(50%, hsl(0, 1, 60.0%))', (_deg( 0 ), 1.00, 1.00, 1.00)),
- ('saturate(10%, hls(0, 1, 85.0%))', (_deg( 0 ), 0.95, 1.00, 1.00)),
- ('desaturate(10%, hls(0, 1, 5%, 0.2))', (_deg( 0 ), 0.00, 1.00, 0.20)),
- ('rgba(255, 0, 0, 20 %)', (_deg( 0 ), 1.00, 0.50, 0.20)),
- ('Y40, 33%, 55%', (_deg(83.23), 0.16, 0.39, 1.00)),
-))
-def test_hsla(test_input, expected):
- assert Color.from_text(test_input).hsla() == expected
-
-@pytest.mark.parametrize('test_input,expected', (
- ('cmyk(0% 37% 0.13 .78)', (0.00, 0.37, 0.13, 0.78, 1.00)),
-))
-def test_cmyka(test_input, expected):
- assert Color.from_text(test_input).cmyka() == expected
-
-@pytest.mark.parametrize('test_input,expected', (
- ('blue',
- ('#0000FF',)),
- (' rgb (1,22, 242 , 50.0% )',
- ('#0116F2', 'rgba(1, 22, 242, 50%)')),
- ('darker(10%, hsl(0, 1, 50.0%))',
- ('#CC0000', 'hsl(0deg, 100%, 40%)')),
- ('hls(0 / 1 0.5 , 0.2)',
- ('#FFFFFF', 'rgba(255, 255, 255, 20%)', 'hsla(0deg, 50%, 100%, 20%)')),
- ('hbw(127 .5)',
- ('#00800F', 'hwb(127deg, 0%, 50%)')),
-))
-def test_css(test_input, expected):
- assert Color.from_text(test_input).css() == expected
-
-# End of file.