Files
cadcraft/crates/engine/tests/text_commands.rs
T
Valentin KleinandClaude Opus 5.5 ca882b38e2 TEXTEDIT prompt, Undo and Mode; MTEXT takes lines until an empty one (#234) (#239)
TEXTEDIT's prompt no longer reads "or or". Undo reverts the last edit made
in this TEXTEDIT, and Mode switches between Multiple (keep selecting) and
Single (end after one edit). At the command line and in scripts, MTEXT
takes one paragraph per input line until an empty line, instead of ending
after the first line and running the next one as a command. The Text
Editor window sends its contents and then Enter, so it still ends MTEXT.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 14:37:52 -04:00

70 lines
2.3 KiB
Rust

//! MTEXT contents from scripts and the command line; TEXTEDIT's prompt and options.
use cadcraft_engine::Session;
use cadcraft_engine::doc::{EntityKind, Space};
fn kinds(s: &Session) -> Vec<EntityKind> {
s.doc().ok().and_then(|d| d.space(&Space::Model)).map(|st| st.iter().map(|e| e.kind.clone()).collect()).unwrap_or_default()
}
fn mtexts(s: &Session) -> Vec<String> {
kinds(s)
.into_iter()
.filter_map(|k| match k {
EntityKind::MText(t) => Some(t.contents),
_ => None,
})
.collect()
}
fn texts(s: &Session) -> Vec<String> {
kinds(s)
.into_iter()
.filter_map(|k| match k {
EntityKind::Text(t) => Some(t.value),
_ => None,
})
.collect()
}
#[test]
fn script_mtext_takes_lines_until_an_empty_line() {
let mut s = Session::new();
s.script("MTEXT 0,10 20,0\nFirst line\nSecond line\n\nLINE 0,0 5,5\n\n").unwrap();
assert!(s.running.is_none(), "{}", s.prompt_text());
assert_eq!(mtexts(&s), vec!["First line\\PSecond line".to_string()]);
assert_eq!(kinds(&s).len(), 2, "{:?}", kinds(&s));
// Typed at the command line: spaces stay in the text, Enter on an empty line ends it.
let mut s = Session::new();
s.cmdline("MTEXT 0,10 20,0").unwrap();
s.cmdline("Hello world").unwrap();
assert!(s.running.is_some());
s.cmdline("").unwrap();
assert!(s.running.is_none(), "{}", s.prompt_text());
assert_eq!(mtexts(&s), vec!["Hello world".to_string()]);
}
#[test]
fn textedit_prompt_mode_and_undo() {
let mut s = Session::new();
s.cmdline("TEXT 0,0 1 0 old").unwrap();
s.cmdline("").unwrap();
s.cmdline("TEXTEDIT").unwrap();
let p = s.prompt_text();
assert!(!p.contains("or or"), "{p}");
// Edit, then Undo restores the old text.
s.cmdline("0.5,0.5").unwrap();
s.cmdline("new").unwrap();
assert_eq!(texts(&s), vec!["new".to_string()]);
s.cmdline("u").unwrap();
assert_eq!(texts(&s), vec!["old".to_string()]);
// Mode Single: the command ends after one edit.
s.cmdline("m").unwrap();
s.cmdline("s").unwrap();
s.cmdline("0.5,0.5").unwrap();
s.cmdline("once").unwrap();
assert!(s.running.is_none(), "{}", s.prompt_text());
assert_eq!(texts(&s), vec!["once".to_string()]);
}