Repository navigation
Expand file tree
/
Copy pathProtocolDefaultsTest.java
More file actions
59 lines (53 loc) · 2.13 KB
/
Copy pathProtocolDefaultsTest.java
File metadata and controls
59 lines (53 loc) · 2.13 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 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 static org.junit.jupiter.api.Assertions.*;
public class ProtocolDefaultsTest {
@Test
void javaCallbacksInheritSwiftDefaults() throws Exception {
DefaultOperations operations = () -> 10;
assertEquals(12, operations.add(2));
assertEquals(12, operations.total());
assertEquals(12, MySwiftLibrary.callDefaultOperations(operations));
assertEquals(12, operations.checked(false));
assertThrows(GreeterError.class, () -> operations.checked(true));
}
@Test
void defaultsPreserveJavaOverridesOfOtherDefaultedRequirements() throws Exception {
DefaultOperations operations = new DefaultOperations() {
public long seed() { return 10; }
public long add(long value) { return 100 + value; }
};
assertEquals(102, operations.total());
assertEquals(102, operations.checked(false));
assertEquals(102, MySwiftLibrary.callDefaultOperations(operations));
}
@Test
void nestedProtocolsUseTheirInheritedJavaCallback() {
DefaultNamespace.Child child = () -> 4;
assertEquals(7, child.extra());
assertEquals(7, MySwiftLibrary.callNestedDefaults(child));
DefaultNamespace.Parent parent = child;
assertEquals(4, parent.seed());
}
@Test
void refinedProtocolsCanDefaultAnInheritedRequirement() {
RefinedDefault child = new RefinedDefault() {};
assertEquals(7, child.value());
assertEquals(7, MySwiftLibrary.callRefinedDefault(child));
DefaultParent parent = child;
assertEquals(7, parent.value());
}
}