xref: /minix3/external/bsd/atf/dist/atf-c++/tests_test.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
111be35a1SLionel Sambuc //
211be35a1SLionel Sambuc // Automated Testing Framework (atf)
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc // All rights reserved.
611be35a1SLionel Sambuc //
711be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc // are met:
1011be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc //    documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc //
1611be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1711be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1811be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1911be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2111be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2211be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2311be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2411be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2511be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2611be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2711be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc //
2911be35a1SLionel Sambuc 
3011be35a1SLionel Sambuc extern "C" {
3111be35a1SLionel Sambuc #include <sys/types.h>
3211be35a1SLionel Sambuc #include <sys/stat.h>
3311be35a1SLionel Sambuc 
3411be35a1SLionel Sambuc #include <fcntl.h>
3511be35a1SLionel Sambuc #include <unistd.h>
3611be35a1SLionel Sambuc }
3711be35a1SLionel Sambuc 
3811be35a1SLionel Sambuc #include <fstream>
3911be35a1SLionel Sambuc #include <sstream>
4011be35a1SLionel Sambuc 
4111be35a1SLionel Sambuc #include "macros.hpp"
4211be35a1SLionel Sambuc 
4311be35a1SLionel Sambuc #include "detail/test_helpers.hpp"
44*0a6a1f1dSLionel Sambuc #include "detail/text.hpp"
4511be35a1SLionel Sambuc 
4611be35a1SLionel Sambuc // ------------------------------------------------------------------------
4711be35a1SLionel Sambuc // Tests for the "atf_tp_writer" class.
4811be35a1SLionel Sambuc // ------------------------------------------------------------------------
4911be35a1SLionel Sambuc 
5011be35a1SLionel Sambuc static
5111be35a1SLionel Sambuc void
print_indented(const std::string & str)5211be35a1SLionel Sambuc print_indented(const std::string& str)
5311be35a1SLionel Sambuc {
5411be35a1SLionel Sambuc     std::vector< std::string > ws = atf::text::split(str, "\n");
5511be35a1SLionel Sambuc     for (std::vector< std::string >::const_iterator iter = ws.begin();
5611be35a1SLionel Sambuc          iter != ws.end(); iter++)
5711be35a1SLionel Sambuc         std::cout << ">>" << *iter << "<<\n";
5811be35a1SLionel Sambuc }
5911be35a1SLionel Sambuc 
6011be35a1SLionel Sambuc // XXX Should this string handling and verbosity level be part of the
6111be35a1SLionel Sambuc // ATF_REQUIRE_EQ macro?  It may be hard to predict sometimes that a
6211be35a1SLionel Sambuc // string can have newlines in it, and so the error message generated
6311be35a1SLionel Sambuc // at the moment will be bogus if there are some.
6411be35a1SLionel Sambuc static
6511be35a1SLionel Sambuc void
check_equal(const atf::tests::tc & tc,const std::string & str,const std::string & exp)6611be35a1SLionel Sambuc check_equal(const atf::tests::tc& tc, const std::string& str,
6711be35a1SLionel Sambuc             const std::string& exp)
6811be35a1SLionel Sambuc {
6911be35a1SLionel Sambuc     if (str != exp) {
7011be35a1SLionel Sambuc         std::cout << "String equality check failed.\n"
7111be35a1SLionel Sambuc             "Adding >> and << to delimit the string boundaries below.\n";
7211be35a1SLionel Sambuc         std::cout << "GOT:\n";
7311be35a1SLionel Sambuc         print_indented(str);
7411be35a1SLionel Sambuc         std::cout << "EXPECTED:\n";
7511be35a1SLionel Sambuc         print_indented(exp);
7611be35a1SLionel Sambuc         tc.fail("Constructed string differs from the expected one");
7711be35a1SLionel Sambuc     }
7811be35a1SLionel Sambuc }
7911be35a1SLionel Sambuc 
8011be35a1SLionel Sambuc ATF_TEST_CASE(atf_tp_writer);
ATF_TEST_CASE_HEAD(atf_tp_writer)8111be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(atf_tp_writer)
8211be35a1SLionel Sambuc {
8311be35a1SLionel Sambuc     set_md_var("descr", "Verifies the application/X-atf-tp writer");
8411be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(atf_tp_writer)8511be35a1SLionel Sambuc ATF_TEST_CASE_BODY(atf_tp_writer)
8611be35a1SLionel Sambuc {
8711be35a1SLionel Sambuc     std::ostringstream expss;
8811be35a1SLionel Sambuc     std::ostringstream ss;
8911be35a1SLionel Sambuc 
9011be35a1SLionel Sambuc #define RESET \
9111be35a1SLionel Sambuc     expss.str(""); \
9211be35a1SLionel Sambuc     ss.str("")
9311be35a1SLionel Sambuc 
9411be35a1SLionel Sambuc #define CHECK \
9511be35a1SLionel Sambuc     check_equal(*this, ss.str(), expss.str())
9611be35a1SLionel Sambuc 
9711be35a1SLionel Sambuc     {
9811be35a1SLionel Sambuc         RESET;
9911be35a1SLionel Sambuc 
10011be35a1SLionel Sambuc         atf::tests::detail::atf_tp_writer w(ss);
10111be35a1SLionel Sambuc         expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
10211be35a1SLionel Sambuc         CHECK;
10311be35a1SLionel Sambuc     }
10411be35a1SLionel Sambuc 
10511be35a1SLionel Sambuc     {
10611be35a1SLionel Sambuc         RESET;
10711be35a1SLionel Sambuc 
10811be35a1SLionel Sambuc         atf::tests::detail::atf_tp_writer w(ss);
10911be35a1SLionel Sambuc         expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
11011be35a1SLionel Sambuc         CHECK;
11111be35a1SLionel Sambuc 
11211be35a1SLionel Sambuc         w.start_tc("test1");
11311be35a1SLionel Sambuc         expss << "ident: test1\n";
11411be35a1SLionel Sambuc         CHECK;
11511be35a1SLionel Sambuc 
11611be35a1SLionel Sambuc         w.end_tc();
11711be35a1SLionel Sambuc         CHECK;
11811be35a1SLionel Sambuc     }
11911be35a1SLionel Sambuc 
12011be35a1SLionel Sambuc     {
12111be35a1SLionel Sambuc         RESET;
12211be35a1SLionel Sambuc 
12311be35a1SLionel Sambuc         atf::tests::detail::atf_tp_writer w(ss);
12411be35a1SLionel Sambuc         expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
12511be35a1SLionel Sambuc         CHECK;
12611be35a1SLionel Sambuc 
12711be35a1SLionel Sambuc         w.start_tc("test1");
12811be35a1SLionel Sambuc         expss << "ident: test1\n";
12911be35a1SLionel Sambuc         CHECK;
13011be35a1SLionel Sambuc 
13111be35a1SLionel Sambuc         w.end_tc();
13211be35a1SLionel Sambuc         CHECK;
13311be35a1SLionel Sambuc 
13411be35a1SLionel Sambuc         w.start_tc("test2");
13511be35a1SLionel Sambuc         expss << "\nident: test2\n";
13611be35a1SLionel Sambuc         CHECK;
13711be35a1SLionel Sambuc 
13811be35a1SLionel Sambuc         w.end_tc();
13911be35a1SLionel Sambuc         CHECK;
14011be35a1SLionel Sambuc     }
14111be35a1SLionel Sambuc 
14211be35a1SLionel Sambuc     {
14311be35a1SLionel Sambuc         RESET;
14411be35a1SLionel Sambuc 
14511be35a1SLionel Sambuc         atf::tests::detail::atf_tp_writer w(ss);
14611be35a1SLionel Sambuc         expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
14711be35a1SLionel Sambuc         CHECK;
14811be35a1SLionel Sambuc 
14911be35a1SLionel Sambuc         w.start_tc("test1");
15011be35a1SLionel Sambuc         expss << "ident: test1\n";
15111be35a1SLionel Sambuc         CHECK;
15211be35a1SLionel Sambuc 
15311be35a1SLionel Sambuc         w.tc_meta_data("descr", "the description");
15411be35a1SLionel Sambuc         expss << "descr: the description\n";
15511be35a1SLionel Sambuc         CHECK;
15611be35a1SLionel Sambuc 
15711be35a1SLionel Sambuc         w.end_tc();
15811be35a1SLionel Sambuc         CHECK;
15911be35a1SLionel Sambuc 
16011be35a1SLionel Sambuc         w.start_tc("test2");
16111be35a1SLionel Sambuc         expss << "\nident: test2\n";
16211be35a1SLionel Sambuc         CHECK;
16311be35a1SLionel Sambuc 
16411be35a1SLionel Sambuc         w.tc_meta_data("descr", "second test case");
16511be35a1SLionel Sambuc         expss << "descr: second test case\n";
16611be35a1SLionel Sambuc         CHECK;
16711be35a1SLionel Sambuc 
16811be35a1SLionel Sambuc         w.tc_meta_data("require.progs", "/bin/cp");
16911be35a1SLionel Sambuc         expss << "require.progs: /bin/cp\n";
17011be35a1SLionel Sambuc         CHECK;
17111be35a1SLionel Sambuc 
17211be35a1SLionel Sambuc         w.tc_meta_data("X-custom", "foo bar baz");
17311be35a1SLionel Sambuc         expss << "X-custom: foo bar baz\n";
17411be35a1SLionel Sambuc         CHECK;
17511be35a1SLionel Sambuc 
17611be35a1SLionel Sambuc         w.end_tc();
17711be35a1SLionel Sambuc         CHECK;
17811be35a1SLionel Sambuc     }
17911be35a1SLionel Sambuc 
18011be35a1SLionel Sambuc #undef CHECK
18111be35a1SLionel Sambuc #undef RESET
18211be35a1SLionel Sambuc }
18311be35a1SLionel Sambuc 
18411be35a1SLionel Sambuc // ------------------------------------------------------------------------
18511be35a1SLionel Sambuc // Tests cases for the header file.
18611be35a1SLionel Sambuc // ------------------------------------------------------------------------
18711be35a1SLionel Sambuc 
18811be35a1SLionel Sambuc HEADER_TC(include, "atf-c++/tests.hpp");
18911be35a1SLionel Sambuc 
19011be35a1SLionel Sambuc // ------------------------------------------------------------------------
19111be35a1SLionel Sambuc // Main.
19211be35a1SLionel Sambuc // ------------------------------------------------------------------------
19311be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)19411be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
19511be35a1SLionel Sambuc {
19611be35a1SLionel Sambuc     // Add tests for the "atf_tp_writer" class.
19711be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, atf_tp_writer);
19811be35a1SLionel Sambuc 
19911be35a1SLionel Sambuc     // Add the test cases for the header file.
20011be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, include);
20111be35a1SLionel Sambuc }
202