"""Test utility functions.""" from vasl_templates.webapp.utils import friendly_fractions # --------------------------------------------------------------------- def test_friendly_fractions(): """Test generating friendly fractions.""" def do_test( val, expected, singular=False ): #pylint: disable=missing-docstring # test without a postfix assert friendly_fractions( val ) == expected # test the singular/plural postfixes expected = expected+" foo" if singular else expected+" foos" assert friendly_fractions( val, "foo", "foos" ) == expected # do the test do_test( 0, "0" ) do_test( 0.124, "0" ) do_test( 0.125, "¼", singular=True ) do_test( 0.374, "¼", singular=True ) do_test( 0.375, "½", singular=True ) do_test( 0.624, "½", singular=True ) do_test( 0.625, "¾", singular=True ) do_test( 0.874, "¾", singular=True ) do_test( 0.875, "1", singular=True ) do_test( 1.124, "1", singular=True ) do_test( 1.125, "1¼" ) do_test( 1.374, "1¼" ) do_test( 1.375, "1½" ) do_test( 1.624, "1½" ) do_test( 1.625, "1¾" ) do_test( 1.874, "1¾" ) do_test( 1.875, "2" ) do_test( 2.125, "2¼" ) do_test( 2.5, "2½" ) do_test( 2.75, "2¾" )