From 0015cdbd5a5d949e7134487f13305364deaf7ead Mon Sep 17 00:00:00 2001 From: "James P. Howard, II" Date: Thu, 4 Aug 2022 22:09:11 -0400 Subject: [PATCH] Start building a unit test framework --- conanfile.py | 1 + docs/installation.md | 14 +++-- test/CMakeLists.txt | 20 ++++--- test/unit-kami-agent.cc | 59 ------------------- test/unit-kami-agentid.cc | 55 ------------------ test/unit-kami-stagedagent.cc | 105 ++++++++++++++++++++++++++++++++++ 6 files changed, 127 insertions(+), 127 deletions(-) delete mode 100644 test/unit-kami-agent.cc delete mode 100644 test/unit-kami-agentid.cc create mode 100644 test/unit-kami-stagedagent.cc diff --git a/conanfile.py b/conanfile.py index c6e3dd4..5757cf0 100644 --- a/conanfile.py +++ b/conanfile.py @@ -48,3 +48,4 @@ class KamiConan(ConanFile): self.requires("spdlog/1.8.5") self.requires("cli11/1.9.1") self.requires("neargye-semver/0.3.0") + self.requires("gtest/cci.20210126") diff --git a/docs/installation.md b/docs/installation.md index 8f6171d..a41c3ee 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -6,15 +6,19 @@ The core of Kami, `libkami`, has no requirements beyond a modern C++ compiler an the unit tests provided rely on three additional C++ packages. The full list is: * cli11/1.9.1 +* gtest/cci.20210126 * neargye-semver/0.3.0" * spdlog/1.8.5 * fmt/7.1.3 -[`CLI11`](https://cliutils.github.io/CLI11/) provides a command line interface for each of the utilities that makeup the -examples and test suite. [`spdlog`](https://github.com/gabime/spdlog) -provides a uniform output interface. Coupled with a command line option to set the output level, `spdlog` allows the -unit tests and example programs to provide variable output levels depending on the users needs. -Finally, [`fmt`](https://fmt.dev/) is required by +[`Google Test`](https://github.com/google/googletest) provides a +unit testing framework. [`CLI11`](https://cliutils.github.io/CLI11/) +provides a command line interface for each of the utilities that +makeup the examples. [`spdlog`](https://github.com/gabime/spdlog) +provides a uniform output interface. Coupled with a command line +option to set the output level, `spdlog` allows the unit tests and +example programs to provide variable output levels depending on the +users needs. Finally, [`fmt`](https://fmt.dev/) is required by `spdlog` for simple and easy string formatting. ## Compiling diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9b2d910..924f5c2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,22 +2,26 @@ # This CMakeLists.txt contains the build descriptions for unit tests ################################################################################ -cmake_minimum_required(VERSION 3.13.0 FATAL_ERROR) - -find_package(spdlog) +create_test( + NAME unit-kami-agent + SOURCES unit-kami-agent.cc + PUBLIC_LINKED_TARGETS gmock gtest kami::libkami + COMMAND unit-kami-agent + PUBLIC_COMPILE_FEATURES ${COVERAGE_FLAGS} +) create_test( NAME unit-kami-agentid SOURCES unit-kami-agentid.cc - PUBLIC_LINKED_TARGETS fmt spdlog kami::libkami + PUBLIC_LINKED_TARGETS gmock gtest kami::libkami COMMAND unit-kami-agentid PUBLIC_COMPILE_FEATURES ${COVERAGE_FLAGS} ) create_test( - NAME unit-kami-agent - SOURCES unit-kami-agent.cc - PUBLIC_LINKED_TARGETS fmt spdlog kami::libkami - COMMAND unit-kami-agent + NAME unit-kami-stagedagent + SOURCES unit-kami-stagedagent.cc + PUBLIC_LINKED_TARGETS gmock gtest kami::libkami + COMMAND unit-kami-stagedagent PUBLIC_COMPILE_FEATURES ${COVERAGE_FLAGS} ) diff --git a/test/unit-kami-agent.cc b/test/unit-kami-agent.cc deleted file mode 100644 index ff5c8eb..0000000 --- a/test/unit-kami-agent.cc +++ /dev/null @@ -1,59 +0,0 @@ -/*- - * Copyright (c) 2020 The Johns Hopkins University Applied Physics - * Laboratory LLC - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation files - * (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, - * publish, distribute, sublicense, and/or sell copies of the Software, - * and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include - -#include -#include - -#include -#include - -#include -#include -#include - -using namespace kami; -using namespace std; - -class TestAgent : public Agent { - void step() override {}; -}; - -int main(int argc, char **argv) { - string ident = "unit-kami-agent"; - CLI::App app{ident}; - auto console = spdlog::stdout_color_st(ident); - string logLevelOption = "info"; - - app.add_option("-l", logLevelOption, "Set the logging level")->check(CLI::IsMember(SPDLOG_LEVEL_NAMES)); - CLI11_PARSE(app, argc, argv); - - console->set_level(spdlog::level::from_str(logLevelOption)); - console->info("Compiled with Kami/{}, log level {}", kami::version.to_string(), logLevelOption); - - TestAgent test_agent; - console->debug("Successfully created Agent with ID {}", test_agent.get_agent_id().to_string()); -} diff --git a/test/unit-kami-agentid.cc b/test/unit-kami-agentid.cc deleted file mode 100644 index 884ca71..0000000 --- a/test/unit-kami-agentid.cc +++ /dev/null @@ -1,55 +0,0 @@ -/*- - * Copyright (c) 2020 The Johns Hopkins University Applied Physics - * Laboratory LLC - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation files - * (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, - * publish, distribute, sublicense, and/or sell copies of the Software, - * and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include - -#include -#include - -#include -#include - -#include -#include -#include - -using namespace kami; -using namespace std; - -int main(int argc, char **argv) { - string ident = "unit-kami-agentid"; - CLI::App app{ident}; - auto console = spdlog::stdout_color_st(ident); - string logLevelOption = "info"; - - app.add_option("-l", logLevelOption, "Set the logging level")->check(CLI::IsMember(SPDLOG_LEVEL_NAMES)); - CLI11_PARSE(app, argc, argv); - - console->set_level(spdlog::level::from_str(logLevelOption)); - console->info("Compiled with Kami/{}, log level {}", kami::version.to_string(), logLevelOption); - - AgentID testAgentID; - console->debug("Successfully created AgentID with ID {}", testAgentID.to_string()); -} diff --git a/test/unit-kami-stagedagent.cc b/test/unit-kami-stagedagent.cc new file mode 100644 index 0000000..78ea84f --- /dev/null +++ b/test/unit-kami-stagedagent.cc @@ -0,0 +1,105 @@ +/*- + * Copyright (c) 2022 The Johns Hopkins University Applied Physics + * Laboratory LLC + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + +#include +#include + +#include + +class TestStagedAgent : public kami::StagedAgent { +public: + kami::AgentID advance(std::shared_ptr model) override { + return get_agent_id(); + } + + kami::AgentID step(std::shared_ptr model) override { + return get_agent_id(); + } +}; + +class TestModel : public kami::Model { +public: + void step() override {} + + void run(unsigned int) override {} +}; + +TEST(Agent, DefaultConstructor) { + const TestStagedAgent agent_foo; + const TestStagedAgent agent_bar; + + EXPECT_EQ(agent_foo, agent_foo); + EXPECT_NE(agent_foo, agent_bar); +} + +TEST(Agent, get_agent_id) { + const TestStagedAgent agent_foo; + const TestStagedAgent agent_bar; + + EXPECT_EQ(agent_foo.get_agent_id(), agent_foo.get_agent_id()); + EXPECT_NE(agent_bar.get_agent_id(), agent_foo.get_agent_id()); +} + +TEST(Agent, advance) { + TestStagedAgent agent_foo; + TestStagedAgent agent_bar; + auto model_world = std::make_shared(); + + EXPECT_EQ(agent_foo.get_agent_id(), agent_foo.advance(model_world)); + EXPECT_NE(agent_bar.get_agent_id(), agent_foo.advance(model_world)); +} + +TEST(Agent, step) { + TestStagedAgent agent_foo; + TestStagedAgent agent_bar; + auto model_world = std::make_shared(); + + EXPECT_EQ(agent_foo.get_agent_id(), agent_foo.step(model_world)); + EXPECT_NE(agent_bar.get_agent_id(), agent_foo.step(model_world)); +} + +TEST(Agent, Equality) { + const TestStagedAgent agent_foo; + const TestStagedAgent agent_bar; + + EXPECT_TRUE(agent_foo == agent_foo); + EXPECT_TRUE(agent_bar == agent_bar); +} + +TEST(Agent, Inequality) { + const TestStagedAgent agent_foo; + const TestStagedAgent agent_bar; + + EXPECT_TRUE(agent_foo != agent_bar); + EXPECT_FALSE(agent_bar != agent_bar); +} + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} +