Skip to content
Repos

rgui

C++ 5.8 KB60 lines Opens in site

Directory Raw Download

// test_stageb.cpp - Stage B regression tests for review.md framework fixes
#include "../tttest.hpp"
#include "../ttgui.hpp"
using namespace ttgui;
TEST_CASE("stageb: vt clamps, caps, SGR extended, resize preserves, RI, OSC-ST, DSR", "[stageb][unit]"){
  VtParser vt(10,4); vt.feed("\033[99999;99999H"); CHECK(vt.cx<10); CHECK(vt.cy<4);
  vt.feed("\033[2000000000L\033[2000000000@\033[2000000000P"); CHECK_EQ((int)vt.grid.size(),4); CHECK_EQ((int)vt.grid[0].size(),10);
  VtParser vt2(20,5); vt2.feed("\033[38;5;196mX\033[48;2;1;2;3mY");
  CHECK_EQ(vt2.grid[0][0].st.fg.idx,196); CHECK_EQ(vt2.grid[0][1].st.bg.r,1); CHECK_EQ(vt2.grid[0][1].st.bg.g,2);
  VtParser vt3(10,4); vt3.feed("hello\r\nworld"); vt3.resize(20,6); CHECK_EQ(vt3.grid[0][0].ch,std::string("h")); CHECK_EQ(vt3.grid[1][0].ch,std::string("w"));
  VtParser vt4(10,4); vt4.feed("a\r\nb\r\nc"); vt4.feed("\033[1;3r\033[1;1H\033M"); // RI at top margin scrolls region down
  CHECK(vt4.cy>=0&&vt4.cy<4);
  VtParser vt5(20,3); vt5.feed("\033]8;;http://x\033\\text"); std::string l; for(auto&c:vt5.grid[0]) l+=c.ch; CHECK(l.find("text")!=std::string::npos); CHECK(l.find("http")==std::string::npos);
  VtParser vt6(10,3); std::string resp; vt6.responseCallback=[&](const std::string& r){resp=r;}; vt6.feed("\033[6n"); CHECK_EQ(resp,std::string("\033[1;1R"));
  VtParser vt7(10,3); vt7.feed("\033[9;1r"); CHECK_EQ(vt7.scrollTop,0); CHECK_EQ(vt7.scrollBottom,2); // invalid DECSTBM resets full
}
TEST_CASE("stageb: json safe int and depth", "[stageb][unit]"){
  auto v=json::parse("{\"x\":1e300}"); REQUIRE(v.has_value()); CHECK_EQ(v->find("x")->asInt(7),INT_MAX);
  std::string deep(500,'['); deep+=std::string(500,']'); auto d=json::parse(deep); CHECK_FALSE(d.has_value());
  json::Value neg(-1e300); CHECK_EQ(neg.asInt(0),INT_MIN);
}
TEST_CASE("stageb: input drag, modifiers, alt-tab, esc flush", "[stageb][unit]"){
  InputParser p; auto d=p.feed("\033[<32;5;5M"); REQUIRE_EQ(d.size(),(size_t)1); // motion no button -> Move (button bits 3? b=32 => b&3=0, mousePressed false => Drag per pressed logic)
  InputParser p2; auto a=p2.feed("\033[1;5A"); REQUIRE_EQ(a.size(),(size_t)1); CHECK(a[0].key.code==KeyCode::Up); CHECK(a[0].key.ctrl);
  InputParser p3; auto t=p3.feed("\033\t"); REQUIRE_EQ(t.size(),(size_t)1); CHECK(t[0].key.code==KeyCode::Tab); CHECK(t[0].key.alt);
  InputParser p4; auto e0=p4.feed("\033"); CHECK(e0.empty()); auto e1=p4.flushPending(); REQUIRE_EQ(e1.size(),(size_t)1); CHECK(e1[0].key.code==KeyCode::Escape);
  InputParser p5; (void)p5.feed("\033[<0;5;5M"); auto dr=p5.feed("\033[<32;6;5M"); REQUIRE_EQ(dr.size(),(size_t)1); CHECK(dr[0].mouse.kind==MouseKind::Drag);
}
TEST_CASE("stageb: canvas getMut, windowColor fallback, caption, children, toolbar, scrollview, dialog", "[stageb][unit]"){
  Canvas cv(4,2); cv.getMut(-1,0).ch="X"; cv.getMut(99,99).ch="Y"; CHECK_EQ(cv.get(0,0).ch,std::string(" "));
  Config cfg=Config::defaults(); cfg.windowColors.erase("grey"); CHECK_EQ(cfg.windowColor("nope").bg.r,170);
  Window w("W","grey"); w.cfg=&cfg; w.bounds={0,0,20,5}; w.resizable=false;
  bool foundShade=false,foundSticky=false; for(auto&b:w.captionButtons()){ if(b.second=="shade") foundShade=true; if(b.second=="sticky") foundSticky=true; }
  CHECK(foundShade); CHECK_FALSE(foundSticky); CHECK(w.isShadeBox(w.captionButtons().back().first.pos())); CHECK_FALSE(w.isResizeBox({19,4}));
  VBox vb; auto* l=vb.emplace<Label>("x"); CHECK_EQ(vb.children().size(),(size_t)1); CHECK(vb.removeChild(l)); CHECK(vb.children().empty());
  Toolbar tb; auto* b=tb.addButton("Go"); tb.layout(); CHECK_EQ(tb.children().size(),(size_t)1); CHECK(b->parent==(Widget*)&tb);
  ScrollView sv; sv.bounds={0,0,20,5}; sv.layout(); CHECK(sv.vbar!=nullptr); CHECK_EQ(sv.children().size(),(size_t)1); CHECK(sv.children()[0].get()==(Widget*)sv.vbar);
  auto q=MessageBox::question("Q","sure?"); q->bounds={0,0,30,6}; q->layout(); CHECK_GE(q->children().size(),(size_t)2); int got=-1; q->onResult.connect([&](int i){got=i;}); KeyEventData ek; ek.code=KeyCode::Enter; q->onKey(ek); CHECK_EQ(got,0);
  ComboBox cb; cb.items={"A","B","C"}; cb.bounds={0,0,10,1}; cb.open=true; Canvas c2(20,10); cb.bounds={0,5,10,1}; c2.resize(20,10); cb.drawTree(c2); CHECK(c2.text().find("B")!=std::string::npos);
}
TEST_CASE("stageb: keybindings remap, delete scoped to non-edit, bell urgency, cascade", "[stageb][integration]"){
  Config cfg=Config::defaults(); cfg.keybindings["quit"]="Ctrl+W"; cfg.bellAudible=false;
  Application app(cfg); app.wm.workArea={0,0,80,24};
  auto* win=app.wm.createWindow("A","grey",{0,0,20,6}); auto* ed=new EditBox(); win->addChild(std::unique_ptr<Widget>(ed)); app.setFocused(ed);
  Event del=Event::makeKey({KeyCode::Delete}); app.dispatch(del); CHECK_FALSE(win->closing); // Delete in edit does not close-all
  app.setFocused(nullptr);
  KeyEventData q; q.code=KeyCode::Char; q.text="q"; q.ctrl=true; q.ch='q'; Event qe=Event::makeKey(q); app.dispatch(qe); CHECK_FALSE(app.quitReason=="Ctrl+Q"); // remapped: Ctrl+Q no longer quits (quitReason stays empty)
  CHECK(app.quitReason.empty());
  // bell urgency: unfocused window
  auto* w2=app.wm.createWindow("B","green",{5,2,20,6}); auto* tw=new TerminalWidget(10,3); w2->addChild(std::unique_ptr<Widget>(tw)); app.registerTerminal(tw); app.wm.focusWindow(win);
  app.bell.nowMs=5000; if(tw->onBell) tw->onBell(); CHECK(w2->urgent);
  Rect preCascade=w2->bounds; app.wm.cascade(); CHECK(w2->bounds.x>=0); CHECK(w2->bounds.y>=0);
  app.wm.tile(); CHECK_GE(w2->bounds.w,10); CHECK_GE(w2->bounds.h,2); (void)preCascade; // tw is owned by w2 (addChild)
}
TEST_CASE("stageb: tttest zero-check and sandbox ownership", "[stageb][unit]"){
  // SandboxDir owns only what it created
  tttest::SandboxDir sb; CHECK(std::filesystem::exists(sb.path())); CHECK(sb.path().find("tttest-sandbox")!=std::string::npos);
  CHECK(std::filesystem::is_directory(sb.path()));
  { std::ofstream o(sb.file("keep.txt")); o<<"x"; } CHECK(std::filesystem::exists(sb.file("keep.txt")));
}
26.6ms