forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextAssertions.swift
More file actions
298 lines (264 loc) · 10 KB
/
TextAssertions.swift
File metadata and controls
298 lines (264 loc) · 10 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import CodePrinting
import JExtractSwiftLib
import SwiftJavaConfigurationShared
import Testing
import struct Foundation.CharacterSet
enum RenderKind {
case swift
case java
}
func assertOutput(
dump: Bool = false,
input: String,
config: Configuration? = nil,
_ mode: JExtractGenerationMode,
_ renderKind: RenderKind,
swiftModuleName: String = "SwiftModule",
detectChunkByInitialLines _detectChunkByInitialLines: Int = 4,
javaClassLookupTable: [String: String] = [:],
expectedChunks: [String],
notExpectedChunks: [String] = [],
fileID: String = #fileID,
filePath: String = #filePath,
line: Int = #line,
column: Int = #column
) throws {
var config = config ?? Configuration()
config.swiftModule = swiftModuleName
let translator = Swift2JavaTranslator(config: config)
translator.dependenciesClasses = Array(javaClassLookupTable.keys)
try! translator.analyze(path: "/fake/Fake.swiftinterface", text: input)
let output: String
var printer: CodePrinter = CodePrinter(mode: .accumulateAll)
switch mode {
case .ffm:
let generator = FFMSwift2JavaGenerator(
config: config,
translator: translator,
javaPackage: "com.example.swift",
swiftOutputDirectory: "/fake",
javaOutputDirectory: "/fake"
)
switch renderKind {
case .swift:
try generator.writeSwiftThunkSources(printer: &printer)
case .java:
try generator.writeExportedJavaSources(printer: &printer)
}
case .jni:
let generator = JNISwift2JavaGenerator(
config: config,
translator: translator,
javaPackage: "com.example.swift",
swiftOutputDirectory: "/fake",
javaOutputDirectory: "/fake",
javaClassLookupTable: javaClassLookupTable,
moduleJavaPackages: [:]
)
switch renderKind {
case .swift:
try generator.writeSwiftThunkSources(&printer)
case .java:
try generator.writeExportedJavaSources(&printer)
}
}
output = printer.finalize()
let sourceLocation = SourceLocation(fileID: fileID, filePath: filePath, line: line, column: column)
for notExpectedChunk in notExpectedChunks {
let outputNotContainsNotExpectedChunk = !output.contains(notExpectedChunk)
#expect(
outputNotContainsNotExpectedChunk,
"""
\("error: Output must not contain not expected chunk!".red)
==== Not Expected output -----------------------------------------------
\(notExpectedChunk.yellow)
==== Got output ----------------------------------------------------
\(output)
==== ---------------------------------------------------------------
""",
sourceLocation: sourceLocation
)
}
let gotLines = output.split(separator: "\n").filter { l in
l.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).count > 0
}
for expectedChunk in expectedChunks {
let expectedLines = expectedChunk.split(separator: "\n")
let detectChunkByInitialLines = min(expectedLines.count, _detectChunkByInitialLines)
precondition(detectChunkByInitialLines > 0, "Chunk size to detect cannot be zero lines!")
var matchingOutputOffset: Int? = nil
let expectedInitialMatchingLines = expectedLines[0..<min(expectedLines.count, detectChunkByInitialLines)]
.map({ $0.trimmingCharacters(in: .whitespacesAndNewlines) })
.joined(separator: "\n")
for lineOffset in 0..<gotLines.count where gotLines.count > (lineOffset + detectChunkByInitialLines) {
let textLinesAtOffset = gotLines[lineOffset..<lineOffset + detectChunkByInitialLines]
.map({ $0.trimmingCharacters(in: .whitespacesAndNewlines) })
.joined(separator: "\n")
if textLinesAtOffset == expectedInitialMatchingLines {
matchingOutputOffset = lineOffset
break
}
}
let sourceLocation = SourceLocation(
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
var diffLineNumbers: [Int] = []
guard let matchingOutputOffset else {
let outputContainsExpectedChunk = output.contains(expectedChunk)
#expect(
outputContainsExpectedChunk,
"""
\("error: Output did not contain expected chunk!".red)
==== Expected output -----------------------------------------------
\(expectedChunk.yellow)
==== Got output ----------------------------------------------------
\(output)
==== ---------------------------------------------------------------
""",
sourceLocation: sourceLocation
)
continue
}
var currentExpectedLine: Int = 0
var currentGotLine: Int = matchingOutputOffset
outer: while currentExpectedLine < expectedLines.count {
let expectedLine = expectedLines[currentExpectedLine].trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
if expectedLine == "..." {
// Ignore the rest of output, if last line is placeholder.
guard currentExpectedLine != (expectedLines.count - 1) else {
break
}
let nextLine = expectedLines[currentExpectedLine + 1].trimmingCharacters(in: .whitespacesAndNewlines)
while currentGotLine < gotLines.count {
let gottenLine = gotLines[currentGotLine].trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
if gottenLine.commonPrefix(with: nextLine) == nextLine {
currentExpectedLine += 2
break
} else if nextLine == "..." {
// Skip any following "..."
currentExpectedLine += 1
break
} else {
currentGotLine += 1
}
if currentGotLine == gotLines.count {
diffLineNumbers.append(currentExpectedLine + matchingOutputOffset + 1)
Issue.record(
"Expected to find '\(nextLine)' after wildcard, but end of file reached.",
sourceLocation: sourceLocation
)
break outer
}
}
currentGotLine += 1
} else {
guard gotLines.count > currentGotLine else {
print("WARNING: index out of bounds when asserting text: \(currentGotLine)")
print("got lines ======")
print(gotLines.joined(separator: "\n").red)
currentExpectedLine += 1
continue
}
let gottenLine = gotLines[currentGotLine].trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
if gottenLine.commonPrefix(with: expectedLine) != expectedLine {
diffLineNumbers.append(currentExpectedLine + matchingOutputOffset)
#expect(gottenLine == expectedLine, sourceLocation: sourceLocation)
}
currentGotLine += 1
currentExpectedLine += 1
}
}
let hasDiff = diffLineNumbers.count > 0
if hasDiff || dump {
print("")
if hasDiff {
print("error: Number of not matching lines: \(diffLineNumbers.count)!".red)
print("==== Expected output -----------------------------------------------")
for (n, e) in expectedLines.enumerated() {
let isMismatch = diffLineNumbers.map({ $0 - matchingOutputOffset }).contains(n)
let marker = isMismatch ? " // <<<<<<<<<<< mismatch" : ""
print("\(n): \(e)\(marker)".yellow(if: isMismatch))
}
}
print("==== Got output ----------------------------------------------------")
let printFromLineNo = matchingOutputOffset
for (n, g) in gotLines.enumerated() where n >= printFromLineNo {
let baseLine = "\(n): \(g)"
print(baseLine)
}
print("==== ---------------------------------------------------------------\n")
}
}
}
func assertOutput(
dump: Bool = false,
_ got: String,
expected: String,
fileID: String = #fileID,
filePath: String = #filePath,
line: Int = #line,
column: Int = #column
) {
let gotLines = got.split(separator: "\n").filter { l in
l.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).count > 0
}
let expectedLines = expected.split(separator: "\n").filter { l in
l.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).count > 0
}
var diffLineNumbers: [Int] = []
for (no, (g, e)) in zip(gotLines, expectedLines).enumerated() {
if g.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).count == 0
|| e.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).count == 0
{
continue
}
let ge = g.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
let ee = e.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
if ge.commonPrefix(with: ee) != ee {
diffLineNumbers.append(no)
let sourceLocation = SourceLocation(
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
#expect(ge == ee, sourceLocation: sourceLocation)
}
}
let hasDiff = diffLineNumbers.count > 0
if hasDiff || dump {
print("")
if hasDiff {
print("error: Number of not matching lines: \(diffLineNumbers.count)!".red)
print("==== Expected output -----------------------------------------------")
for (n, e) in expectedLines.enumerated() {
let isMismatch = diffLineNumbers.contains(n)
let marker = isMismatch ? " // <<<<<<<< error: mismatch" : ""
print("\(e)\(marker)".yellow(if: isMismatch))
}
}
print("==== Got output ----------------------------------------------------")
for (n, g) in gotLines.enumerated() {
let isMismatch = diffLineNumbers.contains(n)
let marker = isMismatch ? "// <<<<<<<< error: mismatch" : ""
print("\(g)\(marker)".red(if: isMismatch))
}
print("==== ---------------------------------------------------------------\n")
}
}