Skip to content

Commit a9cdbd8

Browse files
refactor: Changes some strings to multiline
1 parent 3194f6a commit a9cdbd8

File tree

4 files changed

+349
-127
lines changed

4 files changed

+349
-127
lines changed

Tests/GraphQLTests/LanguageTests/LexerTests.swift

Lines changed: 201 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ class LexerTests: XCTestCase {
4949
}
5050

5151
func testSkipsWhitespace() throws {
52-
let token = try lexOne("\n\n foo\n\n")
52+
let token = try lexOne("""
53+
54+
55+
foo
56+
57+
58+
""")
5359

5460
let expected = Token(
5561
kind: .name,
@@ -64,7 +70,10 @@ class LexerTests: XCTestCase {
6470
}
6571

6672
func testSkipsComments() throws {
67-
let token = try lexOne(" #comment\r\n foo#comment")
73+
let token = try lexOne("""
74+
#comment\r
75+
foo#comment
76+
""")
6877

6978
let expected = Token(
7079
kind: .name,
@@ -94,7 +103,13 @@ class LexerTests: XCTestCase {
94103
}
95104

96105
func testErrorsRespectWhitespaces() throws {
97-
XCTAssertThrowsError(try lexOne("\n\n?\n\n"))
106+
XCTAssertThrowsError(try lexOne("""
107+
108+
109+
?
110+
111+
112+
"""))
98113
// 'Syntax Error GraphQL (3:5) Unexpected character "?".\n' +
99114
// '\n' +
100115
// '2: \n' +
@@ -218,7 +233,10 @@ class LexerTests: XCTestCase {
218233
XCTAssertThrowsError(try lexOne("\"null-byte is not \u{0000} end of file\""))
219234
// "Syntax Error GraphQL (1:19) Invalid character within String: "\\u0000"."
220235

221-
XCTAssertThrowsError(try lexOne("\"multi\nline\""))
236+
XCTAssertThrowsError(try lexOne("""
237+
"multi
238+
line"
239+
"""))
222240
// "Syntax Error GraphQL (1:7) Unterminated string"
223241

224242
XCTAssertThrowsError(try lexOne("\"multi\rline\""))
@@ -700,7 +718,12 @@ class LexerTests: XCTestCase {
700718
}
701719

702720
func testDoubleLinkedList() throws {
703-
let q = "{\n #comment\n field\n }"
721+
let q = """
722+
{
723+
#comment
724+
field
725+
}
726+
"""
704727
let lexer = createLexer(source: Source(body: q))
705728

706729
let startToken = lexer.token
@@ -749,28 +772,83 @@ class LexerTests: XCTestCase {
749772

750773
func testBlockStringIndentAndBlankLine() throws {
751774
let rawString =
752-
"\n\n\n TopLevel {\n indented\n alsoIndented\n }\n\n\n\t\t\n"
775+
"""
776+
777+
778+
779+
TopLevel {
780+
indented
781+
alsoIndented
782+
}
783+
784+
785+
\t\t
786+
787+
"""
753788
let cleanedString = blockStringValue(rawValue: rawString)
754789

755-
XCTAssertEqual(cleanedString, "TopLevel {\n indented\n alsoIndented\n}")
790+
XCTAssertEqual(cleanedString, """
791+
TopLevel {
792+
indented
793+
alsoIndented
794+
}
795+
""")
756796
}
757797

758798
func testBlockStringDoubleIndentAndBlankLine() throws {
759799
let rawString =
760-
"\n\n\n TopLevel {\n indented: {\n foo: String\n }\n alsoIndented\n }\n\n\n\t\t\n"
800+
"""
801+
802+
803+
804+
TopLevel {
805+
indented: {
806+
foo: String
807+
}
808+
alsoIndented
809+
}
810+
811+
812+
\t\t
813+
814+
"""
761815
let cleanedString = blockStringValue(rawValue: rawString)
762816

763817
XCTAssertEqual(
764818
cleanedString,
765-
"TopLevel {\n indented: {\n foo: String\n }\n alsoIndented\n}"
819+
"""
820+
TopLevel {
821+
indented: {
822+
foo: String
823+
}
824+
alsoIndented
825+
}
826+
"""
766827
)
767828
}
768829

769830
func testBlockStringIndentAndBlankLineFirstLineNotIndent() throws {
770-
let rawString = "\n\n\nTopLevel {\n indented\n alsoIndented\n}\n\n\n\t\t\n"
831+
let rawString = """
832+
833+
834+
835+
TopLevel {
836+
indented
837+
alsoIndented
838+
}
839+
840+
841+
\t\t
842+
843+
"""
771844
let cleanedString = blockStringValue(rawValue: rawString)
772845

773-
XCTAssertEqual(cleanedString, "TopLevel {\n indented\n alsoIndented\n}")
846+
XCTAssertEqual(cleanedString, """
847+
TopLevel {
848+
indented
849+
alsoIndented
850+
}
851+
""")
774852
}
775853

776854
func testBlockStringIndentBlankLineFirstLineNotIndentWeird() throws {
@@ -787,21 +865,43 @@ class LexerTests: XCTestCase {
787865
"""
788866
let cleanedString = blockStringValue(rawValue: rawString)
789867

790-
XCTAssertEqual(cleanedString, "TopLevel {\n indented\n alsoIndented\n}")
868+
XCTAssertEqual(cleanedString, """
869+
TopLevel {
870+
indented
871+
alsoIndented
872+
}
873+
""")
791874
}
792875

793876
func testBlockStringIndentMultilineWithSingleSpaceIndent() throws {
794-
let rawString = " Multi-line string\n With Inner \"foo\" \n should be Valid "
877+
let rawString = """
878+
Multi-line string
879+
With Inner \"foo\"
880+
should be Valid
881+
"""
795882
let cleanedString = blockStringValue(rawValue: rawString)
796883

797-
XCTAssertEqual(cleanedString, " Multi-line string\nWith Inner \"foo\" \nshould be Valid ")
884+
XCTAssertEqual(cleanedString, """
885+
Multi-line string
886+
With Inner \"foo\"
887+
should be Valid
888+
""")
798889
}
799890

800891
func testBlockStringIndentMultilineWithSingleSpaceIndentExtraLines() throws {
801-
let rawString = "\n Multi-line string\n With Inner \"foo\" \n should be Valid \n"
892+
let rawString = """
893+
894+
Multi-line string
895+
With Inner \"foo\"
896+
should be Valid
897+
"""
802898
let cleanedString = blockStringValue(rawValue: rawString)
803899

804-
XCTAssertEqual(cleanedString, "Multi-line string\nWith Inner \"foo\" \nshould be Valid ")
900+
XCTAssertEqual(cleanedString, """
901+
Multi-line string
902+
With Inner \"foo\"
903+
should be Valid
904+
""")
805905
}
806906

807907
// Lexer tests for Blockstring token parsing
@@ -820,7 +920,15 @@ class LexerTests: XCTestCase {
820920
XCTAssertEqual(
821921
token,
822922
expected,
823-
"\nexpected: \n \(dump(expected))\n\ngot: \n\(dump(token))\n"
923+
"""
924+
925+
expected:
926+
\(dump(expected))
927+
928+
got:
929+
\(dump(token))
930+
931+
"""
824932
)
825933
}
826934

@@ -838,13 +946,25 @@ class LexerTests: XCTestCase {
838946
end: 61,
839947
line: 1,
840948
column: 1,
841-
value: "Multi-line string\nWith Inner \"foo\"\nshould be Valid"
949+
value: """
950+
Multi-line string
951+
With Inner \"foo\"
952+
should be Valid
953+
"""
842954
)
843955

844956
XCTAssertEqual(
845957
token,
846958
expected,
847-
"\nexpected: \n \(dump(expected))\n\ngot: \n\(dump(token))\n"
959+
"""
960+
961+
expected:
962+
\(dump(expected))
963+
964+
got:
965+
\(dump(token))
966+
967+
"""
848968
)
849969
}
850970

@@ -863,10 +983,20 @@ class LexerTests: XCTestCase {
863983
end: 59,
864984
line: 1,
865985
column: 1,
866-
value: " Multi-line string\nwith Inner \"foo\"\nshould be valid"
986+
value: """
987+
Multi-line string
988+
with Inner "foo"
989+
should be valid
990+
"""
867991
)
868992

869-
XCTAssertEqual(token, expected, "expected: \n \(dump(expected))\ngot: \n\(dump(token))\n")
993+
XCTAssertEqual(token, expected, """
994+
expected:
995+
\(dump(expected))
996+
got:
997+
\(dump(token))
998+
999+
""")
8701000
}
8711001

8721002
func testBlockStringUnescapedReturnsIndentTest() throws {
@@ -885,10 +1015,21 @@ class LexerTests: XCTestCase {
8851015
end: 79,
8861016
line: 1,
8871017
column: 1,
888-
value: "Multi-line string {\n with Inner \"foo\"\n should be valid indented\n}"
1018+
value: """
1019+
Multi-line string {
1020+
with Inner \"foo\"
1021+
should be valid indented
1022+
}
1023+
"""
8891024
)
8901025

891-
XCTAssertEqual(token, expected, "expected: \n \(dump(expected))\ngot: \n\(dump(token))\n")
1026+
XCTAssertEqual(token, expected, """
1027+
expected:
1028+
\(dump(expected))
1029+
got:
1030+
\(dump(token))
1031+
1032+
""")
8921033
}
8931034

8941035
func testIndentedBlockStringWithIndents() throws {
@@ -910,26 +1051,47 @@ class LexerTests: XCTestCase {
9101051
end: 103,
9111052
line: 1,
9121053
column: 5,
913-
value: "Multi-line string {\n with Inner \"foo\"\n should be valid indented\n}"
1054+
value: """
1055+
Multi-line string {
1056+
with Inner \"foo\"
1057+
should be valid indented
1058+
}
1059+
"""
9141060
)
9151061

9161062
print(sourceStr)
9171063

918-
XCTAssertEqual(token, expected, "expected: \n \(dump(expected))\ngot: \n\(dump(token))\n")
1064+
XCTAssertEqual(token, expected, """
1065+
expected:
1066+
\(dump(expected))
1067+
got:
1068+
\(dump(token))
1069+
1070+
""")
9191071
}
9201072

9211073
// Test empty strings & multi-line string lexer token parsing
9221074

9231075
func testEmptyQuote() throws {
9241076
let token = try lexOne(#" "" "#)
9251077
let expected = Token(kind: .string, start: 1, end: 3, line: 1, column: 2, value: "")
926-
XCTAssertEqual(token, expected, "\n\(dump(expected))\n\(dump(token))\n")
1078+
XCTAssertEqual(token, expected, """
1079+
1080+
\(dump(expected))
1081+
\(dump(token))
1082+
1083+
""")
9271084
}
9281085

9291086
func testEmptySimpleBlockString() throws {
9301087
let token = try lexOne(#" """""" "#)
9311088
let expected = Token(kind: .blockstring, start: 1, end: 7, line: 1, column: 2, value: "")
932-
XCTAssertEqual(token, expected, "\n\(dump(expected))\n\(dump(token))\n")
1089+
XCTAssertEqual(token, expected, """
1090+
1091+
\(dump(expected))
1092+
\(dump(token))
1093+
1094+
""")
9331095
}
9341096

9351097
func testEmptyTrimmedCharactersBlockString() throws {
@@ -938,7 +1100,12 @@ class LexerTests: XCTestCase {
9381100
"""
9391101
"""#)
9401102
let expected = Token(kind: .blockstring, start: 0, end: 7, line: 1, column: 1, value: "")
941-
XCTAssertEqual(token, expected, "\n\(dump(expected))\n\(dump(token))\n")
1103+
XCTAssertEqual(token, expected, """
1104+
1105+
\(dump(expected))
1106+
\(dump(token))
1107+
1108+
""")
9421109
}
9431110

9441111
func testEscapedTripleQuoteInBlockString() throws {
@@ -955,6 +1122,11 @@ class LexerTests: XCTestCase {
9551122
column: 1,
9561123
value: "\"\"\""
9571124
)
958-
XCTAssertEqual(token, expected, "\n\(dump(expected))\n\(dump(token))\n")
1125+
XCTAssertEqual(token, expected, """
1126+
1127+
\(dump(expected))
1128+
\(dump(token))
1129+
1130+
""")
9591131
}
9601132
}

Tests/GraphQLTests/LanguageTests/ParserTests.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ class ParserTests: XCTestCase {
1212

1313
XCTAssertEqual(
1414
error.message,
15-
"Syntax Error GraphQL (1:2) Expected Name, found <EOF>\n\n" +
16-
" 1: {\n" +
17-
" ^\n"
15+
"""
16+
Syntax Error GraphQL (1:2) Expected Name, found <EOF>
17+
18+
1: {
19+
^
20+
21+
"""
1822
)
1923

2024
XCTAssertEqual(error.positions, [1])

0 commit comments

Comments
 (0)