Unify WASM and Rust API types

This commit is contained in:
Andras Schmelczer 2025-06-29 17:42:37 +01:00
parent b18a692d46
commit 5378ffb547
16 changed files with 252 additions and 301 deletions

View file

@ -21,12 +21,12 @@ impl ExampleDocument {
pub fn parent(&self) -> String { self.parent.clone() }
#[must_use]
pub fn left(&self) -> TextWithCursors<'static> {
pub fn left(&self) -> TextWithCursors {
ExampleDocument::string_to_text_with_cursors(&self.left)
}
#[must_use]
pub fn right(&self) -> TextWithCursors<'static> {
pub fn right(&self) -> TextWithCursors {
ExampleDocument::string_to_text_with_cursors(&self.right)
}
@ -37,7 +37,7 @@ impl ExampleDocument {
///
/// If the result string does not match the expected string, the program
/// will panic.
pub fn assert_eq(&self, result: &TextWithCursors<'static>) {
pub fn assert_eq(&self, result: &TextWithCursors) {
let result_str = ExampleDocument::text_with_cursors_to_string(result);
assert_eq!(
self.expected, result_str,
@ -53,16 +53,16 @@ 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;
let expected = ExampleDocument::string_to_text_with_cursors(&self.expected).text();
assert_eq!(
expected, result,
"Left (expected) isn't equal to right (actual), Actual: ```\n{result}```",
);
}
fn text_with_cursors_to_string(text: &TextWithCursors<'_>) -> String {
let mut result = text.text.clone().into_owned();
for (i, cursor) in text.cursors.iter().enumerate() {
fn text_with_cursors_to_string(document: &TextWithCursors) -> String {
let mut result = document.text().clone();
for (i, cursor) in document.cursors().iter().enumerate() {
assert!(
cursor.char_index <= result.len(), // equals in case of insert at the end
"Cursor index out of bounds: {} > {} when testing for '{result}'",
@ -82,7 +82,7 @@ impl ExampleDocument {
result
}
fn string_to_text_with_cursors(text: &str) -> TextWithCursors<'static> {
fn string_to_text_with_cursors(text: &str) -> TextWithCursors {
let cursors = Self::parse_cursors(text);
let text = text.replace('|', "");
TextWithCursors::new_owned(text, cursors)

View file

@ -11,8 +11,8 @@ fn test_document_one_way_without_cursors() {
for doc in &get_all_documents() {
doc.assert_eq_without_cursors(&reconcile(
&doc.parent(),
&doc.left().text,
&doc.right().text,
&doc.left().text(),
&doc.right().text(),
));
}
}
@ -22,8 +22,8 @@ fn test_document_one_way_with_cursors() {
for doc in &get_all_documents() {
doc.assert_eq(&reconcile_with_cursors(
&doc.parent(),
doc.left(),
doc.right(),
&doc.left(),
&doc.right(),
));
}
}
@ -33,8 +33,8 @@ fn test_document_inverse_way_without_cursors() {
for doc in &get_all_documents() {
doc.assert_eq_without_cursors(&reconcile(
&doc.parent(),
&doc.right().text,
&doc.left().text,
&doc.right().text(),
&doc.left().text(),
));
}
}
@ -44,8 +44,8 @@ fn test_document_inverse_way_with_cursors() {
for doc in &get_all_documents() {
doc.assert_eq(&reconcile_with_cursors(
&doc.parent(),
doc.right(),
doc.left(),
&doc.right(),
&doc.left(),
));
}
}

View file

@ -1,9 +1,6 @@
#![cfg(feature = "wasm")]
use reconcile::wasm::{
lib::{is_binary, merge, merge_text, merge_text_with_cursors},
types::{JsCursorPosition, JsTextWithCursors},
};
use reconcile::{CursorPosition, TextWithCursors, wasm::*};
use wasm_bindgen_test::*;
#[wasm_bindgen_test(unsupported = test)]
@ -31,18 +28,18 @@ fn test_merge_text() {
fn test_merge_text_with_cursors() {
let result = merge_text_with_cursors(
"hi",
JsTextWithCursors::new("hi world".to_owned(), vec![]),
JsTextWithCursors::new(
&TextWithCursors::new("hi world".to_owned(), vec![]),
&TextWithCursors::new(
"hi".to_owned(),
vec![JsCursorPosition::new(0, 1), JsCursorPosition::new(1, 2)],
vec![CursorPosition::new(0, 1), CursorPosition::new(1, 2)],
),
);
assert_eq!(
result,
JsTextWithCursors::new(
TextWithCursors::new(
"hi world".to_owned(),
vec![JsCursorPosition::new(0, 1), JsCursorPosition::new(1, 2)]
vec![CursorPosition::new(0, 1), CursorPosition::new(1, 2)]
),
);
}