refactor: workspace layout — crates/, app at root, legacy C++ removed
Single mechanical restructure commit: - root Cargo.toml = oakapp bin + workspace; one cargo build produces oakapp, oak-cli, oak-worker, liboakengine.dylib - app/rust/src -> src/ (app at repo root, no rust/ nesting) - src/<mod>/rust -> crates/oak<mod>; src/oakcore-rs -> crates/oakcore; src/bindings/oakotio -> crates/oakotio; src/engine/rust -> crates/oakengine (keeps cdylib+staticlib+rlib) - public C headers include/<mod>/ -> crates/oakengine/include/<mod>/ - OFX SDK headers vendored into crates/oakplugin/ofx/ (HostSupport gone) - legacy deleted: old src/ C++ modules, engine/, core/, ffmpeg_bridge/, app/ (Qt), cli/worker C++, root CMakeLists, third_party/KDDockWidgets submodule, otio-install, all build-* output (~40GB) - oakstorage kept but excluded from the workspace (skeleton w/ todos); gpui excluded (own workspace) - verified: cargo build green, cargo test --workspace 1845/0 (with the documented OCIO_RS_* env override for the homebrew OCIO)
This commit is contained in:
@@ -0,0 +1,241 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Edge-path coverage: error-code mapping, borrowed handles, refcount
|
||||
//! symmetry, shell handles after `command_take`, `Default` impls, and the
|
||||
//! `push_pre_executed` redo-tail/cap paths. Everything here goes through
|
||||
//! the crate's public API or the C ABI.
|
||||
|
||||
use oakundo::error::{
|
||||
Error, OAKUNDO_E_FAILED, OAKUNDO_E_INVALID, OAKUNDO_E_NOMEM, OAKUNDO_E_NOT_FOUND,
|
||||
OAKUNDO_E_STATE,
|
||||
};
|
||||
use oakundo::ffi::command::{oakundo_command_init, oakundo_command_redo_now};
|
||||
use oakundo::ffi::undostack::{
|
||||
oakundo_undostack_index, oakundo_undostack_push, oakundo_undostack_push_pre_executed,
|
||||
oakundo_undostack_undo, oakundo_undostack_init,
|
||||
};
|
||||
use oakundo::handle::{make_borrowed, make_owned};
|
||||
use oakundo::undocommand::{MultiUndoCommand, OakUndoCommandVtable, UndoCommand};
|
||||
use oakundo::undostack::{EmptyCommand, UndoStack, K_MAX_UNDO_COMMANDS};
|
||||
|
||||
/// Every `Error` variant maps to its documented public code.
|
||||
#[test]
|
||||
fn error_code_mapping_is_complete() {
|
||||
assert_eq!(Error::Invalid.code(), OAKUNDO_E_INVALID);
|
||||
assert_eq!(Error::State.code(), OAKUNDO_E_STATE);
|
||||
assert_eq!(Error::Failed("ctx".to_string()).code(), OAKUNDO_E_FAILED);
|
||||
assert_eq!(Error::NotFound.code(), OAKUNDO_E_NOT_FOUND);
|
||||
assert_eq!(Error::NoMem.code(), OAKUNDO_E_NOMEM);
|
||||
}
|
||||
|
||||
/// Borrowed handles: addref/release only touch the shell, never the
|
||||
/// pointee; NULL ctx is a no-op for both.
|
||||
#[test]
|
||||
fn borrowed_handle_refcounting() {
|
||||
let mut value: i32 = 7;
|
||||
let h = unsafe { make_borrowed(&mut value as *mut i32) };
|
||||
assert!(!h.is_null());
|
||||
|
||||
// NULL ctx is a no-op.
|
||||
unsafe { h.addref.unwrap()(std::ptr::null_mut()) };
|
||||
unsafe { h.release.unwrap()(std::ptr::null_mut()) };
|
||||
|
||||
// addref then two releases: the pointee survives (still readable).
|
||||
unsafe { h.addref.unwrap()(h.ctx) };
|
||||
unsafe { h.release.unwrap()(h.ctx) };
|
||||
assert_eq!(value, 7);
|
||||
unsafe { h.release.unwrap()(h.ctx) };
|
||||
assert_eq!(value, 7);
|
||||
}
|
||||
|
||||
/// Owned handles: addref requires a matching extra release; releasing to
|
||||
/// zero destroys the box exactly once.
|
||||
#[test]
|
||||
fn owned_handle_refcounting() {
|
||||
let h = make_owned(String::from("owned"));
|
||||
assert!(!h.is_null());
|
||||
unsafe { h.addref.unwrap()(h.ctx) };
|
||||
unsafe { h.release.unwrap()(h.ctx) };
|
||||
// Still alive (one reference left) and readable.
|
||||
let view = unsafe { oakundo::handle::get::<String>(&h) };
|
||||
assert_eq!(view.map(String::as_str), Some("owned"));
|
||||
unsafe { h.release.unwrap()(h.ctx) };
|
||||
}
|
||||
|
||||
/// `Default` impls mirror `new()`.
|
||||
#[test]
|
||||
fn default_impls_match_new() {
|
||||
let _empty = EmptyCommand::default();
|
||||
let stack = UndoStack::default();
|
||||
assert_eq!(stack.done_count(), 1, "fresh stack holds the bottom command");
|
||||
assert!(!stack.can_undo());
|
||||
let multi = MultiUndoCommand::default();
|
||||
assert_eq!(multi.child_count(), 0);
|
||||
}
|
||||
|
||||
/// `set_prepared` is idempotent and `has_prepared` reflects it.
|
||||
#[test]
|
||||
fn prepared_flag_roundtrip() {
|
||||
let vtable = OakUndoCommandVtable {
|
||||
redo: None,
|
||||
undo: None,
|
||||
free_fn: None,
|
||||
};
|
||||
let mut cmd = UndoCommand::from_vtable(vtable, std::ptr::null_mut());
|
||||
assert!(cmd.has_prepared());
|
||||
cmd.set_prepared();
|
||||
assert!(cmd.has_prepared());
|
||||
}
|
||||
|
||||
/// A command handle whose value was taken by a stack push becomes a
|
||||
/// non-owning shell: redo/undo on it are `E_INVALID`, and pushing it a
|
||||
/// second time is `E_STATE`.
|
||||
#[test]
|
||||
fn taken_command_shell_is_inert() {
|
||||
let vtable = OakUndoCommandVtable {
|
||||
redo: None,
|
||||
undo: None,
|
||||
free_fn: None,
|
||||
};
|
||||
let mut stack = unsafe { oakundo_undostack_init() };
|
||||
let cmd = unsafe { oakundo_command_init(&vtable, std::ptr::null_mut()) };
|
||||
assert!(!cmd.ctx.is_null());
|
||||
|
||||
let name = c"once";
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_push(stack, cmd, name.as_ptr()) },
|
||||
0
|
||||
);
|
||||
|
||||
// The shell no longer holds a command value.
|
||||
assert_eq!(
|
||||
unsafe { oakundo_command_redo_now(cmd) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
// Taking the same box twice is a state error.
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_push(stack, cmd, name.as_ptr()) },
|
||||
OAKUNDO_E_STATE
|
||||
);
|
||||
|
||||
let mut release = cmd;
|
||||
unsafe {
|
||||
oakundo::ffi::command::oakundo_command_free(&mut release);
|
||||
oakundo::ffi::undostack::oakundo_undostack_free(&mut stack);
|
||||
}
|
||||
}
|
||||
|
||||
/// `push_pre_executed` also drops the redoable tail and evicts the oldest
|
||||
/// row past the cap (mirrors `push`).
|
||||
#[test]
|
||||
fn push_pre_executed_clears_redo_tail_and_caps() {
|
||||
let vtable = OakUndoCommandVtable {
|
||||
redo: None,
|
||||
undo: None,
|
||||
free_fn: None,
|
||||
};
|
||||
let mut stack = unsafe { oakundo_undostack_init() };
|
||||
let name = c"row";
|
||||
|
||||
// Push two, undo one, then push_pre_executed: redo tail is dropped.
|
||||
for _ in 0..2 {
|
||||
let cmd = unsafe { oakundo_command_init(&vtable, std::ptr::null_mut()) };
|
||||
assert_eq!(unsafe { oakundo_undostack_push(stack, cmd, name.as_ptr()) }, 0);
|
||||
let mut shell = cmd;
|
||||
unsafe { oakundo::ffi::command::oakundo_command_free(&mut shell) };
|
||||
}
|
||||
assert_eq!(unsafe { oakundo_undostack_undo(stack) }, 0);
|
||||
let cmd = unsafe { oakundo_command_init(&vtable, std::ptr::null_mut()) };
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_push_pre_executed(stack, cmd, name.as_ptr()) },
|
||||
0
|
||||
);
|
||||
let mut can_redo: i32 = 1;
|
||||
assert_eq!(
|
||||
unsafe { oakundo::ffi::undostack::oakundo_undostack_can_redo(stack, &mut can_redo) },
|
||||
0
|
||||
);
|
||||
assert_eq!(can_redo, 0, "push_pre_executed drops the redoable tail");
|
||||
|
||||
// Fill past the cap with pre-executed commands: the oldest rows are
|
||||
// evicted and the count stays at K_MAX_UNDO_COMMANDS.
|
||||
for _ in 0..(K_MAX_UNDO_COMMANDS + 10) {
|
||||
let cmd = unsafe { oakundo_command_init(&vtable, std::ptr::null_mut()) };
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_push_pre_executed(stack, cmd, name.as_ptr()) },
|
||||
0
|
||||
);
|
||||
}
|
||||
let mut index: i64 = 0;
|
||||
assert_eq!(unsafe { oakundo_undostack_index(stack, &mut index) }, 0);
|
||||
assert_eq!(index, K_MAX_UNDO_COMMANDS as i64, "pre-executed rows evict at the cap");
|
||||
|
||||
unsafe { oakundo::ffi::undostack::oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
/// `make_owned` on a stack mutex is what `undostack_init` uses; the
|
||||
/// `CHandle` accessors tolerate empty handles.
|
||||
#[test]
|
||||
fn empty_chandle_accessors() {
|
||||
let h = oakundo::handle::CHandle::null();
|
||||
assert!(h.is_null());
|
||||
assert!(h.addref.is_none() && h.release.is_none());
|
||||
assert_eq!(h.abi_version, 0);
|
||||
let view = unsafe { oakundo::handle::get::<i32>(&h) };
|
||||
assert!(view.is_none());
|
||||
}
|
||||
|
||||
/// Command-box refcounting: addref/release tolerate NULL ctx, a bumped
|
||||
/// refcount needs a matching release, and a taken multi shell reports
|
||||
/// `E_INVALID` instead of dereferencing a null command pointer.
|
||||
#[test]
|
||||
fn command_box_refcounting_and_taken_multi_shell() {
|
||||
let vtable = OakUndoCommandVtable {
|
||||
redo: None,
|
||||
undo: None,
|
||||
free_fn: None,
|
||||
};
|
||||
|
||||
let mut cmd = unsafe { oakundo_command_init(&vtable, std::ptr::null_mut()) };
|
||||
assert!(!cmd.ctx.is_null());
|
||||
|
||||
// NULL ctx is a no-op for both refcount callbacks.
|
||||
unsafe { cmd.addref.unwrap()(std::ptr::null_mut()) };
|
||||
unsafe { cmd.release.unwrap()(std::ptr::null_mut()) };
|
||||
|
||||
// addref then two releases: destroyed exactly once at zero.
|
||||
unsafe { cmd.addref.unwrap()(cmd.ctx) };
|
||||
unsafe { cmd.release.unwrap()(cmd.ctx) };
|
||||
unsafe { cmd.release.unwrap()(cmd.ctx) };
|
||||
cmd.ctx = std::ptr::null_mut();
|
||||
|
||||
// A multi command pushed into a stack is taken; the remaining shell
|
||||
// must fail cleanly on child access.
|
||||
let mut stack = unsafe { oakundo_undostack_init() };
|
||||
let mut multi = unsafe { oakundo::ffi::command::oakundo_command_init_multi() };
|
||||
let name = c"m";
|
||||
assert_eq!(unsafe { oakundo_undostack_push(stack, multi, name.as_ptr()) }, 0);
|
||||
let mut out: i32 = -1;
|
||||
assert_eq!(
|
||||
unsafe { oakundo::ffi::command::oakundo_command_multi_child_count(multi, &mut out) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
unsafe {
|
||||
oakundo::ffi::command::oakundo_command_free(&mut multi);
|
||||
oakundo::ffi::undostack::oakundo_undostack_free(&mut stack);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,742 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! C ABI contract tests (`ffi.rs`). Each exported function gets at least
|
||||
//! one success path and one failure path; complex multi-command and stack
|
||||
//! behavior is exercised as a matrix. The expected semantics are pinned by
|
||||
//! the C++ module (`src/undo/src`, unchanged).
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::ffi::{c_char, c_int, c_void, CString};
|
||||
|
||||
use oakundo::error::{
|
||||
OAKUNDO_E_INVALID, OAKUNDO_E_NOT_FOUND, OAKUNDO_OK,
|
||||
};
|
||||
use oakundo::ffi::command::{
|
||||
oakundo_command_free, oakundo_command_init, oakundo_command_init_multi,
|
||||
oakundo_command_multi_add_child, oakundo_command_multi_child,
|
||||
oakundo_command_multi_child_count, oakundo_command_redo_now, oakundo_command_undo_now,
|
||||
};
|
||||
use oakundo::ffi::undostack::{
|
||||
oakundo_undostack_can_redo, oakundo_undostack_can_undo, oakundo_undostack_clear,
|
||||
oakundo_undostack_command_is_done, oakundo_undostack_command_text, oakundo_undostack_count,
|
||||
oakundo_undostack_free, oakundo_undostack_index, oakundo_undostack_init,
|
||||
oakundo_undostack_jump, oakundo_undostack_push, oakundo_undostack_push_pre_executed,
|
||||
oakundo_undostack_redo, oakundo_undostack_undo,
|
||||
};
|
||||
use oakundo::ffi::{OakUndoCommand, OakUndoStack};
|
||||
use oakundo::handle::CHandle;
|
||||
use oakundo::handle::OAKUNDO_ABI_VERSION;
|
||||
use oakundo::undocommand::OakUndoCommandVtable;
|
||||
|
||||
/// Shared event recorder driven through vtable callbacks.
|
||||
struct Trace {
|
||||
events: RefCell<Vec<String>>,
|
||||
}
|
||||
|
||||
/// Per-command callback payload: a name and a shared trace.
|
||||
struct Probe {
|
||||
name: &'static str,
|
||||
trace: *const Trace,
|
||||
}
|
||||
|
||||
unsafe extern "C" fn probe_redo(u: *mut c_void) {
|
||||
let p = unsafe { &mut *(u as *mut Probe) };
|
||||
let trace = unsafe { &*p.trace };
|
||||
trace.events.borrow_mut().push(format!("redo:{}", p.name));
|
||||
}
|
||||
|
||||
unsafe extern "C" fn probe_undo(u: *mut c_void) {
|
||||
let p = unsafe { &mut *(u as *mut Probe) };
|
||||
let trace = unsafe { &*p.trace };
|
||||
trace.events.borrow_mut().push(format!("undo:{}", p.name));
|
||||
}
|
||||
|
||||
unsafe extern "C" fn probe_free(u: *mut c_void) {
|
||||
let p = unsafe { &mut *(u as *mut Probe) };
|
||||
let trace = unsafe { &*p.trace };
|
||||
trace.events.borrow_mut().push(format!("free:{}", p.name));
|
||||
}
|
||||
|
||||
/// Snapshot of the recorded events, in order.
|
||||
fn events(trace: &Trace) -> Vec<String> {
|
||||
trace.events.borrow().clone()
|
||||
}
|
||||
|
||||
/// A fresh trace plus three named probes (`a`, `b`, `c`) pointing at it.
|
||||
fn setup() -> (Box<Trace>, Vec<Probe>) {
|
||||
let trace = Box::new(Trace {
|
||||
events: RefCell::new(Vec::new()),
|
||||
});
|
||||
let ptr = &*trace as *const Trace;
|
||||
let mut probes = Vec::new();
|
||||
for name in ["a", "b", "c"] {
|
||||
probes.push(Probe {
|
||||
name,
|
||||
trace: ptr,
|
||||
});
|
||||
}
|
||||
(trace, probes)
|
||||
}
|
||||
|
||||
/// A vtable-backed command handle whose callbacks record into `probe`.
|
||||
unsafe fn make_cmd(probe: *mut Probe) -> OakUndoCommand {
|
||||
let vtable = OakUndoCommandVtable {
|
||||
redo: Some(probe_redo),
|
||||
undo: Some(probe_undo),
|
||||
free_fn: Some(probe_free),
|
||||
};
|
||||
oakundo_command_init(&vtable, probe as *mut c_void)
|
||||
}
|
||||
|
||||
/// An empty (all-zero) command handle.
|
||||
fn empty_cmd() -> OakUndoCommand {
|
||||
CHandle {
|
||||
ctx: std::ptr::null_mut(),
|
||||
addref: None,
|
||||
release: None,
|
||||
abi_version: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// An empty (all-zero) stack handle.
|
||||
fn empty_stack() -> OakUndoStack {
|
||||
CHandle {
|
||||
ctx: std::ptr::null_mut(),
|
||||
addref: None,
|
||||
release: None,
|
||||
abi_version: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// A fresh stack handle (refcount 1) for the calling test.
|
||||
unsafe fn new_stack() -> OakUndoStack {
|
||||
oakundo_undostack_init()
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Skeleton contract tests (ffi_contract_test.rs)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Command lifecycle: `init` returns a refcounted handle, `redo_now` marks
|
||||
/// it done (a second `redo_now` is a no-op), `undo_now` un-done it, and
|
||||
/// `free` runs `free_fn` once and clears `ctx`.
|
||||
#[test]
|
||||
fn command_lifecycle() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut cmd = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
|
||||
assert!(!cmd.ctx.is_null());
|
||||
assert_eq!(cmd.abi_version, OAKUNDO_ABI_VERSION);
|
||||
assert!(cmd.addref.is_some() && cmd.release.is_some());
|
||||
|
||||
assert_eq!(unsafe { oakundo_command_redo_now(cmd) }, OAKUNDO_OK);
|
||||
assert_eq!(events(&trace), vec!["redo:a"]);
|
||||
|
||||
// Idempotent redo.
|
||||
assert_eq!(unsafe { oakundo_command_redo_now(cmd) }, OAKUNDO_OK);
|
||||
assert_eq!(events(&trace), vec!["redo:a"]);
|
||||
|
||||
assert_eq!(unsafe { oakundo_command_undo_now(cmd) }, OAKUNDO_OK);
|
||||
assert_eq!(events(&trace), vec!["redo:a", "undo:a"]);
|
||||
|
||||
// Idempotent undo.
|
||||
assert_eq!(unsafe { oakundo_command_undo_now(cmd) }, OAKUNDO_OK);
|
||||
assert_eq!(events(&trace), vec!["redo:a", "undo:a"]);
|
||||
|
||||
// free destroys once: free_fn runs exactly once and ctx is cleared.
|
||||
unsafe { oakundo_command_free(&mut cmd) };
|
||||
assert_eq!(events(&trace), vec!["redo:a", "undo:a", "free:a"]);
|
||||
assert!(cmd.ctx.is_null());
|
||||
}
|
||||
|
||||
/// Multi command: `add_child` → `child_count` reflects it; redo runs
|
||||
/// children in order, undo in reverse order.
|
||||
#[test]
|
||||
fn multi_redo_undo_ordering() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut multi = unsafe { oakundo_command_init_multi() };
|
||||
|
||||
// add children a, b, c.
|
||||
let ca = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
let cb = unsafe { make_cmd(&mut probes[1] as *mut Probe) };
|
||||
let cc = unsafe { make_cmd(&mut probes[2] as *mut Probe) };
|
||||
assert_eq!(unsafe { oakundo_command_multi_add_child(multi, ca) }, OAKUNDO_OK);
|
||||
assert_eq!(unsafe { oakundo_command_multi_add_child(multi, cb) }, OAKUNDO_OK);
|
||||
assert_eq!(unsafe { oakundo_command_multi_add_child(multi, cc) }, OAKUNDO_OK);
|
||||
|
||||
// child_count reflects three.
|
||||
let mut count: c_int = 0;
|
||||
assert_eq!(unsafe { oakundo_command_multi_child_count(multi, &mut count) }, OAKUNDO_OK);
|
||||
assert_eq!(count, 3);
|
||||
|
||||
// redo fires in insertion order.
|
||||
assert_eq!(unsafe { oakundo_command_redo_now(multi) }, OAKUNDO_OK);
|
||||
assert_eq!(events(&trace), vec!["redo:a", "redo:b", "redo:c"]);
|
||||
|
||||
// undo fires in reverse order.
|
||||
assert_eq!(unsafe { oakundo_command_undo_now(multi) }, OAKUNDO_OK);
|
||||
assert_eq!(
|
||||
events(&trace),
|
||||
vec!["redo:a", "redo:b", "redo:c", "undo:c", "undo:b", "undo:a"]
|
||||
);
|
||||
|
||||
// Borrowed child handles are released harmlessly before the multi dies.
|
||||
let mut child0 = empty_cmd();
|
||||
assert_eq!(unsafe { oakundo_command_multi_child(multi, 0, &mut child0) }, OAKUNDO_OK);
|
||||
assert!(!child0.ctx.is_null());
|
||||
unsafe { oakundo_command_free(&mut child0) };
|
||||
|
||||
// free the multi: children are freed (shell only) without double-free.
|
||||
let mut multi_owned = multi;
|
||||
unsafe { oakundo_command_free(&mut multi_owned) };
|
||||
assert_eq!(events(&trace).iter().filter(|e| e.starts_with("free:")).count(), 3);
|
||||
}
|
||||
|
||||
/// Stack: fresh stack has one empty "New/Open Project" command, so
|
||||
/// `can_undo` is 0 and `count` is 1; pushing redoable commands grows
|
||||
/// `count` and makes `can_undo`/`can_redo` track the position.
|
||||
#[test]
|
||||
fn stack_push_undo_redo_queries() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut stack = unsafe { new_stack() };
|
||||
|
||||
let mut count: i64 = 0;
|
||||
let mut value: c_int = 0;
|
||||
assert_eq!(unsafe { oakundo_undostack_count(stack, &mut count) }, OAKUNDO_OK);
|
||||
assert_eq!(count, 1);
|
||||
assert_eq!(unsafe { oakundo_undostack_can_undo(stack, &mut value) }, OAKUNDO_OK);
|
||||
assert_eq!(value, 0);
|
||||
assert_eq!(unsafe { oakundo_undostack_can_redo(stack, &mut value) }, OAKUNDO_OK);
|
||||
assert_eq!(value, 0);
|
||||
|
||||
// Push two commands.
|
||||
let name_a = CString::new("A").unwrap();
|
||||
let mut ca = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_push(stack, ca, name_a.as_ptr()) },
|
||||
OAKUNDO_OK
|
||||
);
|
||||
unsafe { oakundo_command_free(&mut ca) }; // non-owning shell now
|
||||
let name_b = CString::new("B").unwrap();
|
||||
let mut cb = unsafe { make_cmd(&mut probes[1] as *mut Probe) };
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_push(stack, cb, name_b.as_ptr()) },
|
||||
OAKUNDO_OK
|
||||
);
|
||||
unsafe { oakundo_command_free(&mut cb) };
|
||||
|
||||
assert_eq!(events(&trace), vec!["redo:a", "redo:b"]);
|
||||
assert_eq!(unsafe { oakundo_undostack_count(stack, &mut count) }, OAKUNDO_OK);
|
||||
assert_eq!(count, 3);
|
||||
assert_eq!(unsafe { oakundo_undostack_can_undo(stack, &mut value) }, OAKUNDO_OK);
|
||||
assert_eq!(value, 1);
|
||||
assert_eq!(unsafe { oakundo_undostack_can_redo(stack, &mut value) }, OAKUNDO_OK);
|
||||
assert_eq!(value, 0);
|
||||
|
||||
// Undo moves B into the redoable tail.
|
||||
assert_eq!(unsafe { oakundo_undostack_undo(stack) }, OAKUNDO_OK);
|
||||
assert_eq!(events(&trace), vec!["redo:a", "redo:b", "undo:b"]);
|
||||
assert_eq!(unsafe { oakundo_undostack_can_undo(stack, &mut value) }, OAKUNDO_OK);
|
||||
assert_eq!(value, 1);
|
||||
assert_eq!(unsafe { oakundo_undostack_can_redo(stack, &mut value) }, OAKUNDO_OK);
|
||||
assert_eq!(value, 1);
|
||||
|
||||
// Redo restores.
|
||||
assert_eq!(unsafe { oakundo_undostack_redo(stack) }, OAKUNDO_OK);
|
||||
assert_eq!(events(&trace), vec!["redo:a", "redo:b", "undo:b", "redo:b"]);
|
||||
assert_eq!(unsafe { oakundo_undostack_can_redo(stack, &mut value) }, OAKUNDO_OK);
|
||||
assert_eq!(value, 0);
|
||||
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
/// `can_redo`/`index` after undo and redo; `jump(0)` clamps to the
|
||||
/// bottom empty command without spinning; `jump` beyond the top is a no-op.
|
||||
#[test]
|
||||
fn stack_jump_clamps() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut stack = unsafe { new_stack() };
|
||||
|
||||
let na = CString::new("A").unwrap();
|
||||
let nb = CString::new("B").unwrap();
|
||||
let nc = CString::new("C").unwrap();
|
||||
let mut ca = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
unsafe { oakundo_undostack_push(stack, ca, na.as_ptr()) };
|
||||
let mut cb = unsafe { make_cmd(&mut probes[1] as *mut Probe) };
|
||||
unsafe { oakundo_undostack_push(stack, cb, nb.as_ptr()) };
|
||||
let mut cc = unsafe { make_cmd(&mut probes[2] as *mut Probe) };
|
||||
unsafe { oakundo_undostack_push(stack, cc, nc.as_ptr()) };
|
||||
|
||||
let mut index: i64 = 0;
|
||||
assert_eq!(unsafe { oakundo_undostack_index(stack, &mut index) }, OAKUNDO_OK);
|
||||
assert_eq!(index, 4);
|
||||
|
||||
// jump to the bottom (0): undo all three; negative clamps to 0 too.
|
||||
assert_eq!(unsafe { oakundo_undostack_jump(stack, 0) }, OAKUNDO_OK);
|
||||
assert_eq!(unsafe { oakundo_undostack_index(stack, &mut index) }, OAKUNDO_OK);
|
||||
assert_eq!(index, 1);
|
||||
assert_eq!(
|
||||
events(&trace),
|
||||
vec!["redo:a", "redo:b", "redo:c", "undo:c", "undo:b", "undo:a"]
|
||||
);
|
||||
|
||||
// jump back up to 3: redo a and b (c was already undone to reach 1).
|
||||
assert_eq!(unsafe { oakundo_undostack_jump(stack, 3) }, OAKUNDO_OK);
|
||||
assert_eq!(unsafe { oakundo_undostack_index(stack, &mut index) }, OAKUNDO_OK);
|
||||
assert_eq!(index, 3);
|
||||
|
||||
// jump beyond the top redoes up to the top (index 4), matching the C++
|
||||
// `jump` (undone commands are redoable, so the second loop runs).
|
||||
assert_eq!(unsafe { oakundo_undostack_jump(stack, 100) }, OAKUNDO_OK);
|
||||
assert_eq!(unsafe { oakundo_undostack_index(stack, &mut index) }, OAKUNDO_OK);
|
||||
assert_eq!(index, 4);
|
||||
|
||||
// Negative index clamps to the bottom.
|
||||
assert_eq!(unsafe { oakundo_undostack_jump(stack, -5) }, OAKUNDO_OK);
|
||||
assert_eq!(unsafe { oakundo_undostack_index(stack, &mut index) }, OAKUNDO_OK);
|
||||
assert_eq!(index, 1);
|
||||
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
/// `push_pre_executed` records without redoing (stays undoable); empty
|
||||
/// multi commands are discarded on push.
|
||||
#[test]
|
||||
fn stack_pre_executed_and_empty_multi() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut stack = unsafe { new_stack() };
|
||||
|
||||
// push_pre_executed: no redo callback.
|
||||
let name = CString::new("Pre").unwrap();
|
||||
let mut cp = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_push_pre_executed(stack, cp, name.as_ptr()) },
|
||||
OAKUNDO_OK
|
||||
);
|
||||
unsafe { oakundo_command_free(&mut cp) };
|
||||
assert_eq!(events(&trace), Vec::<String>::new());
|
||||
|
||||
// The pre-executed command is recorded as done (undoable).
|
||||
let mut done: c_int = 0;
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_command_is_done(stack, 1, &mut done) },
|
||||
OAKUNDO_OK
|
||||
);
|
||||
assert_eq!(done, 1);
|
||||
|
||||
let mut count: i64 = 0;
|
||||
assert_eq!(unsafe { oakundo_undostack_count(stack, &mut count) }, OAKUNDO_OK);
|
||||
assert_eq!(count, 2);
|
||||
|
||||
// Undoing the pre-executed command still runs its undo callback.
|
||||
assert_eq!(unsafe { oakundo_undostack_undo(stack) }, OAKUNDO_OK);
|
||||
assert_eq!(events(&trace), vec!["undo:a"]);
|
||||
|
||||
// Empty multi command is discarded on push: count stays 2 (bottom
|
||||
// "New/Open Project" + the undone pre-executed command), not 3.
|
||||
let mut multi = unsafe { oakundo_command_init_multi() };
|
||||
let name2 = CString::new("Empty").unwrap();
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_push(stack, multi, name2.as_ptr()) },
|
||||
OAKUNDO_OK
|
||||
);
|
||||
unsafe { oakundo_command_free(&mut multi) };
|
||||
assert_eq!(unsafe { oakundo_undostack_count(stack, &mut count) }, OAKUNDO_OK);
|
||||
assert_eq!(count, 2, "empty multi is discarded on push");
|
||||
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
/// Every handle-returning export returns `ctx == NULL` on failure and a
|
||||
/// valid handle (`abi_version` stamped) on success; `free(NULL)` /
|
||||
/// `free(empty)` are no-ops across command and stack families.
|
||||
#[test]
|
||||
fn handle_and_free_contract() {
|
||||
let (_trace, mut probes) = setup();
|
||||
|
||||
// init with a NULL vtable → empty handle.
|
||||
let empty = unsafe { oakundo_command_init(std::ptr::null(), std::ptr::null_mut()) };
|
||||
assert!(empty.ctx.is_null());
|
||||
assert_eq!(empty.abi_version, 0);
|
||||
assert!(empty.addref.is_none() && empty.release.is_none());
|
||||
|
||||
// init with a valid vtable → stamped handle.
|
||||
let mut cmd = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
assert!(!cmd.ctx.is_null());
|
||||
assert_eq!(cmd.abi_version, OAKUNDO_ABI_VERSION);
|
||||
|
||||
// init_multi → stamped handle.
|
||||
let mut multi = unsafe { oakundo_command_init_multi() };
|
||||
assert!(!multi.ctx.is_null());
|
||||
assert_eq!(multi.abi_version, OAKUNDO_ABI_VERSION);
|
||||
|
||||
// init stack → stamped handle.
|
||||
let mut stack = unsafe { new_stack() };
|
||||
assert!(!stack.ctx.is_null());
|
||||
assert_eq!(stack.abi_version, OAKUNDO_ABI_VERSION);
|
||||
|
||||
// free(NULL) is a no-op for both families.
|
||||
unsafe { oakundo_command_free(std::ptr::null_mut()) };
|
||||
unsafe { oakundo_undostack_free(std::ptr::null_mut()) };
|
||||
|
||||
// free(empty handle value) is a no-op.
|
||||
let mut ecmd = empty_cmd();
|
||||
unsafe { oakundo_command_free(&mut ecmd) };
|
||||
assert!(ecmd.ctx.is_null());
|
||||
let mut estack = empty_stack();
|
||||
unsafe { oakundo_undostack_free(&mut estack) };
|
||||
assert!(estack.ctx.is_null());
|
||||
|
||||
// free(valid) clears ctx.
|
||||
unsafe { oakundo_command_free(&mut multi) };
|
||||
assert!(multi.ctx.is_null());
|
||||
unsafe { oakundo_command_free(&mut cmd) };
|
||||
assert!(cmd.ctx.is_null());
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
assert!(stack.ctx.is_null());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Exhaustive per-export success/failure coverage
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn command_redo_undo_now_null_is_invalid() {
|
||||
assert_eq!(unsafe { oakundo_command_redo_now(empty_cmd()) }, OAKUNDO_E_INVALID);
|
||||
assert_eq!(unsafe { oakundo_command_undo_now(empty_cmd()) }, OAKUNDO_E_INVALID);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn command_multi_add_child_errors() {
|
||||
let (_trace, mut probes) = setup();
|
||||
let mut multi = unsafe { oakundo_command_init_multi() };
|
||||
let mut child = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
let mut vtable_cmd = unsafe { make_cmd(&mut probes[1] as *mut Probe) };
|
||||
|
||||
// multi is null → E_INVALID.
|
||||
assert_eq!(unsafe { oakundo_command_multi_add_child(empty_cmd(), child) }, OAKUNDO_E_INVALID);
|
||||
// child is null → E_INVALID (nothing taken).
|
||||
assert_eq!(unsafe { oakundo_command_multi_add_child(multi, empty_cmd()) }, OAKUNDO_E_INVALID);
|
||||
// target is a vtable command, not a multi → E_INVALID.
|
||||
assert_eq!(unsafe { oakundo_command_multi_add_child(vtable_cmd, child) }, OAKUNDO_E_INVALID);
|
||||
|
||||
// The child handle still owns its value (never taken), so free it.
|
||||
unsafe { oakundo_command_free(&mut child) };
|
||||
unsafe { oakundo_command_free(&mut vtable_cmd) };
|
||||
unsafe { oakundo_command_free(&mut multi) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn command_multi_child_count_errors() {
|
||||
let (_trace, mut probes) = setup();
|
||||
let mut multi = unsafe { oakundo_command_init_multi() };
|
||||
let mut vtable_cmd = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
let mut out: c_int = -1;
|
||||
|
||||
// null out pointer → E_INVALID.
|
||||
assert_eq!(unsafe { oakundo_command_multi_child_count(multi, std::ptr::null_mut()) }, OAKUNDO_E_INVALID);
|
||||
// null multi → E_INVALID.
|
||||
assert_eq!(unsafe { oakundo_command_multi_child_count(empty_cmd(), &mut out) }, OAKUNDO_E_INVALID);
|
||||
// non-multi command → E_INVALID.
|
||||
assert_eq!(unsafe { oakundo_command_multi_child_count(vtable_cmd, &mut out) }, OAKUNDO_E_INVALID);
|
||||
|
||||
unsafe { oakundo_command_free(&mut vtable_cmd) };
|
||||
unsafe { oakundo_command_free(&mut multi) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn command_multi_child_errors() {
|
||||
let (_trace, mut probes) = setup();
|
||||
let mut multi = unsafe { oakundo_command_init_multi() };
|
||||
let mut vtable_cmd = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
let mut out = empty_cmd();
|
||||
|
||||
// null out pointer → E_INVALID.
|
||||
assert_eq!(unsafe { oakundo_command_multi_child(multi, 0, std::ptr::null_mut()) }, OAKUNDO_E_INVALID);
|
||||
// null multi → E_INVALID.
|
||||
assert_eq!(unsafe { oakundo_command_multi_child(empty_cmd(), 0, &mut out) }, OAKUNDO_E_INVALID);
|
||||
// non-multi command → E_INVALID.
|
||||
assert_eq!(unsafe { oakundo_command_multi_child(vtable_cmd, 0, &mut out) }, OAKUNDO_E_INVALID);
|
||||
// empty multi, negative index → E_NOT_FOUND.
|
||||
assert_eq!(unsafe { oakundo_command_multi_child(multi, -1, &mut out) }, OAKUNDO_E_NOT_FOUND);
|
||||
// empty multi, positive OOB → E_NOT_FOUND.
|
||||
assert_eq!(unsafe { oakundo_command_multi_child(multi, 5, &mut out) }, OAKUNDO_E_NOT_FOUND);
|
||||
|
||||
unsafe { oakundo_command_free(&mut vtable_cmd) };
|
||||
unsafe { oakundo_command_free(&mut multi) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn command_multi_child_success_borrowed() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut multi = unsafe { oakundo_command_init_multi() };
|
||||
let mut ca = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
unsafe { oakundo_command_multi_add_child(multi, ca) };
|
||||
|
||||
let mut child = empty_cmd();
|
||||
assert_eq!(unsafe { oakundo_command_multi_child(multi, 0, &mut child) }, OAKUNDO_OK);
|
||||
assert!(!child.ctx.is_null());
|
||||
// A borrowed child is not independently owned: freeing it frees only the
|
||||
// shell and must not free the child the multi still owns.
|
||||
unsafe { oakundo_command_free(&mut child) };
|
||||
assert!(child.ctx.is_null());
|
||||
|
||||
// Redo through the multi still works and no child was freed.
|
||||
assert_eq!(unsafe { oakundo_command_redo_now(multi) }, OAKUNDO_OK);
|
||||
assert_eq!(events(&trace), vec!["redo:a"]);
|
||||
|
||||
unsafe { oakundo_command_free(&mut multi) };
|
||||
assert_eq!(events(&trace), vec!["redo:a", "free:a"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn command_free_fires_exactly_once() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut cmd = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
|
||||
// free → one free callback.
|
||||
unsafe { oakundo_command_free(&mut cmd) };
|
||||
assert_eq!(events(&trace), vec!["free:a"]);
|
||||
|
||||
// free again on a cleared handle → no-op.
|
||||
unsafe { oakundo_command_free(&mut cmd) };
|
||||
assert_eq!(events(&trace), vec!["free:a"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undostack_free_null_and_empty() {
|
||||
let mut stack = empty_stack();
|
||||
unsafe { oakundo_undostack_free(std::ptr::null_mut()) };
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
assert!(stack.ctx.is_null());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undostack_push_errors() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut stack = unsafe { new_stack() };
|
||||
let mut cmd = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
let name = CString::new("A").unwrap();
|
||||
|
||||
// null command → E_INVALID; stack untouched, command still owned.
|
||||
assert_eq!(unsafe { oakundo_undostack_push(stack, empty_cmd(), name.as_ptr()) }, OAKUNDO_E_INVALID);
|
||||
// empty stack → E_INVALID; command NOT drained (still owns its value).
|
||||
assert_eq!(unsafe { oakundo_undostack_push(empty_stack(), cmd, name.as_ptr()) }, OAKUNDO_E_INVALID);
|
||||
assert_eq!(events(&trace), Vec::<String>::new(), "no callbacks ran on failed push");
|
||||
|
||||
// NULL name is accepted as an empty label → OK.
|
||||
assert_eq!(unsafe { oakundo_undostack_push(stack, cmd, std::ptr::null()) }, OAKUNDO_OK);
|
||||
|
||||
unsafe { oakundo_command_free(&mut cmd) }; // now a non-owning shell
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undostack_push_pre_executed_errors() {
|
||||
let (_trace, mut probes) = setup();
|
||||
let mut stack = unsafe { new_stack() };
|
||||
let mut cmd = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
let name = CString::new("A").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_push_pre_executed(empty_stack(), cmd, name.as_ptr()) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
// command was not drained; still owned, so free it.
|
||||
unsafe { oakundo_command_free(&mut cmd) };
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_push_pre_executed(stack, empty_cmd(), name.as_ptr()) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undostack_undo_redo_errors() {
|
||||
let mut stack = unsafe { new_stack() };
|
||||
assert_eq!(unsafe { oakundo_undostack_undo(empty_stack()) }, OAKUNDO_E_INVALID);
|
||||
assert_eq!(unsafe { oakundo_undostack_redo(empty_stack()) }, OAKUNDO_E_INVALID);
|
||||
// Valid stack, but nothing to undo: still OK (no-op).
|
||||
assert_eq!(unsafe { oakundo_undostack_undo(stack) }, OAKUNDO_OK);
|
||||
assert_eq!(unsafe { oakundo_undostack_redo(stack) }, OAKUNDO_OK);
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undostack_jump_clear_errors() {
|
||||
let mut stack = unsafe { new_stack() };
|
||||
assert_eq!(unsafe { oakundo_undostack_jump(empty_stack(), 3) }, OAKUNDO_E_INVALID);
|
||||
assert_eq!(unsafe { oakundo_undostack_clear(empty_stack()) }, OAKUNDO_E_INVALID);
|
||||
|
||||
// Clear on a valid stack resets to the empty bottom command.
|
||||
let mut count: i64 = 0;
|
||||
unsafe { oakundo_undostack_clear(stack) };
|
||||
assert_eq!(unsafe { oakundo_undostack_count(stack, &mut count) }, OAKUNDO_OK);
|
||||
assert_eq!(count, 1);
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undostack_can_undo_redo_errors() {
|
||||
let mut stack = unsafe { new_stack() };
|
||||
let mut value: c_int = 0;
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_can_undo(empty_stack(), &mut value) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_can_redo(empty_stack(), &mut value) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_can_undo(stack, std::ptr::null_mut()) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_can_redo(stack, std::ptr::null_mut()) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undostack_count_index_errors() {
|
||||
let mut stack = unsafe { new_stack() };
|
||||
let mut out: i64 = 0;
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_count(empty_stack(), &mut out) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_index(empty_stack(), &mut out) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_count(stack, std::ptr::null_mut()) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_index(stack, std::ptr::null_mut()) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undostack_command_text_two_stage_and_errors() {
|
||||
let (_trace, mut probes) = setup();
|
||||
let mut stack = unsafe { new_stack() };
|
||||
let name = CString::new("MyAction").unwrap();
|
||||
let mut cmd = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
unsafe { oakundo_undostack_push(stack, cmd, name.as_ptr()) };
|
||||
|
||||
// Failure: empty stack → E_INVALID.
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_command_text(empty_stack(), 1, std::ptr::null_mut(), 0) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
// Failure: OOB row (positive) → E_NOT_FOUND.
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_command_text(stack, 5, std::ptr::null_mut(), 0) },
|
||||
OAKUNDO_E_NOT_FOUND
|
||||
);
|
||||
// Failure: negative row → E_NOT_FOUND.
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_command_text(stack, -1, std::ptr::null_mut(), 0) },
|
||||
OAKUNDO_E_NOT_FOUND
|
||||
);
|
||||
|
||||
// Stage one: null buffer returns the required size ("MyAction" + NUL).
|
||||
let required = unsafe { oakundo_undostack_command_text(stack, 1, std::ptr::null_mut(), 0) };
|
||||
assert_eq!(required, "MyAction".len() as c_int + 1);
|
||||
|
||||
// Stage two: a buffer of that size is populated with a NUL-terminated
|
||||
// string and the required size is returned again.
|
||||
let mut buf = vec![0u8; required as usize];
|
||||
let ret = unsafe {
|
||||
oakundo_undostack_command_text(stack, 1, buf.as_mut_ptr() as *mut c_char, required)
|
||||
};
|
||||
assert_eq!(ret, required);
|
||||
let actual = unsafe { std::ffi::CStr::from_ptr(buf.as_ptr() as *const c_char) };
|
||||
assert_eq!(actual.to_bytes(), b"MyAction");
|
||||
|
||||
// A too-small buffer is safely truncated (NUL-terminated).
|
||||
let mut small = vec![0xffu8; 3];
|
||||
unsafe { oakundo_undostack_command_text(stack, 1, small.as_mut_ptr() as *mut c_char, 3) };
|
||||
assert_eq!(small, [b'M', b'y', 0]);
|
||||
|
||||
unsafe { oakundo_command_free(&mut cmd) };
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undostack_command_is_done_errors() {
|
||||
let mut stack = unsafe { new_stack() };
|
||||
let mut value: c_int = 0;
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_command_is_done(empty_stack(), 0, &mut value) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_command_is_done(stack, 0, std::ptr::null_mut()) },
|
||||
OAKUNDO_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_command_is_done(stack, 5, &mut value) },
|
||||
OAKUNDO_E_NOT_FOUND
|
||||
);
|
||||
assert_eq!(
|
||||
unsafe { oakundo_undostack_command_is_done(stack, -1, &mut value) },
|
||||
OAKUNDO_E_NOT_FOUND
|
||||
);
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undostack_push_drops_redoable_tail_via_ffi() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut stack = unsafe { new_stack() };
|
||||
let na = CString::new("A").unwrap();
|
||||
let nb = CString::new("B").unwrap();
|
||||
let mut ca = unsafe { make_cmd(&mut probes[0] as *mut Probe) };
|
||||
unsafe { oakundo_undostack_push(stack, ca, na.as_ptr()) };
|
||||
let mut cb = unsafe { make_cmd(&mut probes[1] as *mut Probe) };
|
||||
unsafe { oakundo_undostack_push(stack, cb, nb.as_ptr()) };
|
||||
|
||||
// Undo B, then push C → the redoable tail is dropped.
|
||||
assert_eq!(unsafe { oakundo_undostack_undo(stack) }, OAKUNDO_OK);
|
||||
let mut value: c_int = 0;
|
||||
assert_eq!(unsafe { oakundo_undostack_can_redo(stack, &mut value) }, OAKUNDO_OK);
|
||||
assert_eq!(value, 1);
|
||||
|
||||
let nc = CString::new("C").unwrap();
|
||||
let mut cc = unsafe { make_cmd(&mut probes[2] as *mut Probe) };
|
||||
assert_eq!(unsafe { oakundo_undostack_push(stack, cc, nc.as_ptr()) }, OAKUNDO_OK);
|
||||
|
||||
assert_eq!(unsafe { oakundo_undostack_can_redo(stack, &mut value) }, OAKUNDO_OK);
|
||||
assert_eq!(value, 0, "pushing drops the redoable tail");
|
||||
assert_eq!(
|
||||
events(&trace),
|
||||
vec!["redo:a", "redo:b", "undo:b", "free:b", "redo:c"]
|
||||
);
|
||||
|
||||
unsafe { oakundo_command_free(&mut ca) };
|
||||
unsafe { oakundo_command_free(&mut cb) };
|
||||
unsafe { oakundo_command_free(&mut cc) };
|
||||
unsafe { oakundo_undostack_free(&mut stack) };
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Tests for the refcounted-handle scaffolding (`handle.rs`).
|
||||
|
||||
use std::ffi::c_void;
|
||||
use std::sync::atomic::{AtomicI32, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use oakundo::error::{OAKUNDO_E_FAILED, OAKUNDO_E_INVALID, OAKUNDO_OK, Result};
|
||||
use oakundo::handle::{guard, guard_handle, guard_void, make_borrowed, make_owned, get, CHandle};
|
||||
|
||||
/// A value whose `Drop` signals through a shared counter.
|
||||
struct DropProbe {
|
||||
counter: Arc<AtomicI32>,
|
||||
}
|
||||
|
||||
impl DropProbe {
|
||||
fn new() -> (Self, Arc<AtomicI32>) {
|
||||
let counter = Arc::new(AtomicI32::new(1));
|
||||
(
|
||||
DropProbe {
|
||||
counter: counter.clone(),
|
||||
},
|
||||
counter,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DropProbe {
|
||||
fn drop(&mut self) {
|
||||
self.counter.fetch_sub(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn null_handle_is_all_zero() {
|
||||
let h = CHandle::null();
|
||||
assert!(h.is_null());
|
||||
assert!(h.ctx.is_null());
|
||||
assert!(h.addref.is_none());
|
||||
assert!(h.release.is_none());
|
||||
assert_eq!(h.abi_version, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_owned_counts_and_releases() {
|
||||
let (value, counter) = DropProbe::new();
|
||||
let h = make_owned(value);
|
||||
|
||||
assert!(!h.is_null());
|
||||
assert_eq!(h.abi_version, oakundo::handle::OAKUNDO_ABI_VERSION);
|
||||
|
||||
// addref bumps the count; a second release does not double-drop.
|
||||
let addref = h.addref.unwrap();
|
||||
let release = h.release.unwrap();
|
||||
unsafe { addref(h.ctx) };
|
||||
unsafe { release(h.ctx) };
|
||||
assert_eq!(counter.load(Ordering::SeqCst), 1, "still alive after one release");
|
||||
|
||||
// Final release drops the box.
|
||||
unsafe { release(h.ctx) };
|
||||
assert_eq!(counter.load(Ordering::SeqCst), 0, "dropped at refcount zero");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_owned_get_round_trips() {
|
||||
let h = make_owned(42i32);
|
||||
let ch = &h;
|
||||
let v = unsafe { get::<i32>(ch) };
|
||||
assert_eq!(v, Some(&42));
|
||||
|
||||
let empty = CHandle::null();
|
||||
assert_eq!(unsafe { get::<i32>(&empty) }, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn borrowed_release_does_not_free_pointee() {
|
||||
let (mut value, counter) = DropProbe::new();
|
||||
let b = unsafe { make_borrowed(&mut value as *mut DropProbe) };
|
||||
|
||||
let release = b.release.unwrap();
|
||||
unsafe { release(b.ctx) };
|
||||
assert_eq!(
|
||||
counter.load(Ordering::SeqCst),
|
||||
1,
|
||||
"borrowed release frees only the shell"
|
||||
);
|
||||
|
||||
// The owned value is still usable and is dropped normally.
|
||||
drop(value);
|
||||
assert_eq!(counter.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guard_success_error_and_panic() {
|
||||
fn ok() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
fn invalid() -> Result<()> {
|
||||
Err(oakundo::error::Error::Invalid)
|
||||
}
|
||||
fn panic() -> Result<()> {
|
||||
panic!("boom")
|
||||
}
|
||||
assert_eq!(guard(ok), OAKUNDO_OK);
|
||||
assert_eq!(guard(invalid), OAKUNDO_E_INVALID);
|
||||
assert_eq!(guard(panic), OAKUNDO_E_FAILED);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guard_handle_returns_value_or_null() {
|
||||
let h = guard_handle(|| Ok(make_owned(5i32)));
|
||||
assert!(!h.is_null());
|
||||
assert_eq!(h.abi_version, oakundo::handle::OAKUNDO_ABI_VERSION);
|
||||
|
||||
let on_err = guard_handle(|| Err(oakundo::error::Error::NotFound));
|
||||
assert!(on_err.is_null());
|
||||
|
||||
let on_panic = guard_handle(|| -> Result<CHandle> { panic!("boom") });
|
||||
assert!(on_panic.is_null());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guard_void_swallows_panic() {
|
||||
let mut ran = false;
|
||||
guard_void(|| ran = true);
|
||||
assert!(ran);
|
||||
|
||||
guard_void(|| panic!("swallowed"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn addref_release_null_ctx_is_noop() {
|
||||
let h = make_owned(7i32);
|
||||
unsafe { (h.addref.unwrap())(std::ptr::null_mut()) };
|
||||
unsafe { (h.release.unwrap())(std::ptr::null_mut()) };
|
||||
assert!(!h.is_null());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn borrowed_handle_binds_to_correct_type() {
|
||||
// The borrowed handle boxes a `*mut T`; `get` for that type sees the
|
||||
// raw pointer value.
|
||||
let mut value = 99u64;
|
||||
let h = unsafe { make_borrowed(&mut value as *mut u64) };
|
||||
let ch = &h;
|
||||
let p = unsafe { get::<*mut u64>(ch) }.unwrap();
|
||||
assert_eq!(unsafe { **p }, 99);
|
||||
}
|
||||
|
||||
/// `*mut c_void` helper used to drive callbacks directly in one test.
|
||||
#[allow(dead_code)]
|
||||
fn _as_void<T>(p: *mut T) -> *mut c_void {
|
||||
p as *mut c_void
|
||||
}
|
||||
@@ -0,0 +1,356 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Safe-layer behavior matrix for `UndoCommand` / `UndoStack`
|
||||
//! (mirrors `src/undo/src/undostack.cpp` / `undocommand.cpp`).
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::ffi::c_void;
|
||||
|
||||
use oakundo::error::Error;
|
||||
use oakundo::undocommand::{OakUndoCommandVtable, UndoCommand};
|
||||
use oakundo::undostack::{EmptyCommand, UndoStack, K_MAX_UNDO_COMMANDS};
|
||||
|
||||
/// Shared event recorder driven through vtable callbacks.
|
||||
struct Trace {
|
||||
events: RefCell<Vec<String>>,
|
||||
}
|
||||
|
||||
/// Per-command callback payload: a name and a shared trace.
|
||||
struct Probe {
|
||||
name: &'static str,
|
||||
trace: *const Trace,
|
||||
}
|
||||
|
||||
unsafe extern "C" fn probe_redo(u: *mut c_void) {
|
||||
let p = unsafe { &mut *(u as *mut Probe) };
|
||||
let trace = unsafe { &*p.trace };
|
||||
trace.events.borrow_mut().push(format!("redo:{}", p.name));
|
||||
}
|
||||
|
||||
unsafe extern "C" fn probe_undo(u: *mut c_void) {
|
||||
let p = unsafe { &mut *(u as *mut Probe) };
|
||||
let trace = unsafe { &*p.trace };
|
||||
trace.events.borrow_mut().push(format!("undo:{}", p.name));
|
||||
}
|
||||
|
||||
/// A vtable-backed command whose callbacks record into `probe`.
|
||||
fn trace_cmd(probe: &mut Probe) -> UndoCommand {
|
||||
let vtable = OakUndoCommandVtable {
|
||||
redo: Some(probe_redo),
|
||||
undo: Some(probe_undo),
|
||||
free_fn: None,
|
||||
};
|
||||
UndoCommand::from_vtable(vtable, probe as *mut Probe as *mut c_void)
|
||||
}
|
||||
|
||||
fn events(trace: &Trace) -> Vec<String> {
|
||||
trace.events.borrow().clone()
|
||||
}
|
||||
|
||||
fn setup() -> (Box<Trace>, Vec<Probe>) {
|
||||
let trace = Box::new(Trace {
|
||||
events: RefCell::new(Vec::new()),
|
||||
});
|
||||
let ptr = &*trace as *const Trace;
|
||||
let mut probes = Vec::new();
|
||||
for name in ["a", "b", "c"] {
|
||||
probes.push(Probe {
|
||||
name,
|
||||
trace: ptr,
|
||||
});
|
||||
}
|
||||
(trace, probes)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn command_redo_undo_lifecycle() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut cmd = trace_cmd(&mut probes[0]);
|
||||
|
||||
assert!(!cmd.is_done());
|
||||
assert!(cmd.has_prepared());
|
||||
assert_eq!(events(&trace), Vec::<String>::new());
|
||||
|
||||
cmd.redo_now();
|
||||
assert!(cmd.is_done());
|
||||
assert_eq!(events(&trace), vec!["redo:a"]);
|
||||
|
||||
// Idempotent: a second redo does nothing.
|
||||
cmd.redo_now();
|
||||
assert_eq!(events(&trace), vec!["redo:a"]);
|
||||
|
||||
cmd.undo_now();
|
||||
assert!(!cmd.is_done());
|
||||
assert_eq!(events(&trace), vec!["redo:a", "undo:a"]);
|
||||
|
||||
// Idempotent undo.
|
||||
cmd.undo_now();
|
||||
assert_eq!(events(&trace), vec!["redo:a", "undo:a"]);
|
||||
|
||||
// redo_and_set_modified / undo_and_set_modified alias the *_now forms.
|
||||
cmd.redo_and_set_modified();
|
||||
cmd.undo_and_set_modified();
|
||||
assert_eq!(
|
||||
events(&trace),
|
||||
vec!["redo:a", "undo:a", "redo:a", "undo:a"]
|
||||
);
|
||||
|
||||
// set_done marks executed without running anything.
|
||||
let mut probe = Probe {
|
||||
name: "x",
|
||||
trace: probes[0].trace,
|
||||
};
|
||||
let mut cmd2 = trace_cmd(&mut probe);
|
||||
cmd2.set_done(true);
|
||||
assert!(cmd2.is_done());
|
||||
cmd2.redo_now(); // no-op (already done)
|
||||
assert_eq!(events(&trace), vec!["redo:a", "undo:a", "redo:a", "undo:a"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_command_is_noop() {
|
||||
let mut cmd = EmptyCommand::new();
|
||||
assert!(cmd.has_prepared());
|
||||
cmd.redo_now();
|
||||
assert!(cmd.is_done());
|
||||
cmd.undo_now();
|
||||
assert!(!cmd.is_done());
|
||||
// No panics, no side effects.
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_redo_undo_ordering() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut multi = UndoCommand::multi();
|
||||
multi.multi_add_child(trace_cmd(&mut probes[0]));
|
||||
multi.multi_add_child(trace_cmd(&mut probes[1]));
|
||||
multi.multi_add_child(trace_cmd(&mut probes[2]));
|
||||
|
||||
assert_eq!(multi.multi_child_count(), 3);
|
||||
// Children are reachable and named.
|
||||
assert_eq!(multi.multi_child(0).unwrap().is_done(), false);
|
||||
assert_eq!(multi.multi_child(2).unwrap().is_done(), false);
|
||||
assert!(matches!(multi.multi_child(3), Err(Error::NotFound)));
|
||||
|
||||
// Redo in insertion order; undo in reverse.
|
||||
multi.redo_now();
|
||||
assert!(multi.is_done());
|
||||
assert_eq!(events(&trace), vec!["redo:a", "redo:b", "redo:c"]);
|
||||
|
||||
multi.undo_now();
|
||||
assert_eq!(
|
||||
events(&trace),
|
||||
vec!["redo:a", "redo:b", "redo:c", "undo:c", "undo:b", "undo:a"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn multi_add_child_on_vtable_panics() {
|
||||
let (_trace, mut probes) = setup();
|
||||
let mut vtable_cmd = trace_cmd(&mut probes[0]);
|
||||
vtable_cmd.multi_add_child(trace_cmd(&mut probes[1]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_child_helpers_on_non_multi() {
|
||||
let (_trace, mut probes) = setup();
|
||||
let mut vtable_cmd = trace_cmd(&mut probes[0]);
|
||||
assert_eq!(vtable_cmd.multi_child_count(), 0);
|
||||
assert!(matches!(vtable_cmd.multi_child(0), Err(Error::Invalid)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stack_new_has_empty_bottom() {
|
||||
let s = UndoStack::new();
|
||||
assert_eq!(s.command_count(), 1);
|
||||
assert_eq!(s.done_count(), 1);
|
||||
assert!(!s.can_undo());
|
||||
assert!(!s.can_redo());
|
||||
assert_eq!(s.command_name(0).unwrap(), "New/Open Project");
|
||||
assert_eq!(s.command_is_done(0).unwrap(), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stack_push_undo_redo_branch() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut s = UndoStack::new();
|
||||
|
||||
s.push(trace_cmd(&mut probes[0]), "A");
|
||||
s.push(trace_cmd(&mut probes[1]), "B");
|
||||
assert_eq!(s.command_count(), 3);
|
||||
assert!(s.can_undo());
|
||||
assert!(!s.can_redo());
|
||||
assert_eq!(events(&trace), vec!["redo:a", "redo:b"]);
|
||||
|
||||
// Undo the top.
|
||||
s.undo().unwrap();
|
||||
assert_eq!(events(&trace), vec!["redo:a", "redo:b", "undo:b"]);
|
||||
assert!(s.can_redo());
|
||||
assert_eq!(s.done_count(), 2);
|
||||
assert_eq!(s.command_name(2).unwrap(), "B"); // undone row is still labeled
|
||||
assert_eq!(s.command_is_done(2).unwrap(), false);
|
||||
|
||||
// Redo restores.
|
||||
s.redo().unwrap();
|
||||
assert_eq!(
|
||||
events(&trace),
|
||||
vec!["redo:a", "redo:b", "undo:b", "redo:b"]
|
||||
);
|
||||
assert_eq!(s.done_count(), 3);
|
||||
|
||||
// Undo then push drops the redoable tail.
|
||||
s.undo().unwrap();
|
||||
assert!(s.can_redo());
|
||||
s.push(trace_cmd(&mut probes[2]), "C");
|
||||
assert!(!s.can_redo(), "pushing drops redoable tail");
|
||||
assert_eq!(s.command_count(), 3);
|
||||
assert_eq!(s.command_name(2).unwrap(), "C");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stack_undo_redo_noop_when_invalid() {
|
||||
let mut s = UndoStack::new();
|
||||
// Undo on a stack with only the bottom empty command is a no-op.
|
||||
s.undo().unwrap();
|
||||
assert_eq!(s.command_count(), 1);
|
||||
assert!(!s.can_redo());
|
||||
s.redo().unwrap();
|
||||
assert_eq!(s.command_count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stack_jump_clamps_and_lands() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut s = UndoStack::new();
|
||||
s.push(trace_cmd(&mut probes[0]), "A");
|
||||
s.push(trace_cmd(&mut probes[1]), "B");
|
||||
s.push(trace_cmd(&mut probes[2]), "C");
|
||||
assert_eq!(s.done_count(), 4); // empty + A + B + C
|
||||
|
||||
// Jump back to just the empty command (clamps negative to 0).
|
||||
s.jump(0);
|
||||
assert_eq!(s.done_count(), 1);
|
||||
assert!(!s.can_undo());
|
||||
assert!(s.can_redo());
|
||||
assert_eq!(
|
||||
events(&trace),
|
||||
vec![
|
||||
"redo:a",
|
||||
"redo:b",
|
||||
"redo:c",
|
||||
"undo:c",
|
||||
"undo:b",
|
||||
"undo:a"
|
||||
]
|
||||
);
|
||||
|
||||
// Jump forward redoes in order.
|
||||
s.jump(3);
|
||||
assert_eq!(s.done_count(), 3);
|
||||
assert_eq!(
|
||||
events(&trace),
|
||||
vec![
|
||||
"redo:a",
|
||||
"redo:b",
|
||||
"redo:c",
|
||||
"undo:c",
|
||||
"undo:b",
|
||||
"undo:a",
|
||||
"redo:a",
|
||||
"redo:b"
|
||||
]
|
||||
);
|
||||
|
||||
// Jump beyond the top redoes up to the top (matches the C++ `jump`:
|
||||
// the undone C is redoable, so the second loop runs).
|
||||
s.jump(100);
|
||||
assert_eq!(s.done_count(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stack_discards_empty_multi() {
|
||||
let mut s = UndoStack::new();
|
||||
let empty_multi = UndoCommand::multi();
|
||||
s.push(empty_multi, "empty");
|
||||
assert_eq!(
|
||||
s.command_count(),
|
||||
1,
|
||||
"empty multi command is dropped on push"
|
||||
);
|
||||
|
||||
s.push_pre_executed(UndoCommand::multi(), "empty2");
|
||||
assert_eq!(s.command_count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stack_push_pre_executed_skips_redo() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut s = UndoStack::new();
|
||||
s.push_pre_executed(trace_cmd(&mut probes[0]), "A");
|
||||
assert_eq!(
|
||||
events(&trace),
|
||||
Vec::<String>::new(),
|
||||
"push_pre_executed does not run redo"
|
||||
);
|
||||
assert_eq!(s.command_count(), 2);
|
||||
assert!(s.can_undo());
|
||||
assert!(s.command_is_done(1).unwrap());
|
||||
|
||||
// Undoing still runs the undo callback.
|
||||
s.undo().unwrap();
|
||||
assert_eq!(events(&trace), vec!["undo:a"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stack_clear_resets_to_bottom() {
|
||||
let (trace, mut probes) = setup();
|
||||
let mut s = UndoStack::new();
|
||||
s.push(trace_cmd(&mut probes[0]), "A");
|
||||
s.undo().unwrap();
|
||||
assert_eq!(s.command_count(), 2);
|
||||
|
||||
s.clear();
|
||||
assert_eq!(s.command_count(), 1);
|
||||
assert_eq!(s.command_name(0).unwrap(), "New/Open Project");
|
||||
assert!(!s.can_undo());
|
||||
assert!(!s.can_redo());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stack_caps_at_k_max() {
|
||||
let (_trace, mut probes) = setup();
|
||||
let mut s = UndoStack::new();
|
||||
for i in 0..(K_MAX_UNDO_COMMANDS + 20) {
|
||||
let mut probe = Probe {
|
||||
name: "x",
|
||||
trace: probes[0].trace,
|
||||
};
|
||||
let _ = &mut probe;
|
||||
s.push(trace_cmd(&mut probes[i % 3]), &format!("cmd{i}"));
|
||||
}
|
||||
assert_eq!(s.command_count() as usize, K_MAX_UNDO_COMMANDS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stack_query_bounds_errors() {
|
||||
let mut s = UndoStack::new();
|
||||
assert!(matches!(s.command_name(-1), Err(Error::NotFound)));
|
||||
assert!(matches!(s.command_name(1), Err(Error::NotFound)));
|
||||
assert!(matches!(s.command_is_done(-1), Err(Error::NotFound)));
|
||||
assert!(matches!(s.command_is_done(1), Err(Error::NotFound)));
|
||||
}
|
||||
Reference in New Issue
Block a user