reconcile/tests/wasm.rs
Andras Schmelczer 5170bf02ca
All checks were successful
Check and publish / build (push) Successful in 11m5s
Check and publish / publish-crate (push) Has been skipped
Check and publish / publish-pypi (push) Has been skipped
Revert oversimplifications
2026-10-29 22:42:31 +00:00

80 lines
2.3 KiB
Rust

#![cfg(feature = "wasm")]
use reconcile_text::{BuiltinTokenizer, CursorPosition, EditedText, TextWithCursors, wasm::*};
use wasm_bindgen_test::*;
#[wasm_bindgen_test(unsupported = test)]
fn test_merge() {
let left = b"hello ";
let right = b"world";
let result = generic_reconcile(b"", left, right, BuiltinTokenizer::Word);
assert_eq!(result, b"hello world");
let left = b"\0binary";
let right = b"other";
let result = generic_reconcile(b"", left, right, BuiltinTokenizer::Word);
assert_eq!(result, right);
}
#[wasm_bindgen_test(unsupported = test)]
fn test_merge_text() {
let left = "hello ";
let right = "world";
let result = reconcile("", &left.into(), &right.into(), BuiltinTokenizer::Word).text();
assert_eq!(result, "hello world");
}
#[wasm_bindgen_test(unsupported = test)]
fn test_merge_text_with_cursors() {
let result = reconcile(
"hi",
&TextWithCursors::new("hi world".to_owned(), vec![]),
&TextWithCursors::new(
"hi".to_owned(),
vec![CursorPosition::new(0, 1), CursorPosition::new(1, 2)],
),
BuiltinTokenizer::Word,
);
assert_eq!(
result,
TextWithCursors::new(
"hi world".to_owned(),
vec![CursorPosition::new(0, 1), CursorPosition::new(1, 2)]
),
);
}
#[wasm_bindgen_test(unsupported = test)]
fn test_merge_binary() {
let left = [0, 1, 2];
let right = [3, 4, 5];
assert_eq!(
generic_reconcile(b"", &left, &right, BuiltinTokenizer::Word),
right
);
}
#[wasm_bindgen_test(unsupported = test)]
fn test_from_diff_rejects_oversized_lengths() {
for length in [1_i64 << 32, -(1_i64 << 40), i64::MAX, i64::MIN] {
let error = EditedText::from_diff("abc", vec![length.into()], &*BuiltinTokenizer::Word)
.unwrap_err();
assert!(error.to_string().starts_with("Invalid diff:"));
}
}
#[wasm_bindgen_test] // JsValue isn't supported outside of wasm
fn test_diff() {
let parent = "hello ";
let changed = "world";
let result = diff(parent, &changed.into(), BuiltinTokenizer::Word).unwrap();
assert_eq!(result.len(), 2);
let first: i64 = result[0].clone().try_into().unwrap();
let second: String = result[1].clone().try_into().unwrap();
assert_eq!(first, -6);
assert_eq!(second, "world");
}