summaryrefslogtreecommitdiff
path: root/unittest/test_util_TextTable.cpp
blob: 36240731f1e2dff033d15067a53a09e25a79716c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2021 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include <util/TextTable.hpp>

#include <third_party/doctest.h>

#include <iostream> // macOS bug: https://github.com/onqtam/doctest/issues/126

TEST_CASE("TextTable")
{
  using C = util::TextTable::Cell;

  util::TextTable table;

  SUBCASE("empty")
  {
    CHECK(table.render() == "");
  }

  SUBCASE("1x1")
  {
    table.add_row({"a"});
    CHECK(table.render() == "a\n");
  }

  SUBCASE("2x1 with space prefix/suffix")
  {
    table.add_row({std::string(" a "), C(" b ")});
    CHECK(table.render() == " a   b\n");
  }

  SUBCASE("1x2")
  {
    table.add_row({"a"});
    table.add_row({1});
    CHECK(table.render() == "a\n1\n");
  }

  SUBCASE("3 + 2")
  {
    table.add_row({"a", "b", "c"});
    table.add_row({"aa", "bbb"});
    CHECK(table.render()
          == ("a  b   c\n"
              "aa bbb\n"));
  }

  SUBCASE("strings and numbers")
  {
    table.add_row({"a", 123, "cc"});
    table.add_row({"aa", 4, "ccc"});
    table.add_row({"aaa", 56, "c"});
    CHECK(table.render()
          == ("a   123 cc\n"
              "aa    4 ccc\n"
              "aaa  56 c\n"));
  }

  SUBCASE("left align")
  {
    table.add_row({"a", 123, "cc"});
    table.add_row({"aa", C(4).left_align(), "ccc"});
    table.add_row({"aaa", 56, "c"});
    CHECK(table.render()
          == ("a   123 cc\n"
              "aa  4   ccc\n"
              "aaa  56 c\n"));
  }

  SUBCASE("right align")
  {
    table.add_row({"a", "bbb", "cc"});
    table.add_row(
      {C("aa").right_align(), C("b").right_align(), C("ccc").right_align()});
    table.add_row({"aaa", "bb", "c"});
    CHECK(table.render()
          == ("a   bbb cc\n"
              " aa   b ccc\n"
              "aaa bb  c\n"));
  }

  SUBCASE("heading")
  {
    table.add_row({"a", "b", "c"});
    table.add_heading("DDDDDD");
    table.add_row({"aaa", "bbb", "ccc"});
    CHECK(table.render()
          == ("a   b   c\n"
              "DDDDDD\n"
              "aaa bbb ccc\n"));
  }

  SUBCASE("colspan")
  {
    table.add_row({C("22").colspan(2), C("2r").colspan(2).right_align()});
    table.add_row({C("1").colspan(1), C("22222").colspan(2), "1"});
    table.add_row({"1", "1", "1", "1", "1"});
    table.add_row({"1", C("3333333333").colspan(3), "1"});
    CHECK(table.render()
          == ("22        2r\n"      // 4 columns
              "1 22222 1\n"         // 4 columns
              "1 1 1   1    1\n"    // 5 columns
              "1 3333333333 1\n")); // 5 columns
  }
}