Skip to content

Commit

Permalink
Add more tests of actual schemas to cynic-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
obmarg committed Jan 21, 2024
1 parent 7388e4a commit 24d6573
Show file tree
Hide file tree
Showing 13 changed files with 1,876 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cynic-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pretty = { version = "0.12", optional = true }
criterion = "0.4"
graphql-parser = "0.4"
insta = "1.29"
paste = "1"
similar-asserts = "1.5"

# Tests need the `print` functionality so enable it here
Expand Down
2 changes: 1 addition & 1 deletion cynic-parser/src/errors/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Error {
expected,
} => (
format!("unexpected {}", token),
Label::new(*start..*end).with_message("didn't expect to see this"),
Label::new(*dbg!(start)..*dbg!(end)).with_message("didn't expect to see this"),
Some(format!("expected one of {}", expected.join(", "))),
),
Error::ExtraToken {
Expand Down
5 changes: 3 additions & 2 deletions cynic-parser/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,16 +460,17 @@ impl<'a> Pretty<'a, BoxAllocator> for NodeDisplay<DirectiveDefinition<'a>> {
.parens()
.flat_alt(arguments.parens());

builder = builder.append(arguments).append(allocator.space())
builder = builder.append(arguments)
}

if self.0.is_repeatable() {
builder = builder
.append(allocator.text("repeatable"))
.append(allocator.space())
.append(allocator.text("repeatable"))
}

builder
.append(allocator.space())
.append(allocator.text("on"))
.append(allocator.space())
.append(allocator.intersperse(
Expand Down
46 changes: 46 additions & 0 deletions cynic-parser/tests/actual_schemas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
macro_rules! schema_tests {
($name: ident, $path: literal) => {
mod $name {
use similar_asserts::assert_eq;

#[allow(non_upper_case_globals)]
const SCHEMA: &str = include_str!($path);

#[test]
fn snapshot() {
let parsed = cynic_parser::parse_type_system_document(SCHEMA)
.map_err(|error| error.to_report(SCHEMA))
.unwrap();
insta::assert_snapshot!(parsed.to_sdl());
}

#[test]
fn double_roundtrip() {
let parsed = cynic_parser::parse_type_system_document(SCHEMA)
.map_err(|error| error.to_report(SCHEMA))
.unwrap();
let sdl = parsed.to_sdl();

let reparsed = cynic_parser::parse_type_system_document(&sdl)
.map_err(|error| error.to_report(&sdl))
.unwrap();

assert_eq!(sdl, reparsed.to_sdl());
}
}
};
}

schema_tests!(github, "../../schemas/github.graphql");

schema_tests!(books, "../../schemas/books.graphql");

schema_tests!(graphql_jobs, "../../schemas/graphql.jobs.graphql");

schema_tests!(raindancer, "../../schemas/raindancer.graphql");

schema_tests!(simple, "../../schemas/simple.graphql");

schema_tests!(starwars, "../../schemas/starwars.schema.graphql");

schema_tests!(test_cases, "../../schemas/test_cases.graphql");
7 changes: 0 additions & 7 deletions cynic-parser/tests/github.rs

This file was deleted.

43 changes: 43 additions & 0 deletions cynic-parser/tests/snapshots/actual_schemas__books__snapshot.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
source: cynic-parser/tests/actual_schemas.rs
expression: parsed.to_sdl()
---
schema {
query: QueryRoot
mutation: MutationRoot
subscription: SubscriptionRoot
}

directive @ifdef on FIELD

type Book {
id: String!
name: String!
author: String!
}

type BookChanged {
mutationType: MutationType!
id: ID!
book: Book
}

type MutationRoot {
createBook(name: String!, author: String!): ID!
deleteBook(id: ID!): Boolean!
}

enum MutationType {
CREATED
DELETED
}

type QueryRoot {
books: [Book!]!
}

type SubscriptionRoot {
interval(n: Int! = 1): Int!
books(mutationType: MutationType): BookChanged!
}

Loading

0 comments on commit 24d6573

Please sign in to comment.