Files
lightgui_rust/crates/lightgui-debug/tests/gtk.rs
T
cnmcdee 3d53281388 Add lightgui-debug and lightgui-support: GDB MI debugger + theme/preferences/codeview/cmake ports
lightgui-debug: GdbSession over gdb -q -i=mi2 pipes with hand-written MI
parser (mi.rs), settings/breakpoint/variable types, and the 44-check
C++ harness ported as Rust tests (37 console + 7 GTK, gdb 17.1).
lightgui-support: CodeTheme + C tokenizer/highlighter, FontSettings,
CodeView fold/gutter/inspection widget, CMake project builder.
Tests: debug 59/59, support 23/23 (incl. theme/font/cmake suites).
cargo test clean, zero rustc warnings, clippy clean, cargo fmt clean.
2026-11-29 06:10:11 +00:00

153 lines
5.1 KiB
Rust

// GTK debugger suite — Rust port of gtk_tests() in
// tests/debugger/dbg_harness.cpp (7 checks: marks_found + tests 31-36).
// The target is a real GTK app, so these tests need a display:
// xvfb-run -a cargo test -p lightgui-debug --test gtk
// Each check is self-contained (its own GdbSession), unlike the C++
// version where tests 32-33 chained inside test 31's session.
#[path = "common/mod.rs"]
mod common;
use common::{has, loc, mark_line, targets, var_value};
use lightgui_debug::{BpKind, Breakpoint, DbgState, GdbSession};
use std::path::PathBuf;
fn app() -> PathBuf {
targets().gtkapp.clone()
}
fn app_src() -> PathBuf {
targets().gtkapp_src.clone()
}
fn marks() -> (i32, i32) {
let src = app_src();
(mark_line(&src, "MARK:CB"), mark_line(&src, "MARK:CB_NEXT"))
}
#[test]
fn gtk_marks_found() {
let (a, b) = marks();
assert!(a > 0 && b > 0);
}
// 31. breakpoint in the button callback is hit on auto-click
#[test]
fn gtk_bp_in_callback() {
let (l_cb, _) = marks();
let mut sess = GdbSession::default();
sess.stage_breakpoint(&loc(&app_src(), l_cb));
sess.start(app().to_str().unwrap(), "").expect("start");
assert!(sess.wait_for_stop(15_000));
assert_eq!(sess.state(), DbgState::Stopped);
assert_eq!(sess.last_stop().reason, "breakpoint-hit");
assert!(
sess.last_stop().frame.function.contains("on_clicked"),
"func={}",
sess.last_stop().frame.function
);
assert_eq!(sess.last_stop().frame.line, l_cb);
}
// 32. step through the callback: counter becomes 1. (At the CB line
// `before` is still unassigned, so step to the next line first, where
// before == 0.)
#[test]
fn gtk_step_in_callback() {
let (l_cb, l_cb_next) = marks();
let mut sess = GdbSession::default();
sess.stage_breakpoint(&loc(&app_src(), l_cb));
sess.start(app().to_str().unwrap(), "").expect("start");
assert!(sess.wait_for_stop(15_000));
sess.step_over().expect("step_over");
assert!(sess.wait_for_stop(-1));
assert_eq!(sess.last_stop().frame.line, l_cb_next);
sess.refresh_on_stop();
assert!(
has(&sess.locals(), "before", "0"),
"before={}",
var_value(&sess.locals(), "before")
);
sess.step_over().expect("step_over");
assert!(sess.wait_for_stop(-1));
let v = sess.evaluate("counter").expect("evaluate counter");
assert_eq!(v, "1", "counter={}", v);
}
// 33. continue: app finishes on its own timer, exit 0
#[test]
fn gtk_continue_to_exit() {
let (l_cb, _) = marks();
let mut sess = GdbSession::default();
sess.stage_breakpoint(&loc(&app_src(), l_cb));
sess.start(app().to_str().unwrap(), "").expect("start");
assert!(sess.wait_for_stop(15_000));
sess.cont().expect("cont");
assert!(sess.wait_for_stop(15_000));
assert_eq!(sess.state(), DbgState::Exited);
assert_eq!(sess.exit_code(), 0);
assert!(
sess.target_output().contains("CLICKS=1"),
"out: {}",
sess.target_output()
);
}
// 34. GTK app runs to completion with no breakpoints
#[test]
fn gtk_run_to_completion() {
let mut sess = GdbSession::default();
sess.start(app().to_str().unwrap(), "").expect("start");
assert!(sess.wait_for_stop(15_000));
assert_eq!(sess.state(), DbgState::Exited);
assert_eq!(sess.exit_code(), 0);
assert!(
sess.target_output().contains("CLICKS=1"),
"out: {}",
sess.target_output()
);
}
// 35. logpoint in the callback: prints, never stops, app exits 0
#[test]
fn gtk_logpoint_no_stop() {
let (l_cb, _) = marks();
let mut sess = GdbSession::default();
let opts = Breakpoint {
kind: BpKind::Logpoint,
log_message: "CLICK counter={counter}".to_string(),
..Default::default()
};
assert!(sess.stage_breakpoint_opts(&loc(&app_src(), l_cb), &opts));
sess.start(app().to_str().unwrap(), "").expect("start");
assert!(sess.wait_for_stop(15_000));
assert_eq!(sess.state(), DbgState::Exited);
assert_eq!(sess.exit_code(), 0);
assert!(sess.target_output().contains("CLICKS=1"));
// At the callback entry counter is still 0 (the click has not been
// counted yet); the dprintf output is on gdb's console stream.
let log = sess.gdb_log();
assert!(log.contains("CLICK counter=0"), "log: {}", log);
}
// 36. conditional breakpoint in the callback: fires when counter == 0
#[test]
fn gtk_conditional_in_callback() {
let (l_cb, _) = marks();
let mut sess = GdbSession::default();
let opts = Breakpoint {
kind: BpKind::Conditional,
condition: "counter == 0".to_string(),
..Default::default()
};
assert!(sess.stage_breakpoint_opts(&loc(&app_src(), l_cb), &opts));
sess.start(app().to_str().unwrap(), "").expect("start");
assert!(sess.wait_for_stop(15_000));
assert_eq!(sess.state(), DbgState::Stopped);
assert_eq!(sess.last_stop().reason, "breakpoint-hit");
assert_eq!(sess.last_stop().frame.line, l_cb);
sess.cont().expect("cont");
assert!(sess.wait_for_stop(15_000));
assert_eq!(sess.state(), DbgState::Exited);
assert_eq!(sess.exit_code(), 0);
assert!(sess.target_output().contains("CLICKS=1"));
}