aboutsummaryrefslogtreecommitdiff
path: root/tests/test_decoders.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_decoders.py')
-rwxr-xr-xtests/test_decoders.py100
1 files changed, 36 insertions, 64 deletions
diff --git a/tests/test_decoders.py b/tests/test_decoders.py
index 747beb4..785ba32 100755
--- a/tests/test_decoders.py
+++ b/tests/test_decoders.py
@@ -11,7 +11,6 @@ import pytest
from thcolor.angles import * # NOQA
from thcolor.colors import * # NOQA
from thcolor.decoders import * # NOQA
-from thcolor.builtin import * # NOQA
from thcolor.errors import * # NOQA
@@ -45,14 +44,19 @@ class TestInvalidDecoders:
g = alias('f', args=('b',))
+ def test_varargs(self):
+ with pytest.raises(ValueError, match=r'variable arg'):
+ class BogusDecoder(MetaColorDecoder):
+ def f(a: int, b: int = 0, *args) -> int:
+ return a + b + sum(args)
+
class TestBaseDecoder:
+ """ Test the base decoder with no options enabled. """
+
@pytest.fixture
def decoder(self):
class StrictDecoder(MetaColorDecoder):
- __ncol_support__ = False
- __defaults_to_netscape_color__ = False
-
B20 = 12.34
heLLo = 56.78
@@ -121,6 +125,33 @@ class TestBaseDecoder:
with pytest.raises(ColorExpressionSyntaxError, match=r'unknown value'):
decoder.decode('Y40')
+ def test_disabled_extended_hex(self, decoder):
+ """ Test decoding an expression using an extended hex color.
+
+ This should be disabled in this state.
+ """
+
+ with pytest.raises(ColorExpressionSyntaxError, match=r'extended hex'):
+ decoder.decode('#1234')
+
+
+class TestExtendedHexDecoder:
+ """ Test base decoder with extended hex support. """
+
+ @pytest.fixture
+ def decoder(self):
+ class Decoder(MetaColorDecoder):
+ __extended_hex_support__ = True
+
+ return Decoder()
+
+ @pytest.mark.parametrize('test_input,expected', (
+ ('#0003', SRGBColor(0, 0, 0, alpha=.2)),
+ ('#000A', SRGBColor(0, 0, 0, alpha=.666667)),
+ ))
+ def test_extended_hex(self, decoder, test_input, expected):
+ assert decoder.decode(test_input) == (expected,)
+
class TestNColDecoder:
""" Test base decoder with ncol support. """
@@ -129,7 +160,6 @@ class TestNColDecoder:
def decoder(self):
class Decoder(MetaColorDecoder):
__ncol_support__ = True
- __defaults_to_netscape_color__ = False
def sum2(a: int, b: int) -> int:
return a + b
@@ -180,7 +210,7 @@ class TestNColDecoder:
def test_invalid_ncol(self, decoder):
with pytest.raises(ColorExpressionSyntaxError, match=r'unknown value'):
- decoder.decode('Y105, 50%, 50%')
+ assert decoder.decode('Y105, 50%, 50%')
class TestNetscapeDecoder:
@@ -223,62 +253,4 @@ class TestNetscapeDecoder:
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(110%, 0%, 0%)', SRGBColor(1.0, 0, 0, 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.