aboutsummaryrefslogtreecommitdiff
path: root/0.4.0/test/test_framework.cc
diff options
context:
space:
mode:
Diffstat (limited to '0.4.0/test/test_framework.cc')
-rw-r--r--0.4.0/test/test_framework.cc263
1 files changed, 263 insertions, 0 deletions
diff --git a/0.4.0/test/test_framework.cc b/0.4.0/test/test_framework.cc
new file mode 100644
index 0000000..06d5733
--- /dev/null
+++ b/0.4.0/test/test_framework.cc
@@ -0,0 +1,263 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Ciaran McCreesh <ciaran.mccreesh@blueyonder.co.uk>
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "test_framework.hh"
+#include <algorithm>
+#include <iostream>
+#include <paludis/util/attributes.hh>
+#include <unistd.h>
+
+/** \file
+ * Implementation for test framework classes.
+ *
+ * \ingroup Test
+ */
+
+using namespace test;
+
+#ifndef DOXYGEN
+namespace
+{
+ std::string exception_to_debug_string(const std::exception & e)
+ {
+ return e.what() + std::string(" (no further information)");
+ }
+
+ class DebugStringHolder
+ {
+ private:
+ std::string (*_f) (const std::exception &);
+
+ DebugStringHolder() :
+ _f(&exception_to_debug_string)
+ {
+ }
+
+ public:
+ static DebugStringHolder * get_instance()
+ {
+ static DebugStringHolder _instance;
+ return &_instance;
+ }
+
+ void set(std::string (*f) (const std::exception &))
+ {
+ _f = f;
+ }
+
+ std::string (*get() const) (const std::exception &)
+ {
+ return _f;
+ }
+ };
+}
+
+void
+test::set_exception_to_debug_string(std::string (*f) (const std::exception &))
+{
+ DebugStringHolder::get_instance()->set(f);
+}
+
+std::list<std::string>
+TestMessageSuffix::_suffixes;
+
+std::string
+TestMessageSuffix::suffixes()
+{
+ std::string result;
+ std::list<std::string>::const_iterator i(_suffixes.begin()), end(_suffixes.end());
+ while (i != end)
+ {
+ result += *i++;
+ if (end != i)
+ result += ", ";
+ }
+ return result;
+}
+
+TestMessageSuffix::TestMessageSuffix(const std::string & s, bool write)
+{
+ _suffixes.push_back(s);
+ if (write)
+ std::cout << "[" << s << "]" << std::flush;
+}
+
+struct TestCase::Impl
+{
+ const std::string name;
+
+ Impl(const std::string & the_name) :
+ name(the_name)
+ {
+ }
+};
+
+TestCase::TestCase(const std::string & name) :
+ _impl(new Impl(name))
+{
+ TestCaseList::register_test_case(this);
+}
+
+TestCase::~TestCase()
+{
+}
+
+void
+TestCase::check(const char * const function, const char * const file,
+ const long line, bool was_ok, const std::string & message) const
+{
+ std::cout << "." << std::flush;
+ if (! was_ok)
+ throw TestFailedException(function, file, line, message);
+}
+
+void
+TestCase::call_run()
+{
+ try
+ {
+ run();
+ }
+ catch (TestFailedException)
+ {
+ throw;
+ }
+ catch (std::exception &e)
+ {
+ throw TestFailedException(__PRETTY_FUNCTION__, __FILE__, __LINE__,
+ "Test threw unexpected exception " + (DebugStringHolder::get_instance()->get())(e));
+ }
+ catch (...)
+ {
+ throw TestFailedException(__PRETTY_FUNCTION__, __FILE__, __LINE__,
+ "Test threw unexpected unknown exception");
+ }
+}
+
+std::string
+TestCase::name() const
+{
+ return _impl->name;
+}
+
+TestFailedException::TestFailedException(const char * const function, const char * const file,
+ const long line, const std::string & message) throw () :
+ _message(paludis::stringify(file) + ":" + paludis::stringify(line) + ": in " +
+ paludis::stringify(function) + ": " + message + (
+ TestMessageSuffix::suffixes().empty() ? std::string("") : " [context: " +
+ TestMessageSuffix::suffixes() + "]"))
+{
+}
+
+TestFailedException::~TestFailedException() throw ()
+{
+}
+
+TestCaseList::TestCaseList()
+{
+}
+
+TestCaseList::~TestCaseList()
+{
+}
+
+class RunTest
+{
+ private:
+ bool * const _had_a_failure;
+
+ public:
+ RunTest(bool * had_a_failure) :
+ _had_a_failure(had_a_failure)
+ {
+ }
+
+ void operator() (TestCase * test_case) const;
+};
+
+void
+RunTest::operator() (TestCase * test_case) const
+{
+ bool had_local_failure(false);
+
+ std::cout << "* \"" << test_case->name() << "\": " << std::flush;
+
+ for (int repeat = 0 ; repeat < 2 ; ++repeat)
+ {
+ if (test_case->skip())
+ {
+ std::cout << "(skip)" << std::endl;
+ break;
+ }
+
+ if (0 != repeat)
+ std::cout << " (repeat): " << std::flush;
+
+ try
+ {
+ if (TestCaseList::use_alarm)
+ alarm(test_case->max_run_time());
+ test_case->call_run();
+ if (TestCaseList::use_alarm)
+ alarm(0);
+ }
+ catch (std::exception &e)
+ {
+ std::cout << "!{" << std::endl << (DebugStringHolder::get_instance()->get())(e) <<
+ std::endl << " } " << std::flush;
+ had_local_failure = true;
+ *_had_a_failure = true;
+ }
+ catch (...)
+ {
+ std::cout << "!{Unknown exception type} ";
+ had_local_failure = true;
+ *_had_a_failure = true;
+ }
+
+ if (had_local_failure)
+ std::cout << " NOT OK";
+ else
+ std::cout << " OK";
+
+ std::cout << std::endl;
+
+ if (! test_case->repeatable())
+ break;
+ }
+}
+
+bool
+TestCaseList::run_tests()
+{
+ bool had_a_failure(_get_test_case_list().empty());
+
+ std::for_each(
+ TestCaseList::_get_test_case_list().begin(),
+ TestCaseList::_get_test_case_list().end(),
+ RunTest(&had_a_failure));
+
+ return ! had_a_failure;
+}
+
+bool
+TestCaseList::use_alarm(true);
+
+#endif
+