Support multi-document test files
This commit is contained in:
parent
5bb420e162
commit
8e1123016b
2 changed files with 47 additions and 40 deletions
|
|
@ -1,5 +1,3 @@
|
|||
use std::{fs, path::Path};
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
use reconcile::{CursorPosition, TextWithCursors};
|
||||
use serde::Deserialize;
|
||||
|
|
@ -19,17 +17,6 @@ pub struct 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 {
|
||||
let file = fs::File::open(path).expect("Failed to open example file");
|
||||
serde_yaml::from_reader(file).expect("Failed to parse example file")
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn parent(&self) -> String { self.parent.clone() }
|
||||
|
||||
|
|
@ -52,7 +39,10 @@ impl ExampleDocument {
|
|||
/// 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,
|
||||
"Left (actual) isn't equal to right (expected). Actual: ```\n{result_str}```",
|
||||
);
|
||||
}
|
||||
|
||||
/// Asserts that the result string matches the expected string,
|
||||
|
|
@ -63,9 +53,10 @@ impl ExampleDocument {
|
|||
/// If the result string does not match the expected string, the program
|
||||
/// will panic.
|
||||
pub fn assert_eq_without_cursors(&self, result: &str) {
|
||||
let expected = ExampleDocument::string_to_text_with_cursors(&self.expected).text;
|
||||
assert_eq!(
|
||||
result,
|
||||
ExampleDocument::string_to_text_with_cursors(&self.expected).text,
|
||||
result, expected,
|
||||
"Left (actual) isn't equal to right (expected), Actual: ```\n{result}```",
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,46 +1,62 @@
|
|||
mod example_document;
|
||||
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use example_document::ExampleDocument;
|
||||
use reconcile::{reconcile, reconcile_with_cursors};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[test]
|
||||
fn test_with_examples() {
|
||||
let examples_dir = Path::new("tests/examples");
|
||||
let mut entries = fs::read_dir(examples_dir)
|
||||
let 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());
|
||||
let file = fs::File::open(&path).expect("Failed to open example file");
|
||||
for document in serde_yaml::Deserializer::from_reader(file) {
|
||||
println!("Testing with example from {}", path.display());
|
||||
|
||||
doc.assert_eq_without_cursors(&reconcile(
|
||||
&doc.parent(),
|
||||
&doc.left().text,
|
||||
&doc.right().text,
|
||||
));
|
||||
let doc =
|
||||
ExampleDocument::deserialize(document).expect("Failed to deserialize document");
|
||||
|
||||
doc.assert_eq(&reconcile_with_cursors(
|
||||
&doc.parent(),
|
||||
doc.left(),
|
||||
doc.right(),
|
||||
));
|
||||
test_document(doc);
|
||||
|
||||
println!("Test passed for example from {}", path.display());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_document(doc: ExampleDocument) {
|
||||
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(),
|
||||
));
|
||||
|
||||
// inverse direction
|
||||
doc.assert_eq_without_cursors(&reconcile(
|
||||
&doc.parent(),
|
||||
&doc.right().text,
|
||||
&doc.left().text,
|
||||
));
|
||||
|
||||
// inverse direction with cursors
|
||||
doc.assert_eq(&reconcile_with_cursors(
|
||||
&doc.parent(),
|
||||
doc.right(),
|
||||
doc.left(),
|
||||
));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue