def test_format_strings(self): pv = PrettyValue("s") results = pv.format("test") self.assertEquals(results, "test") pv = PrettyValue("10s") results = pv.format("test") self.assertEquals(results, "test ") pv = PrettyValue(">10s") results = pv.format("test") self.assertEquals(results, " test") pv = PrettyValue("^10s") results = pv.format("test") self.assertEquals(results, " test ") pv = PrettyValue("10s") results = pv.format(5.25) self.assertEquals(results, "5.25 ") self.assertRaises(ValueError, PrettyValue, "=10s") self.assertRaises(ValueError, PrettyValue, "=10s") self.assertRaises(ValueError, PrettyValue, "-10s") self.assertRaises(ValueError, PrettyValue, "10.2s")
def test_format_percents(self): pv = PrettyValue(".2%") results = pv.format(".0525") self.assertEquals(results, "5.25%") pv = PrettyValue("%") results = pv.format("1") self.assertEquals(results, "100.0000%")
def test_format_unknowns(self): d = datetime.datetime(2010, 7, 4, 12, 15, 58) pv = PrettyValue("%Y-%m-%d") results = pv.format(d) self.assertEquals(results, "2010-07-04") self.assertRaises(ValueError, PrettyValue, " %Y-%m-%d") pv = PrettyValue(">10%Y") results = pv.format(d) self.assertEquals(results, " 2010")
def test_maxwidth(self): pv = PrettyValue("10s") results = pv.format("test") self.assertEquals(pv.maxwidth, 10) pv = PrettyValue("10s") results = pv.format("testing is lots of fun") self.assertEquals(pv.maxwidth, 22) pv = PrettyValue("i") results = pv.format(5123) self.assertEquals(pv.maxwidth, 4) pv = PrettyValue("5.1f") results = pv.format(5123.23456) self.assertEquals(pv.maxwidth, 6)
def test_format_fill(self): pv = PrettyValue("10s", fill="*") results = pv.format("test") self.assertEquals(results, "test******") pv = PrettyValue("^10s", fill="-") results = pv.format("test") self.assertEquals(results, "---test---") pv = PrettyValue("^10i", fill="-") results = pv.format("5") self.assertEquals(results, "----5-----") pv = PrettyValue("^+10.2f", fill="-") results = pv.format(5.23456) self.assertEquals(results, "--+5.23---") pv = PrettyValue("10i", fill=0) results = pv.format(5) self.assertEquals(results, "0000000005") pv = PrettyValue("-10i", fill=0) results = pv.format(-5) self.assertEquals(results, "-000000005") pv = PrettyValue(">10i", fill=0) results = pv.format(-5) self.assertEquals(results, "00000000-5") pv = PrettyValue("=10i", fill=0) results = pv.format(-5) self.assertEquals(results, "-000000005") pv = PrettyValue(" 10i", fill=0) results = pv.format(5) self.assertEquals(results, " 000000005")
def test_format_floats(self): # default precision is used of 4 and it does round up. pv = PrettyValue("f") results = pv.format("5.256678") self.assertEquals(results, "5.2567") # no sign just size pv = PrettyValue("10f") results = pv.format("5.256678") self.assertEquals(results, " 5.2567") # same size but small precision pv = PrettyValue("10.2f") results = pv.format("5.256678") self.assertEquals(results, " 5.26") # same size but bigger precision pv = PrettyValue("10.8f") results = pv.format(5.256678) self.assertEquals(results, "5.25667800") pv = PrettyValue("<10.2f") results = pv.format(5.256678) self.assertEquals(results, "5.26 ") pv = PrettyValue("^10.1f") results = pv.format(5) self.assertEquals(results, " 5.0 ") pv = PrettyValue("< 10f") results = pv.format(5) self.assertEquals(results, " 5.0000 ") # positive sign and size pv = PrettyValue("+10f") results = pv.format(5) self.assertEquals(results, "+ 5.0000") pv = PrettyValue(">+10.1f") results = pv.format(5) self.assertEquals(results, " +5.0") pv = PrettyValue("<+10.1f") results = pv.format(5.256) self.assertEquals(results, "+5.3 ") pv = PrettyValue("^+10.1f") results = pv.format(5.233) self.assertEquals(results, " +5.2 ") # negative number and size pv = PrettyValue("10f") results = pv.format("-5.25634") self.assertEquals(results, "- 5.2563") pv = PrettyValue(">10.2f") results = pv.format("-5.33333") self.assertEquals(results, " -5.33") pv = PrettyValue("< 10f") results = pv.format("-5.3333") self.assertEquals(results, "-5.3333 ") pv = PrettyValue("<10.2f") results = pv.format(-5.256) self.assertEquals(results, "-5.26 ") pv = PrettyValue("^10.2f") results = pv.format("-5") self.assertEquals(results, " -5.00 ") pv = PrettyValue(" f") results = pv.format(5) self.assertEquals(results, " 5.0000") pv = PrettyValue(" f") results = pv.format(-5) self.assertEquals(results, "-5.0000") pv = PrettyValue("f") self.assertRaises(ValueError, pv.format, "test")
def test_format_ints(self): pv = PrettyValue("i") results = pv.format("5") self.assertEquals(results, "5") # no sign just size pv = PrettyValue("10i") results = pv.format("5") self.assertEquals(results, " 5") pv = PrettyValue("<10i") results = pv.format("5") self.assertEquals(results, "5 ") pv = PrettyValue("^10i") results = pv.format("5") self.assertEquals(results, " 5 ") pv = PrettyValue("< 10i") results = pv.format("5") self.assertEquals(results, " 5 ") # positive sign and size pv = PrettyValue("+10i") results = pv.format("5") self.assertEquals(results, "+ 5") pv = PrettyValue(">+10i") results = pv.format("5") self.assertEquals(results, " +5") pv = PrettyValue("<+10i") results = pv.format("5") self.assertEquals(results, "+5 ") pv = PrettyValue("^+10i") results = pv.format("5") self.assertEquals(results, " +5 ") # negative number and size pv = PrettyValue("10i") results = pv.format("-5") self.assertEquals(results, "- 5") pv = PrettyValue(">10i") results = pv.format("-5") self.assertEquals(results, " -5") pv = PrettyValue("< 10i") results = pv.format("-5") self.assertEquals(results, "-5 ") pv = PrettyValue("<10i") results = pv.format("-5") self.assertEquals(results, "-5 ") pv = PrettyValue("^10i") results = pv.format("-5") self.assertEquals(results, " -5 ") # test float conversion to int formatting pv = PrettyValue("i") results = pv.format(5.25) self.assertEquals(results, "5") pv = PrettyValue(" i") results = pv.format(5) self.assertEquals(results, " 5") pv = PrettyValue(" i") results = pv.format(-5) self.assertEquals(results, "-5") pv = PrettyValue("+10i") results = pv.format(5) self.assertEquals(results, "+ 5") self.assertRaises(ValueError, PrettyValue, "10.2i") pv = PrettyValue("i") self.assertRaises(ValueError, pv.format, "test")