|
| 1 | +import JExtractSwiftLib |
| 2 | +import Testing |
| 3 | + |
| 4 | +@Suite |
| 5 | +struct JNIModuleTests { |
| 6 | + let globalMethodWithPrimitives = """ |
| 7 | + public func helloWorld() |
| 8 | + public func takeIntegers(i1: Int8, i2: Int16, i3: Int32, i4: Int) -> UInt16 |
| 9 | + public func otherPrimitives(b: Bool, f: Float, d: Double) |
| 10 | + """ |
| 11 | + |
| 12 | + @Test |
| 13 | + func generatesModuleJavaClass() throws { |
| 14 | + let input = "public func helloWorld()" |
| 15 | + |
| 16 | + try assertOutput(input: input, .jni, .java, expectedChunks: [ |
| 17 | + """ |
| 18 | + // Generated by jextract-swift |
| 19 | + // Swift module: SwiftModule |
| 20 | +
|
| 21 | + package com.example.swift; |
| 22 | +
|
| 23 | + public final class SwiftModule { |
| 24 | + """ |
| 25 | + ]) |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + func globalMethodWithPrimitives_javaBindings() throws { |
| 30 | + try assertOutput( |
| 31 | + input: globalMethodWithPrimitives, |
| 32 | + .jni, |
| 33 | + .java, |
| 34 | + expectedChunks: [ |
| 35 | + """ |
| 36 | + /** |
| 37 | + * Downcall to Swift: |
| 38 | + * {@snippet lang=swift : |
| 39 | + * public func helloWorld() |
| 40 | + * } |
| 41 | + */ |
| 42 | + public static native void helloWorld(); |
| 43 | + """, |
| 44 | + """ |
| 45 | + /** |
| 46 | + * Downcall to Swift: |
| 47 | + * {@snippet lang=swift : |
| 48 | + * public func takeIntegers(i1: Int8, i2: Int16, i3: Int32, i4: Int) -> UInt16 |
| 49 | + * } |
| 50 | + */ |
| 51 | + public static native char takeIntegers(byte i1, short i2, int i3, long i4); |
| 52 | + """, |
| 53 | + """ |
| 54 | + /** |
| 55 | + * Downcall to Swift: |
| 56 | + * {@snippet lang=swift : |
| 57 | + * public func otherPrimitives(b: Bool, f: Float, d: Double) |
| 58 | + * } |
| 59 | + */ |
| 60 | + public static native void otherPrimitives(boolean b, float f, double d); |
| 61 | + """ |
| 62 | + ] |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + func globalMethodWithPrimitives_swiftThunks() throws { |
| 68 | + try assertOutput( |
| 69 | + input: globalMethodWithPrimitives, |
| 70 | + .jni, |
| 71 | + .swift, |
| 72 | + detectChunkByInitialLines: 1, |
| 73 | + expectedChunks: [ |
| 74 | + """ |
| 75 | + @_cdecl("Java_com_example_swift_SwiftModule_helloWorld") |
| 76 | + func swiftjava_SwiftModule_helloWorld(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) { |
| 77 | + SwiftModule.helloWorld() |
| 78 | + } |
| 79 | + """, |
| 80 | + """ |
| 81 | + @_cdecl("Java_com_example_swift_SwiftModule_takeIntegers") |
| 82 | + func swiftjava_SwiftModule_takeIntegers_i1_i2_i3_i4(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, i1: jbyte, i2: jshort, i3: jint, i4: jlong) -> jchar { |
| 83 | + let result = SwiftModule.takeIntegers(i1: Int8(fromJNI: i1, in: environment!), i2: Int16(fromJNI: i2, in: environment!), i3: Int32(fromJNI: i3, in: environment!), i4: Int(fromJNI: i4, in: environment!)) |
| 84 | + return result.getJNIValue(in: environment)") |
| 85 | + } |
| 86 | + """, |
| 87 | + """ |
| 88 | + @_cdecl("Java_com_example_swift_SwiftModule_otherPrimitives") |
| 89 | + func swiftjava_SwiftModule_otherPrimitives_b_f_d(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, b: jboolean, f: jfloat, d: jdouble) { |
| 90 | + SwiftModule.otherPrimitives(b: Bool(fromJNI: b, in: environment!), f: Float(fromJNI: f, in: environment!), d: Double(fromJNI: d, in: environment!)) |
| 91 | + } |
| 92 | + """ |
| 93 | + ] |
| 94 | + ) |
| 95 | + } |
| 96 | +} |
0 commit comments