示例#1
0
    def test_getattr(self):
        ''' Test that `util.ObjectProxy` can be used with getitem syntax. '''

        st_struct = struct.ObjectProxy(
            {
                'HelloThere': True,
                'IamA': False,
                'StRuct': None
            },
            case_sensitive=False)

        assert st_struct.HelloThere is True
        assert st_struct.hellothere is True
        assert 'HelloThere' in st_struct
        assert 'hellothere' in st_struct
        assert 'idonotexist' not in st_struct

        # try invalid names
        with self.assertRaises(AttributeError):
            st_struct.idonotexist
        with self.assertRaises(AttributeError):
            st_struct.IDoNotExist

        # test special attributes
        assert st_struct._case_sensitive is False
        assert isinstance(st_struct._entries, dict)
示例#2
0
    def test_values(self):
        ''' Test buffered iteration with `util.ObjectProxy.values`. '''

        st_struct = struct.ObjectProxy({
            'HelloThere': 1,
            'IamA': 2,
            'StRuct': 3
        })

        ref = (1, 2, 3)
        for val in st_struct.values():
            assert val in ref
示例#3
0
    def test_items(self):
        ''' Test buffered iteration with `util.ObjectProxy.items`. '''

        st_struct = struct.ObjectProxy(
            {
                'HelloThere': True,
                'IamA': False,
                'StRuct': None
            },
            case_sensitive=True)

        ref = {'HelloThere': True, 'IamA': False, 'StRuct': None}
        for key, value in st_struct.items():
            assert ref[key] == value
示例#4
0
    def test_itervalues(self):
        ''' Test streaming iteration with `util.ObjectProxy.itervalues`. '''

        st_struct = struct.ObjectProxy({
            'HelloThere': 1,
            'IamA': 2,
            'StRuct': 3
        })

        ref = (1, 2, 3)
        for val in st_struct.itervalues():
            assert val in ref

        assert isinstance(st_struct.itervalues(), {}.itervalues().__class__)
示例#5
0
    def test_keys(self):
        ''' Test buffered iteration with `util.ObjectProxy.keys`. '''

        st_struct = struct.ObjectProxy(
            {
                'HelloThere': True,
                'IamA': False,
                'StRuct': None
            },
            case_sensitive=True)

        ref = ('HelloThere', 'IamA', 'StRuct')
        for key in st_struct.keys():
            assert key in ref
示例#6
0
    def test_construct(self):
        ''' Test that `util.ObjectProxy` can be constructed. '''

        # basic construction test
        st = struct.ObjectProxy()

        # construction test with struct
        st_struct = struct.ObjectProxy({
            'hi': True,
            'iam': False,
            'astruct': None
        })

        # construction test with kwargs
        st_kwargs = struct.ObjectProxy(hi=1, iam=2, astruct=3)

        # construction test with struct + kwargs
        st_both = struct.ObjectProxy(
            {
                'hi': True,
                'iam': False,
                'astruct': None
            }, hi=1, iam=2)
示例#7
0
    def test_iteritems(self):
        ''' Test streaming iteration with `util.ObjectProxy.iteritems`. '''

        st_struct = struct.ObjectProxy(
            {
                'HelloThere': True,
                'IamA': False,
                'StRuct': None
            },
            case_sensitive=True)

        ref = {'HelloThere': True, 'IamA': False, 'StRuct': None}
        for key, value in st_struct.iteritems():
            assert ref[key] == value

        assert isinstance(st_struct.iteritems(), {}.iteritems().__class__)
示例#8
0
    def test_iterkeys(self):
        ''' Test streaming iteration with `util.ObjectProxy.iterkeys`. '''

        st_struct = struct.ObjectProxy(
            {
                'HelloThere': True,
                'IamA': False,
                'StRuct': None
            },
            case_sensitive=True)

        ref = ('HelloThere', 'IamA', 'StRuct')
        for key in st_struct.iterkeys():
            assert key in ref

        assert isinstance(st_struct.iterkeys(), {}.iterkeys().__class__)
示例#9
0
    def test_fill_case_insensitive(self):
        ''' Test that `util.ObjectProxy` can be case insensitive. '''

        st_struct = struct.ObjectProxy(
            {
                'HelloThere': True,
                'IamA': False,
                'StRuct': None
            },
            case_sensitive=False)

        assert st_struct.HelloThere is True
        assert st_struct.hellothere is True
        assert 'HelloThere' in st_struct
        assert 'hellothere' in st_struct
        assert 'idonotexist' not in st_struct
示例#10
0
    def test_getitem(self):
        ''' Test that `util.ObjectProxy` can be used with getitem syntax. '''

        st_struct = struct.ObjectProxy(
            {
                'HelloThere': True,
                'IamA': False,
                'StRuct': None
            },
            case_sensitive=False)

        assert st_struct['HelloThere'] is True
        assert st_struct['hellothere'] is True
        assert 'HelloThere' in st_struct
        assert 'hellothere' in st_struct
        assert 'idonotexist' not in st_struct

        # try invalid names
        with self.assertRaises(KeyError):
            st_struct['IDoNotExist']
        with self.assertRaises(KeyError):
            st_struct['idonotexist']