Add integration tests
This commit is contained in:
parent
23c288b1eb
commit
998ee387d2
17 changed files with 181 additions and 1 deletions
75
backend/reconcile/tests/example_document.rs
Normal file
75
backend/reconcile/tests/example_document.rs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
use std::{fs, path::Path};
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
use reconcile::{CursorPosition, TextWithCursors, reconcile_with_cursors};
|
||||
use serde::Deserialize;
|
||||
|
||||
/// ExampleDocument represents a test case for the reconciliation process.
|
||||
/// It contains a parent string, left and right strings with cursor positions,
|
||||
/// and the expected result after reconciliation.
|
||||
///
|
||||
/// '|' characters in the left, right, and expected strings are treated as
|
||||
/// cursor positions and are converted into `CursorPosition` objects.
|
||||
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
|
||||
pub struct ExampleDocument {
|
||||
parent: String,
|
||||
left: String,
|
||||
right: String,
|
||||
expected: String,
|
||||
}
|
||||
|
||||
impl ExampleDocument {
|
||||
pub fn from_yaml(path: &Path) -> Self {
|
||||
let file = fs::File::open(path).expect("Failed to open example file");
|
||||
serde_yaml::from_reader(file).expect("Failed to parse example file")
|
||||
}
|
||||
|
||||
pub fn parent(&self) -> String { self.parent.clone() }
|
||||
|
||||
pub fn left(&self) -> TextWithCursors<'static> {
|
||||
ExampleDocument::string_to_text_with_cursors(&self.left)
|
||||
}
|
||||
|
||||
pub fn right(&self) -> TextWithCursors<'static> {
|
||||
ExampleDocument::string_to_text_with_cursors(&self.right)
|
||||
}
|
||||
|
||||
pub fn assert_eq(&self, result: TextWithCursors<'static>) {
|
||||
let result_str = ExampleDocument::text_with_cursors_to_string(&result);
|
||||
assert_eq!(result_str, self.expected);
|
||||
}
|
||||
|
||||
pub fn assert_eq_without_cursors(&self, result: &str) {
|
||||
assert_eq!(
|
||||
result,
|
||||
ExampleDocument::string_to_text_with_cursors(&self.expected).text,
|
||||
);
|
||||
}
|
||||
|
||||
fn text_with_cursors_to_string<'a>(text: &TextWithCursors<'a>) -> String {
|
||||
let mut result = text.text.clone().into_owned();
|
||||
for (i, cursor) in text.cursors.iter().enumerate() {
|
||||
result.insert(cursor.char_index + i, '|');
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn string_to_text_with_cursors(text: &str) -> TextWithCursors<'static> {
|
||||
let cursors = Self::parse_cursors(&text);
|
||||
let text = text.replace("|", "");
|
||||
TextWithCursors::new_owned(text, cursors)
|
||||
}
|
||||
|
||||
fn parse_cursors(text: &str) -> Vec<CursorPosition> {
|
||||
let mut cursors = Vec::new();
|
||||
for (i, c) in text.chars().enumerate() {
|
||||
if c == '|' {
|
||||
cursors.push(CursorPosition {
|
||||
id: 0,
|
||||
char_index: i - cursors.len(),
|
||||
});
|
||||
}
|
||||
}
|
||||
cursors
|
||||
}
|
||||
}
|
||||
47
backend/reconcile/tests/test.rs
Normal file
47
backend/reconcile/tests/test.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
mod example_document;
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use example_document::ExampleDocument;
|
||||
use reconcile::{CursorPosition, TextWithCursors, reconcile, reconcile_with_cursors};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[test]
|
||||
fn test_with_examples() {
|
||||
let examples_dir = Path::new("test/examples");
|
||||
let mut entries = fs::read_dir(examples_dir)
|
||||
.expect("Failed to read examples directory")
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
entries.sort_by_key(|entry| {
|
||||
let path = entry
|
||||
.as_ref()
|
||||
.expect("Failed to read directory entry")
|
||||
.path();
|
||||
path.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.and_then(|name| name.split(".").next().unwrap().parse::<i32>().ok())
|
||||
.unwrap_or_default()
|
||||
});
|
||||
|
||||
for entry in entries {
|
||||
let entry = entry.expect("Failed to read directory entry");
|
||||
let path = entry.path();
|
||||
|
||||
if path.is_file() && path.extension().and_then(|ext| ext.to_str()) == Some("yml") {
|
||||
let doc = ExampleDocument::from_yaml(&path);
|
||||
println!("Testing with example from {}", path.display());
|
||||
|
||||
doc.assert_eq_without_cursors(&reconcile(
|
||||
&doc.parent(),
|
||||
&doc.left().text,
|
||||
&doc.right().text,
|
||||
));
|
||||
|
||||
doc.assert_eq(reconcile_with_cursors(
|
||||
&doc.parent(),
|
||||
doc.left(),
|
||||
doc.right(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue