|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +package com.example.swift; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.swift.swiftkit.core.JNISwiftInstance; |
| 19 | +import org.swift.swiftkit.core.SwiftArena; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.*; |
| 22 | + |
| 23 | +public class ReturnInheritedProtocolTest { |
| 24 | + @Test |
| 25 | + void returnedRefinedProtocolExposesOwnAndInheritedRequirements() { |
| 26 | + try (var arena = SwiftArena.ofConfined()) { |
| 27 | + ProtocolC protoC = MySwiftLibrary.makeProtocolC(7, 11, arena); |
| 28 | + |
| 29 | + // Own requirement, declared directly on ProtocolC. |
| 30 | + assertEquals(11, protoC.getConstantC()); |
| 31 | + |
| 32 | + // Inherited requirement, declared on ProtocolB, which ProtocolC refines. |
| 33 | + assertEquals(7, protoC.getConstantB()); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void returnedRefinedProtocolIsAlsoUsableAsParentProtocol() { |
| 39 | + try (var arena = SwiftArena.ofConfined()) { |
| 40 | + ProtocolC protoC = MySwiftLibrary.makeProtocolC(7, 11, arena); |
| 41 | + ProtocolB protoB = protoC; |
| 42 | + assertEquals(7, protoB.getConstantB()); |
| 43 | + assertEquals(7, MySwiftLibrary.takeProtocolB(protoB)); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void returnedRefinedProtocolIsABackingSwiftInstance() { |
| 49 | + try (var arena = SwiftArena.ofConfined()) { |
| 50 | + ProtocolC protoC = MySwiftLibrary.makeProtocolC(7, 11, arena); |
| 51 | + assertInstanceOf(JNISwiftInstance.class, protoC); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void returnedProtocolBoxSetterWritesBackToValueTypeStorage() { |
| 57 | + try (var arena = SwiftArena.ofConfined()) { |
| 58 | + // ConcreteProtocolAStruct is a *value type* (struct) conformer to |
| 59 | + // ProtocolA. Opening the existential returned by makeProtocolA |
| 60 | + // yields a copy of the struct, so if setMutable merely mutated the |
| 61 | + // opened copy without writing it back into the box's storage, the |
| 62 | + // new value would be silently lost and getMutable() would still |
| 63 | + // read 0. |
| 64 | + ProtocolA proto = MySwiftLibrary.makeProtocolA(10, arena); |
| 65 | + assertEquals(10, proto.getConstantA()); |
| 66 | + assertEquals(0, proto.getMutable()); |
| 67 | + |
| 68 | + proto.setMutable(42); |
| 69 | + assertEquals(42, proto.getMutable()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void returnedProtocolBoxSetterWritesBackToReferenceTypeStorage() { |
| 75 | + try (var arena = SwiftArena.ofConfined()) { |
| 76 | + // ConcreteProtocolAB is a *reference type* (class) conformer to |
| 77 | + // ProtocolA, exercising the same write-back path against a class |
| 78 | + // conformer boxed as any ProtocolA. |
| 79 | + ProtocolA proto = MySwiftLibrary.makeProtocolAClass(10, 5, arena); |
| 80 | + assertEquals(10, proto.getConstantA()); |
| 81 | + assertEquals(0, proto.getMutable()); |
| 82 | + |
| 83 | + proto.setMutable(7); |
| 84 | + assertEquals(7, proto.getMutable()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void returnedProtocolWithUnsupportedStaticRequirementIsStillUsable() { |
| 90 | + try (var arena = SwiftArena.ofConfined()) { |
| 91 | + // ProtocolWithStatic declares a static func and an init(), neither of which |
| 92 | + // are supported requirements for existential boxing, so ProtocolWithStaticBox |
| 93 | + // simply omits them. The box/interface should still come back usable as a |
| 94 | + // JNISwiftInstance-backed reference. |
| 95 | + ProtocolWithStatic protoWithStatic = MySwiftLibrary.makeProtocolWithStatic(arena); |
| 96 | + assertInstanceOf(JNISwiftInstance.class, protoWithStatic); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments