From 31720d88257c1f0b0f2bb127a80b509db791b8be Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 14 Jul 2023 09:58:34 -0700 Subject: [PATCH] Add randomized syntax map test with elixir within heex within elixir --- .../src/syntax_map/syntax_map_tests.rs | 115 ++++++++---------- 1 file changed, 51 insertions(+), 64 deletions(-) diff --git a/crates/language/src/syntax_map/syntax_map_tests.rs b/crates/language/src/syntax_map/syntax_map_tests.rs index a6d35d0e2f..c7babf207e 100644 --- a/crates/language/src/syntax_map/syntax_map_tests.rs +++ b/crates/language/src/syntax_map/syntax_map_tests.rs @@ -763,11 +763,7 @@ fn test_empty_combined_injections_inside_injections() { } #[gpui::test(iterations = 50)] -fn test_random_syntax_map_edits(mut rng: StdRng) { - let operations = env::var("OPERATIONS") - .map(|i| i.parse().expect("invalid `OPERATIONS` variable")) - .unwrap_or(10); - +fn test_random_syntax_map_edits_rust_macros(rng: StdRng) { let text = r#" fn test_something() { let vec = vec![5, 1, 3, 8]; @@ -788,68 +784,12 @@ fn test_random_syntax_map_edits(mut rng: StdRng) { let registry = Arc::new(LanguageRegistry::test()); let language = Arc::new(rust_lang()); registry.add(language.clone()); - let mut buffer = Buffer::new(0, 0, text); - let mut syntax_map = SyntaxMap::new(); - syntax_map.set_language_registry(registry.clone()); - syntax_map.reparse(language.clone(), &buffer); - - let mut reference_syntax_map = SyntaxMap::new(); - reference_syntax_map.set_language_registry(registry.clone()); - - log::info!("initial text:\n{}", buffer.text()); - - for _ in 0..operations { - let prev_buffer = buffer.snapshot(); - let prev_syntax_map = syntax_map.snapshot(); - - buffer.randomly_edit(&mut rng, 3); - log::info!("text:\n{}", buffer.text()); - - syntax_map.interpolate(&buffer); - check_interpolation(&prev_syntax_map, &syntax_map, &prev_buffer, &buffer); - - syntax_map.reparse(language.clone(), &buffer); - - reference_syntax_map.clear(); - reference_syntax_map.reparse(language.clone(), &buffer); - } - - for i in 0..operations { - let i = operations - i - 1; - buffer.undo(); - log::info!("undoing operation {}", i); - log::info!("text:\n{}", buffer.text()); - - syntax_map.interpolate(&buffer); - syntax_map.reparse(language.clone(), &buffer); - - reference_syntax_map.clear(); - reference_syntax_map.reparse(language.clone(), &buffer); - assert_eq!( - syntax_map.layers(&buffer).len(), - reference_syntax_map.layers(&buffer).len(), - "wrong number of layers after undoing edit {i}" - ); - } - - let layers = syntax_map.layers(&buffer); - let reference_layers = reference_syntax_map.layers(&buffer); - for (edited_layer, reference_layer) in layers.into_iter().zip(reference_layers.into_iter()) { - assert_eq!( - edited_layer.node().to_sexp(), - reference_layer.node().to_sexp() - ); - assert_eq!(edited_layer.node().range(), reference_layer.node().range()); - } + test_random_edits(text, registry, language, rng); } #[gpui::test(iterations = 50)] -fn test_random_syntax_map_edits_with_combined_injections(mut rng: StdRng) { - let operations = env::var("OPERATIONS") - .map(|i| i.parse().expect("invalid `OPERATIONS` variable")) - .unwrap_or(10); - +fn test_random_syntax_map_edits_with_erb(rng: StdRng) { let text = r#"
<% if one?(:two) %> @@ -866,13 +806,60 @@ fn test_random_syntax_map_edits_with_combined_injections(mut rng: StdRng) {
"# .unindent() - .repeat(8); + .repeat(5); let registry = Arc::new(LanguageRegistry::test()); let language = Arc::new(erb_lang()); registry.add(language.clone()); registry.add(Arc::new(ruby_lang())); registry.add(Arc::new(html_lang())); + + test_random_edits(text, registry, language, rng); +} + +#[gpui::test(iterations = 50)] +fn test_random_syntax_map_edits_with_heex(rng: StdRng) { + let text = r#" + defmodule TheModule do + def the_method(assigns) do + ~H""" + <%= if @empty do %> +
+ <% else %> +
+
+
+
+
+
+
+ <% end %> + """ + end + end + "# + .unindent() + .repeat(3); + + let registry = Arc::new(LanguageRegistry::test()); + let language = Arc::new(elixir_lang()); + registry.add(language.clone()); + registry.add(Arc::new(heex_lang())); + registry.add(Arc::new(html_lang())); + + test_random_edits(text, registry, language, rng); +} + +fn test_random_edits( + text: String, + registry: Arc, + language: Arc, + mut rng: StdRng, +) { + let operations = env::var("OPERATIONS") + .map(|i| i.parse().expect("invalid `OPERATIONS` variable")) + .unwrap_or(10); + let mut buffer = Buffer::new(0, 0, text); let mut syntax_map = SyntaxMap::new();