simplex/tests/test_magic_dictionary.py

88 lines
1.9 KiB
Python
Raw Normal View History

2019-11-28 15:53:00 +00:00
# -*- encoding: utf-8 -*-
from simplex.magic_dictionary import magic_dictionary
def test_basic():
2021-07-08 13:48:02 +00:00
"""
Tests that a non-customized magic_dictionary acts
as a standard python dictionary.
"""
2019-11-28 15:53:00 +00:00
d = magic_dictionary()
assert len(d) == 0
d["x"] = 1
d["y"] = "2"
assert len(d) == 2
assert d["x"] == 1
assert d["y"] == "2"
# Test copy:
d2 = magic_dictionary(d)
assert len(d2) == 2
assert d2["x"] == 1
assert d2["y"] == "2"
# Test construction:
d2 = magic_dictionary({"x": 1, "y": "2"})
assert len(d2) == 2
assert d2["x"] == 1
assert d2["y"] == "2"
# Test construction:
d2 = magic_dictionary(x=1, y="2")
assert len(d2) == 2
assert d2["x"] == 1
assert d2["y"] == "2"
# Test construction:
d2 = magic_dictionary((("x", 1), ("y", "2")))
assert len(d2) == 2
assert d2["x"] == 1
assert d2["y"] == "2"
def test_value_converter():
2021-07-08 13:48:02 +00:00
"""
Tests that conversion is correctly done.
"""
2019-11-28 15:53:00 +00:00
# Convert a value to its string representation:
def converter(x):
return str(x)
d = magic_dictionary(value_converter=converter)
assert len(d) == 0
d["x"] = 2
d["y"] = 3
assert len(d) == 2
assert d["x"] == "2"
assert d["y"] == "3"
# Construction:
d = magic_dictionary(d, value_converter=converter)
assert len(d) == 2
assert d["x"] == "2"
assert d["y"] == "3"
# Construction:
d = magic_dictionary({"x": 2, "y": 3}, value_converter=converter)
assert len(d) == 2
assert d["x"] == "2"
assert d["y"] == "3"
# Construction:
d = magic_dictionary(x=2, y=3, value_converter=converter)
assert len(d) == 2
assert d["x"] == "2"
assert d["y"] == "3"
# Construction:
d = magic_dictionary([("x", 2), ("y", 3)], value_converter=converter)
assert len(d) == 2
assert d["x"] == "2"
assert d["y"] == "3"