def test_typename(self): self.assertEqual(NumberValue(name='foo').typename, 'rational number') self.assertEqual( NumberValue(name='foo', min=5).typename, 'rational number (>= 5)') self.assertEqual( NumberValue(name='foo', max=5).typename, 'rational number (<= 5)') self.assertEqual( NumberValue(name='foo', min=-5, max=5).typename, 'rational number (-5 - 5)')
def test_min_larger_than_max(self): with self.assertRaises(ValueError) as cm: NumberValue(name='foo', min=100, max=99) self.assertIn('minimum must be smaller than or equal to maximum', str(cm.exception)) val = NumberValue(name='foo') val.min = 50 with self.assertRaises(ValueError) as cm: val.max = 49 self.assertIn('minimum must be smaller than or equal to maximum', str(cm.exception))
def test_valid_values(self): val = NumberValue(name='foo', default=10) self.assertEqual(val.get(), 10) for newval, exp in ((0, 0), (0.0, 0), ('0', 0), ('0.0', 0), (0.123456789, 0.123456789), (-1e3, -1000), ('+=7', -993), ('-= 3', -996)): val.set(newval) self.assertEqual(val.get(), exp)
def test_prefix(self): val = NumberValue('test', default=1024, prefix='binary') self.assertEqual(val.get(), 1024) self.assertEqual(val.string(), '1Ki') self.assertEqual(val.prefix, 'binary') val.prefix = 'metric' self.assertEqual(val.get(), 1024) self.assertEqual(val.string(), '1.02k') self.assertEqual(val.prefix, 'metric') val.set(1000) self.assertEqual(val.get(), 1000) self.assertEqual(val.string(), '1k') self.assertEqual(val.prefix, 'metric') val.prefix = 'binary' self.assertEqual(val.get(), 1000) self.assertEqual(val.string(), '1000') self.assertEqual(val.prefix, 'binary')
def test_unit(self): val = NumberValue('test', default=15, unit='m') self.assertEqual(val.get(), 15) self.assertEqual(val.unit, 'm') self.assertEqual(val.string(with_unit=True), '15m') self.assertEqual(val.string(with_unit=False), '15') val.unit = 'x' self.assertEqual(val.unit, 'x') self.assertEqual(val.string(with_unit=True), '15x') self.assertEqual(val.string(with_unit=False), '15') val.unit = None self.assertEqual(val.unit, None) self.assertEqual(val.string(with_unit=True), '15') self.assertEqual(val.string(with_unit=False), '15')
def test_max(self): val = NumberValue(name='foo', max=100) with self.assertRaises(ValueError) as cm: val.set(100.001) self.assertIn('Too large', str(cm.exception)) self.assertIn('maximum is 100', str(cm.exception)) val = NumberValue(name='foo', default=100) val.max = 50 self.assertEqual(val.max, 50) self.assertEqual(val.get(), 50) self.assertEqual(val.get_default(), 50) with self.assertRaises(ValueError) as cm: val.set(51) self.assertIn('Too large', str(cm.exception)) self.assertIn('maximum is 50', str(cm.exception))
def test_min(self): val = NumberValue(name='foo', min=10) with self.assertRaises(ValueError) as cm: val.set(9) self.assertIn('Too small', str(cm.exception)) self.assertIn('minimum is 10', str(cm.exception)) val = NumberValue(name='foo', default=100) val.min = 150 self.assertEqual(val.min, 150) self.assertEqual(val.get(), 150) self.assertEqual(val.get_default(), 150) with self.assertRaises(ValueError) as cm: val.set(149) self.assertIn('Too small', str(cm.exception)) self.assertIn('minimum is 150', str(cm.exception))
def test_string_from_specific_value(self): val = NumberValue(name='foo', default=42.3) self.assertEqual(val.string(value=-0.12), '-0.12') self.assertEqual(val.string(default=True), '42.3')
def test___repr__(self): val = NumberValue(name='foo', default=42e3, unit='m', prefix='metric') self.assertEqual(repr(val), 'foo=42km') val.set(1024) val.prefix = 'binary' self.assertEqual(repr(val), 'foo=1Kim')
def test_string_from_default_value(self): val = NumberValue(name='foo', default=42.0) val.set(-5) self.assertEqual(val.string(), '-5') self.assertEqual(val.string(default=True), '42')
def test_string_from_current_value(self): val = NumberValue(name='foo', default=42) self.assertEqual(val.string(), '42')
def test_adjusting_current_value_without_default_value(self): val = NumberValue(name='foo') val.set('+=23') self.assertEqual(val.get(), 23)
def test_adjusting_current_value(self): val = NumberValue(name='foo', default=10) val.set('+=23') self.assertEqual(val.get(), 33) val.set('-=3') self.assertEqual(val.get(), 30) val.set('+=-10') self.assertEqual(val.get(), 20)
def test_invalid_values(self): val = NumberValue(name='foo') for v in (True, False, [1, 2, 3]): with self.assertRaises(ValueError) as cm: val.set(v) self.assertIn('Not a %s' % val.typename, str(cm.exception))