Skip to content

Commit

Permalink
ship it
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Jan 14, 2025
1 parent 7fcbc79 commit 8eda235
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 46 deletions.
22 changes: 13 additions & 9 deletions crates/editor/src/git/project_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,8 @@ mod tests {
path::{Path, PathBuf},
};

use crate::test::editor_test_context::assert_state_with_diff;

use super::*;

// TODO finish
Expand Down Expand Up @@ -1224,15 +1226,17 @@ mod tests {
cx.executor()
.advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
cx.run_until_parked();

project_diff_editor.update(cx, |project_diff_editor, cx| {
assert_eq!(
// TODO assert it better: extract added text (based on the background changes) and deleted text (based on the deleted blocks added)
project_diff_editor.editor.read(cx).text(cx),
format!("{change}{old_text}"),
"Should have a new change shown in the beginning, and the old text shown as deleted text afterwards"
);
});
let editor = project_diff_editor.update(cx, |view, _| view.editor.clone());

assert_state_with_diff(
&editor,
cx,
indoc::indoc! {
"
- This is file_a
+ ˇan edit after git addThis is file_a",
},
);
}

fn init_test(cx: &mut gpui::TestAppContext) {
Expand Down
82 changes: 45 additions & 37 deletions crates/editor/src/test/editor_test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,43 +334,7 @@ impl EditorTestContext {
/// Diff hunks are indicated by lines starting with `+` and `-`.
#[track_caller]
pub fn assert_state_with_diff(&mut self, expected_diff_text: String) {
let (snapshot, selections) = self.editor.update(&mut self.cx, |editor, cx| {
(
editor.snapshot(cx).buffer_snapshot.clone(),
editor.selections.ranges::<usize>(cx),
)
});

let actual_marked_text = generate_marked_text(&snapshot.text(), &selections, true);

// Read the actual diff.
let line_infos = snapshot.row_infos(MultiBufferRow(0)).collect::<Vec<_>>();
let has_diff = line_infos.iter().any(|info| info.diff_status.is_some());
let actual_diff = actual_marked_text
.split('\n')
.zip(line_infos)
.map(|(line, info)| {
let mut marker = match info.diff_status {
Some(DiffHunkStatus::Added) => "+ ",
Some(DiffHunkStatus::Removed) => "- ",
Some(DiffHunkStatus::Modified) => unreachable!(),
None => {
if has_diff {
" "
} else {
""
}
}
};
if line.is_empty() {
marker = marker.trim();
}
format!("{marker}{line}")
})
.collect::<Vec<_>>()
.join("\n");

pretty_assertions::assert_eq!(actual_diff, expected_diff_text, "unexpected diff state");
assert_state_with_diff(&self.editor, &mut self.cx, &expected_diff_text);
}

/// Make an assertion about the editor's text and the ranges and directions
Expand Down Expand Up @@ -463,6 +427,50 @@ impl EditorTestContext {
}
}

pub fn assert_state_with_diff(
editor: &View<Editor>,
cx: &mut VisualTestContext,
expected_diff_text: &str,
) {
let (snapshot, selections) = editor.update(cx, |editor, cx| {
(
editor.snapshot(cx).buffer_snapshot.clone(),
editor.selections.ranges::<usize>(cx),
)
});

let actual_marked_text = generate_marked_text(&snapshot.text(), &selections, true);

// Read the actual diff.
let line_infos = snapshot.row_infos(MultiBufferRow(0)).collect::<Vec<_>>();
let has_diff = line_infos.iter().any(|info| info.diff_status.is_some());
let actual_diff = actual_marked_text
.split('\n')
.zip(line_infos)
.map(|(line, info)| {
let mut marker = match info.diff_status {
Some(DiffHunkStatus::Added) => "+ ",
Some(DiffHunkStatus::Removed) => "- ",
Some(DiffHunkStatus::Modified) => unreachable!(),
None => {
if has_diff {
" "
} else {
""
}
}
};
if line.is_empty() {
marker = marker.trim();
}
format!("{marker}{line}")
})
.collect::<Vec<_>>()
.join("\n");

pretty_assertions::assert_eq!(actual_diff, expected_diff_text, "unexpected diff state");
}

impl Deref for EditorTestContext {
type Target = gpui::VisualTestContext;

Expand Down

0 comments on commit 8eda235

Please sign in to comment.