@@ -6,7 +6,7 @@ import Testing
66 @Test func errorMessages( ) throws {
77 var source : String
88
9- var error = try #require ( throws : GraphQLError . self ) { try parse ( source: " { " ) }
9+ var error = try expectGraphQLError { try parse ( source: " { " ) }
1010 #expect(
1111 error. message == """
1212 Syntax Error GraphQL (1:2) Expected Name, found <EOF>
@@ -20,31 +20,31 @@ import Testing
2020 #expect( error. locations [ 0 ] . line == 1 )
2121 #expect( error. locations [ 0 ] . column == 2 )
2222
23- error = try #require ( throws : GraphQLError . self ) {
23+ error = try expectGraphQLError {
2424 try parse ( source: " { ...MissingOn } \n fragment MissingOn Type \n " )
2525 }
2626 #expect( error. message. contains (
2727 " Syntax Error GraphQL (2:20) Expected \" on \" , found Name \" Type \" "
2828 ) )
2929
30- error = try #require ( throws : GraphQLError . self ) { try parse ( source: " { field: {} } " ) }
30+ error = try expectGraphQLError { try parse ( source: " { field: {} } " ) }
3131 #expect( error. message. contains (
3232 " Syntax Error GraphQL (1:10) Expected Name, found { "
3333 ) )
3434
35- error = try #require ( throws : GraphQLError . self ) {
35+ error = try expectGraphQLError {
3636 try parse ( source: " notanoperation Foo { field } " )
3737 }
3838 #expect( error. message. contains (
3939 " Syntax Error GraphQL (1:1) Unexpected Name \" notanoperation \" "
4040 ) )
4141
42- error = try #require ( throws : GraphQLError . self ) { try parse ( source: " ... " ) }
42+ error = try expectGraphQLError { try parse ( source: " ... " ) }
4343 #expect( error. message. contains (
4444 " Syntax Error GraphQL (1:1) Unexpected ... "
4545 ) )
4646
47- error = try #require ( throws : GraphQLError . self ) {
47+ error = try expectGraphQLError {
4848 try parse ( source: Source (
4949 body: " query " ,
5050 name: " MyQuery.graphql "
@@ -56,24 +56,24 @@ import Testing
5656
5757 source = " query Foo($x: Complex = { a: { b: [ $var ] } }) { field } "
5858
59- error = try #require ( throws : GraphQLError . self ) { try parse ( source: source) }
59+ error = try expectGraphQLError { try parse ( source: source) }
6060 #expect( error. message. contains (
6161 " Syntax Error GraphQL (1:37) Unexpected $ "
6262 ) )
6363
64- error = try #require ( throws : GraphQLError . self ) {
64+ error = try expectGraphQLError {
6565 try parse ( source: " fragment on on on { on } " )
6666 }
6767 #expect( error. message. contains (
6868 " Syntax Error GraphQL (1:10) Unexpected Name \" on \" "
6969 ) )
7070
71- error = try #require ( throws : GraphQLError . self ) { try parse ( source: " { ...on } " ) }
71+ error = try expectGraphQLError { try parse ( source: " { ...on } " ) }
7272 #expect( error. message. contains (
7373 " Syntax Error GraphQL (1:9) Expected Name, found } "
7474 ) )
7575
76- error = try #require ( throws : GraphQLError . self ) {
76+ error = try expectGraphQLError {
7777 try parse (
7878 source: " type WithImplementsButNoTypes implements {} "
7979 )
@@ -82,7 +82,7 @@ import Testing
8282 " Syntax Error GraphQL (1:42) Expected Name, found { "
8383 ) )
8484
85- error = try #require ( throws : GraphQLError . self ) {
85+ error = try expectGraphQLError {
8686 try parse ( source: " type WithImplementsWithTrailingAmp implements AInterface & {} " )
8787 }
8888 #expect( error. message. contains (
@@ -460,3 +460,25 @@ import Testing
460460 #expect( document == expected)
461461 }
462462}
463+
464+ // This function exists because `error = #require(throwing: GraphQLError) { ... }` doesn't work
465+ // until Swift 6.1. Once we drop 6.0 support, we can change all calls of this to
466+ // `error = #require(throwing: GraphQLError) { ... }`
467+ private func expectGraphQLError< T> ( _ test: ( ) throws -> T ) throws -> GraphQLError {
468+ do {
469+ _ = try test ( )
470+ Issue . record ( " Parsing error expected " )
471+ throw ExpectGraphQLError . noErrorThrown
472+ } catch {
473+ guard let error = error as? GraphQLError else {
474+ Issue . record ( " Unexpected error \( error) " )
475+ throw ExpectGraphQLError . incorrectErrorType ( error)
476+ }
477+ return error
478+ }
479+ }
480+
481+ enum ExpectGraphQLError : Error {
482+ case noErrorThrown
483+ case incorrectErrorType( Error )
484+ }
0 commit comments