summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2025-03-02 13:37:53 -0500
committerbd <bdunahu@operationnull.com>2025-03-02 13:37:53 -0500
commita9af4fd3243e470ff33d50968f998bf78c152717 (patch)
treeb9815d43d79b631939cd531512b470829ba16436 /tests
parenta81e74ecfc73e27cceba863b8c780ebde51a8d47 (diff)
Added logger class, tests, arg parsing and cleanup
Diffstat (limited to 'tests')
-rw-r--r--tests/logger.cc65
-rw-r--r--tests/tests.cc10
2 files changed, 65 insertions, 10 deletions
diff --git a/tests/logger.cc b/tests/logger.cc
new file mode 100644
index 0000000..18e4fab
--- /dev/null
+++ b/tests/logger.cc
@@ -0,0 +1,65 @@
+#include "logger.h"
+#include <catch2/catch_test_macros.hpp>
+#include <iostream>
+#include <regex>
+#include <sstream>
+
+TEST_CASE("Logger logs higher log level")
+{
+ std::streambuf *coutBuffer = std::cout.rdbuf();
+ std::ostringstream oss;
+ std::cout.rdbuf(oss.rdbuf());
+
+ Logger logger("");
+ logger.setLevel(INFO);
+
+ logger.log(ERROR, "foo bar baz qux");
+
+ std::cout.rdbuf(coutBuffer);
+
+ std::string actual = oss.str();
+ std::regex expected(
+ "\\[\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\] ERROR: "
+ "foo bar baz qux\\n");
+
+ REQUIRE(std::regex_match(actual, expected));
+}
+
+TEST_CASE("Logger logs equal log level")
+{
+ std::streambuf *coutBuffer = std::cout.rdbuf();
+ std::ostringstream oss;
+ std::cout.rdbuf(oss.rdbuf());
+
+ Logger logger("");
+ logger.setLevel(INFO);
+
+ logger.log(INFO, "foo bar baz qux");
+
+ std::cout.rdbuf(coutBuffer);
+
+ std::string actual = oss.str();
+ std::regex expected("\\[\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\] INFO: "
+ "foo bar baz qux\\n");
+
+ REQUIRE(std::regex_match(actual, expected));
+}
+
+TEST_CASE("Logger ignores lower log level")
+{
+ std::streambuf *coutBuffer = std::cout.rdbuf();
+ std::ostringstream oss;
+ std::cout.rdbuf(oss.rdbuf());
+
+ Logger logger("");
+ logger.setLevel(INFO);
+
+ logger.log(DEBUG, "foo bar baz qux");
+
+ std::cout.rdbuf(coutBuffer);
+
+ std::string actual = oss.str();
+ std::string expected("");
+
+ REQUIRE(actual == expected);
+}
diff --git a/tests/tests.cc b/tests/tests.cc
index 8a23c4d..7817006 100644
--- a/tests/tests.cc
+++ b/tests/tests.cc
@@ -1,11 +1 @@
#define CATCH_CONFIG_MAIN
-#include "fact.h"
-#include<catch2/catch_test_macros.hpp>
-
-TEST_CASE( "factorials are computed", "[factorial]")
-{
- REQUIRE( factorial(1) == 1 );
- REQUIRE( factorial(2) == 2 );
- REQUIRE( factorial(3) == 6 );
- REQUIRE( factorial(10) == 3628800 );
-}