summaryrefslogtreecommitdiff
path: root/runtime/tests/test_utils.py
blob: fcdc02dd27c42deffc19430452e6e9bc9b5e91df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Tests for utils.py"""
from external.bazel_python.pytest_helper import main
import runtime.utils as utils

def test_freeze_thaw_dict():
    """Tests freezedict/thawdict."""
    x = dict({"hello": "there"})
    assert utils.thawdict(utils.freezedict(x)) == x
    assert isinstance(utils.freezedict(x), tuple)

def test_is_empty():
    """Tests is_empty(...)."""
    assert utils.is_empty(x for x in range(0))
    assert not utils.is_empty(x for x in range(5))

def test_translator():
    """Tests the Translator class."""
    translator = utils.Translator(dict({
        "hello": "bonjour",
        "why": "porquoi",
        "what": "quoi",
    }))
    assert translator.translate("hello") == "bonjour"
    assert translator.translate("Matthew") == "Matthew"

    assert (translator.translate_tuple(("hello", "Matthew"))
            == ("bonjour", "Matthew"))

    assert (translator.translate_tuples([("hello", "Matthew"),
                                         ("why", "what")])
            == [("bonjour", "Matthew"), ("porquoi", "quoi")])

    assert (translator.translate_list(["hello", "Matthew"])
            == ["bonjour", "Matthew"])

    composed = translator.compose(dict({
        "bonjour": "salam",
        "porquoi": "chera",
        "merci": "merci",
    }))
    assert composed == dict({"hello": "salam", "why": "chera"})
    composed = translator.compose(dict({
        "bonjour": "salam",
        "porquoi": "chera",
        "merci": "merci",
    }), default_identity=True)
    assert composed == dict({"hello": "salam", "why": "chera", "what": "quoi"})

    concatenated = translator.concatenated_with(dict({
        "thanks": "merci",
    }))
    assert concatenated == dict({
        "hello": "bonjour",
        "why": "porquoi",
        "what": "quoi",
        "thanks": "merci",
    })

def test_real_hash():
    """Regression test for the real_hash method."""
    truth = "ff42aa4718d9da1c7d491e8be8116f0c62db8d910de16e8ee0648147"
    assert utils.real_hash(dict({"hello": "there"})) == truth
    try:
        # We don't yet support tuples, it should throw a NIE.
        utils.real_hash(("hi", "hello"))
    except NotImplementedError:
        pass
    else:
        assert False

main(__name__, __file__)
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback