summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Oram <dev@mitmaro.ca>2020-12-10 22:06:35 -0330
committerTim Oram <dev@mitmaro.ca>2020-12-22 13:28:54 -0330
commitb7e4cbfb692cf678d1edebdd02d791f8c38fedf7 (patch)
tree51baaaec5cfc31f70e9ec8067c638e3e356d9c34
parent1a4335ae551b95a27da71b5c92ba35829bc64db2 (diff)
Add tests for the show_commit module
Also fix a few bugs and inconsistencies in the rendering found while writing the tests.
-rw-r--r--src/show_commit/commit.rs18
-rw-r--r--src/show_commit/file_stat.rs12
-rw-r--r--src/show_commit/mod.rs5
-rw-r--r--src/show_commit/tests.rs1293
-rw-r--r--src/show_commit/util.rs7
-rw-r--r--src/show_commit/view_builder.rs36
6 files changed, 1336 insertions, 35 deletions
diff --git a/src/show_commit/commit.rs b/src/show_commit/commit.rs
index 39f18ad..10191a3 100644
--- a/src/show_commit/commit.rs
+++ b/src/show_commit/commit.rs
@@ -23,15 +23,15 @@ pub(super) struct LoadCommitDiffOptions {
#[derive(Debug)]
pub struct Commit {
- author: User,
- body: Option<String>,
- committer: User,
- date: DateTime<Local>,
- file_stats: Vec<FileStat>,
- hash: String,
- number_files_changed: usize,
- insertions: usize,
- deletions: usize,
+ pub(super) author: User,
+ pub(super) body: Option<String>,
+ pub(super) committer: User,
+ pub(super) date: DateTime<Local>,
+ pub(super) file_stats: Vec<FileStat>,
+ pub(super) hash: String,
+ pub(super) number_files_changed: usize,
+ pub(super) insertions: usize,
+ pub(super) deletions: usize,
}
fn load_commit_state(hash: &str, config: LoadCommitDiffOptions) -> Result<Commit, Error> {
diff --git a/src/show_commit/file_stat.rs b/src/show_commit/file_stat.rs
index 7831cf0..8d9456b 100644
--- a/src/show_commit/file_stat.rs
+++ b/src/show_commit/file_stat.rs
@@ -5,12 +5,12 @@ use crate::show_commit::status::Status;
#[derive(Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub struct FileStat {
- status: Status,
- to_name: String,
- from_name: String,
- largest_old_line_number: u32,
- largest_new_line_number: u32,
- deltas: Vec<Delta>,
+ pub(super) status: Status,
+ pub(super) to_name: String,
+ pub(super) from_name: String,
+ pub(super) largest_old_line_number: u32,
+ pub(super) largest_new_line_number: u32,
+ pub(super) deltas: Vec<Delta>,
}
impl FileStat {
diff --git a/src/show_commit/mod.rs b/src/show_commit/mod.rs
index 115c91d..c9206f3 100644
--- a/src/show_commit/mod.rs
+++ b/src/show_commit/mod.rs
@@ -10,6 +10,9 @@ mod user;
mod util;
mod view_builder;
+#[cfg(test)]
+mod tests;
+
use crate::config::diff_ignore_whitespace_setting::DiffIgnoreWhitespaceSetting;
use crate::config::diff_show_whitespace_setting::DiffShowWhitespaceSetting;
use crate::config::Config;
@@ -88,7 +91,7 @@ impl<'s> ProcessModule for ShowCommit<'s> {
else {
let hash = commit.get_hash();
let max_index = hash.len().min(8);
- format!("{:8} ", hash[0..max_index].to_string())
+ format!("{:8}", hash[0..max_index].to_string())
}
.as_str(),
),
diff --git a/src/show_commit/tests.rs b/src/show_commit/tests.rs
new file mode 100644
index 0000000..90bd5ab
--- /dev/null
+++ b/src/show_commit/tests.rs
@@ -0,0 +1,1293 @@
+use super::*;
+use crate::assert_process_result;
+use crate::assert_rendered_output;
+use crate::process::testutil::{process_module_test, TestContext, ViewState};
+use crate::show_commit::delta::Delta;
+use crate::show_commit::diff_line::DiffLine;
+use crate::show_commit::file_stat::FileStat;
+use crate::show_commit::origin::Origin;
+use crate::show_commit::status::Status;
+use crate::show_commit::user::User;
+use anyhow::anyhow;
+use chrono::Local;
+
+fn create_minimal_commit() -> Commit {
+ Commit {
+ author: User::new(None, None),
+ body: None,
+ committer: User::new(None, None),
+ date: Local::now(),
+ file_stats: vec![],
+ hash: String::from("0123456789abcdef0123456789abcdef"),
+ number_files_changed: 0,
+ insertions: 0,
+ deletions: 0,
+ }
+}
+
+#[test]
+#[serial_test::serial]
+fn load_commit_during_activate() {
+ process_module_test(
+ &["pick 18d82dcc4c36cade807d7cf79700b6bbad8080b9 comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ assert_process_result!(test_context.activate(&mut module, State::List));
+ assert!(module.commit.is_some());
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn cached_commit_in_activate() {
+ process_module_test(
+ &["pick 18d82dcc4c36cade807d7cf79700b6bbad8080b9 comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ assert_process_result!(test_context.activate(&mut module, State::List));
+ assert_process_result!(test_context.activate(&mut module, State::List));
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn activate_error() {
+ process_module_test(
+ &["pick aaaaaaaaaa comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ assert_process_result!(
+ test_context.activate(&mut module, State::List),
+ state = State::List,
+ error = anyhow!(
+ "Error loading commit: aaaaaaaaaa: revspec 'aaaaaaaaaa' not found; class=Reference (4); \
+ code=NotFound (-3)"
+ )
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_minimal_commit() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{BODY}",
+ format!("{{IndicatorColor}}Date: {{Normal}}{}", commit_date).as_str(),
+ "",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_minimal_commit_compact() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (33, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{Normal}01234567",
+ "{BODY}",
+ format!("{{IndicatorColor}}D: {{Normal}}{}", commit_date).as_str(),
+ "",
+ "{IndicatorColor}0{Normal} / {DiffAddColor}0{Normal} / {DiffRemoveColor}0"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_with_author() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.author = User::new(Some("John Doe"), Some("john.doe@example.com"));
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{BODY}",
+ format!("{{IndicatorColor}}Date: {{Normal}}{}", commit_date).as_str(),
+ "{IndicatorColor}Author: {Normal}John Doe <john.doe@example.com>",
+ "",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_with_author_compact() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (33, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.author = User::new(Some("John Doe"), Some("john.doe@example.com"));
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{Normal}01234567",
+ "{BODY}",
+ format!("{{IndicatorColor}}D: {{Normal}}{}", commit_date).as_str(),
+ "{IndicatorColor}A: {Normal}John Doe <john.doe@example.com",
+ "",
+ "{IndicatorColor}0{Normal} / {DiffAddColor}0{Normal} / {DiffRemoveColor}0"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_with_committer() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.committer = User::new(Some("John Doe"), Some("john.doe@example.com"));
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{BODY}",
+ format!("{{IndicatorColor}}Date: {{Normal}}{}", commit_date).as_str(),
+ "{IndicatorColor}Committer: {Normal}John Doe <john.doe@example.com>",
+ "",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_with_committer_compact() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (33, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.committer = User::new(Some("John Doe"), Some("john.doe@example.com"));
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{Normal}01234567",
+ "{BODY}",
+ format!("{{IndicatorColor}}D: {{Normal}}{}", commit_date).as_str(),
+ "{IndicatorColor}C: {Normal}John Doe <john.doe@example.com",
+ "",
+ "{IndicatorColor}0{Normal} / {DiffAddColor}0{Normal} / {DiffRemoveColor}0"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_with_commit_body() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.body = Some(String::from("Commit title\n\nCommit body"));
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{BODY}",
+ format!("{{IndicatorColor}}Date: {{Normal}}{}", commit_date).as_str(),
+ "{Normal}Commit title",
+ "",
+ "{Normal}Commit body",
+ "",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_with_file_stats() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.file_stats = vec![
+ FileStat::new("file.1a", "file.1b", Status::Renamed),
+ FileStat::new("file.2a", "file.2a", Status::Added),
+ FileStat::new("file.3a", "file.3a", Status::Deleted),
+ FileStat::new("file.4a", "file.4b", Status::Copied),
+ FileStat::new("file.5a", "file.5a", Status::Modified),
+ FileStat::new("file.6a", "file.6a", Status::Typechange),
+ FileStat::new("file.7a", "file.7b", Status::Other),
+ ];
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{BODY}",
+ format!("{{IndicatorColor}}Date: {{Normal}}{}", commit_date).as_str(),
+ "",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions",
+ "{DiffChangeColor} renamed: {DiffRemoveColor}file.1b{Normal} → {DiffAddColor}file.1a",
+ "{DiffAddColor} added: {DiffAddColor}file.2a",
+ "{DiffRemoveColor} deleted: {DiffRemoveColor}file.3a",
+ "{DiffAddColor} copied: {Normal}file.4b{Normal} → {DiffAddColor}file.4a",
+ "{DiffChangeColor}modified: {DiffChangeColor}file.5a",
+ "{DiffChangeColor} changed: {DiffChangeColor}file.6a",
+ "{Normal} unknown: {Normal}file.7a"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_with_file_stats_compact() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (33, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.file_stats = vec![
+ FileStat::new("file.1a", "file.1b", Status::Renamed),
+ FileStat::new("file.2a", "file.2a", Status::Added),
+ FileStat::new("file.3a", "file.3a", Status::Deleted),
+ FileStat::new("file.4a", "file.4b", Status::Copied),
+ FileStat::new("file.5a", "file.5a", Status::Modified),
+ FileStat::new("file.6a", "file.6a", Status::Typechange),
+ FileStat::new("file.7a", "file.7b", Status::Other),
+ ];
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{Normal}01234567",
+ "{BODY}",
+ format!("{{IndicatorColor}}D: {{Normal}}{}", commit_date).as_str(),
+ "",
+ "{IndicatorColor}0{Normal} / {DiffAddColor}0{Normal} / {DiffRemoveColor}0",
+ "{DiffChangeColor}R {DiffRemoveColor}file.1b{Normal}→{DiffAddColor}file.1a",
+ "{DiffAddColor}A {DiffAddColor}file.2a",
+ "{DiffRemoveColor}D {DiffRemoveColor}file.3a",
+ "{DiffAddColor}C {Normal}file.4b{Normal}→{DiffAddColor}file.4a",
+ "{DiffChangeColor}M {DiffChangeColor}file.5a",
+ "{DiffChangeColor}T {DiffChangeColor}file.6a",
+ "{Normal}X {Normal}file.7a"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_single_file_changed() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.number_files_changed = 1;
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{BODY}",
+ format!("{{IndicatorColor}}Date: {{Normal}}{}", commit_date).as_str(),
+ "",
+ "{IndicatorColor}1{Normal} file{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_more_than_one_file_changed() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.number_files_changed = 2;
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{BODY}",
+ format!("{{IndicatorColor}}Date: {{Normal}}{}", commit_date).as_str(),
+ "",
+ "{IndicatorColor}2{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_single_insertion() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.insertions = 1;
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{BODY}",
+ format!("{{IndicatorColor}}Date: {{Normal}}{}", commit_date).as_str(),
+ "",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}1{Normal} insertion{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_more_than_one_insertion() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.insertions = 2;
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{BODY}",
+ format!("{{IndicatorColor}}Date: {{Normal}}{}", commit_date).as_str(),
+ "",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}2{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_single_deletion() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.deletions = 1;
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{BODY}",
+ format!("{{IndicatorColor}}Date: {{Normal}}{}", commit_date).as_str(),
+ "",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}1{Normal} deletion"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_overview_more_than_one_deletion() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState::default(),
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let commit_date = commit.get_date().format("%c %z").to_string();
+ commit.deletions = 2;
+ module.commit = Some(commit);
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{BODY}",
+ format!("{{IndicatorColor}}Date: {{Normal}}{}", commit_date).as_str(),
+ "",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}2{Normal} deletions"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_diff_minimal_commit() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (50, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut config = test_context.config.clone();
+ config.diff_show_whitespace = DiffShowWhitespaceSetting::None;
+ let mut module = ShowCommit::new(&config);
+ module.commit = Some(create_minimal_commit());
+ module.state = ShowCommitState::Diff;
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions",
+ "{BODY}",
+ "{Normal}{Pad ―,150}"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_diff_minimal_commit_compact() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (33, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut config = test_context.config.clone();
+ config.diff_show_whitespace = DiffShowWhitespaceSetting::None;
+ let mut module = ShowCommit::new(&config);
+ module.commit = Some(create_minimal_commit());
+ module.state = ShowCommitState::Diff;
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{Normal}01234567",
+ "{IndicatorColor}0{Normal} / {DiffAddColor}0{Normal} / {DiffRemoveColor}0",
+ "{BODY}",
+ "{Normal}{Pad ―,99}"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_diff_basic_file_stats() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (50, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut config = test_context.config.clone();
+ config.diff_show_whitespace = DiffShowWhitespaceSetting::None;
+ let mut module = ShowCommit::new(&config);
+ let mut commit = create_minimal_commit();
+ commit.file_stats = vec![
+ FileStat::new("file.1a", "file.1b", Status::Renamed),
+ FileStat::new("file.2a", "file.2a", Status::Added),
+ FileStat::new("file.3a", "file.3a", Status::Deleted),
+ FileStat::new("file.4a", "file.4b", Status::Copied),
+ FileStat::new("file.5a", "file.5a", Status::Modified),
+ FileStat::new("file.6a", "file.6a", Status::Typechange),
+ FileStat::new("file.7a", "file.7b", Status::Other),
+ ];
+ module.commit = Some(commit);
+ module.state = ShowCommitState::Diff;
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions",
+ "{BODY}",
+ "{Normal}{Pad ―,150}",
+ "{DiffChangeColor} renamed: {DiffRemoveColor}file.1b{Normal} → {DiffAddColor}file.1a",
+ "{Normal}{Pad ―,150}",
+ "{DiffAddColor} added: {DiffAddColor}file.2a",
+ "{Normal}{Pad ―,150}",
+ "{DiffRemoveColor} deleted: {DiffRemoveColor}file.3a",
+ "{Normal}{Pad ―,150}",
+ "{DiffAddColor} copied: {Normal}file.4b{Normal} → {DiffAddColor}file.4a",
+ "{Normal}{Pad ―,150}",
+ "{DiffChangeColor}modified: {DiffChangeColor}file.5a",
+ "{Normal}{Pad ―,150}",
+ "{DiffChangeColor} changed: {DiffChangeColor}file.6a",
+ "{Normal}{Pad ―,150}",
+ "{Normal} unknown: {Normal}file.7a"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_diff_end_new_line_missing() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (50, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut module = ShowCommit::new(test_context.config);
+ let mut commit = create_minimal_commit();
+ let mut file_stat = FileStat::new("file.txt", "file.txt", Status::Modified);
+ let mut delta = Delta::new("@@ -14,2 +13,3 @@ context", 14, 14, 0, 1);
+ delta.add_line(DiffLine::new(Origin::Addition, "new line", None, Some(14), false));
+ delta.add_line(DiffLine::new(Origin::Addition, "", None, Some(15), true));
+ file_stat.add_delta(delta);
+ commit.file_stats = vec![file_stat];
+ module.commit = Some(commit);
+ module.state = ShowCommitState::Diff;
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions",
+ "{BODY}",
+ "{Normal}{Pad ―,150}",
+ "{DiffChangeColor}modified: {DiffChangeColor}file.txt",
+ "",
+ "{Normal,Dimmed}@@{DiffContextColor} -14,0 +14,1 {Normal,Dimmed}@@{DiffContextColor} context",
+ "{Normal,Dimmed}{Pad ┈,150}",
+ "{Normal} {Normal} {Normal}14{Normal}| {DiffAddColor}new line",
+ "{Normal} {DiffContextColor}\\ No newline at end of file"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_diff_add_line() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (50, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut config = test_context.config.clone();
+ config.diff_show_whitespace = DiffShowWhitespaceSetting::None;
+ let mut module = ShowCommit::new(&config);
+ let mut commit = create_minimal_commit();
+ let mut file_stat = FileStat::new("file.txt", "file.txt", Status::Modified);
+ let mut delta = Delta::new("@@ -14,2 +13,3 @@ context", 14, 14, 0, 1);
+ delta.add_line(DiffLine::new(Origin::Addition, "new line", None, Some(14), false));
+ file_stat.add_delta(delta);
+ commit.file_stats = vec![file_stat];
+ module.commit = Some(commit);
+ module.state = ShowCommitState::Diff;
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions",
+ "{BODY}",
+ "{Normal}{Pad ―,150}",
+ "{DiffChangeColor}modified: {DiffChangeColor}file.txt",
+ "",
+ "{Normal,Dimmed}@@{DiffContextColor} -14,0 +14,1 {Normal,Dimmed}@@{DiffContextColor} context",
+ "{Normal,Dimmed}{Pad ┈,150}",
+ "{Normal} {Normal} {Normal}14{Normal}| {DiffAddColor}new line"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_diff_delete_line() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (50, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut config = test_context.config.clone();
+ config.diff_show_whitespace = DiffShowWhitespaceSetting::None;
+ let mut module = ShowCommit::new(&config);
+ let mut commit = create_minimal_commit();
+ let mut file_stat = FileStat::new("file.txt", "file.txt", Status::Modified);
+ let mut delta = Delta::new("@@ -14,2 +13,3 @@ context", 14, 14, 0, 1);
+ delta.add_line(DiffLine::new(Origin::Deletion, "old line", Some(14), None, false));
+ file_stat.add_delta(delta);
+ commit.file_stats = vec![file_stat];
+ module.commit = Some(commit);
+ module.state = ShowCommitState::Diff;
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions",
+ "{BODY}",
+ "{Normal}{Pad ―,150}",
+ "{DiffChangeColor}modified: {DiffChangeColor}file.txt",
+ "",
+ "{Normal,Dimmed}@@{DiffContextColor} -14,0 +14,1 {Normal,Dimmed}@@{DiffContextColor} context",
+ "{Normal,Dimmed}{Pad ┈,150}",
+ "{Normal}14{Normal} {Normal} {Normal}| {DiffRemoveColor}old line"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_diff_context_add_remove_lines() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (50, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut config = test_context.config.clone();
+ config.diff_show_whitespace = DiffShowWhitespaceSetting::None;
+ let mut module = ShowCommit::new(&config);
+ let mut commit = create_minimal_commit();
+ let mut file_stat = FileStat::new("file.txt", "file.txt", Status::Modified);
+ let mut delta = Delta::new("@@ -14,2 +13,3 @@ context", 14, 14, 0, 1);
+ delta.add_line(DiffLine::new(Origin::Context, "context 1", Some(13), Some(13), false));
+ delta.add_line(DiffLine::new(Origin::Deletion, "old line", Some(14), None, false));
+ delta.add_line(DiffLine::new(Origin::Addition, "new line", None, Some(14), false));
+ delta.add_line(DiffLine::new(Origin::Context, "context 2", Some(15), Some(15), false));
+ file_stat.add_delta(delta);
+ commit.file_stats = vec![file_stat];
+ module.commit = Some(commit);
+ module.state = ShowCommitState::Diff;
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions",
+ "{BODY}",
+ "{Normal}{Pad ―,150}",
+ "{DiffChangeColor}modified: {DiffChangeColor}file.txt",
+ "",
+ "{Normal,Dimmed}@@{DiffContextColor} -14,0 +14,1 {Normal,Dimmed}@@{DiffContextColor} context",
+ "{Normal,Dimmed}{Pad ┈,150}",
+ "{Normal}13{Normal} {Normal}13{Normal}| {DiffContextColor}context 1",
+ "{Normal}14{Normal} {Normal} {Normal}| {DiffRemoveColor}old line",
+ "{Normal} {Normal} {Normal}14{Normal}| {DiffAddColor}new line",
+ "{Normal}15{Normal} {Normal}15{Normal}| {DiffContextColor}context 2"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_diff_add_line_with_show_whitespace() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (50, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut config = test_context.config.clone();
+ config.diff_show_whitespace = DiffShowWhitespaceSetting::Both;
+ let mut module = ShowCommit::new(&config);
+ let mut commit = create_minimal_commit();
+ let mut file_stat = FileStat::new("file.txt", "file.txt", Status::Modified);
+ let mut delta = Delta::new("@@ -14,2 +13,3 @@ context", 14, 14, 0, 1);
+ delta.add_line(DiffLine::new(Origin::Addition, "new line", None, Some(14), false));
+ file_stat.add_delta(delta);
+ commit.file_stats = vec![file_stat];
+ module.commit = Some(commit);
+ module.state = ShowCommitState::Diff;
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{IndicatorColor}0{Normal} files{Normal} with {DiffAddColor}0{Normal} insertions{Normal} and \
+ {DiffRemoveColor}0{Normal} deletions",
+ "{BODY}",
+ "{Normal}{Pad ―,150}",
+ "{DiffChangeColor}modified: {DiffChangeColor}file.txt",
+ "",
+ "{Normal,Dimmed}@@{DiffContextColor} -14,0 +14,1 {Normal,Dimmed}@@{DiffContextColor} context",
+ "{Normal,Dimmed}{Pad ┈,150}",
+ "{Normal} {Normal} {Normal}14{Normal}| {DiffAddColor}new line"
+ );
+ },
+ );
+}
+
+#[test]
+#[serial_test::serial]
+fn render_diff_delete_line_with_show_whitespace() {
+ process_module_test(
+ &["pick 0123456789abcdef0123456789abcdef comment1"],
+ ViewState {
+ size: (50, 100),
+ ..ViewState::default()
+ },
+ &[],
+ |test_context: TestContext<'_>| {
+ let mut config = test_context.config.clone();
+ config.diff_show_whitespace = DiffShowWhitespaceSetting::Both;
+ let mut module = ShowCommit::new(&config);
+ let mut commit = create_minimal_commit();
+ let mut file_stat = FileStat::new("file.txt", "file.txt", Status::Modified);
+ let mut delta = Delta::new("@@ -14,2 +13,3 @@ context", 14, 14, 0, 1);
+ delta.add_line(DiffLine::new(Origin::Deletion, "old line", Some(14), None, false));
+ file_stat.add_delta(delta);
+ commit.file_stats = vec![file_stat];
+ module.commit = Some(commit);
+ module.state = ShowCommitState::Diff;
+ assert_rendered_output!(
+ test_context.build_view_data(&mut module),
+ "{TITLE}{HELP}",
+ "{LEADING}",
+ "{IndicatorColor}Commit: {Normal}0123456789abcdef0123456789abcdef",
+ "{IndicatorColor}0{Normal} files{Normal} with