This commit is contained in:
Andras Schmelczer 2025-04-01 22:46:03 +01:00
commit 941100d715
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
4 changed files with 37 additions and 12 deletions

View file

@ -16,6 +16,7 @@ pub struct CursorPosition {
} }
impl CursorPosition { impl CursorPosition {
#[must_use]
pub fn apply_merge_context<T>(&self, context: &MergeContext<T>) -> Self pub fn apply_merge_context<T>(&self, context: &MergeContext<T>) -> Self
where where
T: PartialEq + Clone + std::fmt::Debug, T: PartialEq + Clone + std::fmt::Debug,
@ -40,6 +41,7 @@ pub struct TextWithCursors<'a> {
} }
impl<'a> TextWithCursors<'a> { impl<'a> TextWithCursors<'a> {
#[must_use]
pub fn new(text: &'a str, cursors: Vec<CursorPosition>) -> Self { pub fn new(text: &'a str, cursors: Vec<CursorPosition>) -> Self {
Self { Self {
text: text.into(), text: text.into(),
@ -47,6 +49,7 @@ impl<'a> TextWithCursors<'a> {
} }
} }
#[must_use]
pub fn new_owned(text: String, cursors: Vec<CursorPosition>) -> Self { pub fn new_owned(text: String, cursors: Vec<CursorPosition>) -> Self {
Self { Self {
text: text.into(), text: text.into(),

View file

@ -1,10 +1,10 @@
use std::{fs, path::Path}; use std::{fs, path::Path};
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use reconcile::{CursorPosition, TextWithCursors, reconcile_with_cursors}; use reconcile::{CursorPosition, TextWithCursors};
use serde::Deserialize; use serde::Deserialize;
/// ExampleDocument represents a test case for the reconciliation process. /// `ExampleDocument` represents a test case for the reconciliation process.
/// It contains a parent string, left and right strings with cursor positions, /// It contains a parent string, left and right strings with cursor positions,
/// and the expected result after reconciliation. /// and the expected result after reconciliation.
/// ///
@ -19,26 +19,49 @@ pub struct ExampleDocument {
} }
impl ExampleDocument { impl ExampleDocument {
/// Creates a new `ExampleDocument` instance from a YAML file.
///
/// # Panics
///
/// If the file cannot be opened or parsed, the program will panic.
#[must_use]
pub fn from_yaml(path: &Path) -> Self { pub fn from_yaml(path: &Path) -> Self {
let file = fs::File::open(path).expect("Failed to open example file"); let file = fs::File::open(path).expect("Failed to open example file");
serde_yaml::from_reader(file).expect("Failed to parse example file") serde_yaml::from_reader(file).expect("Failed to parse example file")
} }
#[must_use]
pub fn parent(&self) -> String { self.parent.clone() } pub fn parent(&self) -> String { self.parent.clone() }
#[must_use]
pub fn left(&self) -> TextWithCursors<'static> { pub fn left(&self) -> TextWithCursors<'static> {
ExampleDocument::string_to_text_with_cursors(&self.left) ExampleDocument::string_to_text_with_cursors(&self.left)
} }
#[must_use]
pub fn right(&self) -> TextWithCursors<'static> { pub fn right(&self) -> TextWithCursors<'static> {
ExampleDocument::string_to_text_with_cursors(&self.right) ExampleDocument::string_to_text_with_cursors(&self.right)
} }
pub fn assert_eq(&self, result: TextWithCursors<'static>) { /// Asserts that the result string matches the expected string,
let result_str = ExampleDocument::text_with_cursors_to_string(&result); /// including cursor positions.
///
/// # Panics
///
/// If the result string does not match the expected string, the program
/// will panic.
pub fn assert_eq(&self, result: &TextWithCursors<'static>) {
let result_str = ExampleDocument::text_with_cursors_to_string(result);
assert_eq!(result_str, self.expected); assert_eq!(result_str, self.expected);
} }
/// Asserts that the result string matches the expected string,
/// ignoring cursor positions.
///
/// # Panics
///
/// If the result string does not match the expected string, the program
/// will panic.
pub fn assert_eq_without_cursors(&self, result: &str) { pub fn assert_eq_without_cursors(&self, result: &str) {
assert_eq!( assert_eq!(
result, result,
@ -46,7 +69,7 @@ impl ExampleDocument {
); );
} }
fn text_with_cursors_to_string<'a>(text: &TextWithCursors<'a>) -> String { fn text_with_cursors_to_string(text: &TextWithCursors<'_>) -> String {
let mut result = text.text.clone().into_owned(); let mut result = text.text.clone().into_owned();
for (i, cursor) in text.cursors.iter().enumerate() { for (i, cursor) in text.cursors.iter().enumerate() {
result.insert(cursor.char_index + i, '|'); result.insert(cursor.char_index + i, '|');
@ -55,8 +78,8 @@ impl ExampleDocument {
} }
fn string_to_text_with_cursors(text: &str) -> TextWithCursors<'static> { fn string_to_text_with_cursors(text: &str) -> TextWithCursors<'static> {
let cursors = Self::parse_cursors(&text); let cursors = Self::parse_cursors(text);
let text = text.replace("|", ""); let text = text.replace('|', "");
TextWithCursors::new_owned(text, cursors) TextWithCursors::new_owned(text, cursors)
} }

View file

@ -2,8 +2,7 @@ mod example_document;
use std::{fs, path::Path}; use std::{fs, path::Path};
use example_document::ExampleDocument; use example_document::ExampleDocument;
use reconcile::{CursorPosition, TextWithCursors, reconcile, reconcile_with_cursors}; use reconcile::{reconcile, reconcile_with_cursors};
use serde::Deserialize;
#[test] #[test]
fn test_with_examples() { fn test_with_examples() {
@ -19,7 +18,7 @@ fn test_with_examples() {
.path(); .path();
path.file_name() path.file_name()
.and_then(|name| name.to_str()) .and_then(|name| name.to_str())
.and_then(|name| name.split(".").next().unwrap().parse::<i32>().ok()) .and_then(|name| name.split('.').next().unwrap().parse::<i32>().ok())
.unwrap_or_default() .unwrap_or_default()
}); });

View file

@ -25,7 +25,7 @@ impl From<OwnedTextWithCursors> for TextWithCursors<'_> {
owned owned
.cursors .cursors
.into_iter() .into_iter()
.map(|cursor| cursor.into()) .map(std::convert::Into::into)
.collect(), .collect(),
) )
} }
@ -38,7 +38,7 @@ impl From<TextWithCursors<'_>> for OwnedTextWithCursors {
cursors: text_with_cursors cursors: text_with_cursors
.cursors .cursors
.into_iter() .into_iter()
.map(|cursor| cursor.into()) .map(std::convert::Into::into)
.collect(), .collect(),
} }
} }