From 1b277b45cc4059760072095f3bd6e8a4e4c4d406 Mon Sep 17 00:00:00 2001 From: nessita <124304+nessita@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:15:53 -0300 Subject: [PATCH] Added dedicated test for invalid inputs in floatformat template filter tests. Co-authored-by: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> --- .../filter_tests/test_floatformat.py | 43 +++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/tests/template_tests/filter_tests/test_floatformat.py b/tests/template_tests/filter_tests/test_floatformat.py index db176223096..145858b75fd 100644 --- a/tests/template_tests/filter_tests/test_floatformat.py +++ b/tests/template_tests/filter_tests/test_floatformat.py @@ -60,12 +60,8 @@ class FunctionTests(SimpleTestCase): floatformat(Decimal("123456.123456789012345678901"), 21), "123456.123456789012345678901", ) - self.assertEqual(floatformat("foo"), "") self.assertEqual(floatformat(13.1031, "bar"), "13.1031") self.assertEqual(floatformat(18.125, 2), "18.13") - self.assertEqual(floatformat("foo", "bar"), "") - self.assertEqual(floatformat("¿Cómo esta usted?"), "") - self.assertEqual(floatformat(None), "") self.assertEqual( floatformat(-1.323297138040798e35, 2), "-132329713804079800000000000000000000.00", @@ -78,6 +74,45 @@ class FunctionTests(SimpleTestCase): self.assertEqual(floatformat(1.5e-15, -20), "0.00000000000000150000") self.assertEqual(floatformat(1.00000000000000015, 16), "1.0000000000000002") + def test_invalid_inputs(self): + cases = [ + # Non-numeric strings. + None, + [], + {}, + object(), + "abc123", + "123abc", + "foo", + "error", + "¿Cómo esta usted?", + # Scientific notation - missing exponent value. + "1e", + "1e+", + "1e-", + # Scientific notation - missing base number. + "e400", + "e+400", + "e-400", + # Scientific notation - invalid exponent value. + "1e^2", + "1e2e3", + "1e2a", + "1e2.0", + "1e2,0", + # Scientific notation - misplaced decimal point. + "1e.2", + "1e2.", + # Scientific notation - misplaced '+' sign. + "1+e2", + "1e2+", + ] + for value in cases: + with self.subTest(value=value): + self.assertEqual(floatformat(value), "") + with self.subTest(value=value, arg="bar"): + self.assertEqual(floatformat(value, "bar"), "") + def test_force_grouping(self): with translation.override("en"): self.assertEqual(floatformat(10000, "g"), "10,000")