def literal(value: Literal.TYPE) -> Arbitrary.TYPE: """ Return the given value. This operation can be useful to create constant resources to be used as input for other operations. :param value: An arbitrary (Python) literal. """ return Literal.convert(value)
def test_format(self): self.assertEqual(Literal.format(None), '') self.assertEqual(Literal.format(434), '434') self.assertEqual(Literal.format(3.4), '3.4') self.assertEqual(Literal.format("abc"), "'abc'") self.assertEqual(Literal.format(True), 'True') self.assertEqual(Literal.format([1, 2, 3]), '[1, 2, 3]')
def test_convert(self): self.assertEqual(Literal.convert(''), None) self.assertEqual(Literal.convert('None'), None) self.assertEqual(Literal.convert('434'), 434) self.assertEqual(Literal.convert('3.4'), 3.4) self.assertEqual(Literal.convert('True'), True) self.assertEqual(Literal.convert('"abc"'), 'abc') # Does not work anymore in Python 3.7 # self.assertEqual(Literal.convert('2 + 6'), 8) self.assertEqual(Literal.convert('[3, 5, 7]'), [3, 5, 7]) self.assertEqual(Literal.convert('(3, 5, 7)'), (3, 5, 7)) with self.assertRaises(ValidationError): Literal.convert('[1,2') with self.assertRaises(ValidationError): Literal.convert('abc')
def test_convert(self): self.assertEqual(Literal.convert(''), None) self.assertEqual(Literal.convert('None'), None) self.assertEqual(Literal.convert('434'), 434) self.assertEqual(Literal.convert('3.4'), 3.4) self.assertEqual(Literal.convert('True'), True) self.assertEqual(Literal.convert('"abc"'), 'abc') self.assertEqual(Literal.convert('2 + 6'), 8) self.assertEqual(Literal.convert('(3, 5, 7)'), (3, 5, 7)) with self.assertRaises(ValidationError): Literal.convert('[1,2') with self.assertRaises(ValidationError): Literal.convert('abc')