tests_test.cpp revision a747113422afaa29ce72d2c5ba7f0b7ea9ec2054
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt//
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// Automated Testing Framework (atf)
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt//
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// Copyright (c) 2007 The NetBSD Foundation, Inc.
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// All rights reserved.
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt//
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// Redistribution and use in source and binary forms, with or without
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// modification, are permitted provided that the following conditions
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// are met:
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// 1. Redistributions of source code must retain the above copyright
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// notice, this list of conditions and the following disclaimer.
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// 2. Redistributions in binary form must reproduce the above copyright
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// notice, this list of conditions and the following disclaimer in the
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// documentation and/or other materials provided with the distribution.
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt//
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt//
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Huntextern "C" {
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt#include <sys/types.h>
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt#include <sys/stat.h>
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt#include <fcntl.h>
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt#include <unistd.h>
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt}
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt#include <fstream>
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt#include <sstream>
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt#include "macros.hpp"
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt#include "detail/parser.hpp"
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt#include "detail/test_helpers.hpp"
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// ------------------------------------------------------------------------
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// Tests for the "atf_tp_writer" class.
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// ------------------------------------------------------------------------
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Huntstatic
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Huntvoid
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Huntprint_indented(const std::string& str)
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt{
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt std::vector< std::string > ws = atf::text::split(str, "\n");
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt for (std::vector< std::string >::const_iterator iter = ws.begin();
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt iter != ws.end(); iter++)
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt std::cout << ">>" << *iter << "<<\n";
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt}
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// XXX Should this string handling and verbosity level be part of the
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// ATF_REQUIRE_EQ macro? It may be hard to predict sometimes that a
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// string can have newlines in it, and so the error message generated
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt// at the moment will be bogus if there are some.
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Huntstatic
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Huntvoid
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Huntcheck_equal(const atf::tests::tc& tc, const std::string& str,
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt const std::string& exp)
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt{
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt if (str != exp) {
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt std::cout << "String equality check failed.\n"
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt "Adding >> and << to delimit the string boundaries below.\n";
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt std::cout << "GOT:\n";
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt print_indented(str);
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt std::cout << "EXPECTED:\n";
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt print_indented(exp);
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt tc.fail("Constructed string differs from the expected one");
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt }
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt}
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan HuntATF_TEST_CASE(atf_tp_writer);
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan HuntATF_TEST_CASE_HEAD(atf_tp_writer)
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt{
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt set_md_var("descr", "Verifies the application/X-atf-tp writer");
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt}
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan HuntATF_TEST_CASE_BODY(atf_tp_writer)
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt{
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt std::ostringstream expss;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt std::ostringstream ss;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt#define RESET \
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt expss.str(""); \
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt ss.str("")
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt#define CHECK \
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt check_equal(*this, ss.str(), expss.str())
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt {
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt RESET;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt atf::tests::detail::atf_tp_writer w(ss);
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt CHECK;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt }
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt {
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt RESET;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt atf::tests::detail::atf_tp_writer w(ss);
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt CHECK;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt w.start_tc("test1");
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt expss << "ident: test1\n";
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt CHECK;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt w.end_tc();
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt CHECK;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt }
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt {
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt RESET;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt atf::tests::detail::atf_tp_writer w(ss);
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt CHECK;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt w.start_tc("test1");
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt expss << "ident: test1\n";
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt CHECK;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt w.end_tc();
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt CHECK;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt w.start_tc("test2");
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt expss << "\nident: test2\n";
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt CHECK;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt w.end_tc();
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt CHECK;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt }
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt {
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt RESET;
e59937c7283216ca22ce6e7937b06eab6d97f4acEvan Hunt
atf::tests::detail::atf_tp_writer w(ss);
expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";
CHECK;
w.start_tc("test1");
expss << "ident: test1\n";
CHECK;
w.tc_meta_data("descr", "the description");
expss << "descr: the description\n";
CHECK;
w.end_tc();
CHECK;
w.start_tc("test2");
expss << "\nident: test2\n";
CHECK;
w.tc_meta_data("descr", "second test case");
expss << "descr: second test case\n";
CHECK;
w.tc_meta_data("require.progs", "/bin/cp");
expss << "require.progs: /bin/cp\n";
CHECK;
w.tc_meta_data("X-custom", "foo bar baz");
expss << "X-custom: foo bar baz\n";
CHECK;
w.end_tc();
CHECK;
}
#undef CHECK
#undef RESET
}
// ------------------------------------------------------------------------
// Tests cases for the header file.
// ------------------------------------------------------------------------
HEADER_TC(include, "atf-c++/tests.hpp");
// ------------------------------------------------------------------------
// Main.
// ------------------------------------------------------------------------
ATF_INIT_TEST_CASES(tcs)
{
// Add tests for the "atf_tp_writer" class.
ATF_ADD_TEST_CASE(tcs, atf_tp_writer);
// Add the test cases for the header file.
ATF_ADD_TEST_CASE(tcs, include);
}