use nadir_core::{Key, Vec2};
use super::{Harness, sprites};
use crate::{Console, ConsoleError};
fn console() -> Console<u8> {
let mut console = Console::new();
console
.register("tp <x> <y>", "moves the player", 1)
.expect("a usage");
console
.register("timescale <scale>", "scales the time", 2)
.expect("a usage");
console
}
fn show(harness: &mut Harness, console: &mut Console<u8>) -> Vec<(u8, Vec<String>)> {
let mut ran = Vec::new();
harness.show(|ui| {
console.show(ui, |command, args, out| {
let words = (0..args.len())
.filter_map(|i| args.get(i))
.map(String::from);
ran.push((command, words.collect()));
out.print("done");
Ok::<(), ConsoleError>(())
});
});
ran
}
fn drawn(harness: &Harness, start: &str) -> Vec<(String, f32)> {
harness
.texts()
.into_iter()
.filter(|(text, _)| text.starts_with(start))
.map(|(text, at)| (text, at.y))
.collect()
}
#[test]
fn shown_the_command_line_takes_the_keyboard_and_enter_runs_it() {
let mut harness = Harness::new();
let mut console = console();
harness.input.text("`");
show(&mut harness, &mut console);
assert!(harness.ui.wants_keyboard());
assert_eq!(console.line(), "");
harness.input.text("tp 1 2");
assert!(show(&mut harness, &mut console).is_empty());
assert_eq!(console.line(), "tp 1 2");
harness.tap(Key::Enter);
let ran = show(&mut harness, &mut console);
assert_eq!(ran, [(1, vec!["1".to_string(), "2".to_string()])]);
assert_eq!(console.line(), "");
assert!(harness.ui.wants_keyboard(), "Enter keeps the keyboard");
assert_eq!(console.lines().collect::<Vec<_>>(), ["> tp 1 2", "done"]);
harness.tap(Key::Escape);
show(&mut harness, &mut console);
assert!(!harness.ui.wants_keyboard());
show(&mut harness, &mut console);
assert!(
!harness.ui.wants_keyboard(),
"still shown: it stays as it is"
);
harness.show(|_| {});
show(&mut harness, &mut console);
assert!(harness.ui.wants_keyboard());
}
#[test]
fn the_log_fills_the_window_from_its_bottom_with_the_lines_in_view_only() {
let mut harness = Harness::new();
let mut console = console();
for n in 0..3 {
console.print(format_args!("line {n}"));
}
show(&mut harness, &mut console);
assert_eq!(
drawn(&harness, "line"),
[
("line 0".to_string(), 86.0),
("line 1".to_string(), 100.0),
("line 2".to_string(), 114.0)
]
);
for n in 0..50 {
console.print(format_args!("more {n}"));
}
show(&mut harness, &mut console);
let shown = drawn(&harness, "more");
assert_eq!(shown.len(), 8, "{shown:?}");
assert!(drawn(&harness, "line").is_empty());
assert_eq!(shown[0], ("more 42".to_string(), 16.0));
assert_eq!(shown[7], ("more 49".to_string(), 114.0));
show(&mut harness, &mut console);
assert!(
sprites(&harness)
.iter()
.all(|(at, _, _)| *at != Vec2::new(194.0, 12.0))
);
}
#[test]
fn the_wheel_and_page_keys_scroll_the_log_which_holds_its_lines_as_more_come() {
let mut harness = Harness::new();
let mut console = console();
for n in 0..50 {
console.print(format_args!("line {n}"));
}
show(&mut harness, &mut console);
let first = |harness: &Harness| drawn(harness, "line")[0].0.clone();
assert_eq!(first(&harness), "line 42");
harness.move_to(Vec2::new(50.0, 50.0));
harness.input.wheel(Vec2::new(0.0, 1.0));
show(&mut harness, &mut console);
assert_eq!(first(&harness), "line 39");
harness.move_to(Vec2::new(50.0, 132.0));
harness.input.wheel(Vec2::new(0.0, 1.0));
show(&mut harness, &mut console);
assert_eq!(first(&harness), "line 39");
console.print("line 50\nline 51");
show(&mut harness, &mut console);
assert_eq!(first(&harness), "line 39");
harness.tap(Key::PageDown);
show(&mut harness, &mut console);
assert_eq!(first(&harness), "line 44");
console.print("line 52");
show(&mut harness, &mut console);
assert_eq!(first(&harness), "line 45");
harness.tap(Key::PageUp);
show(&mut harness, &mut console);
assert_eq!(first(&harness), "line 37");
harness.move_to(Vec2::new(50.0, 50.0));
harness.input.wheel(Vec2::new(0.0, 100.0));
show(&mut harness, &mut console);
assert_eq!(first(&harness), "line 0");
harness.input.wheel(Vec2::new(0.0, -100.0));
show(&mut harness, &mut console);
assert_eq!(first(&harness), "line 45");
}
#[test]
fn tab_up_and_down_work_while_the_command_line_has_the_keyboard() {
let mut harness = Harness::new();
let mut console = console();
show(&mut harness, &mut console);
harness.input.text("ti");
harness.tap(Key::Tab);
show(&mut harness, &mut console);
assert_eq!(console.line(), "timescale ");
harness.input.text("2");
harness.tap(Key::Enter);
assert_eq!(show(&mut harness, &mut console).len(), 1);
harness.input.text("tp");
show(&mut harness, &mut console);
harness.tap(Key::Up);
show(&mut harness, &mut console);
assert_eq!(console.line(), "timescale 2");
harness.tap(Key::Down);
show(&mut harness, &mut console);
assert_eq!(console.line(), "tp", "back to the line typed");
harness.tap(Key::Escape);
show(&mut harness, &mut console);
harness.tap(Key::Up);
harness.tap(Key::Tab);
show(&mut harness, &mut console);
assert_eq!(console.line(), "tp");
harness.click(Vec2::new(50.0, 132.0));
show(&mut harness, &mut console);
harness.tap(Key::Tab);
show(&mut harness, &mut console);
assert_eq!(console.line(), "tp ");
}
#[test]
fn enter_runs_what_was_typed_before_it_and_what_follows_goes_to_the_next_line() {
let mut harness = Harness::new();
let mut console = console();
show(&mut harness, &mut console);
harness.input.text("tp 1 2");
harness.tap(Key::Enter);
harness.input.text("5");
let ran = show(&mut harness, &mut console);
assert_eq!(ran, [(1, vec!["1".to_string(), "2".to_string()])]);
assert!(show(&mut harness, &mut console).is_empty());
assert_eq!(console.line(), "5");
harness.tap(Key::Escape);
show(&mut harness, &mut console);
harness.click(Vec2::new(50.0, 132.0));
show(&mut harness, &mut console);
console.set_line("");
harness.tap(Key::Up);
harness.tap(Key::Enter);
show(&mut harness, &mut console);
assert_eq!(console.line(), "tp 1 2");
let ran = show(&mut harness, &mut console);
assert_eq!(ran, [(1, vec!["1".to_string(), "2".to_string()])]);
assert_eq!(console.line(), "");
}
#[test]
fn the_keys_of_the_frame_that_shows_the_console_are_not_its() {
let mut harness = Harness::new();
let mut console = console();
console.run_line("tp 1 2", |_, _, _| Ok(()));
console.set_line("t");
let before = console.lines().len();
harness.tap(Key::Tab);
show(&mut harness, &mut console);
show(&mut harness, &mut console);
assert_eq!(console.lines().len(), before, "Tab listed nothing");
assert_eq!(console.line(), "t");
harness.show(|_| {});
harness.tap(Key::Up);
show(&mut harness, &mut console);
assert_eq!(console.line(), "t");
harness.tap(Key::Up);
show(&mut harness, &mut console);
assert_eq!(console.line(), "tp 1 2");
}
#[test]
fn a_turn_of_the_wheel_that_is_not_finite_spoils_no_later_turn() {
let mut harness = Harness::new();
let mut console = console();
for n in 0..50 {
console.print(format_args!("line {n}"));
}
harness.move_to(Vec2::new(50.0, 50.0));
for bad in [f32::NAN, f32::INFINITY, f32::MAX] {
harness.input.wheel(Vec2::new(0.0, bad));
show(&mut harness, &mut console);
harness.input.wheel(Vec2::new(0.0, -100.0));
show(&mut harness, &mut console);
assert_eq!(drawn(&harness, "line")[0].0, "line 42", "{bad}");
harness.input.wheel(Vec2::new(0.0, 1.0));
show(&mut harness, &mut console);
assert_eq!(drawn(&harness, "line")[0].0, "line 39", "{bad}");
harness.input.wheel(Vec2::new(0.0, -1.0));
show(&mut harness, &mut console);
}
}
#[test]
fn a_line_run_brings_a_log_scrolled_up_back_to_its_newest() {
let mut harness = Harness::new();
let mut console = console();
for n in 0..50 {
console.print(format_args!("line {n}"));
}
show(&mut harness, &mut console);
harness.move_to(Vec2::new(50.0, 50.0));
harness.input.wheel(Vec2::new(0.0, 5.0));
show(&mut harness, &mut console);
assert_eq!(drawn(&harness, "line")[0].0, "line 27");
harness.input.text("tp 1 2");
harness.tap(Key::Enter);
show(&mut harness, &mut console);
show(&mut harness, &mut console);
let shown = drawn(&harness, "");
assert!(
shown.iter().any(|(text, _)| text == "> tp 1 2"),
"{shown:?}"
);
assert_eq!(
shown.iter().rev().nth(1).map(|(text, _)| text.as_str()),
Some("done"),
"{shown:?}"
);
}