mdml-cgi/tests/Application-test.cpp

123 lines
2.9 KiB
C++

/* Application-test.cpp
* Copyright © 2023 Saul D. Beniquez
* License: Mozilla Public License v. 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v.2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Exception.hpp"
#include "tests-common.hpp"
#include "Application.hpp"
#include <functional>
#include <optional>
struct LaunchParameters {
static const char* argv[];
static const char* env[];
};
inline const char* LaunchParameters::argv[] = { "param1", "param2", "param3" };
inline const char* LaunchParameters::env[] = {
"PATH=/usr/bin",
"VAR2=TWO",
};
BEGIN_TEST_SUITE("Application-test")
{
using IOCore::Application;
struct DerivedApplication : public Application {
DerivedApplication(
int argc = 0, c::const_string argv[] = {},
c::const_string env[] = {}
)
: Application(argc, argv, env)
{
}
auto run() -> int override { return 0; }
};
struct TestFixture {
TestFixture() : derived_app() {}
DerivedApplication derived_app;
};
TEST("Application Class construction")
{
auto simple_construction = []() {
auto test_object = DerivedApplication(
3, LaunchParameters::argv, LaunchParameters::env
);
};
auto double_construction = []() {
auto test_object = DerivedApplication(
3, LaunchParameters::argv, LaunchParameters::env
);
auto throws_errors = DerivedApplication(
3, LaunchParameters::argv, LaunchParameters::env
);
};
SECTION("First construction")
{
REQUIRE_NOTHROW(simple_construction());
}
SECTION("Incorrect parameters")
{
auto incorrect_construction = []() {
DerivedApplication obj(0, nullptr, nullptr);
};
REQUIRE_THROWS_AS(
incorrect_construction(), IOCore::Exception
);
}
}
TEST("Application Parameter capture")
{
SECTION("getArguments are captured in vector")
{
auto correct_construction = [&]() {
auto test_object = DerivedApplication(
3,
LaunchParameters::argv,
LaunchParameters::env
);
auto args_list = test_object.getArguments();
SECTION("getArgument verification")
{
REQUIRE(args_list[0] == "param1");
REQUIRE(args_list[1] == "param2");
}
};
REQUIRE_NOTHROW(correct_construction());
}
SECTION("Environemnt variables are catpured in map")
{
auto correct_construction = [&]() {
auto test_object = DerivedApplication(
3,
LaunchParameters::argv,
LaunchParameters::env
);
auto environ = test_object.getEnvironment();
SECTION("Varaible verification")
{
REQUIRE(environ["PATH"] == "/usr/bin");
REQUIRE(environ["VAR2"] == "TWO");
}
};
REQUIRE_NOTHROW(correct_construction());
}
}
}
// clang-format off
// vim: set foldmethod=syntax foldlevel=1000 textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :