Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 52 additions & 13 deletions Tests/JExtractSwiftTests/Asserts/TextAssertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,49 @@ func assertOutput(
continue
}

for (no, (g, e)) in zip(gotLines.dropFirst(matchingOutputOffset), expectedLines).enumerated() {
if g.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).count == 0
|| e.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).count == 0 {
continue
}
var currentExpectedLine: Int = 0
var currentGotLine: Int = matchingOutputOffset

outer: while currentExpectedLine < expectedLines.count {
let expectedLine = expectedLines[currentExpectedLine].trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)

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

#expect(ge == ee, sourceLocation: sourceLocation)
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 {
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
}
}

Expand All @@ -155,9 +186,17 @@ func assertOutput(
print("==== ---------------------------------------------------------------")
print("Got output:")
let printFromLineNo = matchingOutputOffset
let printToLineNo = matchingOutputOffset + expectedLines.count
for (n, g) in gotLines.enumerated() where n >= printFromLineNo && n <= printToLineNo {
print("\(n): \(g)".red(if: diffLineNumbers.contains(n)))
for (n, g) in gotLines.enumerated() where n >= printFromLineNo {
let baseLine = "\(n): \(g)"
var line = baseLine
if diffLineNumbers.contains(n) {
line += "\n"
let leadingCount = "\(n): ".count
let message = "\(String(repeating: " ", count: leadingCount))\(String(repeating: "^", count: 8)) EXPECTED MATCH OR SEARCHING FROM HERE "
line += "\(message)\(String(repeating: "^", count: max(0, line.count - message.count)))"
line = line.red
}
print(line)
}
print("==== ---------------------------------------------------------------\n")
}
Expand Down
72 changes: 12 additions & 60 deletions Tests/JExtractSwiftTests/JNI/JNIVariablesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ struct JNIVariablesTests {
"""
@_cdecl("Java_com_example_swift_MyClass__00024getConstant__J")
func Java_com_example_swift_MyClass__00024getConstant__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
assert(self != 0, "self memory address was null")
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
guard let self$ else {
fatalError("self memory address was null in call to \\(#function)!")
}
...
return self$.pointee.constant.getJNIValue(in: environment!)
}
"""
Expand Down Expand Up @@ -134,23 +129,20 @@ struct JNIVariablesTests {
@_cdecl("Java_com_example_swift_MyClass__00024getMutable__J")
func Java_com_example_swift_MyClass__00024getMutable__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
assert(self != 0, "self memory address was null")
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
...
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
guard let self$ else {
fatalError("self memory address was null in call to \\(#function)!")
}
...
return self$.pointee.mutable.getJNIValue(in: environment!)
}
""",
"""
@_cdecl("Java_com_example_swift_MyClass__00024setMutable__JJ")
func Java_com_example_swift_MyClass__00024setMutable__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, newValue: jlong, self: jlong) {
assert(self != 0, "self memory address was null")
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
guard let self$ else {
fatalError("self memory address was null in call to \\(#function)!")
}
...
self$.pointee.mutable = Int64(fromJNI: newValue, in: environment!)
}
"""
Expand Down Expand Up @@ -195,12 +187,7 @@ struct JNIVariablesTests {
"""
@_cdecl("Java_com_example_swift_MyClass__00024getComputed__J")
func Java_com_example_swift_MyClass__00024getComputed__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
assert(self != 0, "self memory address was null")
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
guard let self$ else {
fatalError("self memory address was null in call to \\(#function)!")
}
...
return self$.pointee.computed.getJNIValue(in: environment!)
}
""",
Expand Down Expand Up @@ -245,12 +232,7 @@ struct JNIVariablesTests {
"""
@_cdecl("Java_com_example_swift_MyClass__00024getComputedThrowing__J")
func Java_com_example_swift_MyClass__00024getComputedThrowing__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
assert(self != 0, "self memory address was null")
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
guard let self$ else {
fatalError("self memory address was null in call to \\(#function)!")
}
...
do {
return try self$.pointee.computedThrowing.getJNIValue(in: environment!)
} catch {
Expand Down Expand Up @@ -314,24 +296,14 @@ struct JNIVariablesTests {
"""
@_cdecl("Java_com_example_swift_MyClass__00024getGetterAndSetter__J")
func Java_com_example_swift_MyClass__00024getGetterAndSetter__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
assert(self != 0, "self memory address was null")
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
guard let self$ else {
fatalError("self memory address was null in call to \\(#function)!")
}
...
return self$.pointee.getterAndSetter.getJNIValue(in: environment!)
}
""",
"""
@_cdecl("Java_com_example_swift_MyClass__00024setGetterAndSetter__JJ")
func Java_com_example_swift_MyClass__00024setGetterAndSetter__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, newValue: jlong, self: jlong) {
assert(self != 0, "self memory address was null")
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
guard let self$ else {
fatalError("self memory address was null in call to \\(#function)!")
}
...
self$.pointee.getterAndSetter = Int64(fromJNI: newValue, in: environment!)
}
"""
Expand Down Expand Up @@ -390,24 +362,14 @@ struct JNIVariablesTests {
"""
@_cdecl("Java_com_example_swift_MyClass__00024isSomeBoolean__J")
func Java_com_example_swift_MyClass__00024isSomeBoolean__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jboolean {
assert(self != 0, "self memory address was null")
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
guard let self$ else {
fatalError("self memory address was null in call to \\(#function)!")
}
...
return self$.pointee.someBoolean.getJNIValue(in: environment!)
}
""",
"""
@_cdecl("Java_com_example_swift_MyClass__00024setSomeBoolean__ZJ")
func Java_com_example_swift_MyClass__00024setSomeBoolean__ZJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, newValue: jboolean, self: jlong) {
assert(self != 0, "self memory address was null")
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
guard let self$ else {
fatalError("self memory address was null in call to \\(#function)!")
}
...
self$.pointee.someBoolean = Bool(fromJNI: newValue, in: environment!)
}
"""
Expand Down Expand Up @@ -466,24 +428,14 @@ struct JNIVariablesTests {
"""
@_cdecl("Java_com_example_swift_MyClass__00024isBoolean__J")
func Java_com_example_swift_MyClass__00024isBoolean__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jboolean {
assert(self != 0, "self memory address was null")
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
guard let self$ else {
fatalError("self memory address was null in call to \\(#function)!")
}
...
return self$.pointee.isBoolean.getJNIValue(in: environment!)
}
""",
"""
@_cdecl("Java_com_example_swift_MyClass__00024setBoolean__ZJ")
func Java_com_example_swift_MyClass__00024setBoolean__ZJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, newValue: jboolean, self: jlong) {
assert(self != 0, "self memory address was null")
let selfBits$ = Int(Int64(fromJNI: self, in: environment!))
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
guard let self$ else {
fatalError("self memory address was null in call to \\(#function)!")
}
...
self$.pointee.isBoolean = Bool(fromJNI: newValue, in: environment!)
}
"""
Expand Down
Loading