Skip to content

Add source code and test helper in GraphTest#789

Closed
soutaro wants to merge 1 commit intomainfrom
soutaro-test_helpers
Closed

Add source code and test helper in GraphTest#789
soutaro wants to merge 1 commit intomainfrom
soutaro-test_helpers

Conversation

@soutaro
Copy link
Copy Markdown
Contributor

@soutaro soutaro commented May 7, 2026

Add GraphTest::source_of(definition_id), which the source code for the definition. The tests in #697 use these helpers.

Extracted from #697.

`GraphTest::source_of(definition_id)` returns the source code for the definition.
@soutaro soutaro requested a review from a team as a code owner May 7, 2026 07:50
let def = self.graph.definitions().get(definition_id).unwrap();
let uri = self.graph.documents().get(def.uri_id()).unwrap().uri();
let source = self.source(uri);
&source[def.offset().start() as usize..def.offset().end() as usize]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Morriar suggested to get the content through Document, but Document doesn't have the actual source code. (It has offset of each line, but doesn't have the source text.)

@soutaro
Copy link
Copy Markdown
Contributor Author

soutaro commented May 7, 2026

We won't need this test helper if the dealiase returns DeclarationId, not DefinitionId as in #779.

@soutaro soutaro self-assigned this May 7, 2026
#[must_use]
pub fn source_of(&self, definition_id: &DefinitionId) -> &str {
let def = self.graph.definitions().get(definition_id).unwrap();
let uri = self.graph.documents().get(def.uri_id()).unwrap().uri();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this method work with definitions coming from built_in.rs? They have uri but don't have document AFAIK. So it'll probably panic on this line?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. But I think we can easily avoid testing with builtins...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we'll intentionally test against the built in definitions. What could happen is that we build other helpers on top of this method, which takes a declaration and get all the sources of it. In that case it'd be reasonable to pass Object etc to it and then it'd explode.
We can likely prevent it by checking if the definition is a built in before invoking this.

}

pub fn delete_uri(&mut self, uri: &str) {
self.graph.delete_document(uri);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also update sources when we remove a uri?

@soutaro
Copy link
Copy Markdown
Contributor Author

soutaro commented May 7, 2026

The test utility is not used. Closing this PR for this time. Reopen if we need it in future.

@soutaro soutaro closed this May 7, 2026
@soutaro soutaro deleted the soutaro-test_helpers branch May 7, 2026 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants