tasciitables.nim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import stdtest/unittest_light
  2. import std/private/asciitables
  3. import strformat
  4. proc alignTableCustom(s: string, delim = '\t', sep = ","): string =
  5. for cell in parseTableCells(s, delim):
  6. result.add fmt"({cell.row},{cell.col}): "
  7. for i in cell.text.len..<cell.width:
  8. result.add " "
  9. result.add cell.text
  10. if cell.col < cell.ncols-1:
  11. result.add sep
  12. if cell.col == cell.ncols-1 and cell.row < cell.nrows - 1:
  13. result.add '\n'
  14. proc testAlignTable() =
  15. block: # test with variable width columns
  16. var ret = ""
  17. ret.add "12\t143\tbcdef\n"
  18. ret.add "2\t14394852020\tbcdef\n"
  19. ret.add "45342\t1\tbf\n"
  20. ret.add "45342\t1\tbsadfasdfasfdasdff\n"
  21. ret.add "453232323232342\t1\tbsadfasdfasfdasdff\n"
  22. ret.add "45342\t1\tbf\n"
  23. ret.add "45342\t1\tb afasf a ff\n"
  24. ret.add "4\t1\tbf\n"
  25. assertEquals alignTable(ret),
  26. """
  27. 12 143 bcdef
  28. 2 14394852020 bcdef
  29. 45342 1 bf
  30. 45342 1 bsadfasdfasfdasdff
  31. 453232323232342 1 bsadfasdfasfdasdff
  32. 45342 1 bf
  33. 45342 1 b afasf a ff
  34. 4 1 bf
  35. """
  36. assertEquals alignTable(ret, fill = '.', sep = ","),
  37. """
  38. 12.............,143........,bcdef.............
  39. 2..............,14394852020,bcdef.............
  40. 45342..........,1..........,bf................
  41. 45342..........,1..........,bsadfasdfasfdasdff
  42. 453232323232342,1..........,bsadfasdfasfdasdff
  43. 45342..........,1..........,bf................
  44. 45342..........,1..........,b afasf a ff......
  45. 4..............,1..........,bf................
  46. """
  47. assertEquals alignTableCustom(ret, sep = " "),
  48. """
  49. (0,0): 12 (0,1): 143 (0,2): bcdef
  50. (1,0): 2 (1,1): 14394852020 (1,2): bcdef
  51. (2,0): 45342 (2,1): 1 (2,2): bf
  52. (3,0): 45342 (3,1): 1 (3,2): bsadfasdfasfdasdff
  53. (4,0): 453232323232342 (4,1): 1 (4,2): bsadfasdfasfdasdff
  54. (5,0): 45342 (5,1): 1 (5,2): bf
  55. (6,0): 45342 (6,1): 1 (6,2): b afasf a ff
  56. (7,0): 4 (7,1): 1 (7,2): bf
  57. """
  58. block: # test with 1 column
  59. var ret = "12\nasdfa\nadf"
  60. assertEquals alignTable(ret), """
  61. 12
  62. asdfa
  63. adf """
  64. block: # test with empty input
  65. var ret = ""
  66. assertEquals alignTable(ret), ""
  67. block: # test with 1 row
  68. var ret = "abc\tdef"
  69. assertEquals alignTable(ret), """
  70. abc def"""
  71. block: # test with 1 row ending in \t
  72. var ret = "abc\tdef\t"
  73. assertEquals alignTable(ret), """
  74. abc def """
  75. block: # test with 1 row starting with \t
  76. var ret = "\tabc\tdef\t"
  77. assertEquals alignTable(ret), """
  78. abc def """
  79. block: # test with variable number of cols per row
  80. var ret = """
  81. a1,a2,a3
  82. b1
  83. c1,c2
  84. ,d1
  85. """
  86. assertEquals alignTableCustom(ret, delim = ',', sep = ","),
  87. """
  88. (0,0): a1,(0,1): a2,(0,2): a3
  89. (1,0): ,(1,1): ,(1,2):
  90. (2,0): b1,(2,1): ,(2,2):
  91. (3,0): c1,(3,1): c2,(3,2):
  92. (4,0): ,(4,1): d1,(4,2):
  93. """
  94. testAlignTable()