Skip to content

Commit 130e6f1

Browse files
committed
Fix intValue() that always failed
Remove unused error cases
1 parent 023af5a commit 130e6f1

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

Sources/GraphQL/Map/Map.swift

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ public enum MapError : Error {
44
case incompatibleType
55
case outOfBounds
66
case valueNotFound
7-
case notMapInitializable(Any.Type)
8-
case notMapRepresentable(Any.Type)
9-
case notMapDictionaryKeyInitializable(Any.Type)
10-
case notMapDictionaryKeyRepresentable(Any.Type)
11-
case cannotInitialize(type: Any.Type, from: Any.Type)
127
}
138

149
// MARK: Map
@@ -229,27 +224,27 @@ extension Map {
229224

230225
extension Map {
231226
public var bool: Bool? {
232-
return try? get()
227+
return try? boolValue()
233228
}
234229

235230
public var int: Int? {
236-
return try? (get() as Number).intValue
231+
return try? intValue()
237232
}
238233

239234
public var double: Double? {
240-
return try? (get() as Number).doubleValue
235+
return try? doubleValue()
241236
}
242237

243238
public var string: String? {
244-
return try? get()
239+
return try? stringValue()
245240
}
246241

247242
public var array: [Map]? {
248-
return try? get()
243+
return try? arrayValue()
249244
}
250245

251246
public var dictionary: [String: Map]? {
252-
return try? get()
247+
return try? dictionaryValue()
253248
}
254249
}
255250

@@ -288,7 +283,7 @@ extension Map {
288283

289284
public func intValue(converting: Bool = false) throws -> Int {
290285
guard converting else {
291-
return try get()
286+
return try (get() as Number).intValue
292287
}
293288

294289
switch self {
@@ -312,7 +307,7 @@ extension Map {
312307

313308
public func doubleValue(converting: Bool = false) throws -> Double {
314309
guard converting else {
315-
return try get()
310+
return try (get() as Number).doubleValue
316311
}
317312

318313
switch self {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
@testable import GraphQL
2+
import XCTest
3+
4+
class MapTests: XCTestCase {
5+
func testThrowableConversion() throws {
6+
XCTAssertEqual(try Map.number(5).intValue(), 5)
7+
XCTAssertEqual(try Map.number(3.14).doubleValue(), 3.14)
8+
XCTAssertEqual(try Map.bool(false).boolValue(), false)
9+
XCTAssertEqual(try Map.bool(true).boolValue(), true)
10+
XCTAssertEqual(try Map.string("Hello world").stringValue(), "Hello world")
11+
12+
}
13+
14+
func testOptionalConversion() {
15+
XCTAssertEqual(Map.number(5).int, 5)
16+
XCTAssertEqual(Map.number(3.14).double, 3.14)
17+
XCTAssertEqual(Map.bool(false).bool, false)
18+
XCTAssertEqual(Map.bool(true).bool, true)
19+
XCTAssertEqual(Map.string("Hello world").string, "Hello world")
20+
}
21+
22+
func testArrayConversion() throws {
23+
let map = Map.array([.number(1), .number(4), .number(9)])
24+
XCTAssertEqual(map.array?.count, 3)
25+
26+
let array = try map.arrayValue()
27+
XCTAssertEqual(array.count, 3)
28+
29+
XCTAssertEqual(try array[0].intValue(), 1)
30+
XCTAssertEqual(try array[1].intValue(), 4)
31+
XCTAssertEqual(try array[2].intValue(), 9)
32+
}
33+
34+
func testDictionaryConversion() throws {
35+
let map = Map.dictionary(
36+
[
37+
"first": .number(1),
38+
"second": .number(4),
39+
"third": .number(9)
40+
]
41+
)
42+
XCTAssertEqual(map.dictionary?.count, 3)
43+
44+
let dictionary = try map.dictionaryValue()
45+
46+
XCTAssertEqual(dictionary.count, 3)
47+
XCTAssertEqual(try dictionary["first"]?.intValue(), 1)
48+
XCTAssertEqual(try dictionary["second"]?.intValue(), 4)
49+
XCTAssertEqual(try dictionary["third"]?.intValue(), 9)
50+
}
51+
}

0 commit comments

Comments
 (0)