From a6f505fc72596e0ceac971397cc685bfda0b9d24 Mon Sep 17 00:00:00 2001 From: Trond Norbye Date: Thu, 6 Sep 2012 13:26:01 +0200 Subject: [PATCH] Move getopt test to gtest Change-Id: I7a34ab2906568d6e7951f2024a6b4b83b3b1c485 Reviewed-on: http://review.couchbase.org/20575 Reviewed-by: Sergey Avseyev Tested-by: Trond Norbye --- Makefile.am | 10 +- NMakefile | 11 +- .../{getopt-test.cc => getopt-unit-tests.cc} | 288 ++++++------------ 3 files changed, 95 insertions(+), 214 deletions(-) rename tests/{getopt-test.cc => getopt-unit-tests.cc} (59%) diff --git a/Makefile.am b/Makefile.am index e39f88dd0..5c808a656 100644 --- a/Makefile.am +++ b/Makefile.am @@ -243,19 +243,13 @@ noinst_LTLIBRARIES += libmockserver.la tests_unit_tests_SOURCES += tests/loopfactory.c tests/server.h \ tests/mock-unit-test.h tests/testutil.h \ tests/testutil.cc tests/http-tests.cc \ - tests/mock-unit-test.cc tests/server.c + tests/mock-unit-test.cc tests/server.c \ + tests/getopt-unit-tests.cc endif check_PROGRAMS += \ tests/config-test \ tests/smoke-test -if BUILD_TOOLS -# and therefore have C++ compiler to build this test -check_PROGRAMS += tests/getopt-test -endif - -tests_getopt_test_SOURCES = tests/getopt-test.cc - tests_config_test_SOURCES = tests/test.h tests/config-test.c tests_config_test_DEPENDENCIES = libcouchbase.la libmockserver.la libvbucket.la tests_config_test_LDADD = $(tests_config_test_DEPENDENCIES) diff --git a/NMakefile b/NMakefile index f71194102..3f831a25e 100644 --- a/NMakefile +++ b/NMakefile @@ -24,7 +24,7 @@ LD = link.exe RM = del lib_LTLIBRARIES = libcouchbase.dll libcouchbase_debug.dll -check_PROGRAMS = tests\getopt-test.exe +check_PROGRAMS = bin_PROGRAMS = tools\cbc.exe example_PROGRAMS = example\pillowfight.exe @@ -46,11 +46,6 @@ libcouchbase_SOURCES = src\arithmetic.c src\base64.c src\behavior.c \ libcouchbase_la_OBJECTS=$(libcouchbase_SOURCES:.c=.obj) libcouchbase_la_debug_OBJECTS=$(libcouchbase_SOURCES:.c=.dobj) -tests_getopt_test_CXX_SOURCES = tests\getopt-test.cc -tests_getopt_test_C_SOURCES = win32\getopt.c -tests_getopt_test_OBJECTS = $(tests_getopt_test_C_SOURCES:.c=.dobj) \ - $(tests_getopt_test_CXX_SOURCES:.cc=.dobj) - tools_cbc_CXX_SOURCES = tools\cbc.cc \ tools\cbc-util.cc tools\commandlineparser.cc tools\configuration.cc tools_cbc_C_SOURCES = win32\getopt.c @@ -63,7 +58,6 @@ example_pillowfight_OBJECTS = $(example_pillowfight_CXX_SOURCES:.cc=.obj) \ $(example_pillowfight_C_SOURCES:.c=.obj) all_OBJS=$(libcouchbase_la_OBJECTS) $(libcouchbase_la_debug_OBJECTS) \ - $(tests_getopt_test_OBJECTS) \ $(tools_cbc_OBJECTS) $(example_pillowfight_OBJECTS) all: $(lib_LTLIBRARIES) \ @@ -86,9 +80,6 @@ libcouchbase_debug.dll: $(libcouchbase_la_debug_OBJECTS) $(libcouchbase_la_debug_OBJECTS) \ ws2_32.lib -tests\getopt-test.exe: $(tests_getopt_test_OBJECTS) - $(LD) $(LDFLAGS) -out:tests\getopt-test.exe $(tests_getopt_test_OBJECTS) - tools\cbc.exe: $(tools_cbc_OBJECTS) libcouchbase.dll $(LD) $(LDFLAGS) -out:tools\cbc.exe $(tools_cbc_OBJECTS) \ libcouchbase.lib ws2_32.lib diff --git a/tests/getopt-test.cc b/tests/getopt-unit-tests.cc similarity index 59% rename from tests/getopt-test.cc rename to tests/getopt-unit-tests.cc index 5708c6d86..50c98d197 100644 --- a/tests/getopt-test.cc +++ b/tests/getopt-unit-tests.cc @@ -1,6 +1,6 @@ -/* -*- Mode: CPP; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ +/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* - * Copyright 2011 Couchbase, Inc. + * Copyright 2012 Couchbase, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,42 +14,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/** - * We don't have a real getopt_long implementation on Windows, so I've - * added a scaled down version in the win32 directory. This small test - * program tries to verify that it at least got some basic functionality - * working. - * - * @author Trond Norbye - */ #include "config.h" +#include +#include #include #include #include #include -int error = 0; - -#define fail(message) \ - do { \ - std::cerr << "Failed: " << __FILE__ << ":" \ - << __LINE__ << ": " << #message \ - << std::endl; \ - return 1; \ - } while (0) - -#define verify(expression) \ - do { \ - if (!(expression)) { \ - fail(expression); \ - } \ - } while (0) - -#define assertTrue(a) verify(a) -#define assertFalse(a) verify(!a) -#define assertEquals(a, b) verify(a == b) - class CommandLineOption { public: @@ -153,70 +125,65 @@ class Getopt std::vector options; }; + +class GetoptUnitTests : public ::testing::Test +{ +protected: +}; + static void setup(Getopt &getopt) { getopt.addOption(new CommandLineOption('a', "alpha", true)). - addOption(new CommandLineOption('b', "bravo", false)). - addOption(new CommandLineOption('c', "charlie", false)); + addOption(new CommandLineOption('b', "bravo", false)). + addOption(new CommandLineOption('c', "charlie", false)); } -static int testParseEmptyNoOptions(void) + +// Verify that we allow no options and that the option array is empty +TEST_F(GetoptUnitTests, testParseEmptyNoOptions) { std::vector argv; Getopt getopt; - if (!getopt.parse(argv)) { - fail("Parse should allow no arguments"); - } - - // validate that none of the options is set - std::vector::const_iterator iter; - for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { - assertFalse((*iter)->found); - } - - return 0; + ASSERT_TRUE(getopt.parse(argv)); + ASSERT_EQ(0, getopt.options.size()); + ASSERT_EQ(getopt.options.end(), getopt.options.begin()); } -static int testParseEmpty(void) +// Verify that we allow no options and that the option array is empty +TEST_F(GetoptUnitTests, testParseEmpty) { std::vector argv; Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("Parse should allow no arguments"); - } + ASSERT_TRUE(getopt.parse(argv)); + EXPECT_NE(getopt.options.end(), getopt.options.begin()); // validate that none of the options is set std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { - assertFalse((*iter)->found); + EXPECT_FALSE((*iter)->found); } - return 0; } -static int testParseOnlyArguments(void) +TEST_F(GetoptUnitTests, testParseOnlyArguments) { std::vector argv; argv.push_back("foo"); argv.push_back("bar"); Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("Parse should allow no arguments"); - } + ASSERT_TRUE(getopt.parse(argv)); // validate that none of the options is set std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { - assertFalse((*iter)->found); + ASSERT_FALSE((*iter)->found); } - assertEquals(1, optind); - - return 0; + ASSERT_EQ(1, optind); } -static int testParseOnlyArgumentsWithSeparatorInThere() +TEST_F(GetoptUnitTests, testParseOnlyArgumentsWithSeparatorInThere) { std::vector argv; argv.push_back("foo"); @@ -224,101 +191,85 @@ static int testParseOnlyArgumentsWithSeparatorInThere() argv.push_back("bar"); Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("Parse should allow no arguments"); - } + ASSERT_TRUE(getopt.parse(argv)); // validate that none of the options is set std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { - assertFalse((*iter)->found); + EXPECT_FALSE((*iter)->found); } - - return 0; } -static int testParseSingleLongoptWithoutArgument() +TEST_F(GetoptUnitTests,testParseSingleLongoptWithoutArgument) { std::vector argv; argv.push_back("--bravo"); Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("parse should succeed"); - } + ASSERT_TRUE(getopt.parse(argv)); // validate that --bravo is set std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { if ((*iter)->shortopt == 'b') { - assertTrue((*iter)->found); + EXPECT_TRUE((*iter)->found); } else { - assertFalse((*iter)->found); + EXPECT_FALSE((*iter)->found); } } - return 0; } -static int testParseSingleLongoptWithoutRequiredArgument() +TEST_F(GetoptUnitTests,testParseSingleLongoptWithoutRequiredArgument) { std::vector argv; argv.push_back("--alpha"); Getopt getopt; setup(getopt); - if (getopt.parse(argv)) { - fail("parse should fail"); - } - return 0; + ASSERT_FALSE(getopt.parse(argv)); } -static int testParseSingleLongoptWithRequiredArgument() +TEST_F(GetoptUnitTests,testParseSingleLongoptWithRequiredArgument) { std::vector argv; argv.push_back("--alpha=foo"); Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("parse should succeed"); - } + ASSERT_TRUE(getopt.parse(argv)); // validate that --alpha is set std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { if ((*iter)->shortopt == 'a') { - assertTrue((*iter)->found); - verify(strcmp((*iter)->argument, "foo") == 0); + EXPECT_TRUE((*iter)->found); + EXPECT_STREQ("foo", (*iter)->argument); } else { - assertFalse((*iter)->found); + EXPECT_FALSE((*iter)->found); } } - return 0; } -static int testParseSingleLongoptWithRequiredArgument1() +TEST_F(GetoptUnitTests,testParseSingleLongoptWithRequiredArgument1) { std::vector argv; argv.push_back("--alpha"); argv.push_back("foo"); Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("parse should succeed"); - } + ASSERT_TRUE(getopt.parse(argv)); // validate that --alpha is set std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { if ((*iter)->shortopt == 'a') { - assertTrue((*iter)->found); - verify(strcmp((*iter)->argument, "foo") == 0); + EXPECT_TRUE((*iter)->found); + EXPECT_STREQ("foo", (*iter)->argument); } else { - assertFalse((*iter)->found); + EXPECT_FALSE((*iter)->found); } } - return 0; } -static int testParseMulipleLongoptWithArgumentsAndOptions() +TEST_F(GetoptUnitTests, testParseMulipleLongoptWithArgumentsAndOptions) { std::vector argv; argv.push_back("--alpha=foo"); @@ -327,24 +278,21 @@ static int testParseMulipleLongoptWithArgumentsAndOptions() argv.push_back("foo"); Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("parse should succeed"); - } + ASSERT_TRUE(getopt.parse(argv)); // validate that --alpha, bravo and charlie is set std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { - assertTrue((*iter)->found); + EXPECT_TRUE((*iter)->found); if ((*iter)->shortopt == 'a') { - verify(strcmp((*iter)->argument, "foo") == 0); + ASSERT_STREQ("foo", (*iter)->argument); } } - assertEquals(4, optind); - return 0; + ASSERT_EQ(4, optind); } -static int testParseMulipleLongoptWithArgumentsAndOptionsAndSeparator() +TEST_F(GetoptUnitTests, testParseMulipleLongoptWithArgumentsAndOptionsAndSeparator) { std::vector argv; @@ -355,24 +303,21 @@ static int testParseMulipleLongoptWithArgumentsAndOptionsAndSeparator() argv.push_back("foo"); Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("parse should succeed"); - } + ASSERT_TRUE(getopt.parse(argv)); std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { if ((*iter)->shortopt == 'a') { - assertTrue((*iter)->found); - verify(strcmp((*iter)->argument, "foo") == 0); + EXPECT_TRUE((*iter)->found); + EXPECT_STREQ("foo", (*iter)->argument); } else { - assertFalse((*iter)->found); + EXPECT_FALSE((*iter)->found); } } - assertEquals(3, optind); - return 0; + ASSERT_EQ(3, optind); } -static int testParseMulipleLongoptWithArgumentsAndOptionsAndSeparator1() +TEST_F(GetoptUnitTests, testParseMulipleLongoptWithArgumentsAndOptionsAndSeparator1) { std::vector argv; @@ -384,88 +329,76 @@ static int testParseMulipleLongoptWithArgumentsAndOptionsAndSeparator1() argv.push_back("foo"); Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("parse should succeed"); - } + ASSERT_TRUE(getopt.parse(argv)); std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { if ((*iter)->shortopt == 'a') { - assertTrue((*iter)->found); - verify(strcmp((*iter)->argument, "foo") == 0); + EXPECT_TRUE((*iter)->found); + EXPECT_STREQ("foo", (*iter)->argument); } else { - assertFalse((*iter)->found); + EXPECT_FALSE((*iter)->found); } } - assertEquals(4, optind); - return 0; + ASSERT_EQ(4, optind); } -static int testParseSingleShortoptWithoutArgument() +TEST_F(GetoptUnitTests, testParseSingleShortoptWithoutArgument) { std::vector argv; argv.push_back("-b"); Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("parse should succeed"); - } + ASSERT_TRUE(getopt.parse(argv)); // validate that -b is set std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { if ((*iter)->shortopt == 'b') { - assertTrue((*iter)->found); + EXPECT_TRUE((*iter)->found); } else { - assertFalse((*iter)->found); + EXPECT_FALSE((*iter)->found); } } - return 0; } -static int testParseSingleShortoptWithoutRequiredArgument() +TEST_F(GetoptUnitTests, testParseSingleShortoptWithoutRequiredArgument) { std::vector argv; argv.push_back("-a"); Getopt getopt; setup(getopt); - if (getopt.parse(argv)) { - fail("parse should fail with a missing argument"); - } + ASSERT_FALSE(getopt.parse(argv)); // validate that none is set std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { - assertFalse((*iter)->found); + EXPECT_FALSE((*iter)->found); } - return 0; } -static int testParseSingleShortoptWithRequiredArgument() +TEST_F(GetoptUnitTests, testParseSingleShortoptWithRequiredArgument) { std::vector argv; argv.push_back("-a"); argv.push_back("foo"); Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("parse should succeed"); - } + ASSERT_TRUE(getopt.parse(argv)); // validate that none is set std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { if ((*iter)->shortopt == 'a') { - assertTrue((*iter)->found); - verify(strcmp((*iter)->argument, "foo") == 0); + EXPECT_TRUE((*iter)->found); + EXPECT_STREQ("foo", (*iter)->argument); } else { - assertFalse((*iter)->found); + EXPECT_FALSE((*iter)->found); } } - return 0; } -static int testParseMulipleShortoptWithArgumentsAndOptions() +TEST_F(GetoptUnitTests, testParseMulipleShortoptWithArgumentsAndOptions) { std::vector argv; argv.push_back("-a"); @@ -476,22 +409,19 @@ static int testParseMulipleShortoptWithArgumentsAndOptions() Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("parse should succeed"); - } + ASSERT_TRUE(getopt.parse(argv)); std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { - assertTrue((*iter)->found); + EXPECT_TRUE((*iter)->found); if ((*iter)->shortopt == 'a') { - verify(strcmp((*iter)->argument, "foo") == 0); + EXPECT_STREQ("foo", (*iter)->argument); } } - assertEquals(5, optind); - return 0; + ASSERT_EQ(5, optind); } -static int testParseMulipleShortoptWithArgumentsAndOptionsAndSeparator() +TEST_F(GetoptUnitTests, testParseMulipleShortoptWithArgumentsAndOptionsAndSeparator) { std::vector argv; argv.push_back("-a"); @@ -503,24 +433,21 @@ static int testParseMulipleShortoptWithArgumentsAndOptionsAndSeparator() Getopt getopt; setup(getopt); - if (!getopt.parse(argv)) { - fail("parse should succeed"); - } + ASSERT_TRUE(getopt.parse(argv)); std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { if ((*iter)->shortopt == 'a') { - assertTrue((*iter)->found); - verify(strcmp((*iter)->argument, "foo") == 0); + EXPECT_TRUE((*iter)->found); + EXPECT_STREQ("foo", (*iter)->argument); } else { - assertFalse((*iter)->found); + EXPECT_FALSE((*iter)->found); } } - assertEquals(4, optind); - return 0; + ASSERT_EQ(4, optind); } -static int testParseMix() +TEST_F(GetoptUnitTests, testParseMix) { std::vector argv; argv.push_back("-alpha"); @@ -536,49 +463,18 @@ static int testParseMix() setup(getopt); #ifdef _WIN32 - assertFalse(getopt.parse(argv)); + ASSERT_FALSE(getopt.parse(argv)); #else - if (!getopt.parse(argv)) { - fail("parse should succeed"); - } + ASSERT_TRUE(getopt.parse(argv)); std::vector::const_iterator iter; for (iter = getopt.options.begin(); iter != getopt.options.end(); ++iter) { - assertTrue((*iter)->found); + EXPECT_TRUE((*iter)->found); if ((*iter)->shortopt == 'a') { // the second -a overrides the first - verify(strcmp((*iter)->argument, "bar") == 0); + EXPECT_STREQ("bar", (*iter)->argument); } } - assertEquals(7, optind); + ASSERT_EQ(7, optind); #endif - return 0; -} - -int main() -{ - error += testParseEmptyNoOptions(); - error += testParseEmpty(); - error += testParseOnlyArguments(); - error += testParseOnlyArgumentsWithSeparatorInThere(); - error += testParseSingleLongoptWithoutArgument(); - error += testParseSingleLongoptWithoutRequiredArgument(); - error += testParseSingleLongoptWithRequiredArgument(); - error += testParseSingleLongoptWithRequiredArgument1(); - error += testParseMulipleLongoptWithArgumentsAndOptions(); - error += testParseMulipleLongoptWithArgumentsAndOptionsAndSeparator(); - error += testParseMulipleLongoptWithArgumentsAndOptionsAndSeparator1(); - error += testParseSingleShortoptWithoutArgument(); - error += testParseSingleShortoptWithoutRequiredArgument(); - error += testParseSingleShortoptWithRequiredArgument(); - error += testParseMulipleShortoptWithArgumentsAndOptions(); - error += testParseMulipleShortoptWithArgumentsAndOptionsAndSeparator(); - error += testParseMix(); - - if (error != 0) { - std::cerr << error << " tests failed" << std::endl; - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; }