Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvanmele committed Nov 1, 2024
1 parent d31556f commit 1081598
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/compas/colors/colordict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
Expand Down Expand Up @@ -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):
Expand Down
47 changes: 47 additions & 0 deletions tests/compas/colors/test_colordict.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 1081598

Please sign in to comment.