-
Notifications
You must be signed in to change notification settings - Fork 3
/
GriftKitTests.swift
216 lines (166 loc) · 7.4 KB
/
GriftKitTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import Foundation
@testable import GriftKit
import SourceKittenFramework
import SwiftGraph
import XCTest
class GriftKitTests: XCTestCase {
// func testFolderGivesStructureArrayOfAllFilesInIt() {
//
// var path = "./example.swift.test"
// let thing = try! structure(forFile: path)
// print(thing)
//
// var args = ["-sdk",
// "/Applications/Xcode-9.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk",
// "-module-name",
// "Blah",
// "-c",
// path]
//
// let cursorInfo = try! Request.cursorInfo(file: path, offset: 489, arguments: args).send()
// print(cursorInfo)
//
//// {
//// "key.kind" : "source.lang.swift.expr.call",
//// "key.offset" : 489,
//// "key.nameoffset" : 489,
//// "key.namelength" : 5,
//// "key.bodyoffset" : 495,
//// "key.bodylength" : 0,
//// "key.length" : 7,
//// "key.name" : "thing"
//// }
// args = ["-sdk",
// "/Applications/Xcode-8.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk",
// "-module-name",
// "Blah",
// "-c",
// path]
//// args += files
//
// let index = Request.index(
// file: path,
// arguments:args)
// try! print(toJSON(index.send()))
// }
private func buildGraph(for code: String) throws -> UnweightedGraph<Vertex> {
return try GraphBuilder.build(structures: structures(for: code))
// return GraphBuilder.build(docs: docs(for: code))
// return GraphBuilder.build(syntax: syntaxMaps(for: code))
}
func testSingleStructWithNoFieldsCreatesSingleVertexGraph() throws {
let code = "struct Thing { }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 1)
XCTAssertEqual(graph.edgeCount, 0)
XCTAssertTrue(graph.vertexInGraph(vertex: "Thing"))
}
func testSingleStructSwiftCodeCreatesOneEdgeGraph() throws {
let code = "struct Thing { var x: String }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 2)
XCTAssertEqual(graph.edgeCount, 1)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "String"))
}
func testTwoStructSwiftCodeCreatesTwoEdgeGraphGraph() throws {
let code = "struct Thing { var x: String }; struct Foo { var bar: Int }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 4)
XCTAssertEqual(graph.edgeCount, 2)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "String"))
XCTAssertTrue(graph.edgeExists(from: "Foo", to: "Int"))
}
func testNestedStructSwiftCodeCreatesExpectedGraph() throws {
let code = "struct Thing { struct Foo {} var x: Foo }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 2)
XCTAssertEqual(graph.edgeCount, 1)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Foo"))
}
func testMoreComplexNestedStructSwiftCodeCreatesExpectedGraph() throws {
let code = "struct Thing { struct Foo { let s: Int } var x: Foo }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 3)
XCTAssertEqual(graph.edgeCount, 2)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Foo"))
XCTAssertTrue(graph.edgeExists(from: "Foo", to: "Int"))
}
func testStructWithFunctionParametersShowsParametersProperly() throws {
let code = "struct Thing { func foo(d: Double) { } }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 2)
XCTAssertEqual(graph.edgeCount, 1)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Double"))
}
func testClassWithFunctionParametersShowsParametersProperly() throws {
let code = "class Thing { func foo(d: Double) { } }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 2)
XCTAssertEqual(graph.edgeCount, 1)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Double"))
}
func testEnumWithFunctionParametersShowsParametersProperly() throws {
let code = "enum Thing { func foo(d: Double) { } }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 2)
XCTAssertEqual(graph.edgeCount, 1)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Double"))
}
func testProtocolWithFunctionParametersShowsParametersProperly() throws {
let code = "protocol Thing { func foo(d: Double) }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 2)
XCTAssertEqual(graph.edgeCount, 1)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Double"))
}
func testTwoReferencesToTheSameTypeOnlyYieldOneEdge() throws {
let code = "struct Thing { var x: String; var y: String }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 2)
XCTAssertEqual(graph.edgeCount, 1)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "String"))
}
func testThatGenericTypesPointToBothTypeAndGenericTypeParameter() throws {
let code = "struct Thing { var x: Array<String> }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 3)
XCTAssertEqual(graph.edgeCount, 2)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Array"))
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "String"))
}
func testArrayTypesAreNormalizedToNotHaveBrackets() throws {
let code = "struct Thing { var x: [String] }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 3)
XCTAssertEqual(graph.edgeCount, 2)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Array"))
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "String"))
}
func testThatGenericTypesWithMultipleGenericParamsPointToEachParameter() throws {
let code = "struct Thing { var x: Dictionary<String, Int> }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 4)
XCTAssertEqual(graph.edgeCount, 3)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Dictionary"))
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "String"))
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Int"))
}
func testThatDictionaryTypesAreNormalizedToNotHaveBracketsOrColons() throws {
let code = "open class Thing { var x: [String: Int] }"
let graph = try buildGraph(for: code)
XCTAssertEqual(graph.vertexCount, 4)
XCTAssertEqual(graph.edgeCount, 3)
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Dictionary"))
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "String"))
XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Int"))
}
// func testStructWithFunctionShowsFunctionReturnTypeProperly() throws {
// let code = "struct Thing { func foo() -> Double { return 0 } }"
//
// let graph = try buildGraph(for: code)
//
// XCTAssertEqual(graph.vertexCount, 2)
// XCTAssertEqual(graph.edgeCount, 1)
// XCTAssertTrue(graph.edgeExists(from: "Thing", to: "Double"))
// }
}