xref: /minix3/external/bsd/kyua-cli/dist/cli/common_test.cpp (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
111be35a1SLionel Sambuc // Copyright 2011 Google Inc.
211be35a1SLionel Sambuc // All rights reserved.
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
511be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
611be35a1SLionel Sambuc // met:
711be35a1SLionel Sambuc //
811be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
911be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
1011be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
1111be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
1211be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
1311be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
1411be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
1511be35a1SLionel Sambuc //   without specific prior written permission.
1611be35a1SLionel Sambuc //
1711be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1811be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1911be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2011be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2111be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2211be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2311be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2411be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2511be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2611be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2711be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc 
2911be35a1SLionel Sambuc #include "cli/common.hpp"
3011be35a1SLionel Sambuc 
3111be35a1SLionel Sambuc #include <fstream>
3211be35a1SLionel Sambuc 
3311be35a1SLionel Sambuc #include <atf-c++.hpp>
3411be35a1SLionel Sambuc 
3511be35a1SLionel Sambuc #include "engine/exceptions.hpp"
3611be35a1SLionel Sambuc #include "engine/filters.hpp"
3711be35a1SLionel Sambuc #include "engine/test_case.hpp"
3811be35a1SLionel Sambuc #include "engine/test_program.hpp"
3911be35a1SLionel Sambuc #include "engine/test_result.hpp"
4011be35a1SLionel Sambuc #include "utils/cmdline/exceptions.hpp"
4111be35a1SLionel Sambuc #include "utils/cmdline/globals.hpp"
4211be35a1SLionel Sambuc #include "utils/cmdline/parser.ipp"
4311be35a1SLionel Sambuc #include "utils/cmdline/ui_mock.hpp"
4411be35a1SLionel Sambuc #include "utils/datetime.hpp"
4511be35a1SLionel Sambuc #include "utils/env.hpp"
4611be35a1SLionel Sambuc #include "utils/fs/exceptions.hpp"
4711be35a1SLionel Sambuc #include "utils/fs/operations.hpp"
4811be35a1SLionel Sambuc #include "utils/fs/path.hpp"
4911be35a1SLionel Sambuc #include "utils/optional.ipp"
5011be35a1SLionel Sambuc #include "utils/sanity.hpp"
5111be35a1SLionel Sambuc 
5211be35a1SLionel Sambuc namespace cmdline = utils::cmdline;
5311be35a1SLionel Sambuc namespace config = utils::config;
5411be35a1SLionel Sambuc namespace datetime = utils::datetime;
5511be35a1SLionel Sambuc namespace fs = utils::fs;
5611be35a1SLionel Sambuc 
5711be35a1SLionel Sambuc using utils::optional;
5811be35a1SLionel Sambuc 
5911be35a1SLionel Sambuc 
6011be35a1SLionel Sambuc namespace {
6111be35a1SLionel Sambuc 
6211be35a1SLionel Sambuc 
6311be35a1SLionel Sambuc /// Syntactic sugar to instantiate engine::test_filter objects.
6411be35a1SLionel Sambuc inline engine::test_filter
mkfilter(const char * test_program,const char * test_case)6511be35a1SLionel Sambuc mkfilter(const char* test_program, const char* test_case)
6611be35a1SLionel Sambuc {
6711be35a1SLionel Sambuc     return engine::test_filter(fs::path(test_program), test_case);
6811be35a1SLionel Sambuc }
6911be35a1SLionel Sambuc 
7011be35a1SLionel Sambuc 
7111be35a1SLionel Sambuc }  // anonymous namespace
7211be35a1SLionel Sambuc 
7311be35a1SLionel Sambuc 
7411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(build_root_path__default);
ATF_TEST_CASE_BODY(build_root_path__default)7511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(build_root_path__default)
7611be35a1SLionel Sambuc {
7711be35a1SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
7811be35a1SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
7911be35a1SLionel Sambuc 
8011be35a1SLionel Sambuc     ATF_REQUIRE(!cli::build_root_path(mock_cmdline));
8111be35a1SLionel Sambuc }
8211be35a1SLionel Sambuc 
8311be35a1SLionel Sambuc 
8411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(build_root_path__explicit);
ATF_TEST_CASE_BODY(build_root_path__explicit)8511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(build_root_path__explicit)
8611be35a1SLionel Sambuc {
8711be35a1SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
8811be35a1SLionel Sambuc     options["build-root"].push_back("/my//path");
8911be35a1SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
9011be35a1SLionel Sambuc 
9111be35a1SLionel Sambuc     ATF_REQUIRE(cli::build_root_path(mock_cmdline));
9211be35a1SLionel Sambuc     ATF_REQUIRE_EQ("/my/path", cli::build_root_path(mock_cmdline).get().str());
9311be35a1SLionel Sambuc }
9411be35a1SLionel Sambuc 
9511be35a1SLionel Sambuc 
9611be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_home__ok);
ATF_TEST_CASE_BODY(get_home__ok)9711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_home__ok)
9811be35a1SLionel Sambuc {
9911be35a1SLionel Sambuc     const fs::path home("/foo/bar");
10011be35a1SLionel Sambuc     utils::setenv("HOME", home.str());
10111be35a1SLionel Sambuc     const optional< fs::path > computed = cli::get_home();
10211be35a1SLionel Sambuc     ATF_REQUIRE(computed);
10311be35a1SLionel Sambuc     ATF_REQUIRE_EQ(home, computed.get());
10411be35a1SLionel Sambuc }
10511be35a1SLionel Sambuc 
10611be35a1SLionel Sambuc 
10711be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_home__missing);
ATF_TEST_CASE_BODY(get_home__missing)10811be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_home__missing)
10911be35a1SLionel Sambuc {
11011be35a1SLionel Sambuc     utils::unsetenv("HOME");
11111be35a1SLionel Sambuc     ATF_REQUIRE(!cli::get_home());
11211be35a1SLionel Sambuc }
11311be35a1SLionel Sambuc 
11411be35a1SLionel Sambuc 
11511be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(get_home__invalid);
ATF_TEST_CASE_BODY(get_home__invalid)11611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(get_home__invalid)
11711be35a1SLionel Sambuc {
11811be35a1SLionel Sambuc     utils::setenv("HOME", "");
11911be35a1SLionel Sambuc     ATF_REQUIRE(!cli::get_home());
12011be35a1SLionel Sambuc }
12111be35a1SLionel Sambuc 
12211be35a1SLionel Sambuc 
12311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(kyuafile_path__default);
ATF_TEST_CASE_BODY(kyuafile_path__default)12411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(kyuafile_path__default)
12511be35a1SLionel Sambuc {
12611be35a1SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
12711be35a1SLionel Sambuc     options["kyuafile"].push_back(cli::kyuafile_option.default_value());
12811be35a1SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
12911be35a1SLionel Sambuc 
13011be35a1SLionel Sambuc     ATF_REQUIRE_EQ(cli::kyuafile_option.default_value(),
13111be35a1SLionel Sambuc                    cli::kyuafile_path(mock_cmdline).str());
13211be35a1SLionel Sambuc }
13311be35a1SLionel Sambuc 
13411be35a1SLionel Sambuc 
13511be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(kyuafile_path__explicit);
ATF_TEST_CASE_BODY(kyuafile_path__explicit)13611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(kyuafile_path__explicit)
13711be35a1SLionel Sambuc {
13811be35a1SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
13911be35a1SLionel Sambuc     options["kyuafile"].push_back("/my//path");
14011be35a1SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
14111be35a1SLionel Sambuc 
14211be35a1SLionel Sambuc     ATF_REQUIRE_EQ("/my/path", cli::kyuafile_path(mock_cmdline).str());
14311be35a1SLionel Sambuc }
14411be35a1SLionel Sambuc 
14511be35a1SLionel Sambuc 
146*84d9c625SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(result_types__default);
ATF_TEST_CASE_BODY(result_types__default)147*84d9c625SLionel Sambuc ATF_TEST_CASE_BODY(result_types__default)
148*84d9c625SLionel Sambuc {
149*84d9c625SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
150*84d9c625SLionel Sambuc     options["results-filter"].push_back(
151*84d9c625SLionel Sambuc         cli::results_filter_option.default_value());
152*84d9c625SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
153*84d9c625SLionel Sambuc 
154*84d9c625SLionel Sambuc     cli::result_types exp_types;
155*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::skipped);
156*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::expected_failure);
157*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::broken);
158*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::failed);
159*84d9c625SLionel Sambuc     ATF_REQUIRE(exp_types == cli::get_result_types(mock_cmdline));
160*84d9c625SLionel Sambuc }
161*84d9c625SLionel Sambuc 
162*84d9c625SLionel Sambuc 
163*84d9c625SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(result_types__empty);
ATF_TEST_CASE_BODY(result_types__empty)164*84d9c625SLionel Sambuc ATF_TEST_CASE_BODY(result_types__empty)
165*84d9c625SLionel Sambuc {
166*84d9c625SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
167*84d9c625SLionel Sambuc     options["results-filter"].push_back("");
168*84d9c625SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
169*84d9c625SLionel Sambuc 
170*84d9c625SLionel Sambuc     cli::result_types exp_types;
171*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::passed);
172*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::skipped);
173*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::expected_failure);
174*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::broken);
175*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::failed);
176*84d9c625SLionel Sambuc     ATF_REQUIRE(exp_types == cli::get_result_types(mock_cmdline));
177*84d9c625SLionel Sambuc }
178*84d9c625SLionel Sambuc 
179*84d9c625SLionel Sambuc 
180*84d9c625SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(result_types__explicit__all);
ATF_TEST_CASE_BODY(result_types__explicit__all)181*84d9c625SLionel Sambuc ATF_TEST_CASE_BODY(result_types__explicit__all)
182*84d9c625SLionel Sambuc {
183*84d9c625SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
184*84d9c625SLionel Sambuc     options["results-filter"].push_back("passed,skipped,xfail,broken,failed");
185*84d9c625SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
186*84d9c625SLionel Sambuc 
187*84d9c625SLionel Sambuc     cli::result_types exp_types;
188*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::passed);
189*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::skipped);
190*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::expected_failure);
191*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::broken);
192*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::failed);
193*84d9c625SLionel Sambuc     ATF_REQUIRE(exp_types == cli::get_result_types(mock_cmdline));
194*84d9c625SLionel Sambuc }
195*84d9c625SLionel Sambuc 
196*84d9c625SLionel Sambuc 
197*84d9c625SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(result_types__explicit__some);
ATF_TEST_CASE_BODY(result_types__explicit__some)198*84d9c625SLionel Sambuc ATF_TEST_CASE_BODY(result_types__explicit__some)
199*84d9c625SLionel Sambuc {
200*84d9c625SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
201*84d9c625SLionel Sambuc     options["results-filter"].push_back("skipped,broken");
202*84d9c625SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
203*84d9c625SLionel Sambuc 
204*84d9c625SLionel Sambuc     cli::result_types exp_types;
205*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::skipped);
206*84d9c625SLionel Sambuc     exp_types.push_back(engine::test_result::broken);
207*84d9c625SLionel Sambuc     ATF_REQUIRE(exp_types == cli::get_result_types(mock_cmdline));
208*84d9c625SLionel Sambuc }
209*84d9c625SLionel Sambuc 
210*84d9c625SLionel Sambuc 
211*84d9c625SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(result_types__explicit__invalid);
ATF_TEST_CASE_BODY(result_types__explicit__invalid)212*84d9c625SLionel Sambuc ATF_TEST_CASE_BODY(result_types__explicit__invalid)
213*84d9c625SLionel Sambuc {
214*84d9c625SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
215*84d9c625SLionel Sambuc     options["results-filter"].push_back("skipped,foo,broken");
216*84d9c625SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
217*84d9c625SLionel Sambuc 
218*84d9c625SLionel Sambuc     ATF_REQUIRE_THROW_RE(std::runtime_error, "Unknown result type 'foo'",
219*84d9c625SLionel Sambuc                          cli::get_result_types(mock_cmdline));
220*84d9c625SLionel Sambuc }
221*84d9c625SLionel Sambuc 
222*84d9c625SLionel Sambuc 
22311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(store_path__default__create_directory__ok);
ATF_TEST_CASE_BODY(store_path__default__create_directory__ok)22411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(store_path__default__create_directory__ok)
22511be35a1SLionel Sambuc {
22611be35a1SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
22711be35a1SLionel Sambuc     options["store"].push_back(cli::store_option.default_value());
22811be35a1SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
22911be35a1SLionel Sambuc 
23011be35a1SLionel Sambuc     const fs::path home("homedir");
23111be35a1SLionel Sambuc     utils::setenv("HOME", home.str());
23211be35a1SLionel Sambuc 
23311be35a1SLionel Sambuc     ATF_REQUIRE(!fs::exists(home / ".kyua"));
23411be35a1SLionel Sambuc     ATF_REQUIRE_EQ(home / ".kyua/store.db", cli::store_path(mock_cmdline));
23511be35a1SLionel Sambuc     ATF_REQUIRE(fs::exists(home / ".kyua"));
23611be35a1SLionel Sambuc }
23711be35a1SLionel Sambuc 
23811be35a1SLionel Sambuc 
23911be35a1SLionel Sambuc ATF_TEST_CASE(store_path__default__create_directory__fail);
ATF_TEST_CASE_HEAD(store_path__default__create_directory__fail)24011be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(store_path__default__create_directory__fail)
24111be35a1SLionel Sambuc {
24211be35a1SLionel Sambuc     set_md_var("require.user", "unprivileged");
24311be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(store_path__default__create_directory__fail)24411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(store_path__default__create_directory__fail)
24511be35a1SLionel Sambuc {
24611be35a1SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
24711be35a1SLionel Sambuc     options["store"].push_back(cli::store_option.default_value());
24811be35a1SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
24911be35a1SLionel Sambuc 
25011be35a1SLionel Sambuc     const fs::path home("homedir");
25111be35a1SLionel Sambuc     utils::setenv("HOME", home.str());
25211be35a1SLionel Sambuc     fs::mkdir(home, 0555);
25311be35a1SLionel Sambuc 
25411be35a1SLionel Sambuc     ATF_REQUIRE_THROW(fs::error, cli::store_path(mock_cmdline).str());
25511be35a1SLionel Sambuc     ATF_REQUIRE(!fs::exists(home / ".kyua"));
25611be35a1SLionel Sambuc }
25711be35a1SLionel Sambuc 
25811be35a1SLionel Sambuc 
25911be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(store_path__default__no_home);
ATF_TEST_CASE_BODY(store_path__default__no_home)26011be35a1SLionel Sambuc ATF_TEST_CASE_BODY(store_path__default__no_home)
26111be35a1SLionel Sambuc {
26211be35a1SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
26311be35a1SLionel Sambuc     options["store"].push_back(cli::store_option.default_value());
26411be35a1SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
26511be35a1SLionel Sambuc 
26611be35a1SLionel Sambuc     utils::unsetenv("HOME");
26711be35a1SLionel Sambuc 
26811be35a1SLionel Sambuc     ATF_REQUIRE_EQ("kyua-store.db", cli::store_path(mock_cmdline).str());
26911be35a1SLionel Sambuc }
27011be35a1SLionel Sambuc 
27111be35a1SLionel Sambuc 
27211be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(store_path__explicit);
ATF_TEST_CASE_BODY(store_path__explicit)27311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(store_path__explicit)
27411be35a1SLionel Sambuc {
27511be35a1SLionel Sambuc     std::map< std::string, std::vector< std::string > > options;
27611be35a1SLionel Sambuc     options["store"].push_back("/my//path");
27711be35a1SLionel Sambuc     const cmdline::parsed_cmdline mock_cmdline(options, cmdline::args_vector());
27811be35a1SLionel Sambuc 
27911be35a1SLionel Sambuc     const fs::path home("homedir");
28011be35a1SLionel Sambuc     ATF_REQUIRE_EQ("/my/path", cli::store_path(mock_cmdline).str());
28111be35a1SLionel Sambuc     ATF_REQUIRE(!fs::exists(home / ".kyua"));
28211be35a1SLionel Sambuc }
28311be35a1SLionel Sambuc 
28411be35a1SLionel Sambuc 
28511be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(parse_filters__none);
ATF_TEST_CASE_BODY(parse_filters__none)28611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(parse_filters__none)
28711be35a1SLionel Sambuc {
28811be35a1SLionel Sambuc     const cmdline::args_vector args;
28911be35a1SLionel Sambuc     const std::set< engine::test_filter > filters = cli::parse_filters(args);
29011be35a1SLionel Sambuc     ATF_REQUIRE(filters.empty());
29111be35a1SLionel Sambuc }
29211be35a1SLionel Sambuc 
29311be35a1SLionel Sambuc 
29411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(parse_filters__ok);
ATF_TEST_CASE_BODY(parse_filters__ok)29511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(parse_filters__ok)
29611be35a1SLionel Sambuc {
29711be35a1SLionel Sambuc     cmdline::args_vector args;
29811be35a1SLionel Sambuc     args.push_back("foo");
29911be35a1SLionel Sambuc     args.push_back("bar/baz");
30011be35a1SLionel Sambuc     args.push_back("other:abc");
30111be35a1SLionel Sambuc     args.push_back("other:bcd");
30211be35a1SLionel Sambuc     const std::set< engine::test_filter > filters = cli::parse_filters(args);
30311be35a1SLionel Sambuc 
30411be35a1SLionel Sambuc     std::set< engine::test_filter > exp_filters;
30511be35a1SLionel Sambuc     exp_filters.insert(mkfilter("foo", ""));
30611be35a1SLionel Sambuc     exp_filters.insert(mkfilter("bar/baz", ""));
30711be35a1SLionel Sambuc     exp_filters.insert(mkfilter("other", "abc"));
30811be35a1SLionel Sambuc     exp_filters.insert(mkfilter("other", "bcd"));
30911be35a1SLionel Sambuc 
31011be35a1SLionel Sambuc     ATF_REQUIRE(exp_filters == filters);
31111be35a1SLionel Sambuc }
31211be35a1SLionel Sambuc 
31311be35a1SLionel Sambuc 
31411be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(parse_filters__duplicate);
ATF_TEST_CASE_BODY(parse_filters__duplicate)31511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(parse_filters__duplicate)
31611be35a1SLionel Sambuc {
31711be35a1SLionel Sambuc     cmdline::args_vector args;
31811be35a1SLionel Sambuc     args.push_back("foo/bar//baz");
31911be35a1SLionel Sambuc     args.push_back("hello/world:yes");
32011be35a1SLionel Sambuc     args.push_back("foo//bar/baz");
32111be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(cmdline::error, "Duplicate.*'foo/bar/baz'",
32211be35a1SLionel Sambuc                          cli::parse_filters(args));
32311be35a1SLionel Sambuc }
32411be35a1SLionel Sambuc 
32511be35a1SLionel Sambuc 
32611be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(parse_filters__nondisjoint);
ATF_TEST_CASE_BODY(parse_filters__nondisjoint)32711be35a1SLionel Sambuc ATF_TEST_CASE_BODY(parse_filters__nondisjoint)
32811be35a1SLionel Sambuc {
32911be35a1SLionel Sambuc     cmdline::args_vector args;
33011be35a1SLionel Sambuc     args.push_back("foo/bar");
33111be35a1SLionel Sambuc     args.push_back("hello/world:yes");
33211be35a1SLionel Sambuc     args.push_back("foo/bar:baz");
33311be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(cmdline::error, "'foo/bar'.*'foo/bar:baz'.*disjoint",
33411be35a1SLionel Sambuc                          cli::parse_filters(args));
33511be35a1SLionel Sambuc }
33611be35a1SLionel Sambuc 
33711be35a1SLionel Sambuc 
33811be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(report_unused_filters__none);
ATF_TEST_CASE_BODY(report_unused_filters__none)33911be35a1SLionel Sambuc ATF_TEST_CASE_BODY(report_unused_filters__none)
34011be35a1SLionel Sambuc {
34111be35a1SLionel Sambuc     std::set< engine::test_filter > unused;
34211be35a1SLionel Sambuc 
34311be35a1SLionel Sambuc     cmdline::ui_mock ui;
34411be35a1SLionel Sambuc     ATF_REQUIRE(!cli::report_unused_filters(unused, &ui));
34511be35a1SLionel Sambuc     ATF_REQUIRE(ui.out_log().empty());
34611be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
34711be35a1SLionel Sambuc }
34811be35a1SLionel Sambuc 
34911be35a1SLionel Sambuc 
35011be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(report_unused_filters__some);
ATF_TEST_CASE_BODY(report_unused_filters__some)35111be35a1SLionel Sambuc ATF_TEST_CASE_BODY(report_unused_filters__some)
35211be35a1SLionel Sambuc {
35311be35a1SLionel Sambuc     std::set< engine::test_filter > unused;
35411be35a1SLionel Sambuc     unused.insert(mkfilter("a/b", ""));
35511be35a1SLionel Sambuc     unused.insert(mkfilter("hey/d", "yes"));
35611be35a1SLionel Sambuc 
35711be35a1SLionel Sambuc     cmdline::ui_mock ui;
35811be35a1SLionel Sambuc     cmdline::init("progname");
35911be35a1SLionel Sambuc     ATF_REQUIRE(cli::report_unused_filters(unused, &ui));
36011be35a1SLionel Sambuc     ATF_REQUIRE(ui.out_log().empty());
36111be35a1SLionel Sambuc     ATF_REQUIRE_EQ(2, ui.err_log().size());
36211be35a1SLionel Sambuc     ATF_REQUIRE( atf::utils::grep_collection("No.*matched.*'a/b'",
36311be35a1SLionel Sambuc                                              ui.err_log()));
36411be35a1SLionel Sambuc     ATF_REQUIRE( atf::utils::grep_collection("No.*matched.*'hey/d:yes'",
36511be35a1SLionel Sambuc                                              ui.err_log()));
36611be35a1SLionel Sambuc }
36711be35a1SLionel Sambuc 
36811be35a1SLionel Sambuc 
36911be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_delta);
ATF_TEST_CASE_BODY(format_delta)37011be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_delta)
37111be35a1SLionel Sambuc {
37211be35a1SLionel Sambuc     ATF_REQUIRE_EQ("0.000s", cli::format_delta(datetime::delta()));
37311be35a1SLionel Sambuc     ATF_REQUIRE_EQ("0.012s", cli::format_delta(datetime::delta(0, 12300)));
37411be35a1SLionel Sambuc     ATF_REQUIRE_EQ("0.999s", cli::format_delta(datetime::delta(0, 999000)));
37511be35a1SLionel Sambuc     ATF_REQUIRE_EQ("51.321s", cli::format_delta(datetime::delta(51, 321000)));
37611be35a1SLionel Sambuc }
37711be35a1SLionel Sambuc 
37811be35a1SLionel Sambuc 
37911be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_result__no_reason);
ATF_TEST_CASE_BODY(format_result__no_reason)38011be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_result__no_reason)
38111be35a1SLionel Sambuc {
38211be35a1SLionel Sambuc     ATF_REQUIRE_EQ("passed", cli::format_result(
38311be35a1SLionel Sambuc         engine::test_result(engine::test_result::passed)));
38411be35a1SLionel Sambuc     ATF_REQUIRE_EQ("failed", cli::format_result(
38511be35a1SLionel Sambuc         engine::test_result(engine::test_result::failed)));
38611be35a1SLionel Sambuc }
38711be35a1SLionel Sambuc 
38811be35a1SLionel Sambuc 
38911be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_result__with_reason);
ATF_TEST_CASE_BODY(format_result__with_reason)39011be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_result__with_reason)
39111be35a1SLionel Sambuc {
39211be35a1SLionel Sambuc     ATF_REQUIRE_EQ("broken: Something", cli::format_result(
39311be35a1SLionel Sambuc         engine::test_result(engine::test_result::broken, "Something")));
39411be35a1SLionel Sambuc     ATF_REQUIRE_EQ("expected_failure: A B C", cli::format_result(
39511be35a1SLionel Sambuc         engine::test_result(engine::test_result::expected_failure, "A B C")));
39611be35a1SLionel Sambuc     ATF_REQUIRE_EQ("failed: More text", cli::format_result(
39711be35a1SLionel Sambuc         engine::test_result(engine::test_result::failed, "More text")));
39811be35a1SLionel Sambuc     ATF_REQUIRE_EQ("skipped: Bye", cli::format_result(
39911be35a1SLionel Sambuc         engine::test_result(engine::test_result::skipped, "Bye")));
40011be35a1SLionel Sambuc }
40111be35a1SLionel Sambuc 
40211be35a1SLionel Sambuc 
40311be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_test_case_id__test_case);
ATF_TEST_CASE_BODY(format_test_case_id__test_case)40411be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_test_case_id__test_case)
40511be35a1SLionel Sambuc {
40611be35a1SLionel Sambuc     const engine::test_program test_program(
40711be35a1SLionel Sambuc         "mock", fs::path("foo/bar/baz"), fs::path("unused-root"),
40811be35a1SLionel Sambuc         "unused-suite-name", engine::metadata_builder().build());
40911be35a1SLionel Sambuc     const engine::test_case test_case("mock", test_program, "abc",
41011be35a1SLionel Sambuc                                       engine::metadata_builder().build());
41111be35a1SLionel Sambuc     ATF_REQUIRE_EQ("foo/bar/baz:abc", cli::format_test_case_id(test_case));
41211be35a1SLionel Sambuc }
41311be35a1SLionel Sambuc 
41411be35a1SLionel Sambuc 
41511be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(format_test_case_id__test_filter);
ATF_TEST_CASE_BODY(format_test_case_id__test_filter)41611be35a1SLionel Sambuc ATF_TEST_CASE_BODY(format_test_case_id__test_filter)
41711be35a1SLionel Sambuc {
41811be35a1SLionel Sambuc     const engine::test_filter filter(fs::path("foo/bar"), "baz");
41911be35a1SLionel Sambuc     ATF_REQUIRE_EQ("foo/bar:baz", cli::format_test_case_id(filter));
42011be35a1SLionel Sambuc }
42111be35a1SLionel Sambuc 
42211be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)42311be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
42411be35a1SLionel Sambuc {
42511be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, build_root_path__default);
42611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, build_root_path__explicit);
42711be35a1SLionel Sambuc 
42811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_home__ok);
42911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_home__missing);
43011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, get_home__invalid);
43111be35a1SLionel Sambuc 
43211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, kyuafile_path__default);
43311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, kyuafile_path__explicit);
43411be35a1SLionel Sambuc 
435*84d9c625SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, result_types__default);
436*84d9c625SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, result_types__empty);
437*84d9c625SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, result_types__explicit__all);
438*84d9c625SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, result_types__explicit__some);
439*84d9c625SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, result_types__explicit__invalid);
440*84d9c625SLionel Sambuc 
44111be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, store_path__default__create_directory__ok);
44211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, store_path__default__create_directory__fail);
44311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, store_path__default__no_home);
44411be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, store_path__explicit);
44511be35a1SLionel Sambuc 
44611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, parse_filters__none);
44711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, parse_filters__ok);
44811be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, parse_filters__duplicate);
44911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, parse_filters__nondisjoint);
45011be35a1SLionel Sambuc 
45111be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, report_unused_filters__none);
45211be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, report_unused_filters__some);
45311be35a1SLionel Sambuc 
45411be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_delta);
45511be35a1SLionel Sambuc 
45611be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_result__no_reason);
45711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_result__with_reason);
45811be35a1SLionel Sambuc 
45911be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_test_case_id__test_case);
46011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, format_test_case_id__test_filter);
46111be35a1SLionel Sambuc }
462