Skip to content

Commit 47e7aaa

Browse files
committed
pretty print Map in debug mode
1 parent 4b036b1 commit 47e7aaa

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

Sources/GraphQL/Map/Map.swift

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,8 @@ extension Map : ExpressibleByDictionaryLiteral {
697697

698698
extension Map : CustomStringConvertible {
699699
public var description: String {
700+
var indentLevel = 0
701+
700702
let escapeMapping: [Character: String] = [
701703
"\r": "\\r",
702704
"\n": "\\n",
@@ -739,33 +741,74 @@ extension Map : CustomStringConvertible {
739741

740742
func serialize(array: [Map]) -> String {
741743
var string = "["
744+
745+
if _isDebugAssertConfiguration() {
746+
indentLevel += 1
747+
}
742748

743749
for index in 0 ..< array.count {
750+
if _isDebugAssertConfiguration() {
751+
string += "\n"
752+
string += indent()
753+
}
754+
744755
string += serialize(map: array[index])
745756

746757
if index != array.count - 1 {
747-
string += ","
758+
if _isDebugAssertConfiguration() {
759+
string += ", "
760+
} else {
761+
string += ","
762+
}
748763
}
749764
}
750-
751-
return string + "]"
765+
766+
if _isDebugAssertConfiguration() {
767+
indentLevel -= 1
768+
return string + "\n" + indent() + "]"
769+
} else {
770+
return string + "]"
771+
}
752772
}
753773

754774
func serialize(dictionary: [String: Map]) -> String {
755775
var string = "{"
756776
var index = 0
777+
778+
if _isDebugAssertConfiguration() {
779+
indentLevel += 1
780+
}
757781

758782
for (key, value) in dictionary.sorted(by: {$0.0 < $1.0}) {
759-
string += escape(key) + ":" + serialize(map: value)
760-
783+
if _isDebugAssertConfiguration() {
784+
string += "\n"
785+
string += indent()
786+
string += escape(key) + ": " + serialize(map: value)
787+
} else {
788+
string += escape(key) + ":" + serialize(map: value)
789+
}
790+
761791
if index != dictionary.count - 1 {
762-
string += ","
792+
if _isDebugAssertConfiguration() {
793+
string += ", "
794+
} else {
795+
string += ","
796+
}
763797
}
764798

765799
index += 1
766800
}
767-
768-
return string + "}"
801+
802+
if _isDebugAssertConfiguration() {
803+
indentLevel -= 1
804+
return string + "\n" + indent() + "}"
805+
} else {
806+
return string + "}"
807+
}
808+
}
809+
810+
func indent() -> String {
811+
return String(repeating: " ", count: indentLevel)
769812
}
770813

771814
return serialize(map: self)

Tests/GraphQLTests/HelloWorldTests/HelloWorldTests.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,29 @@ class HelloWorldTests : XCTestCase {
1616

1717
func testHello() throws {
1818
let query = "{ hello }"
19-
let expected: Map = ["hello": "world"]
19+
let expected: Map = [
20+
"data": [
21+
"hello": "world"
22+
]
23+
]
2024
let result = try graphql(schema: schema, request: query)
21-
XCTAssertEqual(result["data"], expected)
25+
XCTAssertEqual(result, expected)
2226
}
2327

2428
func testBoyhowdy() throws {
2529
let query = "{ boyhowdy }"
2630

27-
let expectedErrors: Map = [[
28-
"message": "Cannot query field \"boyhowdy\" on type \"RootQueryType\".",
29-
"locations": [["line": 1, "column": 3]]
30-
]]
31+
let expectedErrors: Map = [
32+
"errors": [
33+
[
34+
"message": "Cannot query field \"boyhowdy\" on type \"RootQueryType\".",
35+
"locations": [["line": 1, "column": 3]]
36+
]
37+
]
38+
]
3139

3240
let result = try graphql(schema: schema, request: query)
33-
XCTAssertEqual(result["errors"], expectedErrors)
41+
XCTAssertEqual(result, expectedErrors)
3442
}
3543
}
3644

0 commit comments

Comments
 (0)