def test_access(): MyNamedStruct = named_struct('foo', "MyNamedStruct") s = MyNamedStruct() assert s.foo is None s.foo = 17 assert s.foo == 17 assert s['foo'] == 17
def test_write_constraints(): MyNamedStruct = named_struct('foo', "MyNamedStruct") s = MyNamedStruct() with pytest.raises(AttributeError): # noinspection PyStatementEffect s.bar = 17 with pytest.raises(KeyError) as e: s['bar'] assert "KeyError: 'bar'" in str(e)
def test_init(): assert list(named_struct('foo')().keys()) == ['foo'] assert sorted(named_struct(['foo', 'bar'])().keys()) == ['bar', 'foo'] assert sorted(named_struct('foo, bar')().keys()) == ['bar', 'foo'] assert named_struct('foo, bar')().__class__.__name__ == 'NamedStruct' assert sorted(named_struct('foo, bar', 'SomeName')().keys()) == ['bar', 'foo'] assert named_struct('foo, bar', 'SomeName')().__class__.__name__ == 'SomeName'
def test_repr(): assert repr(named_struct("foo, bar")(foo=17, bar=42)) == "NamedStruct(bar=42, foo=17)" assert repr(named_struct("foo, bar", "SomeNamedStruct")(foo=17, bar=42)) == "SomeNamedStruct(bar=42, foo=17)"
def test_position_arg_constructor(): MyNamedStruct = named_struct('foo, bar') s = MyNamedStruct(17, 42) assert s == dict(foo=17, bar=42)
def test_constructor(): MyNamedStruct = named_struct('foo, bar') s = MyNamedStruct(bar=42, foo=17) assert s == dict(foo=17, bar=42)
def test_read_constraints(): MyNamedStruct = named_struct('foo', "MyNamedStruct") s = MyNamedStruct() with pytest.raises(AttributeError): # noinspection PyStatementEffect s.bar
def test_freeze(): MyNamedStruct = named_struct('foo, bar') s = MyNamedStruct(foo=17) s.bar = 42 assert FrozenStruct(s) == FrozenStruct(foo=17, bar=42)