Path: blob/main/contrib/kyua/utils/text/table_test.cpp
48178 views
// Copyright 2012 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/text/table.hpp"2930#include <algorithm>3132#include <atf-c++.hpp>3334#include "utils/text/operations.ipp"3536namespace text = utils::text;373839/// Performs a check on text::table_formatter.40///41/// This is provided for test simplicity's sake. Having to match the result of42/// the formatting on a line by line basis would result in too verbose tests43/// (maybe not with C++11, but not using this yet).44///45/// Because of the flattening of the formatted table into a string, we risk46/// misdetecting problems when the algorithm bundles newlines into the lines of47/// a table. This should not happen, and not accounting for this little detail48/// makes testing so much easier.49///50/// \param expected Textual representation of the table, as a collection of51/// lines separated by newline characters.52/// \param formatter The formatter to use.53/// \param table The table to format.54static void55table_formatter_check(const std::string& expected,56const text::table_formatter& formatter,57const text::table& table)58{59ATF_REQUIRE_EQ(expected, text::join(formatter.format(table), "\n") + "\n");60}61626364ATF_TEST_CASE_WITHOUT_HEAD(table__ncolumns);65ATF_TEST_CASE_BODY(table__ncolumns)66{67ATF_REQUIRE_EQ(5, text::table(5).ncolumns());68ATF_REQUIRE_EQ(10, text::table(10).ncolumns());69}707172ATF_TEST_CASE_WITHOUT_HEAD(table__column_width);73ATF_TEST_CASE_BODY(table__column_width)74{75text::table_row row1;76row1.push_back("1234");77row1.push_back("123456");78text::table_row row2;79row2.push_back("12");80row2.push_back("12345678");8182text::table table(2);83table.add_row(row1);84table.add_row(row2);8586ATF_REQUIRE_EQ(4, table.column_width(0));87ATF_REQUIRE_EQ(8, table.column_width(1));88}899091ATF_TEST_CASE_WITHOUT_HEAD(table__column_widths);92ATF_TEST_CASE_BODY(table__column_widths)93{94text::table_row row1;95row1.push_back("1234");96row1.push_back("123456");97text::table_row row2;98row2.push_back("12");99row2.push_back("12345678");100101text::table table(2);102table.add_row(row1);103table.add_row(row2);104105ATF_REQUIRE_EQ(4, table.column_widths()[0]);106ATF_REQUIRE_EQ(8, table.column_widths()[1]);107}108109110ATF_TEST_CASE_WITHOUT_HEAD(table__empty);111ATF_TEST_CASE_BODY(table__empty)112{113text::table table(2);114ATF_REQUIRE(table.empty());115table.add_row(text::table_row(2));116ATF_REQUIRE(!table.empty());117}118119120ATF_TEST_CASE_WITHOUT_HEAD(table__iterate);121ATF_TEST_CASE_BODY(table__iterate)122{123text::table_row row1;124row1.push_back("foo");125text::table_row row2;126row2.push_back("bar");127128text::table table(1);129table.add_row(row1);130table.add_row(row2);131132text::table::const_iterator iter = table.begin();133ATF_REQUIRE(iter != table.end());134ATF_REQUIRE(row1 == *iter);135++iter;136ATF_REQUIRE(iter != table.end());137ATF_REQUIRE(row2 == *iter);138++iter;139ATF_REQUIRE(iter == table.end());140}141142143ATF_TEST_CASE_WITHOUT_HEAD(table_formatter__empty);144ATF_TEST_CASE_BODY(table_formatter__empty)145{146ATF_REQUIRE(text::table_formatter().set_separator(" ")147.format(text::table(1)).empty());148ATF_REQUIRE(text::table_formatter().set_separator(" ")149.format(text::table(10)).empty());150}151152153ATF_TEST_CASE_WITHOUT_HEAD(table_formatter__defaults);154ATF_TEST_CASE_BODY(table_formatter__defaults)155{156text::table table(3);157{158text::table_row row;159row.push_back("First");160row.push_back("Second");161row.push_back("Third");162table.add_row(row);163}164{165text::table_row row;166row.push_back("Fourth with some text");167row.push_back("Fifth with some more text");168row.push_back("Sixth foo");169table.add_row(row);170}171172table_formatter_check(173"First Second Third\n"174"Fourth with some textFifth with some more textSixth foo\n",175text::table_formatter(), table);176}177178179ATF_TEST_CASE_WITHOUT_HEAD(table_formatter__one_column__no_max_width);180ATF_TEST_CASE_BODY(table_formatter__one_column__no_max_width)181{182text::table table(1);183{184text::table_row row;185row.push_back("First row with some words");186table.add_row(row);187}188{189text::table_row row;190row.push_back("Second row with some words");191table.add_row(row);192}193194table_formatter_check(195"First row with some words\n"196"Second row with some words\n",197text::table_formatter().set_separator(" | "), table);198}199200201ATF_TEST_CASE_WITHOUT_HEAD(table_formatter__one_column__explicit_width);202ATF_TEST_CASE_BODY(table_formatter__one_column__explicit_width)203{204text::table table(1);205{206text::table_row row;207row.push_back("First row with some words");208table.add_row(row);209}210{211text::table_row row;212row.push_back("Second row with some words");213table.add_row(row);214}215216table_formatter_check(217"First row with some words\n"218"Second row with some words\n",219text::table_formatter().set_separator(" | ").set_column_width(0, 1024),220table);221}222223224ATF_TEST_CASE_WITHOUT_HEAD(table_formatter__one_column__max_width);225ATF_TEST_CASE_BODY(table_formatter__one_column__max_width)226{227text::table table(1);228{229text::table_row row;230row.push_back("First row with some words");231table.add_row(row);232}233{234text::table_row row;235row.push_back("Second row with some words");236table.add_row(row);237}238239table_formatter_check(240"First row\nwith some\nwords\n"241"Second row\nwith some\nwords\n",242text::table_formatter().set_separator(" | ").set_table_width(11),243table);244}245246247ATF_TEST_CASE_WITHOUT_HEAD(table_formatter__many_columns__no_max_width);248ATF_TEST_CASE_BODY(table_formatter__many_columns__no_max_width)249{250text::table table(3);251{252text::table_row row;253row.push_back("First");254row.push_back("Second");255row.push_back("Third");256table.add_row(row);257}258{259text::table_row row;260row.push_back("Fourth with some text");261row.push_back("Fifth with some more text");262row.push_back("Sixth foo");263table.add_row(row);264}265266table_formatter_check(267"First | Second | Third\n"268"Fourth with some text | Fifth with some more text | Sixth foo\n",269text::table_formatter().set_separator(" | "), table);270}271272273ATF_TEST_CASE_WITHOUT_HEAD(table_formatter__many_columns__explicit_width);274ATF_TEST_CASE_BODY(table_formatter__many_columns__explicit_width)275{276text::table table(3);277{278text::table_row row;279row.push_back("First");280row.push_back("Second");281row.push_back("Third");282table.add_row(row);283}284{285text::table_row row;286row.push_back("Fourth with some text");287row.push_back("Fifth with some more text");288row.push_back("Sixth foo");289table.add_row(row);290}291292table_formatter_check(293"First | Second | Third\n"294"Fourth with some text | Fifth with some more text | Sixth foo\n",295text::table_formatter().set_separator(" | ").set_column_width(0, 23)296.set_column_width(1, 28), table);297}298299300ATF_TEST_CASE_WITHOUT_HEAD(table_formatter__many_columns__max_width);301ATF_TEST_CASE_BODY(table_formatter__many_columns__max_width)302{303text::table table(3);304{305text::table_row row;306row.push_back("First");307row.push_back("Second");308row.push_back("Third");309table.add_row(row);310}311{312text::table_row row;313row.push_back("Fourth with some text");314row.push_back("Fifth with some more text");315row.push_back("Sixth foo");316table.add_row(row);317}318319table_formatter_check(320"First | Second | Third\n"321"Fourth with some text | Fifth with | Sixth foo\n"322" | some more | \n"323" | text | \n",324text::table_formatter().set_separator(" | ").set_table_width(46)325.set_column_width(1, text::table_formatter::width_refill)326.set_column_width(0, text::table_formatter::width_auto), table);327328table_formatter_check(329"First | Second | Third\n"330"Fourth with some text | Fifth with | Sixth foo\n"331" | some more | \n"332" | text | \n",333text::table_formatter().set_separator(" | ").set_table_width(48)334.set_column_width(1, text::table_formatter::width_refill)335.set_column_width(0, 23), table);336}337338339ATF_TEST_CASE_WITHOUT_HEAD(table_formatter__use_case__cli_help);340ATF_TEST_CASE_BODY(table_formatter__use_case__cli_help)341{342text::table options_table(2);343{344text::table_row row;345row.push_back("-a a_value");346row.push_back("This is the description of the first flag");347options_table.add_row(row);348}349{350text::table_row row;351row.push_back("-b");352row.push_back("And this is the text for the second flag");353options_table.add_row(row);354}355356text::table commands_table(2);357{358text::table_row row;359row.push_back("first");360row.push_back("This is the first command");361commands_table.add_row(row);362}363{364text::table_row row;365row.push_back("second");366row.push_back("And this is the second command");367commands_table.add_row(row);368}369370const text::widths_vector::value_type first_width =371std::max(options_table.column_width(0), commands_table.column_width(0));372373table_formatter_check(374"-a a_value This is the description\n"375" of the first flag\n"376"-b And this is the text for\n"377" the second flag\n",378text::table_formatter().set_separator(" ").set_table_width(36)379.set_column_width(0, first_width)380.set_column_width(1, text::table_formatter::width_refill),381options_table);382383table_formatter_check(384"first This is the first\n"385" command\n"386"second And this is the second\n"387" command\n",388text::table_formatter().set_separator(" ").set_table_width(36)389.set_column_width(0, first_width)390.set_column_width(1, text::table_formatter::width_refill),391commands_table);392}393394395ATF_INIT_TEST_CASES(tcs)396{397ATF_ADD_TEST_CASE(tcs, table__ncolumns);398ATF_ADD_TEST_CASE(tcs, table__column_width);399ATF_ADD_TEST_CASE(tcs, table__column_widths);400ATF_ADD_TEST_CASE(tcs, table__empty);401ATF_ADD_TEST_CASE(tcs, table__iterate);402403ATF_ADD_TEST_CASE(tcs, table_formatter__empty);404ATF_ADD_TEST_CASE(tcs, table_formatter__defaults);405ATF_ADD_TEST_CASE(tcs, table_formatter__one_column__no_max_width);406ATF_ADD_TEST_CASE(tcs, table_formatter__one_column__explicit_width);407ATF_ADD_TEST_CASE(tcs, table_formatter__one_column__max_width);408ATF_ADD_TEST_CASE(tcs, table_formatter__many_columns__no_max_width);409ATF_ADD_TEST_CASE(tcs, table_formatter__many_columns__explicit_width);410ATF_ADD_TEST_CASE(tcs, table_formatter__many_columns__max_width);411ATF_ADD_TEST_CASE(tcs, table_formatter__use_case__cli_help);412}413414415