From 36e5809f638028644d8a51e1ed2467ea402de170 Mon Sep 17 00:00:00 2001 From: Skyler Hawthorne Date: Sun, 17 Apr 2022 21:04:59 -0400 Subject: [PATCH] add test for ensuring the initial cursor on a newly opened file --- Cargo.lock | 42 ++++++++++++++++++++++++ helix-term/Cargo.toml | 1 + helix-term/tests/integration.rs | 1 + helix-term/tests/integration/helpers.rs | 13 ++++++++ helix-term/tests/integration/movement.rs | 30 +++++++++++++++++ 5 files changed, 87 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 39afd1414..6b49f722f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -221,6 +221,15 @@ dependencies = [ "thiserror", ] +[[package]] +name = "fastrand" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3fcf0cee53519c866c09b5de1f6c56ff9d647101f81c1964fa632e148896cdf" +dependencies = [ + "instant", +] + [[package]] name = "fern" version = "0.6.1" @@ -462,6 +471,7 @@ dependencies = [ "signal-hook", "signal-hook-tokio", "smallvec", + "tempfile", "tokio", "tokio-stream", "toml", @@ -555,6 +565,15 @@ dependencies = [ "unindent", ] +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + [[package]] name = "itoa" version = "1.0.1" @@ -852,6 +871,15 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + [[package]] name = "retain_mut" version = "0.1.7" @@ -1056,6 +1084,20 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "tempfile" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" +dependencies = [ + "cfg-if", + "fastrand", + "libc", + "redox_syscall", + "remove_dir_all", + "winapi", +] + [[package]] name = "textwrap" version = "0.15.0" diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index 05f8eed4c..f1903f04b 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -78,3 +78,4 @@ helix-loader = { version = "0.6", path = "../helix-loader" } [dev-dependencies] smallvec = "1.8" indoc = "1.0.3" +tempfile = "3.3.0" diff --git a/helix-term/tests/integration.rs b/helix-term/tests/integration.rs index a388cf6bf..4b0a2346e 100644 --- a/helix-term/tests/integration.rs +++ b/helix-term/tests/integration.rs @@ -25,4 +25,5 @@ async fn hello_world() -> anyhow::Result<()> { mod auto_indent; mod auto_pairs; mod movement; + mod write; } diff --git a/helix-term/tests/integration/helpers.rs b/helix-term/tests/integration/helpers.rs index d22bcc3ce..5a853ad1b 100644 --- a/helix-term/tests/integration/helpers.rs +++ b/helix-term/tests/integration/helpers.rs @@ -1,3 +1,5 @@ +use std::io::Write; + use crossterm::event::{Event, KeyEvent}; use helix_core::{test, Selection, Transaction}; use helix_term::{application::Application, args::Args, config::Config}; @@ -85,3 +87,14 @@ pub fn test_key_sequence_text_result>( Ok(()) } + +pub fn temp_file_with_contents>(content: S) -> tempfile::NamedTempFile { + let mut temp_file = tempfile::NamedTempFile::new().unwrap(); + temp_file + .as_file_mut() + .write_all(content.as_ref().as_bytes()) + .unwrap(); + temp_file.flush().unwrap(); + temp_file.as_file_mut().sync_all().unwrap(); + temp_file +} diff --git a/helix-term/tests/integration/movement.rs b/helix-term/tests/integration/movement.rs index d2e01e71c..fc2583c16 100644 --- a/helix-term/tests/integration/movement.rs +++ b/helix-term/tests/integration/movement.rs @@ -1,3 +1,5 @@ +use helix_term::application::Application; + use super::*; #[tokio::test] @@ -94,3 +96,31 @@ async fn insert_to_normal_mode_cursor_position() -> anyhow::Result<()> { Ok(()) } + +/// Ensure the very initial cursor in an opened file is the width of +/// the first grapheme +#[tokio::test] +async fn cursor_position_newly_opened_file() -> anyhow::Result<()> { + let test = |content: &str, expected_sel: Selection| { + let file = helpers::temp_file_with_contents(content); + + let mut app = Application::new( + Args { + files: vec![(file.path().to_path_buf(), Position::default())], + ..Default::default() + }, + Config::default(), + ) + .unwrap(); + + let (view, doc) = helix_view::current!(app.editor); + let sel = doc.selection(view.id).clone(); + assert_eq!(expected_sel, sel); + }; + + test("foo", Selection::single(0, 1)); + test("๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ foo", Selection::single(0, 7)); + test("", Selection::single(0, 0)); + + Ok(()) +}