]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- added unit test
authorArvin Schnell <aschnell@suse.de>
Fri, 5 Oct 2018 10:07:06 +0000 (12:07 +0200)
committerArvin Schnell <aschnell@suse.de>
Fri, 5 Oct 2018 10:07:06 +0000 (12:07 +0200)
testsuite/Makefile.am
testsuite/table.cc [new file with mode: 0644]

index 3058449e1232a69c8be55a093aaff08355599aaa..5607a37876aa8376896c66d2e79b1a1ab2170251 100644 (file)
@@ -7,7 +7,7 @@ AM_CPPFLAGS = -I$(top_srcdir) $(DBUS_CFLAGS)
 LDADD = ../snapper/libsnapper.la ../dbus/libdbus.la -lboost_unit_test_framework
 
 check_PROGRAMS = sysconfig-get1.test dirname1.test basename1.test              \
-       equal-date.test dbus-escape.test cmp-lt.test humanstring.test
+       equal-date.test dbus-escape.test cmp-lt.test humanstring.test table.test
 
 if ENABLE_BTRFS_QUOTA
 check_PROGRAMS +=  qgroup1.test
@@ -23,3 +23,5 @@ equal_date_test_LDADD = -lboost_unit_test_framework ../client/utils/libutils.la
 
 humanstring_test_LDADD = -lboost_unit_test_framework ../client/utils/libutils.la
 
+table_test_LDADD = -lboost_unit_test_framework ../client/utils/libutils.la
+
diff --git a/testsuite/table.cc b/testsuite/table.cc
new file mode 100644 (file)
index 0000000..5c5c4dd
--- /dev/null
@@ -0,0 +1,73 @@
+
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_MODULE snapper
+
+#include <boost/test/unit_test.hpp>
+
+#include <iostream>
+#include <numeric>
+#include <iomanip>
+
+#include "../client/utils/Table.h"
+
+
+using namespace std;
+
+
+void
+check(const Table& table, const vector<string>& output)
+{
+    ostringstream tmp;
+    tmp << setw(42) << table;
+    string lhs = tmp.str();
+
+    string rhs = accumulate(output.begin(), output.end(), (string)(""),
+                            [](const string& a, const string& b) { return a + b + '\n'; });
+
+    BOOST_CHECK_EQUAL(lhs, rhs);
+}
+
+
+BOOST_AUTO_TEST_CASE(test1)
+{
+    locale::global(locale("en_GB.UTF-8"));
+
+    Table table;
+
+    TableHeader header;
+    header.add("Number", TableAlign::RIGHT);
+    header.add("Name EN");
+    header.add("Name DE");
+    header.add("Square", TableAlign::RIGHT);
+    table.setHeader(header);
+
+    TableRow row1;
+    row1.add("0");
+    row1.add("zero");
+    row1.add("Null");
+    row1.add("0");
+    table.add(row1);
+
+    TableRow row2;
+    row2 << "1" << "one" << "Eins" << "1";
+    table.add(row2);
+
+    TableRow row3;
+    row3 << "5" << "five" << "Fünf" << "25";
+    table.add(row3);
+
+    TableRow row4;
+    row4 << "12" << "twelve" << "Zwölf" << "144";
+    table.add(row4);
+
+    vector<string> output = {
+       "Number | Name EN | Name DE | Square",
+       "-------+---------+---------+-------",
+       "     0 | zero    | Null    |      0",
+       "     1 | one     | Eins    |      1",
+       "     5 | five    | Fünf    |     25",
+       "    12 | twelve  | Zwölf   |    144"
+    };
+
+    check(table, output);
+}