use pdfrum_doc::vt::{Config, Metrics};
use pdfrum_form::edit::ops::{self, TextEdit, insert_char};
fn metrics() -> Metrics<'static> {
Metrics {
width: &|_| 1000,
ascent: 800,
descent: -200,
}
}
fn config() -> Config {
Config {
plate: kurbo::Rect::new(0.0, 0.0, 10.0, 20.0),
font_size: 1.0,
..Config::default()
}
}
fn comb_config() -> Config {
Config {
char_array: 10,
..config()
}
}
fn fixed(config: &Config) -> TextEdit {
let mut edit = TextEdit::new("", config, &metrics(), true);
edit.auto_scroll = false;
edit
}
fn type_all(edit: &mut TextEdit, config: &Config, text: &str) -> usize {
text.chars()
.filter(|ch| insert_char(edit, config, &metrics(), *ch, None))
.count()
}
#[test]
fn a_fixed_field_stops_taking_characters_when_its_plate_is_full() {
let config = config();
let mut edit = fixed(&config);
let accepted = type_all(&mut edit, &config, "abcdefghijklmno");
assert_eq!(
accepted, 11,
"ten fit and the eleventh overflows; the twelfth onward are refused"
);
assert_eq!(
edit.text, "abcdefghijk",
"the refused characters are not in the value either"
);
}
#[test]
fn a_scrolling_field_takes_every_character() {
let config = config();
let mut edit = TextEdit::new("", &config, &metrics(), true);
let accepted = type_all(&mut edit, &config, "abcdefghijklmno");
assert_eq!(accepted, 15);
assert_eq!(edit.text, "abcdefghijklmno");
}
#[test]
fn a_comb_field_overflows_rather_than_refusing() {
let config = comb_config();
let mut edit = fixed(&config);
let accepted = type_all(&mut edit, &config, "abcdefghijklmno");
assert_eq!(
accepted, 15,
"a comb accepts past its cells; only /MaxLen caps it"
);
}
#[test]
fn a_full_field_still_deletes() {
let config = config();
let mut edit = fixed(&config);
type_all(&mut edit, &config, "abcdefghijklmno");
let before = edit.text.clone();
assert!(ops::backspace(&mut edit, &config, &metrics()));
assert_eq!(edit.text.chars().count(), before.chars().count() - 1);
}
#[test]
fn typing_over_the_whole_selection_of_a_full_field_replaces_it() {
let config = config();
let mut edit = fixed(&config);
type_all(&mut edit, &config, "abcdefghijklmno");
edit.select_all();
assert!(
insert_char(&mut edit, &config, &metrics(), 'z', None),
"clearing the selection empties the plate, so the insert fits"
);
assert_eq!(edit.text, "z");
}
#[test]
fn a_paste_into_a_full_field_with_nothing_selected_inserts_nothing() {
let config = config();
let mut edit = fixed(&config);
type_all(&mut edit, &config, "abcdefghijklmno");
let before = edit.text.clone();
edit.set_caret_index(0);
assert!(!ops::replace_selection(
&mut edit,
&config,
&metrics(),
"12345678",
None
));
assert_eq!(edit.text, before, "a full field takes no paste");
}
#[test]
fn a_paste_that_first_empties_the_field_lands() {
let config = config();
let mut edit = fixed(&config);
type_all(&mut edit, &config, "abcdefghijklmno");
edit.select_all();
assert!(ops::replace_selection(
&mut edit,
&config,
&metrics(),
"12345678",
None
));
assert_eq!(edit.text, "12345678");
}
#[test]
fn the_wheel_does_not_move_a_field_that_declines_to_scroll() {
let config = Config {
plate: kurbo::Rect::new(0.0, 0.0, 10.0, 3.0),
font_size: 1.0,
multi_line: true,
..Config::default()
};
let text = "a\nb\nc\nd\ne\nf";
let mut scrolling = TextEdit::new(text, &config, &metrics(), false);
let mut fixed = TextEdit::new(text, &config, &metrics(), false);
fixed.auto_scroll = false;
assert!(
ops::scroll_by(&mut scrolling, &config, -1),
"a scrolling field with more content than plate moves on a notch"
);
assert!(scrolling.scroll.1 > 0.0);
assert!(
!ops::scroll_by(&mut fixed, &config, -1),
"DoNotScroll makes the wheel a no-op"
);
assert!(
(fixed.scroll.1 - 0.0).abs() < f32::EPSILON,
"and it writes nothing, got {}",
fixed.scroll.1
);
}