forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtocolCallbacksTest.java
More file actions
201 lines (168 loc) · 6.16 KB
/
ProtocolCallbacksTest.java
File metadata and controls
201 lines (168 loc) · 6.16 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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
package com.example.swift;
import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.SwiftArena;
import org.swift.swiftkit.core.annotations.Unsigned;
import java.util.Optional;
import java.util.OptionalLong;
import static org.junit.jupiter.api.Assertions.*;
public class ProtocolCallbacksTest {
static class JavaCallbacks implements CallbackProtocol {
@Override
public boolean withBool(boolean input) {
return input;
}
@Override
public byte withInt8(byte input) {
return input;
}
@Override
public @Unsigned char withUInt16(char input) {
return input;
}
@Override
public short withInt16(short input) {
return input;
}
@Override
public int withInt32(int input) {
return input;
}
@Override
public long withInt64(long input) {
return input;
}
@Override
public float withFloat(float input) {
return input;
}
@Override
public double withDouble(double input) {
return input;
}
@Override
public String withString(String input) {
return input;
}
@Override
public void withVoid() {}
@Override
public MySwiftClass withObject(MySwiftClass input, SwiftArena swiftArena$) {
return input;
}
@Override
public OptionalLong withOptionalInt64(OptionalLong input) {
return input;
}
@Override
public Optional<MySwiftClass> withOptionalObject(Optional<MySwiftClass> input, SwiftArena swiftArena$) {
return input;
}
@Override
public long[] withInt64Array(long[] input) {
return input;
}
@Override
public String[] withStringArray(String[] input) {
return input;
}
@Override
public MySwiftClass[] withObjectArray(MySwiftClass[] input, SwiftArena swiftArena$) {
return input;
}
@Override
public void throwingFunction() throws Exception {
throw new Exception("Failed in Java");
}
@Override
public void successfulThrowingFunction() throws Exception {
}
@Override
public Optional<String> withOptionalString(Optional<String> input) {
return input;
}
}
@Test
void voidTest() {
JavaCallbacks callbacks = new JavaCallbacks();
MySwiftLibrary.callProtocolVoid(callbacks);
}
@Test
void throwingFunction_thatDoesNotThrow() {
JavaCallbacks callbacks = new JavaCallbacks();
assertDoesNotThrow(() -> MySwiftLibrary.callProtocolWithSuccessfulThrowingFunction(callbacks));
}
@Test
void throwingFunction_thatThrows() {
JavaCallbacks callbacks = new JavaCallbacks();
Exception exception = assertThrows(Exception.class, () -> MySwiftLibrary.callProtocolWithFailedThrowingFunction(callbacks));
assertEquals("Failed in Java", exception.getMessage());
}
@Test
void primitiveCallbacks() {
try (var arena = SwiftArena.ofConfined()) {
JavaCallbacks callbacks = new JavaCallbacks();
var object = MySwiftClass.init(5, 3, arena);
var optionalObject = Optional.of(MySwiftClass.init(10, 10, arena));
var int64Array = new long[]{1, 2, 3};
var stringArray = new String[]{"Hey", "there"};
var objectArray = new MySwiftClass[]{MySwiftClass.init(1, 1, arena), MySwiftClass.init(2, 2, arena)};
var optionalString = Optional.of("Hey there!");
var output = MySwiftLibrary.outputCallbacks(
callbacks,
true,
(byte) 1,
(char) 16,
(short) 16,
(int) 32,
64L,
1.34f,
1.34,
"Hello from Java!",
object,
OptionalLong.empty(),
optionalObject,
int64Array,
stringArray,
objectArray,
optionalString,
arena
);
assertEquals(1, output.getInt8());
assertEquals(16, output.getUint16());
assertEquals(16, output.getInt16());
assertEquals(32, output.getInt32());
assertEquals(64, output.getInt64());
assertEquals(1.34f, output.get_float());
assertEquals(1.34, output.get_double());
assertEquals("Hello from Java!", output.getString());
assertFalse(output.getOptionalInt64().isPresent());
assertEquals(5, output.getObject(arena).getX());
assertEquals(3, output.getObject(arena).getY());
var optionalObjectOutput = output.getOptionalObject(arena);
assertTrue(optionalObjectOutput.isPresent());
assertEquals(10, optionalObjectOutput.get().getX());
assertArrayEquals(new long[]{1, 2,3}, output.getInt64Array());
assertArrayEquals(new String[]{"Hey", "there"}, output.getStringArray());
var objectArrayOutput = output.getObjectArray(arena);
assertEquals(2, objectArrayOutput.length);
assertEquals(1, objectArrayOutput[0].getX());
assertEquals(2, objectArrayOutput[1].getX());
var optionalStringOutput = output.getOptionalString();
assertTrue(optionalStringOutput.isPresent());
assertEquals("Hey there!", optionalStringOutput.get());
}
}
}