forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtocolTest.java
More file actions
113 lines (98 loc) · 3.64 KB
/
ProtocolTest.java
File metadata and controls
113 lines (98 loc) · 3.64 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
//===----------------------------------------------------------------------===//
//
// 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 static org.junit.jupiter.api.Assertions.*;
public class ProtocolTest {
@Test
void takeProtocol() {
try (var arena = SwiftArena.ofConfined()) {
ConcreteProtocolAB proto1 = ConcreteProtocolAB.init(10, 5, arena);
ConcreteProtocolAB proto2 = ConcreteProtocolAB.init(20, 1, arena);
assertEquals(30, MySwiftLibrary.takeProtocol(proto1, proto2));
}
}
@Test
void takeCombinedProtocol() {
try (var arena = SwiftArena.ofConfined()) {
ConcreteProtocolAB proto1 = ConcreteProtocolAB.init(10, 5, arena);
assertEquals(15, MySwiftLibrary.takeCombinedProtocol(proto1));
}
}
@Test
void takeGenericProtocol() {
try (var arena = SwiftArena.ofConfined()) {
ConcreteProtocolAB proto1 = ConcreteProtocolAB.init(10, 5, arena);
ConcreteProtocolAB proto2 = ConcreteProtocolAB.init(20, 1, arena);
assertEquals(11, MySwiftLibrary.takeGenericProtocol(proto1, proto2));
}
}
@Test
void takeCombinedGenericProtocol() {
try (var arena = SwiftArena.ofConfined()) {
ConcreteProtocolAB proto1 = ConcreteProtocolAB.init(10, 5, arena);
assertEquals(15, MySwiftLibrary.takeCombinedGenericProtocol(proto1));
}
}
@Test
void protocolVariables() {
try (var arena = SwiftArena.ofConfined()) {
ProtocolA proto1 = ConcreteProtocolAB.init(10, 5, arena);
assertEquals(10, proto1.getConstantA());
assertEquals(0, proto1.getMutable());
proto1.setMutable(3);
assertEquals(3, proto1.getMutable());
}
}
@Test
void protocolMethod() {
try (var arena = SwiftArena.ofConfined()) {
ProtocolA proto1 = ConcreteProtocolAB.init(10, 5, arena);
assertEquals("ConcreteProtocolAB", proto1.name());
}
}
@Test
void protocolClassMethod() {
try (var arena = SwiftArena.ofConfined()) {
ProtocolA proto1 = ConcreteProtocolAB.init(10, 5, arena);
assertEquals(10, proto1.makeClass(arena).getX());
}
}
static class JavaStorage implements Storage {
StorageItem item;
JavaStorage(StorageItem item) {
this.item = item;
}
@Override
public StorageItem load(SwiftArena swiftArena) {
return item;
}
@Override
public void save(StorageItem item) {
this.item = item;
}
}
@Test
void useStorage() {
try (var arena = SwiftArena.ofConfined()) {
JavaStorage storage = new JavaStorage(null);
MySwiftLibrary.saveWithStorage(StorageItem.init(10, arena), storage);
assertEquals(10, MySwiftLibrary.loadWithStorage(storage, arena).getValue());
MySwiftLibrary.saveWithStorage(StorageItem.init(7, arena), storage);
MySwiftLibrary.saveWithStorage(StorageItem.init(5, arena), storage);
assertEquals(5, MySwiftLibrary.loadWithStorage(storage, arena).getValue());
}
}
}