diff --git a/lpstd.hpp b/lpstd.hpp index db94ea7..8dbd313 100644 --- a/lpstd.hpp +++ b/lpstd.hpp @@ -3,5 +3,6 @@ #define __LPSTD_HPP__ #include "src/singleton.hpp" +#include "src/testing/testing.hpp" #endif // __LPSTD_HPP__ diff --git a/src/testing/testing.hpp b/src/testing/testing.hpp new file mode 100644 index 0000000..b5dcf22 --- /dev/null +++ b/src/testing/testing.hpp @@ -0,0 +1,149 @@ + +#include +#include + +#ifdef _WIN32 +#include +#include +#endif + +namespace lpstd +{ + namespace testing + { + + template + class Expect + { + public: + Expect(T value) + { + +#ifdef _WIN32 + // Set console code page to UTF-8 so console known how to interpret string data + SetConsoleOutputCP(CP_UTF8); + + // Enable buffering to prevent VS from chopping up UTF-8 byte sequences + setvbuf(stdout, nullptr, _IOFBF, 1000); +#endif + + this->value = value; + } + + bool toBe(T expected) + { + bool result = this->value == expected; + if (result) + { + std::cout << "✅ Expected " << this->value << " to be " << expected << std::endl; + } + else + { + std::cout << "❌ Expected " << expected << " but got " << this->value << std::endl; + } + return result; + } + + bool toBeGreaterThan(T expected) + { + bool result = this->value > expected; + if (result) + { + std::cout << "✅ Expected " << this->value << " to be greater than " << expected << std::endl; + } + else + { + std::cout << "❌ Expected " << this->value << " to be greater than " << expected << std::endl; + } + + return result; + } + + bool toBeLessThan(T expected) + { + bool result = this->value < expected; + if (result) + { + std::cout << "✅ Expected " << this->value << " to be less than " << expected << std::endl; + } + else + { + std::cout << "❌ Expected " << this->value << " to be less than " << expected << std::endl; + } + return result; + } + + bool toBeTrue() + { + bool result = this->value; + if (result) + { + std::cout << "✅ Expected " << this->value << " to be true" << std::endl; + } + else + { + std::cout << "❌ Expected " << this->value << " to be true" << std::endl; + } + return result; + } + + bool toBeFalse() + { + bool result = !this->value; + if (result) + { + std::cout << "✅ Expected " << this->value << " to be false" << std::endl; + } + else + { + std::cout << "❌ Expected " << this->value << " to be false" << std::endl; + } + return result; + } + + bool toBeCloseTo(T expected, T delta = 0.01) + { + bool result = this->value >= expected - delta && this->value <= expected + delta; + if (result) + { + std::cout << "✅ Expected " << this->value << " to be close to " << expected << std::endl; + } + else + { + std::cout << "❌ Expected " << this->value << " to be close to " << expected << std::endl; + } + return result; + } + + private: + T value; + }; + + template + Expect expect(T value) + { + return Expect(value); + } + + // void test(std::string description, void (*test)()) + // { + // std::cout << description << std::endl; + // test(); + // } + + // Custom test function to execute a single test case + void test(std::string description, std::function testCase) + { + std::cout << description << std::endl; + testCase(); + } + + // Custom describe function to group related test cases + void describe(std::string description, std::function testSuite) + { + std::cout << "Describing " << description << std::endl; + testSuite(); + } + + } +} \ No newline at end of file