bidict/tests/test_namedbidict.txt

59 lines
1.6 KiB
Plaintext

Test script for :func:`bidict.namedbidict`::
>>> from bidict import namedbidict
>>> ElementMap = namedbidict('ElementMap', 'symbol', 'element')
>>> noble_gases = ElementMap(He='helium')
>>> noble_gases.element_for['He']
'helium'
>>> noble_gases.symbol_for['helium']
'He'
>>> noble_gases.element_for['Ne'] = 'neon'
>>> del noble_gases.symbol_for['helium']
>>> noble_gases
ElementMap({'Ne': 'neon'})
Pickling works::
>>> import pickle
>>> pickle.dumps(noble_gases) is not 'a PicklingError'
True
Invalid names are rejected::
>>> invalid = namedbidict('0xabad1d3a', 'keys', 'vals')
Traceback (most recent call last):
...
ValueError: "0xabad1d3a" does not match pattern ^[a-zA-Z][a-zA-Z0-9_]*$
Comparison works as expected::
>>> from bidict import bidict
>>> noble_gases2 = ElementMap({'Ne': 'neon'})
>>> noble_gases2 == noble_gases
True
>>> noble_gases2 == bidict(noble_gases)
True
>>> noble_gases2 == dict(noble_gases)
True
>>> noble_gases2['Rn'] = 'radon'
>>> noble_gases2 == noble_gases
False
>>> noble_gases2 != noble_gases
True
>>> noble_gases2 != bidict(noble_gases)
True
>>> noble_gases2 != dict(noble_gases)
True
Test ``base_type`` keyword arg::
>>> from bidict import frozenbidict
>>> ElMap = namedbidict('ElMap', 'sym', 'el', base_type=frozenbidict)
>>> noble = ElMap(He='helium')
>>> hash(noble) is not 'an exception'
True
>>> noble['C'] = 'carbon'
Traceback (most recent call last):
...
TypeError...