Skip to content

Commit 3881e50

Browse files
committed
Dev: fix int 8 tests
1 parent d5c1038 commit 3881e50

6 files changed

Lines changed: 68 additions & 37 deletions

File tree

compiler/src/main/java/com/readdle/codegen/SwiftCallbackFuncDescriptor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,12 @@ void generateCode(SwiftWriter swiftWriter, String javaFullName, String swiftType
229229

230230
if (returnSwiftType != null) {
231231
if (!isReturnTypeOptional && returnSwiftType.isPrimitiveType()) {
232-
swiftWriter.emitStatement("return optionalResult");
232+
if (returnSwiftType.swiftType.equals("Bool")) {
233+
swiftWriter.emitStatement("return optionalResult == JNI_TRUE");
234+
}
235+
else {
236+
swiftWriter.emitStatement("return optionalResult");
237+
}
233238
}
234239
else {
235240
swiftWriter.emitStatement("guard let result = optionalResult else {");

compiler/src/main/java/com/readdle/codegen/SwiftFuncDescriptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ else if (param.isOptional) {
177177
}
178178

179179
if (returnSwiftType != null) {
180-
if (returnSwiftType.isPrimitiveType()) {
180+
if (!isReturnTypeOptional && returnSwiftType.isPrimitiveType()) {
181181
if (returnSwiftType.swiftType.equals("Bool")) {
182182
swiftWriter.emitStatement("return jboolean(result ? JNI_TRUE : JNI_FALSE)");
183183
}

compiler/src/main/java/com/readdle/codegen/SwiftGetterDescriptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void generateCode(SwiftWriter swiftWriter, String javaFullName, String sw
7575
swiftWriter.emitStatement(String.format("let result = %s.%s", isStatic ? swiftType : "swiftSelf", swiftName));
7676

7777
if (returnSwiftType != null) {
78-
if (returnSwiftType.isPrimitiveType()) {
78+
if (!isReturnTypeOptional && returnSwiftType.isPrimitiveType()) {
7979
if (returnSwiftType.swiftType.equals("Bool")) {
8080
swiftWriter.emitStatement("return jboolean(result ? JNI_TRUE : JNI_FALSE)");
8181
}

sample/src/androidTest/java/com/readdle/swiftjava/sample/Int8Tests.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.readdle.swiftjava.sample;
22

33
import android.support.test.runner.AndroidJUnit4;
4-
import android.util.Log;
54

65
import com.readdle.codegen.anotation.JavaSwift;
76

@@ -52,12 +51,37 @@ public void testOptionalParam() {
5251
Assert.assertFalse(Int8Test.testOptionalParam(Byte.MIN_VALUE));
5352
}
5453

54+
public void testOptionalReturnType() {
55+
Byte result = Int8Test.testOptionalReturnType();
56+
Assert.assertNotNull(result);
57+
Assert.assertEquals(result.byteValue(), Byte.MAX_VALUE);
58+
}
59+
60+
@Test
61+
public void testProtocolParam() {
62+
boolean result = Int8Test.testProtocolParam(param -> param == Byte.MAX_VALUE);
63+
Assert.assertTrue(result);
64+
}
65+
5566
@Test
5667
public void testProtocolReturnType() {
5768
byte result = Int8Test.testProtocolReturnType(() -> (byte) 42);
5869
Assert.assertEquals(result, 42);
5970
}
6071

72+
@Test
73+
public void testProtocolOptionalParam() {
74+
boolean result = Int8Test.testProtocolOptionalParam(param -> param != null && param == Byte.MAX_VALUE);
75+
Assert.assertTrue(result);
76+
}
77+
78+
@Test
79+
public void testProtocolOptionalReturnType() {
80+
Byte result = Int8Test.testProtocolOptionalReturnType(() -> (byte) 42);
81+
Assert.assertNotNull(result);
82+
Assert.assertEquals(result.byteValue(), 42);
83+
}
84+
6185
@Test
6286
public void testEncode() {
6387
Int8TestStruct result = Int8Test.testEncode();

sample/src/main/java/com/readdle/swiftjava/sample/Int8Test.kt

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,6 @@ import com.readdle.codegen.anotation.SwiftReference
66
import com.readdle.codegen.anotation.SwiftValue
77
import java.lang.annotation.Native
88

9-
// TODO: fix it
10-
// @SwiftDelegate(protocols = ["Int8TestParamProtocol"])
11-
// interface ParamProtocol {
12-
// @SwiftCallbackFunc
13-
// fun testParam(param: Byte): Boolean
14-
// }
15-
16-
@SwiftDelegate(protocols = ["Int8TestReturnTypeProtocol"])
17-
interface ReturnTypeProtocol {
18-
@SwiftCallbackFunc
19-
fun testReturnType(): Byte
20-
}
21-
// TODO: fix it
22-
// @SwiftDelegate(protocols = ["Int8TestOptionalParamProtocol"])
23-
// interface OptionalParamProtocol {
24-
// @SwiftCallbackFunc
25-
// fun testOptionalParam(param: Byte?): Boolean
26-
// }
27-
28-
@SwiftDelegate(protocols = ["Int8TestOptionalReturnTypeProtocol"])
29-
interface OptionalReturnTypeProtocol {
30-
@SwiftCallbackFunc
31-
fun testOptionalReturnType(): Byte?
32-
}
33-
349
@SwiftValue
3510
data class Int8TestStruct(var zero: Byte = 0,
3611
var max: Byte = Byte.MAX_VALUE,
@@ -41,6 +16,30 @@ data class Int8TestStruct(var zero: Byte = 0,
4116
@SwiftReference
4217
class Int8Test private constructor() {
4318

19+
@SwiftDelegate(protocols = ["Int8TestParamProtocol"])
20+
interface Int8ParamProtocol {
21+
@SwiftCallbackFunc
22+
fun testParam(param: Byte): Boolean
23+
}
24+
25+
@SwiftDelegate(protocols = ["Int8TestReturnTypeProtocol"])
26+
interface Int8ReturnTypeProtocol {
27+
@SwiftCallbackFunc
28+
fun testReturnType(): Byte
29+
}
30+
31+
@SwiftDelegate(protocols = ["Int8TestOptionalParamProtocol"])
32+
interface Int8OptionalParamProtocol {
33+
@SwiftCallbackFunc
34+
fun testOptionalParam(param: Byte?): Boolean
35+
}
36+
37+
@SwiftDelegate(protocols = ["Int8TestOptionalReturnTypeProtocol"])
38+
interface Int8OptionalReturnTypeProtocol {
39+
@SwiftCallbackFunc
40+
fun testOptionalReturnType(): Byte?
41+
}
42+
4443
companion object {
4544
@JvmStatic
4645
external fun testZero(): Byte
@@ -60,16 +59,20 @@ class Int8Test private constructor() {
6059
@JvmStatic
6160
external fun testOptionalParam(param: Byte?): Boolean
6261

63-
// TODO: fix it
64-
// @JvmStatic
65-
// external fun testOptionalReturnType(): Byte?
62+
@JvmStatic
63+
external fun testOptionalReturnType(): Byte?
64+
65+
@JvmStatic
66+
external fun testProtocolParam(callback: Int8ParamProtocol): Boolean
6667

6768
@JvmStatic
68-
external fun testProtocolReturnType(callback: ReturnTypeProtocol): Byte
69+
external fun testProtocolReturnType(callback: Int8ReturnTypeProtocol): Byte
6970

70-
// TODO: fix it
71-
// @JvmStatic
72-
// external fun testProtocolOptionalReturnType(callback: OptionalReturnTypeProtocol): Byte?
71+
@JvmStatic
72+
external fun testProtocolOptionalParam(callback: Int8OptionalParamProtocol): Boolean
73+
74+
@JvmStatic
75+
external fun testProtocolOptionalReturnType(callback: Int8OptionalReturnTypeProtocol): Byte?
7376

7477
@JvmStatic
7578
external fun testEncode(): Int8TestStruct

sample/src/main/swift/Sources/SampleAppCore/Int8Test.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public class Int8Test {
7979
}
8080

8181
public static func testDecode(_ value: Int8TestStruct) -> Bool {
82-
NSLog("!!!\(value.max)!!!")
8382
return value == Int8TestStruct()
8483
}
8584

0 commit comments

Comments
 (0)