aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2011-03-19 22:21:33 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2011-03-20 16:20:40 +0000
commitc9b51672c21efcc2e52e4a744a0e4d7ca5406dea (patch)
treeab9e34deb2b2b393f7d1b7625b28a9f63d46f575
parent762c75fadaeff8b333f74143646a6fd69c1460cf (diff)
downloadpaludis-c9b51672c21efcc2e52e4a744a0e4d7ca5406dea.tar.gz
paludis-c9b51672c21efcc2e52e4a744a0e4d7ca5406dea.tar.xz
Start using Google Test for unit tests
-rw-r--r--configure.ac28
-rw-r--r--paludis/util/Makefile.am.m415
-rw-r--r--paludis/util/deferred_construction_ptr_TEST.cc122
-rw-r--r--paludis/util/destringify_TEST.cc177
-rw-r--r--paludis/util/enum_iterator_TEST.cc41
-rw-r--r--paludis/util/files.m46
-rw-r--r--paludis/util/gtest_runner.cc27
7 files changed, 201 insertions, 215 deletions
diff --git a/configure.ac b/configure.ac
index 5f195a0..1b4ad2e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1580,6 +1580,34 @@ fi
dnl }}}
+dnl {{{ gtest
+
+AC_MSG_CHECKING([whether to run tests that require Google Test])
+AC_ARG_ENABLE([gtest],
+ AS_HELP_STRING([--enable-gtest], [Run tests that require Google Test]),
+ [HAVE_GTEST=$enableval
+ AC_MSG_RESULT([$enableval])],
+ [HAVE_GTEST=yes
+ AC_MSG_RESULT([yes])])
+AC_SUBST([HAVE_GTEST])
+AM_CONDITIONAL([HAVE_GTEST], test "x$HAVE_HTMLTIDY" = "xyes")
+
+if test "x$HAVE_GTEST" = "xyes" ; then
+ AC_CHECK_PROG(HAVE_GTEST_CONFIG, [gtest-config], [yes], [no])
+ if test "x$HAVE_GTEST_CONFIG" = xno ; then
+ AC_MSG_ERROR([gtest-config from Google Test not found, but --disable-gtest was not used])
+ fi
+
+ GTESTDEPS_CXXFLAGS=`gtest-config --cxxflags`
+ GTESTDEPS_LDFLAGS=`gtest-config --ldflags`
+ GTESTDEPS_LIBS=`gtest-config --libs`
+ AC_SUBST(GTESTDEPS_CXXFLAGS)
+ AC_SUBST(GTESTDEPS_LDFLAGS)
+ AC_SUBST(GTESTDEPS_LIBS)
+fi
+
+dnl }}}
+
dnl {{{ libxml2 check
NEED_XML=$need_xml_check
if test "x$need_xml_check" = "xyes" ; then
diff --git a/paludis/util/Makefile.am.m4 b/paludis/util/Makefile.am.m4
index f485ae5..990aea0 100644
--- a/paludis/util/Makefile.am.m4
+++ b/paludis/util/Makefile.am.m4
@@ -8,6 +8,7 @@ include $(top_srcdir)/misc/common-makefile.am
define(`filelist', `')dnl
define(`headerlist', `')dnl
define(`testlist', `')dnl
+define(`gtestlist', `')dnl
define(`testscriptlist', `')dnl
define(`selist', `')dnl
define(`secleanlist', `')dnl
@@ -20,6 +21,14 @@ $1_TEST_LDADD = \
libpaludisutil_@PALUDIS_PC_SLOT@.la
$1_TEST_CXXFLAGS = -I$(top_srcdir) $(AM_CXXFLAGS) @PALUDIS_CXXFLAGS_NO_DEBUGGING@
')dnl
+define(`addgtest', `define(`gtestlist', gtestlist `$1_TEST')dnl
+$1_TEST_SOURCES = $1_TEST.cc
+$1_TEST_LDADD = \
+ gtest_runner.o \
+ libpaludisutil_@PALUDIS_PC_SLOT@.la
+$1_TEST_LDFLAGS = @GTESTDEPS_LDFLAGS@ @GTESTDEPS_LIBS@
+$1_TEST_CXXFLAGS = -I$(top_srcdir) $(AM_CXXFLAGS) @PALUDIS_CXXFLAGS_NO_DEBUGGING@ @GTESTDEPS_CXXFLAGS@
+')dnl
define(`addtestscript', `define(`testscriptlist', testscriptlist `$1_TEST_setup.sh $1_TEST_cleanup.sh')')dnl
define(`addhh', `define(`filelist', filelist `$1.hh')define(`headerlist', headerlist `$1.hh')')dnl
define(`addfwd', `define(`filelist', filelist `$1-fwd.hh')define(`headerlist', headerlist `$1-fwd.hh')')dnl
@@ -44,6 +53,7 @@ ifelse(`$2', `cc', `addcc(`$1')', `')dnl
ifelse(`$2', `impl', `addimpl(`$1')', `')dnl
ifelse(`$2', `se', `addse(`$1')', `')dnl
ifelse(`$2', `test', `addtest(`$1')', `')dnl
+ifelse(`$2', `gtest', `addgtest(`$1')', `')dnl
ifelse(`$2', `testscript', `addtestscript(`$1')', `')')dnl
define(`add', `addthis(`$1',`$2')addthis(`$1',`$3')addthis(`$1',`$4')dnl
addthis(`$1',`$5')addthis(`$1',`$6')addthis(`$1',`$7')addthis(`$1',`$8')')dnl
@@ -62,6 +72,7 @@ DEFS=\
EXTRA_DIST = util.hh.m4 Makefile.am.m4 files.m4 selist secleanlist \
testscriptlist \
test_extras.cc \
+ gtest_runner.cc \
echo_functions.bash.in
SUBDIRS = .
@@ -69,7 +80,11 @@ libpaludisutil_@PALUDIS_PC_SLOT@_la_SOURCES = filelist
libpaludisutil_@PALUDIS_PC_SLOT@_la_LDFLAGS = -version-info @VERSION_LIB_CURRENT@:@VERSION_LIB_REVISION@:0 $(PTHREAD_LIBS) $(RT_LIBS)
libpaludisutil_@PALUDIS_PC_SLOT@_la_LIBADD = $(PTHREAD_LIBS) $(RT_LIBS)
+if HAVE_GTEST
+TESTS = testlist gtestlist
+else
TESTS = testlist
+endif
check_PROGRAMS = $(TESTS)
check_SCRIPTS = testscriptlist
diff --git a/paludis/util/deferred_construction_ptr_TEST.cc b/paludis/util/deferred_construction_ptr_TEST.cc
index 06ae1a8..0deca72 100644
--- a/paludis/util/deferred_construction_ptr_TEST.cc
+++ b/paludis/util/deferred_construction_ptr_TEST.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2008, 2010 Ciaran McCreesh
+ * Copyright (c) 2008, 2010, 2011 Ciaran McCreesh
*
* 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
@@ -19,12 +19,10 @@
#include <paludis/util/deferred_construction_ptr.hh>
#include <paludis/util/active_object_ptr.hh>
-#include <test/test_runner.hh>
-#include <test/test_framework.hh>
-#include <test/test_concepts.hh>
+
+#include <gtest/gtest.h>
using namespace paludis;
-using namespace test;
namespace
{
@@ -47,91 +45,51 @@ namespace
{
return std::make_shared<std::string>("chimp");
}
-}
-namespace test_cases
-{
- typedef DeferredConstructionPtr<int *> DeferredIntPtr;
- TESTCASE_SEMIREGULAR(DeferredIntPtr, DeferredIntPtr(make_ten));
-
- typedef DeferredConstructionPtr<std::shared_ptr<int> > DeferredSharedIntPtr;
- TESTCASE_SEMIREGULAR(DeferredSharedIntPtr, DeferredSharedIntPtr(make_ten_shared));
+ struct Flag
+ {
+ bool value;
+ Flag() : value(false) { }
+ };
- struct TestDereference : TestCase
+ static std::shared_ptr<Flag> make_flag()
{
- TestDereference() : TestCase("dereference") { }
+ return std::make_shared<Flag>();
+ }
+}
- void run()
- {
- DeferredConstructionPtr<std::string *> p(make_monkey);
- TEST_CHECK_EQUAL(p->length(), 6u);
+TEST(DeferredConstructionPtr, Dereference)
+{
+ DeferredConstructionPtr<std::string *> p(make_monkey);
+ EXPECT_EQ(6u, p->length());
- DeferredConstructionPtr<std::shared_ptr<std::string> > q(make_chimp_shared);
- TEST_CHECK_EQUAL(q->length(), 5u);
- }
- } test_dereference;
+ DeferredConstructionPtr<std::shared_ptr<std::string> > q(make_chimp_shared);
+ EXPECT_EQ(5u, q->length());
+}
- struct TestDeferredActive : TestCase
- {
- TestDeferredActive() : TestCase("dereferred active") { }
+TEST(DeferredConstructionPtr, Active)
+{
+ ActiveObjectPtr<DeferredConstructionPtr<std::string *> > p((
+ DeferredConstructionPtr<std::string *>(make_monkey)));
+ EXPECT_EQ(6u, p->length());
- void run()
- {
- ActiveObjectPtr<DeferredConstructionPtr<std::string *> > p((
- DeferredConstructionPtr<std::string *>(make_monkey)));
- TEST_CHECK_EQUAL(p->length(), 6u);
+ ActiveObjectPtr<DeferredConstructionPtr<std::shared_ptr<std::string> > > q((
+ DeferredConstructionPtr<std::shared_ptr<std::string> >(make_chimp_shared)));
+ EXPECT_EQ(5u, q->length());
+}
- ActiveObjectPtr<DeferredConstructionPtr<std::shared_ptr<std::string> > > q((
- DeferredConstructionPtr<std::shared_ptr<std::string> >(make_chimp_shared)));
- TEST_CHECK_EQUAL(q->length(), 5u);
- }
- } test_dereferred_active;
+TEST(DeferredConstructionPtr, ConstructOnceOnly)
+{
+ DeferredConstructionPtr<std::shared_ptr<Flag> > f(make_flag);
+ f->value = true;
+ EXPECT_TRUE(f->value);
+}
- struct TestConstructOnlyOnce : TestCase
- {
- TestConstructOnlyOnce() : TestCase("construct only once") { }
-
- struct Flag
- {
- bool value;
- Flag() : value(false) { }
- };
-
- static std::shared_ptr<Flag> make_flag()
- {
- return std::make_shared<Flag>();
- }
-
- void run()
- {
- DeferredConstructionPtr<std::shared_ptr<Flag> > f(make_flag);
- f->value = true;
- TEST_CHECK(f->value);
- }
- } test_construct_only_once;
-
- struct TestConstructOnlyOnceActive : TestCase
- {
- TestConstructOnlyOnceActive() : TestCase("construct only once active") { }
-
- struct Flag
- {
- bool value;
- Flag() : value(false) { }
- };
-
- static std::shared_ptr<Flag> make_flag()
- {
- return std::make_shared<Flag>();
- }
-
- void run()
- {
- ActiveObjectPtr<DeferredConstructionPtr<std::shared_ptr<Flag> > > f((
- DeferredConstructionPtr<std::shared_ptr<Flag> >(make_flag)));
- f->value = true;
- TEST_CHECK(f->value);
- }
- } test_construct_only_once_active;
+TEST(DeferredConstructionPtr, ConstructOnceOnlyActive)
+{
+ ActiveObjectPtr<DeferredConstructionPtr<std::shared_ptr<Flag> > > f((
+ DeferredConstructionPtr<std::shared_ptr<Flag> >(make_flag)));
+ f->value = true;
+ EXPECT_TRUE(f->value);
}
diff --git a/paludis/util/destringify_TEST.cc b/paludis/util/destringify_TEST.cc
index 0bb56cf..df870d5 100644
--- a/paludis/util/destringify_TEST.cc
+++ b/paludis/util/destringify_TEST.cc
@@ -2,6 +2,7 @@
/*
* Copyright (c) 2006 Stephen Bennett
+ * Copyright (c) 2011 Ciaran McCreesh
*
* 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
@@ -19,117 +20,83 @@
#include <paludis/util/destringify.hh>
#include <paludis/util/stringify.hh>
+
#include <string>
-#include <test/test_framework.hh>
-#include <test/test_runner.hh>
-#include <cmath>
-using namespace test;
+#include <gtest/gtest.h>
+
using namespace paludis;
-/** \file
- * Test cases for destringify.hh
- *
- */
+TEST(DestringifyInt, Works)
+{
+ EXPECT_EQ(0, destringify<int>("0"));
+ EXPECT_EQ(1, destringify<int>("1"));
+ EXPECT_EQ(99, destringify<int>("99"));
+ EXPECT_EQ(-99, destringify<int>("-99"));
+ EXPECT_EQ(12345, destringify<int>(" 12345"));
+}
+
+TEST(DestringifyInt, Throws)
+{
+ EXPECT_THROW(destringify<int>(""), DestringifyError);
+ EXPECT_THROW(destringify<int>("x"), DestringifyError);
+ EXPECT_THROW(destringify<int>("10000000000000000000000000000000000000000000000"), DestringifyError);
+}
+
+TEST(DestringifyFloat, Works)
+{
+ EXPECT_FLOAT_EQ(0.0f, destringify<float>("0"));
+ EXPECT_FLOAT_EQ(0.0f, destringify<float>("0.0"));
+ EXPECT_FLOAT_EQ(0.1f, destringify<float>("0.1"));
+ EXPECT_FLOAT_EQ(-1.54f, destringify<float>("-1.54"));
+}
+
+TEST(DestringifyFloat, Throws)
+{
+ EXPECT_THROW(destringify<float>("I am a fish"), DestringifyError);
+ EXPECT_THROW(destringify<float>(""), DestringifyError);
+}
-namespace test_cases
+TEST(DestringifyString, Works)
{
- /** \test
- * Test destringify for integers.
- *
- */
- struct DestringifyIntTests : TestCase
- {
- DestringifyIntTests() : TestCase("destringify int") { }
-
- void run()
- {
- TEST_CHECK_EQUAL(destringify<int>("0"), 0);
- TEST_CHECK_EQUAL(destringify<int>("1"), 1);
- TEST_CHECK_EQUAL(destringify<int>("99"), 99);
- TEST_CHECK_EQUAL(destringify<int>("-99"), -99);
- TEST_CHECK_EQUAL(destringify<int>(" 12345"), 12345);
- TEST_CHECK_THROWS(destringify<int>(""), DestringifyError);
- TEST_CHECK_THROWS(destringify<int>("x"), DestringifyError);
- TEST_CHECK_THROWS(destringify<int>("10000000000000000000000000000000000000000000000"), DestringifyError);
- }
- } test_case_destringify_int;
-
- /** \test
- * Test destringify for floats.
- *
- */
- struct DestringifyFloatTests : TestCase
- {
- DestringifyFloatTests() : TestCase("destringify float") { }
-
- void run()
- {
- TEST_CHECK(std::fabs(destringify<float>("0") - 0.0f) < 0.01f);
- TEST_CHECK(std::fabs(destringify<float>("0.0") - 0.0f) < 0.01f);
- TEST_CHECK(std::fabs(destringify<float>("0.1") - 0.1f) < 0.01f);
- TEST_CHECK(std::fabs(destringify<float>("-1.54") - -1.54f) < 0.01f);
- TEST_CHECK_THROWS(destringify<float>("I am a fish"), DestringifyError);
- TEST_CHECK_THROWS(destringify<float>(""), DestringifyError);
- }
- } test_case_destringify_float;
-
- /** \test
- * Test destringify for strings.
- *
- */
- struct DestringifyStringTests : TestCase
- {
- DestringifyStringTests() : TestCase("destringify string") { }
-
- void run()
- {
- TEST_CHECK_EQUAL(destringify<std::string>("asdf"), "asdf");
- TEST_CHECK_EQUAL(destringify<std::string>(" a f e b "), " a f e b ");
- TEST_CHECK_THROWS(destringify<std::string>(""), DestringifyError);
- }
- } test_case_destringify_string;
-
- /** \test
- * Test destringify for bool.
- *
- */
- struct DestringifyBoolTests : TestCase
- {
- DestringifyBoolTests() : TestCase("destringify bool") { }
-
- void run()
- {
- TEST_CHECK( destringify<bool>("true"));
- TEST_CHECK( destringify<bool>("1"));
- TEST_CHECK( destringify<bool>("5"));
- TEST_CHECK(!destringify<bool>("false"));
- TEST_CHECK(!destringify<bool>("0"));
- TEST_CHECK(!destringify<bool>("-1"));
- TEST_CHECK_THROWS(destringify<bool>("flase"), DestringifyError);
- TEST_CHECK_THROWS(destringify<bool>("432.2413"), DestringifyError);
- TEST_CHECK_THROWS(destringify<bool>(""), DestringifyError);
- }
- } test_case_destringify_bool;
-
- /** \test
- * Test destringify for char.
- *
- */
- struct DestringifyCharTests : TestCase
- {
- DestringifyCharTests() : TestCase("destringify char") { }
-
- void run()
- {
- TEST_CHECK_EQUAL(destringify<char>("x"), 'x');
- TEST_CHECK_EQUAL(destringify<char>("0"), '0');
- TEST_CHECK_THROWS(destringify<char>("aa"), DestringifyError);
- TEST_CHECK_THROWS(destringify<char>("a a"), DestringifyError);
- TEST_CHECK_THROWS(destringify<char>("11"), DestringifyError);
- TEST_CHECK_THROWS(destringify<char>(""), DestringifyError);
- }
- } test_case_destringify_char;
+ EXPECT_EQ("asdf", destringify<std::string>("asdf"));
+ EXPECT_EQ( " a f e b ", destringify<std::string>(" a f e b "));
+}
+
+TEST(DestringifyString, Throws)
+{
+ EXPECT_THROW(destringify<std::string>(""), DestringifyError);
+}
+
+TEST(DestringifyBool, Works)
+{
+ EXPECT_TRUE(destringify<bool>("true"));
+ EXPECT_TRUE(destringify<bool>("1"));
+ EXPECT_TRUE(destringify<bool>("5"));
+
+ EXPECT_FALSE(destringify<bool>("false"));
+ EXPECT_FALSE(destringify<bool>("0"));
+ EXPECT_FALSE(destringify<bool>("-1"));
+}
+TEST(DestringifyBool, Throws)
+{
+ EXPECT_THROW(destringify<bool>("flase"), DestringifyError);
+ EXPECT_THROW(destringify<bool>("432.2413"), DestringifyError);
+ EXPECT_THROW(destringify<bool>(""), DestringifyError);
+}
+
+TEST(DestringifyChar, Works)
+{
+ EXPECT_EQ('x', destringify<char>("x"));
+ EXPECT_EQ('0', destringify<char>("0"));
+}
+
+TEST(DestringifyChar, Throws)
+{
+ EXPECT_THROW(destringify<char>("aa"), DestringifyError);
+ EXPECT_THROW(destringify<char>("a a"), DestringifyError);
+ EXPECT_THROW(destringify<char>("11"), DestringifyError);
+ EXPECT_THROW(destringify<char>(""), DestringifyError);
}
diff --git a/paludis/util/enum_iterator_TEST.cc b/paludis/util/enum_iterator_TEST.cc
index 5cf4509..851edc9 100644
--- a/paludis/util/enum_iterator_TEST.cc
+++ b/paludis/util/enum_iterator_TEST.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2009 Ciaran McCreesh
+ * Copyright (c) 2009, 2011 Ciaran McCreesh
*
* 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
@@ -18,11 +18,10 @@
*/
#include <paludis/util/enum_iterator.hh>
-#include <test/test_framework.hh>
-#include <test/test_runner.hh>
+
+#include <gtest/gtest.h>
using namespace paludis;
-using namespace test;
namespace
{
@@ -35,31 +34,23 @@ namespace
};
}
-namespace test_cases
+TEST(EnumIterator, Works)
{
- struct EnumIteratorTest : TestCase
- {
- EnumIteratorTest() : TestCase("enum iterator") { }
-
- void run()
- {
- EnumIterator<Numbers> n, n_end(last_number);
+ EnumIterator<Numbers> n, n_end(last_number);
- TEST_CHECK(n != n_end);
- TEST_CHECK_EQUAL(*n, one);
- ++n;
+ EXPECT_TRUE(n != n_end);
+ EXPECT_EQ(one, *n);
+ ++n;
- TEST_CHECK(n != n_end);
- TEST_CHECK_EQUAL(*n, two);
- ++n;
+ EXPECT_TRUE(n != n_end);
+ EXPECT_EQ(two, *n);
+ ++n;
- TEST_CHECK(n != n_end);
- TEST_CHECK_EQUAL(*n, three);
- ++n;
+ EXPECT_TRUE(n != n_end);
+ EXPECT_EQ(three, *n);
+ ++n;
- TEST_CHECK(n == n_end);
- TEST_CHECK_EQUAL(*n, last_number);
- }
- } test_enum_iterator;
+ EXPECT_TRUE(n == n_end);
+ EXPECT_EQ(last_number, *n);
}
diff --git a/paludis/util/files.m4 b/paludis/util/files.m4
index 1a84de7..ca8ff77 100644
--- a/paludis/util/files.m4
+++ b/paludis/util/files.m4
@@ -22,8 +22,8 @@ add(`condition_variable', `hh', `cc', `test')
add(`cookie', `hh', `cc')
add(`create_iterator', `hh', `fwd', `impl', `test')
add(`damerau_levenshtein', `hh', `cc', `test')
-add(`destringify', `hh', `cc', `test')
-add(`deferred_construction_ptr', `hh', `cc', `fwd', `test')
+add(`destringify', `hh', `cc', `gtest')
+add(`deferred_construction_ptr', `hh', `cc', `fwd', `gtest')
add(`discard_output_stream', `hh', `cc')
add(`elf', `hh', `cc')
add(`elf_dynamic_section', `hh', `cc')
@@ -31,7 +31,7 @@ add(`elf_relocation_section', `hh', `cc')
add(`elf_sections', `hh', `cc')
add(`elf_symbol_section', `hh', `cc')
add(`elf_types', `hh')
-add(`enum_iterator', `hh', `cc', `fwd', `test')
+add(`enum_iterator', `hh', `cc', `fwd', `gtest')
add(`exception', `hh', `cc')
add(`executor', `hh', `cc', `fwd')
add(`extract_host_from_url', `hh', `cc', `fwd', `test')
diff --git a/paludis/util/gtest_runner.cc b/paludis/util/gtest_runner.cc
new file mode 100644
index 0000000..4af9554
--- /dev/null
+++ b/paludis/util/gtest_runner.cc
@@ -0,0 +1,27 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2011 Ciaran McCreesh
+ *
+ * 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 <gtest/gtest.h>
+
+int main(int argc, char * argv[])
+{
+ testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
+