simplex/tests/test_magic_dictionary.py
2019-11-28 17:06:23 +01:00

84 lines
1.9 KiB
Python

# -*- encoding: utf-8 -*-
from simplex.magic_dictionary import magic_dictionary
def test_basic():
""" Tests that a non-customized magic_dictionary acts
as a standard python dictionary. """
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():
""" Tests that conversion is correctly done. """
# 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"