|
| 1 | +from textwrap import dedent |
| 2 | + |
| 3 | +from texttable import Texttable, get_color_string, bcolors |
| 4 | + |
| 5 | +def test_colored(): |
| 6 | + table = Texttable() |
| 7 | + table.set_cols_align(["l", "r", "c"]) |
| 8 | + table.set_cols_valign(["t", "m", "b"]) |
| 9 | + table.add_rows([ |
| 10 | + [get_color_string(bcolors.GREEN, "Name Of Person"), "Age", "Nickname"], |
| 11 | + ["Mr\nXavier\nHuon", 32, "Xav'"], |
| 12 | + [get_color_string(bcolors.BLUE,"Mr\nBaptiste\nClement"), |
| 13 | + 1, |
| 14 | + get_color_string(bcolors.RED,"Baby")] ]) |
| 15 | + expected_output = dedent(""" |
| 16 | + +----------------+-----+----------+ |
| 17 | + | [92mName Of Person[0m | Age | Nickname | |
| 18 | + +================+=====+==========+ |
| 19 | + | Mr | | | |
| 20 | + | Xavier | 32 | | |
| 21 | + | Huon | | Xav' | |
| 22 | + +----------------+-----+----------+ |
| 23 | + | [94mMr[0m | | | |
| 24 | + | [94mBaptiste[0m | 1 | | |
| 25 | + | [94mClement[0m | | [91mBaby[0m | |
| 26 | + +----------------+-----+----------+ |
| 27 | + """).strip('\n') |
| 28 | + |
| 29 | + assert table.draw() == expected_output |
| 30 | + |
| 31 | +def test_typed_columns(): |
| 32 | + table = Texttable() |
| 33 | + table.set_deco(Texttable.HEADER) |
| 34 | + table.set_cols_dtype(['t', # text |
| 35 | + 'f', # float (decimal) |
| 36 | + 'e', # float (exponent) |
| 37 | + 'i', # integer |
| 38 | + 'a']) # automatic |
| 39 | + table.set_cols_align(["l", "r", "r", "r", "l"]) |
| 40 | + table.add_rows([["text", "float", "exp", "int", "auto"], |
| 41 | + ["abcd", "67", 654, 89, 128.001], |
| 42 | + ["efghijk", 67.5434, .654, 89.6, 12800000000000000000000.00023], |
| 43 | + ["lmn", 5e-78, 5e-78, 89.4, .000000000000128], |
| 44 | + ["opqrstu", .023, 5e+78, 92., 12800000000000000000000]]) |
| 45 | + expected_output = dedent(""" |
| 46 | + text float exp int auto |
| 47 | + ============================================== |
| 48 | + abcd 67.000 6.540e+02 89 128.001 |
| 49 | + efghijk 67.543 6.540e-01 90 1.280e+22 |
| 50 | + lmn 0.000 5.000e-78 89 0.000 |
| 51 | + opqrstu 0.023 5.000e+78 92 1.280e+22 |
| 52 | + """).strip('\n') |
| 53 | + |
| 54 | + assert table.draw() == expected_output |
0 commit comments