Home | History | Annotate | Line # | Download | only in tools
      1 //
      2 // Automated Testing Framework (atf)
      3 //
      4 // Copyright (c) 2009 The NetBSD Foundation, Inc.
      5 // All rights reserved.
      6 //
      7 // Redistribution and use in source and binary forms, with or without
      8 // modification, are permitted provided that the following conditions
      9 // are met:
     10 // 1. Redistributions of source code must retain the above copyright
     11 //    notice, this list of conditions and the following disclaimer.
     12 // 2. Redistributions in binary form must reproduce the above copyright
     13 //    notice, this list of conditions and the following disclaimer in the
     14 //    documentation and/or other materials provided with the distribution.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 //
     29 
     30 #if defined(TOOLS_TEST_HELPERS_H)
     31 #   error "Cannot include test_helpers.hpp more than once."
     32 #else
     33 #   define TOOLS_TEST_HELPERS_H
     34 #endif
     35 
     36 #include <iostream>
     37 #include <sstream>
     38 #include <utility>
     39 
     40 #include <atf-c++.hpp>
     41 
     42 #include "parser.hpp"
     43 #include "text.hpp"
     44 
     45 namespace test_helpers_detail {
     46 
     47 typedef std::vector< std::string > string_vector;
     48 
     49 template< class Reader >
     50 std::pair< string_vector, string_vector >
     51 do_read(const char* input)
     52 {
     53     string_vector errors;
     54 
     55     std::istringstream is(input);
     56     Reader reader(is);
     57     try {
     58         reader.read();
     59     } catch (const tools::parser::parse_errors& pes) {
     60         for (std::vector< tools::parser::parse_error >::const_iterator iter =
     61              pes.begin(); iter != pes.end(); iter++)
     62             errors.push_back(*iter);
     63     } catch (const tools::parser::parse_error& pe) {
     64         ATF_FAIL("Raised a lonely parse error: " +
     65                  tools::text::to_string(pe.first) + ": " + pe.second);
     66     }
     67 
     68     return std::make_pair(reader.m_calls, errors);
     69 }
     70 
     71 void
     72 check_equal(const char* expected[], const string_vector& actual)
     73 {
     74     const char** expected_iter = expected;
     75     string_vector::const_iterator actual_iter = actual.begin();
     76 
     77     bool equals = true;
     78     while (equals && *expected_iter != NULL && actual_iter != actual.end()) {
     79         if (*expected_iter != *actual_iter) {
     80             equals = false;
     81         } else {
     82             expected_iter++;
     83             actual_iter++;
     84         }
     85     }
     86     if (equals && ((*expected_iter == NULL && actual_iter != actual.end()) ||
     87                    (*expected_iter != NULL && actual_iter == actual.end())))
     88         equals = false;
     89 
     90     if (!equals) {
     91         std::cerr << "EXPECTED:\n";
     92         for (expected_iter = expected; *expected_iter != NULL; expected_iter++)
     93             std::cerr << *expected_iter << "\n";
     94 
     95         std::cerr << "ACTUAL:\n";
     96         for (actual_iter = actual.begin(); actual_iter != actual.end();
     97              actual_iter++)
     98             std::cerr << *actual_iter << "\n";
     99 
    100         ATF_FAIL("Expected results differ to actual values");
    101     }
    102 }
    103 
    104 } // namespace test_helpers_detail
    105 
    106 template< class Reader >
    107 void
    108 do_parser_test(const char* input, const char* exp_calls[],
    109                const char* exp_errors[])
    110 {
    111     const std::pair< test_helpers_detail::string_vector,
    112                      test_helpers_detail::string_vector >
    113         actual = test_helpers_detail::do_read< Reader >(input);
    114     test_helpers_detail::check_equal(exp_calls, actual.first);
    115     test_helpers_detail::check_equal(exp_errors, actual.second);
    116 }
    117