2020-02-23 13:43:44 +01:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from util.format import *
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-23 13:43:44 +01:00
|
|
|
class format(unittest.TestCase):
|
|
|
|
def test_int_from_string(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(100, asint("100"))
|
|
|
|
self.assertEqual(-100, asint("-100"))
|
|
|
|
self.assertEqual(0, asint("0"))
|
2020-02-23 13:43:44 +01:00
|
|
|
|
|
|
|
def test_int_from_none(self):
|
|
|
|
self.assertEqual(0, asint(None))
|
|
|
|
|
|
|
|
def test_int_from_int(self):
|
|
|
|
self.assertEqual(100, asint(100))
|
|
|
|
self.assertEqual(-100, asint(-100))
|
|
|
|
self.assertEqual(0, asint(0))
|
|
|
|
|
|
|
|
def test_int_minimum(self):
|
|
|
|
self.assertEqual(100, asint(100, minimum=10))
|
|
|
|
self.assertEqual(100, asint(100, minimum=100))
|
|
|
|
self.assertEqual(10, asint(5, minimum=10))
|
|
|
|
|
|
|
|
def test_int_maximum(self):
|
|
|
|
self.assertEqual(100, asint(100, maximum=200))
|
|
|
|
self.assertEqual(100, asint(100, maximum=100))
|
|
|
|
self.assertEqual(100, asint(200, maximum=100))
|
|
|
|
|
|
|
|
def test_true_from_str(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertTrue(asbool("true"))
|
2020-02-23 13:43:44 +01:00
|
|
|
self.assertTrue(asbool(True))
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertTrue(asbool("t"))
|
|
|
|
self.assertTrue(asbool("1"))
|
|
|
|
self.assertTrue(asbool("yes"))
|
|
|
|
self.assertTrue(asbool("y"))
|
|
|
|
self.assertTrue(asbool("on"))
|
2020-02-23 13:43:44 +01:00
|
|
|
|
|
|
|
def test_false_from_str(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertFalse(asbool("false"))
|
2020-02-23 13:43:44 +01:00
|
|
|
self.assertFalse(asbool(False))
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertFalse(asbool("f"))
|
|
|
|
self.assertFalse(asbool("0"))
|
|
|
|
self.assertFalse(asbool("no"))
|
|
|
|
self.assertFalse(asbool("n"))
|
|
|
|
self.assertFalse(asbool("off"))
|
2020-02-23 13:43:44 +01:00
|
|
|
self.assertFalse(asbool(None))
|
|
|
|
|
|
|
|
def test_list_from_None(self):
|
|
|
|
self.assertEqual([], aslist(None))
|
|
|
|
|
|
|
|
def test_list_from_list(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual([1, 2, 3], aslist([1, 2, 3]))
|
2020-02-23 13:43:44 +01:00
|
|
|
|
|
|
|
def test_list_from_str(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(["12", "13", "14"], aslist("12,13,14"))
|
2020-02-23 13:43:44 +01:00
|
|
|
|
2020-03-06 15:00:03 +01:00
|
|
|
def test_byteformat(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("500.00B", byte(500))
|
|
|
|
self.assertEqual("1.00KiB", byte(1024))
|
|
|
|
self.assertEqual("1KiB", byte(1024, "{:.0f}"))
|
|
|
|
self.assertEqual("1.50KiB", byte(1024 + 512))
|
|
|
|
self.assertEqual("2.50MiB", byte(1024 * 1024 * 2 + 1024 * 512))
|
|
|
|
self.assertEqual("4.50GiB", byte(1024 * 1024 * 1024 * 4 + 1024 * 1024 * 512))
|
|
|
|
self.assertEqual("2048.00GiB", byte(1024 * 1024 * 1024 * 1024 * 2))
|
2020-03-06 15:00:03 +01:00
|
|
|
|
2020-03-07 13:33:28 +01:00
|
|
|
def test_duration(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("04:20:00", duration(4 * 60 * 60 + 20 * 60))
|
|
|
|
self.assertEqual("04:20:00h", duration(4 * 60 * 60 + 20 * 60, unit=True))
|
|
|
|
self.assertEqual(
|
|
|
|
"04:20h", duration(4 * 60 * 60 + 20 * 60, compact=True, unit=True)
|
|
|
|
)
|
2020-03-07 13:33:28 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("20:00", duration(20 * 60))
|
|
|
|
self.assertEqual("20:00m", duration(20 * 60, unit=True))
|
|
|
|
self.assertEqual("20:00m", duration(20 * 60, compact=True, unit=True))
|
2020-03-07 13:33:28 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("00:20", duration(20))
|
|
|
|
self.assertEqual("00:20m", duration(20, unit=True))
|
|
|
|
self.assertEqual("00:20m", duration(20, compact=True, unit=True))
|
2020-03-07 13:33:28 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("n/a", duration(-1))
|
2020-04-19 14:07:15 +02:00
|
|
|
|
2020-04-02 16:54:57 +02:00
|
|
|
def test_seconds(self):
|
|
|
|
self.assertEqual(10, seconds(10))
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(10, seconds("10"))
|
2020-04-02 16:54:57 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(300, seconds("5m"))
|
|
|
|
self.assertEqual(320, seconds("5m20s"))
|
2020-04-02 16:54:57 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(4 * 3600, seconds("4h"))
|
|
|
|
self.assertEqual(4 * 3600 + 5 * 60 + 22, seconds("4h5m22s"))
|
2020-04-02 16:54:57 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(4 * 3600 + 5 * 60, seconds("4h5m"))
|
2020-04-02 16:54:57 +02:00
|
|
|
|
2020-04-04 07:47:53 +02:00
|
|
|
def test_temperature(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("10°C", astemperature(10))
|
|
|
|
self.assertEqual("10°C", astemperature(10, "metric"))
|
|
|
|
self.assertEqual("-100°F", astemperature(-100, "imperial"))
|
|
|
|
self.assertEqual("-100°K", astemperature("-100", "kelvin"))
|
|
|
|
|
2020-04-04 07:47:53 +02:00
|
|
|
|
2020-02-23 13:43:44 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|