// test_tttest.cpp - self-tests of tttest itself (sandbox registry, never reddens real suite) #include "../tttest.hpp" #include "../ttgui.hpp" #include using namespace ttgui; TEST_CASE("tttest: CHECK records and continues, REQUIRE aborts case", "[tttest][unit]"){ // In-process sandbox: run a synthetic context manually tttest::Context ctx; auto* outerCtx=tttest::currentCtx(); tttest::currentCtx()=&ctx; bool afterCheck=false, afterRequire=false; // emulate CHECK failure without macro fatal tttest::recordCheck(); tttest::recordFailure(__FILE__,__LINE__,"1==2","synthetic",false); afterCheck=true; try { tttest::recordFailure(__FILE__,__LINE__,"REQUIRE(1==2)","synthetic fatal",true); } catch(const tttest::RequireAbort&){ /* expected */ } afterRequire=true; // code after catch, not after REQUIRE in same scope semantics proven by exception tttest::currentCtx()=outerCtx; CHECK(afterCheck); CHECK(afterRequire); CHECK_EQ(ctx.failures.size(), (size_t)2); CHECK_FALSE(ctx.failures[0].fatal); CHECK_TRUE(ctx.failures[1].fatal); CHECK_NE(ctx.failures[0].line, 0); } TEST_CASE("tttest: sections re-run and nested sections execute", "[tttest][unit]"){ // Assert inside each section (the original form asserted only after both // sections had run, which the re-run passes never reach together, so it // performed zero checks - caught by the NOCHECKS rule). static int aRuns=0, bRuns=0, nestedRuns=0; SECTION("alpha"){ aRuns++; SECTION("nested"){ nestedRuns++; CHECK_GE(nestedRuns,1); } CHECK_GE(aRuns,1); } SECTION("beta"){ bRuns++; CHECK_GE(bRuns,1); } CHECK_GE(aRuns+bRuns,1); } TEST_CASE("tttest: tag parsing and filtering helpers", "[tttest][unit]"){ tttest::TestCaseInfo tc; tc.name="x"; tc.tags="[config][slow]"; CHECK(tc.hasTag("config")); CHECK(tc.hasTag("slow")); CHECK_FALSE(tc.hasTag("widget")); std::vector out; tttest::parseTagsArg("[a][b]",out); CHECK_EQ(out.size(),(size_t)2); CHECK_EQ(out[0],std::string("a")); auto& reg=tttest::registry(); CHECK_FALSE(reg.empty()); bool found=false; for(auto& c: reg) if(c.name.find("tttest:")!=std::string::npos) found=true; CHECK(found); } TEST_CASE("tttest: assertions EQ/NE/order/near/throws", "[tttest][unit]"){ CHECK_EQ(2+2,4); CHECK_NE(1,2); CHECK_LT(1,2); CHECK_LE(2,2); CHECK_GT(3,2); CHECK_GE(3,3); CHECK_NEAR(1.0,1.05,0.1); CHECK_TRUE(2>1); CHECK_FALSE(2<1); CHECK_THROWS_AS(throw std::runtime_error("x"), std::runtime_error); CHECK_NOTHROW((void)42); REQUIRE_EQ(std::string("ab"),std::string("ab")); } TEST_CASE("tttest: FakeClock, SeededRng, SandboxDir determinism", "[tttest][unit]"){ tttest::FakeClock clk; clk.advance(399); CHECK_EQ(clk.now(),399LL); CHECK_TRUE(clk.withinDoubleClick(0,399,400)); CHECK_FALSE(clk.withinDoubleClick(0,400,400)); tttest::SeededRng r1(7), r2(7); CHECK_EQ(r1.nextInt(0,1000), r2.nextInt(0,1000)); tttest::SandboxDir sb; CHECK_TRUE(std::filesystem::exists(sb.path())); { std::ofstream o(sb.file("a.txt")); o<<"hi"; } CHECK_TRUE(std::filesystem::exists(sb.file("a.txt"))); } TEST_CASE("tttest: registry-sandbox zero-check FAIL, failing FAIL, passing PASS with counts", "[tttest][unit]"){ // Registry-sandbox: synthetic cases run in an isolated registry; the real // registry, cout and outer Context are restored afterwards (see tttest.hpp). auto mk=[](const char* n, std::function f){ tttest::TestCaseInfo tc; tc.name=n; tc.tags="[sandbox]"; tc.file=__FILE__; tc.line=__LINE__; tc.func=std::move(f); return tc; }; std::vector cases; cases.push_back(mk("sandbox-zero", [](){ /* no checks */ })); cases.push_back(mk("sandbox-fail", [](){ CHECK_EQ(1,2); })); cases.push_back(mk("sandbox-pass", [](){ CHECK_EQ(2,2); CHECK_TRUE(1<2); })); tttest::Options opt; std::string out=tttest::runRegistrySandbox(cases,opt); CHECK(out.find("FAIL sandbox-zero")!=std::string::npos); CHECK(out.find("NOCHECKS")!=std::string::npos); CHECK(out.find("FAIL sandbox-fail")!=std::string::npos); CHECK(out.find("PASS sandbox-pass [2 checks")!=std::string::npos); CHECK(out.find("passed=1")!=std::string::npos); CHECK(out.find("failed=2")!=std::string::npos); // outer context still live after the sandbox (this CHECK itself must count) CHECK_EQ(tttest::registry().empty(),false); } TEST_CASE("tttest: full-cell helpers distinguish colour/attrs, not only glyphs", "[tttest][unit]"){ VirtualTerminal vt(4,2); CellStyle red; red.fg=Color::rgb(255,0,0); red.bold=true; vt.canvas.set(0,0,"A",red); vt.canvas.set(1,0,"B",{}); std::string full=tttest::fullCellRect(vt.canvas,0,0,2,1); CHECK(full.find("AB")!=std::string::npos); CHECK(full.find("(255,0,0,-1)")!=std::string::npos); CHECK(full.find(" B")!=std::string::npos); // bold flag recorded // same glyphs, different colour -> different full-cell serialization VirtualTerminal vt2(4,2); CellStyle blu; blu.fg=Color::rgb(0,0,255); blu.bold=true; vt2.canvas.set(0,0,"A",blu); vt2.canvas.set(1,0,"B",{}); CHECK_NE(tttest::fullCellRect(vt2.canvas,0,0,2,1), full); CHECK_CELLS_FULL(vt.canvas,0,0,2,1,full); std::string why; VBox root; auto* a=root.emplace