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 24, 2024
1 parent 1c8b373 commit c12143c
Showing 1 changed file with 8 additions and 29 deletions.
37 changes: 8 additions & 29 deletions tools/lsp/common/rename_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,27 +381,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 +416,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 +441,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 +482,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 +576,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 +691,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 +767,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 +790,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 +842,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

0 comments on commit c12143c

Please sign in to comment.