diff options
author | Ilya Etingof <etingof@gmail.com> | 2017-11-16 22:32:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-16 22:32:14 +0100 |
commit | 4cfceb7716d5f553643ad4509de1808989f0fc4d (patch) | |
tree | 0b62df991c5487f95818e9bc37e0eab0a23bae73 /tests | |
parent | 9ec03d3837d3333f0edb2a0f9919dcf82c1f679c (diff) | |
download | pyasn1-4cfceb7716d5f553643ad4509de1808989f0fc4d.tar.gz |
Add default to .getComponentBy*() (#100)
* `default` kwarg added to .getComponentBy*() methods
Diffstat (limited to 'tests')
-rw-r--r-- | tests/type/test_univ.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/type/test_univ.py b/tests/type/test_univ.py index 57fec99..7c3162a 100644 --- a/tests/type/test_univ.py +++ b/tests/type/test_univ.py @@ -1139,6 +1139,19 @@ class SequenceOf(BaseTestCase): assert n == o + def testGetComponentWithDefault(self): + + class SequenceOf(univ.SequenceOf): + componentType = univ.OctetString() + + s = SequenceOf() + assert s.getComponentByPosition(0, default=None, instantiate=False) is None + assert s.getComponentByPosition(0, default=None) is None + s[0] = 'test' + assert s.getComponentByPosition(0, default=None) is not None + assert s.getComponentByPosition(0, default=None) == str2octs('test') + s.clear() + assert s.getComponentByPosition(0, default=None) is None def testGetComponentNoInstantiation(self): @@ -1376,6 +1389,25 @@ class Sequence(BaseTestCase): s['name'] = 'abc' assert s['name'] == str2octs('abc') + def testGetComponentWithDefault(self): + + class Sequence(univ.Sequence): + componentType = namedtype.NamedTypes( + namedtype.NamedType('name', univ.OctetString('')), + namedtype.OptionalNamedType('nick', univ.OctetString()), + ) + + s = Sequence() + + assert s[0] == str2octs('') + assert s.getComponentByPosition(1, default=None, instantiate=False) is None + assert s.getComponentByName('nick', default=None) is None + s[1] = 'test' + assert s.getComponentByPosition(1, default=None) is not None + assert s.getComponentByPosition(1, default=None) == str2octs('test') + s.clear() + assert s.getComponentByPosition(1, default=None) is None + def testGetComponentNoInstantiation(self): class Sequence(univ.Sequence): @@ -1619,6 +1651,24 @@ class Set(BaseTestCase): s['name'] = 'abc' assert s['name'] == str2octs('abc') + def testGetComponentWithDefault(self): + + class Set(univ.Set): + componentType = namedtype.NamedTypes( + namedtype.NamedType('id', univ.Integer(123)), + namedtype.OptionalNamedType('nick', univ.OctetString()), + ) + + s = Set() + assert s[0] == 123 + assert s.getComponentByPosition(1, default=None, instantiate=False) is None + assert s.getComponentByName('nick', default=None) is None + s[1] = 'test' + assert s.getComponentByPosition(1, default=None) is not None + assert s.getComponentByPosition(1, default=None) == str2octs('test') + s.clear() + assert s.getComponentByPosition(1, default=None) is None + def testGetComponentNoInstantiation(self): class Set(univ.Set): @@ -1792,6 +1842,27 @@ class Choice(BaseTestCase): c.setComponentByType(univ.OctetString.tagSet, 'abc') assert c.getName() == 'name' + def testGetComponentWithDefault(self): + + s = univ.Choice( + componentType=namedtype.NamedTypes( + namedtype.NamedType('name', univ.OctetString()), + namedtype.NamedType('id', univ.Integer()) + ) + ) + + assert s.getComponentByPosition(0, default=None, instantiate=False) is None + assert s.getComponentByPosition(1, default=None, instantiate=False) is None + assert s.getComponentByName('name', default=None, instantiate=False) is None + assert s.getComponentByName('id', default=None, instantiate=False) is None + assert s.getComponentByType(univ.OctetString.tagSet, default=None) is None + assert s.getComponentByType(univ.Integer.tagSet, default=None) is None + s[1] = 123 + assert s.getComponentByPosition(1, default=None) is not None + assert s.getComponentByPosition(1, univ.noValue) == 123 + s.clear() + assert s.getComponentByPosition(1, default=None, instantiate=False) is None + def testGetComponentNoInstantiation(self): s = univ.Choice( |