From 1081598f0d24b0fa7e5d0d57003f045c6cf26d04 Mon Sep 17 00:00:00 2001 From: tomvanmele Date: Fri, 1 Nov 2024 15:34:40 +0100 Subject: [PATCH] add tests --- src/compas/colors/colordict.py | 6 ++-- tests/compas/colors/test_colordict.py | 47 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/compas/colors/colordict.py b/src/compas/colors/colordict.py index 365bb29f4715..18a69bd587e9 100644 --- a/src/compas/colors/colordict.py +++ b/src/compas/colors/colordict.py @@ -24,7 +24,7 @@ class ColorDict(Data): """ - keymap = { + KEYMAP = { int: lambda x: str(x), tuple: lambda x: ",".join(map(str, sorted(x))), list: lambda x: ",".join(map(str, sorted(x))), @@ -62,8 +62,8 @@ def default(self, default): self._default = default def keymapper(self, key): - if key.__class__ in self.keymap: - return self.keymap[key.__class__](key) + if key.__class__ in self.KEYMAP: + return self.KEYMAP[key.__class__](key) return key def __getitem__(self, key): diff --git a/tests/compas/colors/test_colordict.py b/tests/compas/colors/test_colordict.py index e69de29bb2d1..5c8c3d800519 100644 --- a/tests/compas/colors/test_colordict.py +++ b/tests/compas/colors/test_colordict.py @@ -0,0 +1,47 @@ +import pytest +import json +import compas + +from compas.colors import Color +from compas.colors import ColorDict + + +def test_colordict(): + cd = ColorDict(Color.red()) + assert cd.default == Color.red() + + cd = ColorDict((255, 0, 0)) + assert cd.default == Color.red() + + cd = ColorDict((1.0, 0.0, 0.0)) + assert cd.default == Color.red() + + +def test_colordict_keys(): + cd = ColorDict(Color.red()) + cd[1] = Color.blue() + cd[(1, 0)] = Color.green() + + assert cd[1] == Color.blue() + assert cd["1"] == Color.blue() + + assert cd[(1, 0)] == Color.green() + assert cd["0,1"] == Color.green() + + assert cd["1,0"] == Color.red() + + +def test_colordict_json(): + cd1 = ColorDict(Color.red()) + cd1[1] = Color.blue() + cd1[(1, 0)] = Color.green() + + cd2: ColorDict = compas.json_loads(compas.json_dumps(cd1)) # type: ignore + + assert cd2[1] == Color.blue() + assert cd2["1"] == Color.blue() + + assert cd2[(1, 0)] == Color.green() + assert cd2["0,1"] == Color.green() + + assert cd2["1,0"] == Color.red()