Skip to content

Commit

Permalink
live-preview: Use production code in test
Browse files Browse the repository at this point in the history
... over implementing the same fucntionality again for the tests.
  • Loading branch information
hunger committed Jun 25, 2024
1 parent 539abfc commit 3d52486
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 30 deletions.
38 changes: 9 additions & 29 deletions tools/lsp/common/rename_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ mod tests {

use crate::common::test;
use crate::common::text_edit;
use crate::preview;

#[track_caller]
fn compile_test_changes(
Expand Down Expand Up @@ -381,27 +382,6 @@ mod tests {
changed_text
}

fn find_component_declared_identifier(
document_node: &syntax_nodes::Document,
name: &str,
) -> Option<syntax_nodes::DeclaredIdentifier> {
for c in document_node.Component() {
let cid = c.DeclaredIdentifier();
if cid.text().to_string().trim() == name {
return Some(cid);
}
}
for e in document_node.ExportsList() {
if let Some(c) = e.Component() {
let cid = c.DeclaredIdentifier();
if cid.text().to_string().trim() == name {
return Some(cid);
}
}
}
None
}

#[test]
fn test_rename_component_from_definition_ok() {
let document_cache = test::compile_test_with_sources(
Expand Down Expand Up @@ -437,7 +417,7 @@ export component Bar {
let doc = document_cache.get_document_by_path(&test::main_test_file_name()).unwrap();

let foo_identifier =
find_component_declared_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
preview::find_component_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
let edit = rename_component_from_definition(&document_cache, &foo_identifier, "XxxYyyZzz")
.unwrap();

Expand All @@ -462,7 +442,7 @@ export component Bar {
let doc = document_cache.get_document_by_path(&test::main_test_file_name()).unwrap();

let foo_identifier =
find_component_declared_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
preview::find_component_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
let edit =
rename_component_from_definition(&document_cache, &foo_identifier, "FooXXX").unwrap();

Expand Down Expand Up @@ -503,7 +483,7 @@ export { Foo as FExport }
document_cache.get_document_by_path(&test::test_file_name("source.slint")).unwrap();

let foo_identifier =
find_component_declared_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
preview::find_component_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
let edit = rename_component_from_definition(&document_cache, &foo_identifier, "XxxYyyZzz")
.unwrap();

Expand Down Expand Up @@ -597,7 +577,7 @@ export { Foo as User4Fxx }
document_cache.get_document_by_path(&test::test_file_name("source.slint")).unwrap();

let foo_identifier =
find_component_declared_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
preview::find_component_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
let edit = rename_component_from_definition(&document_cache, &foo_identifier, "XxxYyyZzz")
.unwrap();

Expand Down Expand Up @@ -712,7 +692,7 @@ export { Foo as User4Fxx }
document_cache.get_document_by_path(&test::test_file_name("s/source.slint")).unwrap();

let foo_identifier =
find_component_declared_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
preview::find_component_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
let edit = rename_component_from_definition(&document_cache, &foo_identifier, "XxxYyyZzz")
.unwrap();

Expand Down Expand Up @@ -788,7 +768,7 @@ export component Foo { }
document_cache.get_document_by_path(&test::test_file_name("user1.slint")).unwrap();

let foo_identifier =
find_component_declared_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
preview::find_component_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
let edit = rename_component_from_definition(&document_cache, &foo_identifier, "XxxYyyZzz")
.unwrap();

Expand All @@ -811,7 +791,7 @@ export component Foo { }
document_cache.get_document_by_path(&test::test_file_name("user2.slint")).unwrap();

let foo_identifier =
find_component_declared_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
preview::find_component_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
let edit = rename_component_from_definition(&document_cache, &foo_identifier, "XxxYyyZzz")
.unwrap();

Expand Down Expand Up @@ -863,7 +843,7 @@ export component Bar {
let doc = document_cache.get_document_by_path(&test::main_test_file_name()).unwrap();

let foo_identifier =
find_component_declared_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();
preview::find_component_identifier(doc.node.as_ref().unwrap(), "Foo").unwrap();

assert!(rename_component_from_definition(&document_cache, &foo_identifier, "Foo").is_err());
assert!(rename_component_from_definition(&document_cache, &foo_identifier, "UsedStruct")
Expand Down
2 changes: 1 addition & 1 deletion tools/lsp/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn add_new_component() {
}

/// Find the identifier that belongs to a component of the given `name` in the `document`
fn find_component_identifier(
pub fn find_component_identifier(
document: &syntax_nodes::Document,
name: &str,
) -> Option<syntax_nodes::DeclaredIdentifier> {
Expand Down

0 comments on commit 3d52486

Please sign in to comment.