def test_errors(self): with self.assert_raises(RuntimeError, 'No options provided'): Option('fooo', options=()) with self.assert_raises(ValueError, 'Not foo'): Option('fooo', options=('foo', )) with self.assert_raises(ValueError, 'Not one of: foo, bar'): Option('fooo', options=('foo', 'bar'))
def test_aliases(self): options = ('foo', 'bar') aliases = {'f': 'foo'} self.assertEqual(Option('f', options=options, aliases=aliases), 'foo') self.assertEqual(Option('foo', options=options, aliases=aliases), 'foo') self.assertEqual(Option('bar', options=options, aliases=aliases), 'bar') with self.assert_raises(ValueError, 'Not one of: foo, bar'): Option('fo', options=options, aliases=aliases)
def test_typename(self): constructors = (Int.partial(min=-1, max=100), Option.partial(options=('foo', 'bar')), String) exp_typename = ' or '.join( (Int.partial(min=-1, max=100).typename, Option.partial(options=('foo', 'bar')).typename, String.partial().typename)) mt = multitype(*constructors) self.assertEqual(mt.typename, exp_typename) inst = mt('hello') self.assertEqual(inst.typename, exp_typename)
def test_syntax(self): constructors = (Int.partial(min=-1, max=100), Option.partial(options=('foo', 'bar')), String) exp_syntax = ' or '.join( (Int.partial(min=-1, max=100).syntax, Option.partial(options=('foo', 'bar')).syntax, String.partial().syntax)) mt = multitype(*constructors) self.assertEqual(mt.syntax, exp_syntax) inst = mt('hello') self.assertEqual(inst.syntax, exp_syntax)
def test_issubclass(self): mt = multitype(Float, Option.partial(options=('foo', 'bar', 'baz')), Tuple.partial(sep=' / ', dedup=True)) for subcls in (Float, Option, Tuple): self.assertTrue(issubclass(subcls, mt)) for subcls in (String, Int, Path): self.assertFalse(issubclass(subcls, mt))
def test_invalid_value(self): mt = multitype(Option.partial(options=('foo', 'bar', 'baz')), Int.partial(min=10)) with self.assert_raises(ValueError, 'Not one of: foo, bar, baz; Not a number'): mt('hi') with self.assert_raises( ValueError, 'Not one of: foo, bar, baz; Too small (minimum is 10)'): mt(9)
def test_aliases_inverse_property(self): o = Option('1', options=('1', '2', '3'), aliases={ 'one': '1', 'two': '2', 'too': '2' }) self.assertEqual(o.aliases_inverse, { '1': ['one'], '2': ['two', 'too'] })
def test_aliases_inverse_property(self): o = Option('1', options=('1', '2', '3'), aliases={ 'one': '1', 'two': '2', 'too': '2' }) import sys if sys.hexversion < 0x03060000: # Python <= 3.6 dicts are not ordered yet inverse = o.aliases_inverse inverse['2'] = set(inverse['2']) self.assertEqual(inverse, {'1': ['one'], '2': {'two', 'too'}}) else: self.assertEqual(o.aliases_inverse, { '1': ['one'], '2': ['two', 'too'] })
def test_aliases_property(self): o = Option('1', options=('1', '2', '3'), aliases={'one': '1'}) self.assertEqual(o.aliases, {'one': '1'}) o = Option('1', options=('1', '2', '3')) self.assertEqual(o.aliases, {})
def test_options_property(self): o = Option('1', options=('1', '2', '3')) self.assertEqual(o.options, ('1', '2', '3'))
def test_syntax(self): self.assertEqual(Option('1', options=('1', '2', '3')).syntax, '1|2|3')