Path: blob/main/contrib/atf/atf-c++/tests_test.cpp
39507 views
// Copyright (c) 2007 The NetBSD Foundation, Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions5// are met:6// 1. Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// 2. Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11//12// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425#include "atf-c++/tests.hpp"2627extern "C" {28#include <sys/types.h>29#include <sys/stat.h>3031#include <fcntl.h>32#include <unistd.h>33}3435#include <fstream>36#include <iostream>37#include <sstream>3839#include <atf-c++.hpp>4041#include "atf-c++/detail/text.hpp"4243// ------------------------------------------------------------------------44// Tests for the "atf_tp_writer" class.45// ------------------------------------------------------------------------4647static48void49print_indented(const std::string& str)50{51std::vector< std::string > ws = atf::text::split(str, "\n");52for (std::vector< std::string >::const_iterator iter = ws.begin();53iter != ws.end(); iter++)54std::cout << ">>" << *iter << "<<\n";55}5657// XXX Should this string handling and verbosity level be part of the58// ATF_REQUIRE_EQ macro? It may be hard to predict sometimes that a59// string can have newlines in it, and so the error message generated60// at the moment will be bogus if there are some.61static62void63check_equal(const atf::tests::tc& tc, const std::string& str,64const std::string& exp)65{66if (str != exp) {67std::cout << "String equality check failed.\n"68"Adding >> and << to delimit the string boundaries below.\n";69std::cout << "GOT:\n";70print_indented(str);71std::cout << "EXPECTED:\n";72print_indented(exp);73tc.fail("Constructed string differs from the expected one");74}75}7677ATF_TEST_CASE(atf_tp_writer);78ATF_TEST_CASE_HEAD(atf_tp_writer)79{80set_md_var("descr", "Verifies the application/X-atf-tp writer");81}82ATF_TEST_CASE_BODY(atf_tp_writer)83{84std::ostringstream expss;85std::ostringstream ss;8687#define RESET \88expss.str(""); \89ss.str("")9091#define CHECK \92check_equal(*this, ss.str(), expss.str())9394{95RESET;9697atf::tests::detail::atf_tp_writer w(ss);98expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";99CHECK;100}101102{103RESET;104105atf::tests::detail::atf_tp_writer w(ss);106expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";107CHECK;108109w.start_tc("test1");110expss << "ident: test1\n";111CHECK;112113w.end_tc();114CHECK;115}116117{118RESET;119120atf::tests::detail::atf_tp_writer w(ss);121expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";122CHECK;123124w.start_tc("test1");125expss << "ident: test1\n";126CHECK;127128w.end_tc();129CHECK;130131w.start_tc("test2");132expss << "\nident: test2\n";133CHECK;134135w.end_tc();136CHECK;137}138139{140RESET;141142atf::tests::detail::atf_tp_writer w(ss);143expss << "Content-Type: application/X-atf-tp; version=\"1\"\n\n";144CHECK;145146w.start_tc("test1");147expss << "ident: test1\n";148CHECK;149150w.tc_meta_data("descr", "the description");151expss << "descr: the description\n";152CHECK;153154w.end_tc();155CHECK;156157w.start_tc("test2");158expss << "\nident: test2\n";159CHECK;160161w.tc_meta_data("descr", "second test case");162expss << "descr: second test case\n";163CHECK;164165w.tc_meta_data("require.progs", "/bin/cp");166expss << "require.progs: /bin/cp\n";167CHECK;168169w.tc_meta_data("X-custom", "foo bar baz");170expss << "X-custom: foo bar baz\n";171CHECK;172173w.end_tc();174CHECK;175}176177#undef CHECK178#undef RESET179}180181// ------------------------------------------------------------------------182// Main.183// ------------------------------------------------------------------------184185ATF_INIT_TEST_CASES(tcs)186{187// Add tests for the "atf_tp_writer" class.188ATF_ADD_TEST_CASE(tcs, atf_tp_writer);189}190191192