Start building a unit test framework

This commit is contained in:
James P. Howard, II
2022-08-04 22:09:11 -04:00
parent 7a335feae2
commit 0015cdbd5a
6 changed files with 127 additions and 127 deletions

View File

@@ -48,3 +48,4 @@ class KamiConan(ConanFile):
self.requires("spdlog/1.8.5") self.requires("spdlog/1.8.5")
self.requires("cli11/1.9.1") self.requires("cli11/1.9.1")
self.requires("neargye-semver/0.3.0") self.requires("neargye-semver/0.3.0")
self.requires("gtest/cci.20210126")

View File

@@ -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: the unit tests provided rely on three additional C++ packages. The full list is:
* cli11/1.9.1 * cli11/1.9.1
* gtest/cci.20210126
* neargye-semver/0.3.0" * neargye-semver/0.3.0"
* spdlog/1.8.5 * spdlog/1.8.5
* fmt/7.1.3 * fmt/7.1.3
[`CLI11`](https://cliutils.github.io/CLI11/) provides a command line interface for each of the utilities that makeup the [`Google Test`](https://github.com/google/googletest) provides a
examples and test suite. [`spdlog`](https://github.com/gabime/spdlog) unit testing framework. [`CLI11`](https://cliutils.github.io/CLI11/)
provides a uniform output interface. Coupled with a command line option to set the output level, `spdlog` allows the provides a command line interface for each of the utilities that
unit tests and example programs to provide variable output levels depending on the users needs. makeup the examples. [`spdlog`](https://github.com/gabime/spdlog)
Finally, [`fmt`](https://fmt.dev/) is required by 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. `spdlog` for simple and easy string formatting.
## Compiling ## Compiling

View File

@@ -2,22 +2,26 @@
# This CMakeLists.txt contains the build descriptions for unit tests # This CMakeLists.txt contains the build descriptions for unit tests
################################################################################ ################################################################################
cmake_minimum_required(VERSION 3.13.0 FATAL_ERROR) create_test(
NAME unit-kami-agent
find_package(spdlog) SOURCES unit-kami-agent.cc
PUBLIC_LINKED_TARGETS gmock gtest kami::libkami
COMMAND unit-kami-agent
PUBLIC_COMPILE_FEATURES ${COVERAGE_FLAGS}
)
create_test( create_test(
NAME unit-kami-agentid NAME unit-kami-agentid
SOURCES unit-kami-agentid.cc SOURCES unit-kami-agentid.cc
PUBLIC_LINKED_TARGETS fmt spdlog kami::libkami PUBLIC_LINKED_TARGETS gmock gtest kami::libkami
COMMAND unit-kami-agentid COMMAND unit-kami-agentid
PUBLIC_COMPILE_FEATURES ${COVERAGE_FLAGS} PUBLIC_COMPILE_FEATURES ${COVERAGE_FLAGS}
) )
create_test( create_test(
NAME unit-kami-agent NAME unit-kami-stagedagent
SOURCES unit-kami-agent.cc SOURCES unit-kami-stagedagent.cc
PUBLIC_LINKED_TARGETS fmt spdlog kami::libkami PUBLIC_LINKED_TARGETS gmock gtest kami::libkami
COMMAND unit-kami-agent COMMAND unit-kami-stagedagent
PUBLIC_COMPILE_FEATURES ${COVERAGE_FLAGS} PUBLIC_COMPILE_FEATURES ${COVERAGE_FLAGS}
) )

View File

@@ -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 <string>
#include <kami/agent.h>
#include <kami/config.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
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());
}

View File

@@ -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 <string>
#include <kami/agent.h>
#include <kami/config.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
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());
}

View File

@@ -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 <memory>
#include <kami/agent.h>
#include <kami/model.h>
#include <gtest/gtest.h>
class TestStagedAgent : public kami::StagedAgent {
public:
kami::AgentID advance(std::shared_ptr<kami::Model> model) override {
return get_agent_id();
}
kami::AgentID step(std::shared_ptr<kami::Model> 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<TestModel>();
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<TestModel>();
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();
}