#![deny(unsafe_code)]
use super::*;
#[test]
fn external_links_are_handed_to_the_system_opener() {
let mut p = pager_at("/c/README.md");
seek_status(&mut p, "https://example.com/x");
let before = current(&p);
key(&mut p, Key::Enter);
assert_eq!(current(&p), before, "must not navigate the reader");
assert_eq!(p.take_open().as_deref(), Some("https://example.com/x"));
assert_eq!(p.take_open(), None);
assert_eq!(
p.focused_link_yank().as_deref(),
Some("https://example.com/x")
);
}
#[test]
fn no_browser_shows_the_url_and_refuses() {
let mut p = pager_at("/c/README.md");
p.set_browser(false);
seek_status(&mut p, "https://example.com/x");
key(&mut p, Key::Enter);
assert_eq!(p.take_open(), None, "nothing may be queued");
assert_eq!(
p.message.as_deref(),
Some("external link (not opened): https://example.com/x")
);
}
#[test]
fn a_scheme_off_the_allowlist_is_refused_by_name() {
for (url, scheme) in [
("javascript:alert", "javascript"),
("file:///etc/passwd", "file"),
] {
let mut p = pager_at("/c/README.md");
seek_link(&mut p, url, |p| {
p.focused_link().map(|s| s.url.as_str()) == Some(url)
});
key(&mut p, Key::Enter);
assert_eq!(p.take_open(), None, "{url} must never be queued");
let msg = p.message.clone().unwrap_or_default();
assert!(msg.contains(scheme), "{msg} must name the scheme");
assert!(msg.contains("refusing"), "{msg}");
assert_eq!(p.focused_link_yank().as_deref(), Some(url));
}
}
#[test]
fn a_mailto_link_opens_like_any_other_external_link() {
let src = "Write to [me](mailto:someone@example.com).\n";
let source = MarkdownSource::new(md::parse(src));
let mut p = Pager::new(Box::new(source), "x".into(), 80, 10, Some(80));
press(&mut p, "n");
key(&mut p, Key::Enter);
assert_eq!(p.take_open().as_deref(), Some("mailto:someone@example.com"));
}
fn focus_url(p: &mut Pager, url: &str) {
seek_link(p, url, |p| {
p.focused_link().map(|s| s.url.as_str()) == Some(url)
});
let anchor = p.focused_link().unwrap().anchor;
assert_eq!(p.row_of(anchor), Some(p.cursor), "cursor is on the link's row");
}
fn focused_url(p: &Pager) -> Option<String> {
p.focused_link().map(|s| s.url.clone())
}
#[test]
fn the_arrows_walk_the_links_on_the_cursor_row() {
let mut p = pager_at("/c/README.md");
focus_url(&mut p, "https://example.com/s");
assert_eq!(p.row_links().len(), 3, "three links share that row");
key(&mut p, Key::Left);
assert_eq!(focused_url(&p).as_deref(), Some("models/B.md"));
key(&mut p, Key::Left);
assert_eq!(focused_url(&p).as_deref(), Some("models/A.md"));
key(&mut p, Key::Right);
assert_eq!(focused_url(&p).as_deref(), Some("models/B.md"));
key(&mut p, Key::Enter);
assert_eq!(current(&p), "/c/models/B.md");
}
#[test]
fn the_arrows_stop_at_the_ends_of_the_row_and_never_move_the_cursor() {
let mut p = pager_at("/c/README.md");
focus_url(&mut p, "https://example.com/s");
let (row, top) = (p.cursor, p.top);
for _ in 0..5 {
key(&mut p, Key::Right);
}
assert_eq!(focused_url(&p).as_deref(), Some("https://example.com/s"));
for _ in 0..5 {
key(&mut p, Key::Left);
}
assert_eq!(focused_url(&p).as_deref(), Some("models/A.md"));
assert_eq!((p.cursor, p.top), (row, top), "nothing vertical happens");
assert_eq!(p.message, None, "and nothing is said about it");
}
#[test]
fn h_and_l_are_still_only_scrolling() {
let mut p = pager_at("/c/README.md");
focus_url(&mut p, "https://example.com/s");
press(&mut p, "hl");
assert_eq!(focused_url(&p).as_deref(), Some("https://example.com/s"));
}
fn wide_table(cols: usize) -> Pager {
let src = "\
| Name | Site | Spec | Padding |\n\
|---|---|---|---|\n\
| A | [site](https://a.example.com/very/long/path/here) | [spec](models/A.md) \
| pppppppppppppppppppppppppppppppppppppppp |\n";
let source = MarkdownSource::new(md::parse(src));
Pager::new(Box::new(source), "x".into(), cols, 12, Some(200))
}
#[test]
fn the_arrows_walk_the_links_on_a_table_row_that_also_scrolls() {
let mut p = wide_table(40);
press(&mut p, "n");
let site = "https://a.example.com/very/long/path/here";
assert_eq!(focused_url(&p).as_deref(), Some(site));
assert!(p.cursor_scrolls(), "this row really does scroll");
assert_eq!(p.row_links().len(), 2, "and really does hold two links");
key(&mut p, Key::Right);
assert_eq!(focused_url(&p).as_deref(), Some("models/A.md"));
assert_eq!(p.hoff, 0, "the arrow selected instead of scrolling");
key(&mut p, Key::Left);
assert_eq!(focused_url(&p).as_deref(), Some(site));
assert_eq!(p.hoff, 0);
press(&mut p, "l");
assert!(p.hoff > 0, "h/l still scroll this row");
}
#[test]
fn a_scrollable_row_with_one_link_still_scrolls() {
let src = "\
| Name | Spec | Padding |\n\
|---|---|---|\n\
| A | [spec](models/A.md) | pppppppppppppppppppppppppppppppppppppppppppppppppp |\n";
let source = MarkdownSource::new(md::parse(src));
let mut p = Pager::new(Box::new(source), "x".into(), 40, 12, Some(200));
press(&mut p, "n");
assert_eq!(focused_url(&p).as_deref(), Some("models/A.md"));
assert!(p.cursor_scrolls());
assert_eq!(p.row_links().len(), 1);
key(&mut p, Key::Right);
assert!(p.hoff > 0, "one link is no reason to stop scrolling");
assert_eq!(focused_url(&p).as_deref(), Some("models/A.md"));
}
#[test]
fn selecting_on_a_wide_row_never_moves_the_cursor() {
let mut p = wide_table(40);
press(&mut p, "n");
let (row, top) = (p.cursor, p.top);
for _ in 0..6 {
key(&mut p, Key::Right);
}
for _ in 0..6 {
key(&mut p, Key::Left);
}
assert_eq!((p.cursor, p.top), (row, top));
assert_eq!(p.message, None);
}