Ejemplo n.º 1
0
    def test_hash(self, Struct):
        s = Struct(x=17)
        with pytest.raises(TypeError) as e:
            hash(s)
        if platform.python_implementation() == "PyPy":
            assert "" in str(e)
        else:
            assert "unhashable type: 'Struct'" in str(e)

        f = FrozenStruct(x=17)
        assert isinstance(hash(f), int)
        assert not '_hash' in f.keys()
Ejemplo n.º 2
0
    def test_modify_frozen_struct(self):
        f = FrozenStruct(x=17)
        with pytest.raises(TypeError) as e:
            f.x = 42
        assert "'FrozenStruct' object attributes are read-only" in str(e)

        with pytest.raises(TypeError) as e:
            f['x'] = 42
        assert "'FrozenStruct' object attributes are read-only" in str(e)

        with pytest.raises(TypeError) as e:
            f.update(dict(x=42))
        assert "'FrozenStruct' object attributes are read-only" in str(e)

        with pytest.raises(TypeError) as e:
            f.setdefault('foo', 11)
        assert "'FrozenStruct' object attributes are read-only" in str(e)

        with pytest.raises(TypeError) as e:
            f.clear()
        assert "'FrozenStruct' object attributes are read-only" in str(e)

        with pytest.raises(TypeError) as e:
            del f.x
        assert "'FrozenStruct' object attributes are read-only" in str(e)

        with pytest.raises(TypeError) as e:
            del f['x']
        assert "'FrozenStruct' object attributes are read-only" in str(e)