Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler committed Jun 14, 2024
1 parent 322a5ad commit 1cb3855
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 42 deletions.
23 changes: 23 additions & 0 deletions compiler/crates/graphql-syntax/src/node/type_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub enum TypeSystemDefinition {
}

impl TypeSystemDefinition {
/// Returns the span for the entire type definition.
pub fn span(&self) -> Span {
match self {
TypeSystemDefinition::SchemaDefinition(definition) => definition.span,
Expand All @@ -59,6 +60,28 @@ impl TypeSystemDefinition {
TypeSystemDefinition::ScalarTypeExtension(extension) => extension.span,
}
}

/// Returns the span for the type definition name
/// or the span for the entire definition, if a name is non-existent.
pub fn name_span(&self) -> Span {
match self {
TypeSystemDefinition::SchemaDefinition(definition) => definition.span,
TypeSystemDefinition::SchemaExtension(extension) => extension.span,
TypeSystemDefinition::ObjectTypeDefinition(definition) => definition.name.span,
TypeSystemDefinition::ObjectTypeExtension(extension) => extension.name.span,
TypeSystemDefinition::InterfaceTypeDefinition(definition) => definition.name.span,
TypeSystemDefinition::InterfaceTypeExtension(extension) => extension.name.span,
TypeSystemDefinition::UnionTypeDefinition(definition) => definition.name.span,
TypeSystemDefinition::UnionTypeExtension(extension) => extension.name.span,
TypeSystemDefinition::DirectiveDefinition(definition) => definition.name.span,
TypeSystemDefinition::InputObjectTypeDefinition(definition) => definition.name.span,
TypeSystemDefinition::InputObjectTypeExtension(extension) => extension.name.span,
TypeSystemDefinition::EnumTypeDefinition(definition) => definition.name.span,
TypeSystemDefinition::EnumTypeExtension(extension) => extension.name.span,
TypeSystemDefinition::ScalarTypeDefinition(definition) => definition.name.span,
TypeSystemDefinition::ScalarTypeExtension(extension) => extension.name.span,
}
}
}

impl fmt::Display for TypeSystemDefinition {
Expand Down
114 changes: 78 additions & 36 deletions compiler/crates/relay-lsp/src/node_resolution_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn create_node_resolution_info_for_schema_document(
let definition = document
.definitions
.iter()
.find(|definition| definition.location().contains(position_span))
.find(|definition| definition.span().contains(position_span))
.ok_or(LSPRuntimeError::ExpectedError)?;

let mut node_resolution_info = NodeResolutionInfo::new(NodeKind::SchemaDocument);
Expand Down Expand Up @@ -398,17 +398,19 @@ mod test {
use common::SourceLocationKey;
use common::Span;
use graphql_syntax::parse_executable;
use graphql_syntax::parse_schema_document;
use intern::string_key::Intern;

use super::create_node_resolution_info;
use super::create_node_resolution_info_for_schema_document;
use super::NodeKind;
use super::NodeResolutionInfo;

fn parse_and_get_node_info(source: &str, pos: u32) -> NodeResolutionInfo {
fn parse_and_get_node_info(source: &str, sub_str: &str) -> NodeResolutionInfo {
let document =
parse_executable(source, SourceLocationKey::standalone("/test/file")).unwrap();

// Select the `uri` field
let pos = source.find(sub_str).unwrap() as u32;
let position_span = Span {
start: pos,
end: pos,
Expand All @@ -417,19 +419,59 @@ mod test {
create_node_resolution_info(document, position_span).unwrap()
}

fn parse_and_get_schema_document_node_info(source: &str, sub_str: &str) -> NodeResolutionInfo {
let document =
parse_schema_document(source, SourceLocationKey::standalone("/test/file")).unwrap();

let pos = source.find(sub_str).unwrap() as u32;
let position_span = Span {
start: pos,
end: pos,
};

create_node_resolution_info_for_schema_document(document, position_span).unwrap()
}

#[test]
fn create_node_resolution_info_for_schema_document_object_type_field() {
let node_resolution_info = parse_and_get_schema_document_node_info(
r#"
type Object {
uri: String
}
"#,
"uri",
);

assert_eq!(node_resolution_info.kind, NodeKind::FieldName);
}

#[test]
fn create_node_resolution_info_for_schema_document_interface_field() {
let node_resolution_info = parse_and_get_schema_document_node_info(
r#"
interface Interface {
uri: String
}
"#,
"uri",
);

assert_eq!(node_resolution_info.kind, NodeKind::FieldName);
}

#[test]
fn create_node_resolution_info_test() {
let node_resolution_info = parse_and_get_node_info(
r#"
fragment User_data on User {
name
profile_picture {
uri
fragment User_data on User {
name
profile_picture {
uri
}
}
}
"#,
// Select the `uri` field
117,
"#,
"uri",
);

assert_eq!(node_resolution_info.kind, NodeKind::FieldName);
Expand All @@ -439,15 +481,18 @@ mod test {
fn create_node_resolution_info_test_position_outside() {
let document = parse_executable(
r#"
fragment User_data on User {
name
}
"#,
fragment User_data on User {
name
}
"#,
SourceLocationKey::standalone("/test/file"),
)
.unwrap();
// Position is outside of the document
let position_span = Span { start: 86, end: 87 };
let position_span = Span {
start: 1000,
end: 1001,
};
let result = create_node_resolution_info(document, position_span);
assert!(result.is_err());
}
Expand All @@ -456,12 +501,11 @@ mod test {
fn create_node_resolution_info_fragment_def_name() {
let node_resolution_info = parse_and_get_node_info(
r#"
fragment User_data on User {
name
}
"#,
// Select the `User_data` fragment name
26,
fragment User_data on User {
name
}
"#,
"User_data",
);

match node_resolution_info.kind {
Expand All @@ -476,12 +520,11 @@ mod test {
fn create_node_resolution_info_fragment_def_type_condition() {
let node_resolution_info = parse_and_get_node_info(
r#"
fragment User_data on User {
name
}
"#,
// Select the `User` type in fragment declaration
35,
fragment Test on User {
name
}
"#,
"User",
);

assert_eq!(
Expand All @@ -494,15 +537,14 @@ mod test {
fn create_node_resolution_info_inline_fragment_type_condition() {
let node_resolution_info = parse_and_get_node_info(
r#"
fragment User_data on User {
name
... on User {
id
fragment Test on Person {
name
... on User {
id
}
}
}
"#,
// Select the `User` type in fragment declaration
84,
"#,
"User",
);

assert_eq!(
Expand Down
4 changes: 0 additions & 4 deletions compiler/crates/resolution-path/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub(super) fn test_resolution(source: &str, sub_str: &str, cb: impl Fn(&Resoluti
.unwrap();

let pos = source.find(sub_str).unwrap() as u32;

// Select the `uri` field
let position_span = Span {
start: pos,
end: pos,
Expand All @@ -47,8 +45,6 @@ pub(super) fn test_schema_resolution(
parse_schema_document(source, SourceLocationKey::standalone("/test/file")).unwrap();

let pos = source.find(sub_str).unwrap() as u32;

// Select the `uri` field
let position_span = Span {
start: pos,
end: pos,
Expand Down
6 changes: 4 additions & 2 deletions compiler/crates/schema/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,10 @@ impl InMemorySchema {
let mut insert_into_type_map = |name: StringKey, type_: Type| {
match type_map.entry(name) {
Entry::Occupied(existing_entry) => {
duplicate_definitions
.push((*existing_entry.get(), location.with_span(definition.span())));
duplicate_definitions.push((
*existing_entry.get(),
location.with_span(definition.name_span()),
));
}
Entry::Vacant(vacant) => {
vacant.insert(type_);
Expand Down

0 comments on commit 1cb3855

Please sign in to comment.