Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions rust/rubydex/src/test_utils/graph_test.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
use std::collections::HashMap;

use super::normalize_indentation;
#[cfg(test)]
use crate::diagnostic::Rule;
use crate::indexing::{self, LanguageId};
use crate::model::graph::{Graph, NameDependent};
use crate::model::ids::{NameId, StringId};
use crate::model::ids::{DefinitionId, NameId, StringId};
use crate::resolution::Resolver;

#[derive(Default)]
pub struct GraphTest {
graph: Graph,
sources: HashMap<String, String>,
}

impl GraphTest {
#[must_use]
pub fn new() -> Self {
Self { graph: Graph::new() }
Self {
graph: Graph::new(),
sources: HashMap::new(),
}
}

#[must_use]
Expand All @@ -31,12 +37,37 @@ impl GraphTest {
pub fn index_uri(&mut self, uri: &str, source: &str) {
let source = normalize_indentation(source);
indexing::index_source(&mut self.graph, uri, &source, &LanguageId::Ruby);
self.sources.insert(uri.to_string(), source);
}

/// Indexes an RBS source
pub fn index_rbs_uri(&mut self, uri: &str, source: &str) {
let source = normalize_indentation(source);
indexing::index_source(&mut self.graph, uri, &source, &LanguageId::Rbs);
self.sources.insert(uri.to_string(), source);
}

/// Returns the normalized source for the given URI.
///
/// # Panics
///
/// Panics if the URI has not been indexed.
#[must_use]
pub fn source(&self, uri: &str) -> &str {
self.sources.get(uri).expect("source not found for URI")
}

/// Returns the source text for a definition, sliced by its offset.
///
/// # Panics
///
/// Panics if the definition or its document does not exist.
#[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.

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.)

}

pub fn delete_uri(&mut self, uri: &str) {
Expand Down
Loading