forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicleEnumTest.java
More file actions
205 lines (182 loc) · 7.16 KB
/
VehicleEnumTest.java
File metadata and controls
205 lines (182 loc) · 7.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
202
203
204
205
//===----------------------------------------------------------------------===//
//
// 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.ConfinedSwiftMemorySession;
import org.swift.swiftkit.core.SwiftArena;
import java.util.Optional;
import java.util.OptionalInt;
import static org.junit.jupiter.api.Assertions.*;
public class VehicleEnumTest {
@Test
void bicycle() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.bicycle(arena);
assertNotNull(vehicle);
}
}
@Test
void car() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.car("Porsche 911", Optional.empty(), arena);
assertNotNull(vehicle);
}
}
@Test
void motorbike() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.motorbike("Yamaha", 750, OptionalInt.empty(), arena);
assertNotNull(vehicle);
}
}
@Test
void initName() {
try (var arena = SwiftArena.ofConfined()) {
assertFalse(Vehicle.init("bus", arena).isPresent());
Optional<Vehicle> vehicle = Vehicle.init("car", arena);
assertTrue(vehicle.isPresent());
assertNotNull(vehicle.get());
}
}
@Test
void nameProperty() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.bicycle(arena);
assertEquals("bicycle", vehicle.getName());
}
}
@Test
void isFasterThan() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle bicycle = Vehicle.bicycle(arena);
Vehicle car = Vehicle.car("Porsche 911", Optional.empty(), arena);
assertFalse(bicycle.isFasterThan(car));
assertTrue(car.isFasterThan(bicycle));
}
}
@Test
void upgrade() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.bicycle(arena);
assertEquals("bicycle", vehicle.getName());
vehicle.upgrade();
assertEquals("car", vehicle.getName());
vehicle.upgrade();
assertEquals("motorbike", vehicle.getName());
}
}
@Test
void getAsBicycle() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.bicycle(arena);
Vehicle.Case.Bicycle bicycle = vehicle.getAsBicycle().orElseThrow();
assertNotNull(bicycle);
}
}
@Test
void getAsCar() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.car("BMW", Optional.empty(), arena);
Vehicle.Case.Car car = vehicle.getAsCar().orElseThrow();
assertEquals("BMW", car.arg0());
vehicle = Vehicle.car("BMW", Optional.of("Long trailer"), arena);
car = vehicle.getAsCar().orElseThrow();
assertEquals("Long trailer", car.trailer().orElseThrow());
}
}
@Test
void getAsMotorbike() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.motorbike("Yamaha", 750, OptionalInt.empty(), arena);
Vehicle.Case.Motorbike motorbike = vehicle.getAsMotorbike().orElseThrow();
assertEquals("Yamaha", motorbike.arg0());
assertEquals(750, motorbike.horsePower());
assertEquals(OptionalInt.empty(), motorbike.helmets());
vehicle = Vehicle.motorbike("Yamaha", 750, OptionalInt.of(2), arena);
motorbike = vehicle.getAsMotorbike().orElseThrow();
assertEquals(OptionalInt.of(2), motorbike.helmets());
}
}
@Test
void getAsTransformer() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.transformer(Vehicle.bicycle(arena), Vehicle.car("BMW", Optional.empty(), arena), arena);
Vehicle.Case.Transformer transformer = vehicle.getAsTransformer(arena).orElseThrow();
assertTrue(transformer.front().getAsBicycle().isPresent());
assertEquals("BMW", transformer.back().getAsCar().orElseThrow().arg0());
}
}
@Test
void getAsBoat() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.boat(OptionalInt.of(10), Optional.of((short) 1), arena);
Vehicle.Case.Boat boat = vehicle.getAsBoat().orElseThrow();
assertEquals(OptionalInt.of(10), boat.passengers());
assertEquals(Optional.of((short) 1), boat.length());
}
}
@Test
void associatedValuesAreCopied() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.car("BMW", Optional.empty(), arena);
Vehicle.Case.Car car = vehicle.getAsCar().orElseThrow();
assertEquals("BMW", car.arg0());
vehicle.upgrade();
Vehicle.Case.Motorbike motorbike = vehicle.getAsMotorbike().orElseThrow();
assertNotNull(motorbike);
// Motorbike should still remain
assertEquals("BMW", car.arg0());
}
}
@Test
void getDiscriminator() {
try (var arena = SwiftArena.ofConfined()) {
assertEquals(Vehicle.Discriminator.BICYCLE, Vehicle.bicycle(arena).getDiscriminator());
assertEquals(Vehicle.Discriminator.CAR, Vehicle.car("BMW", Optional.empty(), arena).getDiscriminator());
assertEquals(Vehicle.Discriminator.MOTORBIKE, Vehicle.motorbike("Yamaha", 750, OptionalInt.empty(), arena).getDiscriminator());
assertEquals(Vehicle.Discriminator.TRANSFORMER, Vehicle.transformer(Vehicle.bicycle(arena), Vehicle.bicycle(arena), arena).getDiscriminator());
}
}
@Test
void getCase() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.bicycle(arena);
Vehicle.Case caseElement = vehicle.getCase(arena);
assertInstanceOf(Vehicle.Case.Bicycle.class, caseElement);
}
}
@Test
void switchGetCase() {
try (var arena = SwiftArena.ofConfined()) {
Vehicle vehicle = Vehicle.car("BMW", Optional.empty(), arena);
switch (vehicle.getCase(arena)) {
case Vehicle.Case.Bicycle b:
fail("Was bicycle");
break;
case Vehicle.Case.Car car:
assertEquals("BMW", car.arg0());
break;
case Vehicle.Case.Motorbike motorbike:
fail("Was motorbike");
break;
case Vehicle.Case.Transformer transformer:
fail("Was transformer");
break;
case Vehicle.Case.Boat b:
fail("Was boat");
break;
}
}
}
}