Skip to content

Commit 94cb7ec

Browse files
madsodgaardktoso
andauthored
Add "..." wildcard syntax to output assertions in JExtract tests (#362)
Co-authored-by: Konrad `ktoso` Malawski <[email protected]>
1 parent a720622 commit 94cb7ec

File tree

2 files changed

+64
-73
lines changed

2 files changed

+64
-73
lines changed

Tests/JExtractSwiftTests/Asserts/TextAssertions.swift

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,49 @@ func assertOutput(
124124
continue
125125
}
126126

127-
for (no, (g, e)) in zip(gotLines.dropFirst(matchingOutputOffset), expectedLines).enumerated() {
128-
if g.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).count == 0
129-
|| e.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).count == 0 {
130-
continue
131-
}
127+
var currentExpectedLine: Int = 0
128+
var currentGotLine: Int = matchingOutputOffset
129+
130+
outer: while currentExpectedLine < expectedLines.count {
131+
let expectedLine = expectedLines[currentExpectedLine].trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
132132

133-
let ge = g.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
134-
let ee = e.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
135-
if ge.commonPrefix(with: ee) != ee {
136-
diffLineNumbers.append(no + matchingOutputOffset)
133+
if expectedLine == "..." {
134+
// Ignore the rest of output, if last line is placeholder.
135+
guard currentExpectedLine != (expectedLines.count - 1) else {
136+
break
137+
}
137138

138-
#expect(ge == ee, sourceLocation: sourceLocation)
139+
let nextLine = expectedLines[currentExpectedLine + 1].trimmingCharacters(in: .whitespacesAndNewlines)
140+
while currentGotLine < gotLines.count {
141+
let gottenLine = gotLines[currentGotLine].trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
142+
if gottenLine.commonPrefix(with: nextLine) == nextLine {
143+
currentExpectedLine += 2
144+
break
145+
} else if nextLine == "..." {
146+
// Skip any following "..."
147+
currentExpectedLine += 1
148+
break
149+
} else {
150+
currentGotLine += 1
151+
}
152+
153+
if currentGotLine == gotLines.count {
154+
diffLineNumbers.append(currentExpectedLine + matchingOutputOffset + 1)
155+
Issue.record("Expected to find '\(nextLine)' after wildcard, but end of file reached.", sourceLocation: sourceLocation)
156+
break outer
157+
}
158+
}
159+
160+
currentGotLine += 1
161+
} else {
162+
let gottenLine = gotLines[currentGotLine].trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
163+
if gottenLine.commonPrefix(with: expectedLine) != expectedLine {
164+
diffLineNumbers.append(currentExpectedLine + matchingOutputOffset)
165+
166+
#expect(gottenLine == expectedLine, sourceLocation: sourceLocation)
167+
}
168+
currentGotLine += 1
169+
currentExpectedLine += 1
139170
}
140171
}
141172

@@ -155,9 +186,17 @@ func assertOutput(
155186
print("==== ---------------------------------------------------------------")
156187
print("Got output:")
157188
let printFromLineNo = matchingOutputOffset
158-
let printToLineNo = matchingOutputOffset + expectedLines.count
159-
for (n, g) in gotLines.enumerated() where n >= printFromLineNo && n <= printToLineNo {
160-
print("\(n): \(g)".red(if: diffLineNumbers.contains(n)))
189+
for (n, g) in gotLines.enumerated() where n >= printFromLineNo {
190+
let baseLine = "\(n): \(g)"
191+
var line = baseLine
192+
if diffLineNumbers.contains(n) {
193+
line += "\n"
194+
let leadingCount = "\(n): ".count
195+
let message = "\(String(repeating: " ", count: leadingCount))\(String(repeating: "^", count: 8)) EXPECTED MATCH OR SEARCHING FROM HERE "
196+
line += "\(message)\(String(repeating: "^", count: max(0, line.count - message.count)))"
197+
line = line.red
198+
}
199+
print(line)
161200
}
162201
print("==== ---------------------------------------------------------------\n")
163202
}

Tests/JExtractSwiftTests/JNI/JNIVariablesTests.swift

Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ struct JNIVariablesTests {
6969
"""
7070
@_cdecl("Java_com_example_swift_MyClass__00024getConstant__J")
7171
func Java_com_example_swift_MyClass__00024getConstant__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
72-
assert(self != 0, "self memory address was null")
73-
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
74-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
75-
guard let self$ else {
76-
fatalError("self memory address was null in call to \\(#function)!")
77-
}
72+
...
7873
return self$.pointee.constant.getJNIValue(in: environment!)
7974
}
8075
"""
@@ -134,23 +129,20 @@ struct JNIVariablesTests {
134129
@_cdecl("Java_com_example_swift_MyClass__00024getMutable__J")
135130
func Java_com_example_swift_MyClass__00024getMutable__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
136131
assert(self != 0, "self memory address was null")
137-
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
132+
...
138133
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
139134
guard let self$ else {
140135
fatalError("self memory address was null in call to \\(#function)!")
141136
}
137+
...
142138
return self$.pointee.mutable.getJNIValue(in: environment!)
143139
}
144140
""",
145141
"""
146142
@_cdecl("Java_com_example_swift_MyClass__00024setMutable__JJ")
147143
func Java_com_example_swift_MyClass__00024setMutable__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, newValue: jlong, self: jlong) {
148144
assert(self != 0, "self memory address was null")
149-
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
150-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
151-
guard let self$ else {
152-
fatalError("self memory address was null in call to \\(#function)!")
153-
}
145+
...
154146
self$.pointee.mutable = Int64(fromJNI: newValue, in: environment!)
155147
}
156148
"""
@@ -195,12 +187,7 @@ struct JNIVariablesTests {
195187
"""
196188
@_cdecl("Java_com_example_swift_MyClass__00024getComputed__J")
197189
func Java_com_example_swift_MyClass__00024getComputed__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
198-
assert(self != 0, "self memory address was null")
199-
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
200-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
201-
guard let self$ else {
202-
fatalError("self memory address was null in call to \\(#function)!")
203-
}
190+
...
204191
return self$.pointee.computed.getJNIValue(in: environment!)
205192
}
206193
""",
@@ -245,12 +232,7 @@ struct JNIVariablesTests {
245232
"""
246233
@_cdecl("Java_com_example_swift_MyClass__00024getComputedThrowing__J")
247234
func Java_com_example_swift_MyClass__00024getComputedThrowing__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
248-
assert(self != 0, "self memory address was null")
249-
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
250-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
251-
guard let self$ else {
252-
fatalError("self memory address was null in call to \\(#function)!")
253-
}
235+
...
254236
do {
255237
return try self$.pointee.computedThrowing.getJNIValue(in: environment!)
256238
} catch {
@@ -314,24 +296,14 @@ struct JNIVariablesTests {
314296
"""
315297
@_cdecl("Java_com_example_swift_MyClass__00024getGetterAndSetter__J")
316298
func Java_com_example_swift_MyClass__00024getGetterAndSetter__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
317-
assert(self != 0, "self memory address was null")
318-
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
319-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
320-
guard let self$ else {
321-
fatalError("self memory address was null in call to \\(#function)!")
322-
}
299+
...
323300
return self$.pointee.getterAndSetter.getJNIValue(in: environment!)
324301
}
325302
""",
326303
"""
327304
@_cdecl("Java_com_example_swift_MyClass__00024setGetterAndSetter__JJ")
328305
func Java_com_example_swift_MyClass__00024setGetterAndSetter__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, newValue: jlong, self: jlong) {
329-
assert(self != 0, "self memory address was null")
330-
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
331-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
332-
guard let self$ else {
333-
fatalError("self memory address was null in call to \\(#function)!")
334-
}
306+
...
335307
self$.pointee.getterAndSetter = Int64(fromJNI: newValue, in: environment!)
336308
}
337309
"""
@@ -390,24 +362,14 @@ struct JNIVariablesTests {
390362
"""
391363
@_cdecl("Java_com_example_swift_MyClass__00024isSomeBoolean__J")
392364
func Java_com_example_swift_MyClass__00024isSomeBoolean__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jboolean {
393-
assert(self != 0, "self memory address was null")
394-
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
395-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
396-
guard let self$ else {
397-
fatalError("self memory address was null in call to \\(#function)!")
398-
}
365+
...
399366
return self$.pointee.someBoolean.getJNIValue(in: environment!)
400367
}
401368
""",
402369
"""
403370
@_cdecl("Java_com_example_swift_MyClass__00024setSomeBoolean__ZJ")
404371
func Java_com_example_swift_MyClass__00024setSomeBoolean__ZJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, newValue: jboolean, self: jlong) {
405-
assert(self != 0, "self memory address was null")
406-
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
407-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
408-
guard let self$ else {
409-
fatalError("self memory address was null in call to \\(#function)!")
410-
}
372+
...
411373
self$.pointee.someBoolean = Bool(fromJNI: newValue, in: environment!)
412374
}
413375
"""
@@ -466,24 +428,14 @@ struct JNIVariablesTests {
466428
"""
467429
@_cdecl("Java_com_example_swift_MyClass__00024isBoolean__J")
468430
func Java_com_example_swift_MyClass__00024isBoolean__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jboolean {
469-
assert(self != 0, "self memory address was null")
470-
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
471-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
472-
guard let self$ else {
473-
fatalError("self memory address was null in call to \\(#function)!")
474-
}
431+
...
475432
return self$.pointee.isBoolean.getJNIValue(in: environment!)
476433
}
477434
""",
478435
"""
479436
@_cdecl("Java_com_example_swift_MyClass__00024setBoolean__ZJ")
480437
func Java_com_example_swift_MyClass__00024setBoolean__ZJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, newValue: jboolean, self: jlong) {
481-
assert(self != 0, "self memory address was null")
482-
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
483-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
484-
guard let self$ else {
485-
fatalError("self memory address was null in call to \\(#function)!")
486-
}
438+
...
487439
self$.pointee.isBoolean = Bool(fromJNI: newValue, in: environment!)
488440
}
489441
"""

0 commit comments

Comments
 (0)