-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathMySwiftClassTest.java
More file actions
204 lines (175 loc) · 6.06 KB
/
Copy pathMySwiftClassTest.java
File metadata and controls
204 lines (175 loc) · 6.06 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
202
203
204
//===----------------------------------------------------------------------===//
//
// 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 java.lang.reflect.Method;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
import static org.junit.jupiter.api.Assertions.*;
public class MySwiftClassTest {
@Test
void init_noParameters() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(arena);
assertNotNull(c);
}
}
@Test
void init_withParameters() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(1337, 42, arena);
assertNotNull(c);
}
}
@Test
void init_throwing() {
try (var arena = SwiftArena.ofConfined()) {
Exception exception = assertThrows(Exception.class, () -> MySwiftClass.init(true, arena));
assertEquals("swiftError", exception.getMessage());
MySwiftClass c = assertDoesNotThrow(() -> MySwiftClass.init(false, arena));
assertNotNull(c);
}
}
@Test
void sum() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(20, 10, arena);
assertEquals(30, c.sum());
}
}
@Test
void xMultiplied() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(20, 10, arena);
assertEquals(200, c.xMultiplied(10));
}
}
@Test
void throwingFunction() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(20, 10, arena);
Exception exception = assertThrows(Exception.class, () -> c.throwingFunction());
assertEquals("swiftError", exception.getMessage());
}
}
@Test
void constant() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(20, 10, arena);
assertEquals(100, c.getConstant());
}
}
@Test
void mutable() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(20, 10, arena);
assertEquals(0, c.getMutable());
c.setMutable(42);
assertEquals(42, c.getMutable());
}
}
@Test
void product() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(20, 10, arena);
assertEquals(200, c.getProduct());
}
}
@Test
void throwingVariable() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(20, 10, arena);
Exception exception = assertThrows(Exception.class, () -> c.getThrowingVariable());
assertEquals("swiftError", exception.getMessage());
}
}
@Test
void mutableDividedByTwo() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(20, 10, arena);
assertEquals(0, c.getMutableDividedByTwo());
c.setMutable(20);
assertEquals(10, c.getMutableDividedByTwo());
c.setMutableDividedByTwo(5);
assertEquals(10, c.getMutable());
}
}
@Test
void isWarm() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(20, 10, arena);
assertFalse(c.isWarm());
}
}
@Test
void sumWithX() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c1 = MySwiftClass.init(20, 10, arena);
MySwiftClass c2 = MySwiftClass.init(50, 10, arena);
assertEquals(70, c1.sumX(c2));
}
}
@Test
void copy() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c1 = MySwiftClass.init(20, 10, arena);
MySwiftClass c2 = c1.copy(arena);
assertEquals(20, c2.getX());
assertEquals(10, c2.getY());
assertNotEquals(c1.$memoryAddress(), c2.$memoryAddress());
}
}
@Test
void addXWithJavaLong() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c1 = MySwiftClass.init(20, 10, arena);
Long javaLong = 50L;
assertEquals(70, c1.addXWithJavaLong(javaLong));
}
}
@Test
void getAsyncVariable() throws Exception {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c1 = MySwiftClass.init(20, 10, arena);
assertEquals(42, c1.getGetAsync().get());
}
}
@Test
void toStringTest() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c1 = MySwiftClass.init(20, 10, arena);
assertEquals("MySwiftClass(x: 20, y: 10)", c1.toString());
}
}
@Test
void toDebugStringTest() {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c1 = MySwiftClass.init(20, 10, arena);
assertEquals("debug: MySwiftClass(x: 20, y: 10)", c1.toDebugString());
}
}
@Test
void privateSetCounter_getterOnly() throws Exception {
try (var arena = SwiftArena.ofConfined()) {
MySwiftClass c = MySwiftClass.init(20, 10, arena);
assertEquals(7, c.getPrivateSetCounter());
}
// The setter must not be exposed to Java for `public private(set) var`
Method getter = MySwiftClass.class.getMethod("getPrivateSetCounter");
assertNotNull(getter);
assertThrows(NoSuchMethodException.class, () -> MySwiftClass.class.getMethod("setPrivateSetCounter", long.class));
}
}