Add tests for nice_number formatting
import unittest
from mycroft.util.format import nice_number
+numbers_fixture = {
+ 1.435634: '1.436',
+ 2: '2',
+ 5.0: '5',
+ 0.027: '0.027',
+ 0.5: '1 half',
+ 1.333: '1 and 1 third',
+ 2.666: '2 and 2 thirds',
+ 0.25: '1 forth',
+ 1.25: '1 and 1 forth',
+ 0.75: '3 forths',
+ 1.75: '1 and 3 forths',
+ 3.4: '3 and 2 fifths',
+ 16.8333: '16 and 5 sixths',
+ 12.5714: '12 and 4 sevenths',
+ 9.625: '9 and 5 eigths',
+ 6.777: '6 and 7 ninths',
+ 3.1: '3 and 1 tenth',
+ 2.272: '2 and 3 elevenths',
+ 5.583: '5 and 7 twelveths',
+ 8.384: '8 and 5 thirteenths',
+ 0.071: '1 fourteenth',
+ 6.466: '6 and 7 fifteenths',
+ 8.312: '8 and 5 sixteenths',
+ 2.176: '2 and 3 seventeenths',
+ 200.722: '200 and 13 eighteenths',
+ 7.421: '7 and 8 nineteenths',
+ 0.05: '1 twentyith'
+}
-class TestNiceNumber(unittest.TestCase):
+class TestNieceNumberFormat(unittest.TestCase):
- def test_vulgar_fractions(self):
- res = nice_number(0.25, speech=True)
- self.assertIn(res, ['quarter', 'one forth'])
- res = nice_number(0.50, speech=True)
- self.assertIn(res, ['half'])
- res = nice_number(0.75, speech=True)
- self.assertIn(res, ['three quarters', 'three forths'])
+ def test_convert_float_to_nice_number(self):
+ for number, number_str in numbers_fixture.items():
+ self.assertEqual(nice_number(number), number_str, \
+ 'should format {} as {} and not {}'.format(
+ number, number_str, nice_number(number)))
+ def test_specify_denominator(self):
+ self.assertEqual(nice_number(5.5, denominators=[1,2,3]), \
+ '5 and 1 half', \
+ 'should format 5.5 as 5 and 1 half not {}'.format(
+ nice_number(5.5, denominators=[1,2,3])
+ ))
+ self.assertEqual(nice_number(2.333, denominators=[1,2]), \
+ '2.333', \
+ 'should format 2.333 as 2.333 not {}'.format(
+ nice_number(2.333, denominators=[1,2])
+ ))
+
+ def test_different_language(self):
+ self.assertEqual(nice_number(5.5, lang="es-us"), '5.5', \
+ 'should format 5.5 as 5.5 not {}'.format(
+ nice_number(5.5, lang="es-us")
+ ))
+
+
+if __name__ == "__main__":
+ unittest.main()
