def test_arithmetic_operation_returns_correct_type(self): self.assertIsInstance(Float(2.5) + 1.5, Int) self.assertIsInstance(Float(2.5) - 1.5, Int) self.assertIsInstance(Float(2.5) * 2, Int) self.assertIsInstance(Float(10) % 1, Int) self.assertIsInstance(Int(5) + 4.3, Float) self.assertIsInstance(Int(5) - 0.1, Float) self.assertIsInstance(Int(5) / 2, Float) self.assertIsInstance(Int(5) % 0.3, Float) self.assertIsInstance(round(Float(5.4)), Int) self.assertIsInstance(Float('inf') / 2, Float)
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_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_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_rounding(self): self.assertEqual(Int(1.4), 1) self.assertEqual(Int(1.5), 2) self.assertEqual(Int('1.4'), 1) self.assertEqual(Int('1.5'), 2)