|
| 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 java.util.Optional; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.*; |
| 24 | + |
| 25 | +/** |
| 26 | + * Exercises the dynamic {@code as} downcast (Swift's {@code as?}) that recovers a |
| 27 | + * concrete jextracted Swift type from a value returned as {@code any P} / {@code some P}. |
| 28 | + */ |
| 29 | +public class ReturnProtocolCastTest { |
| 30 | + |
| 31 | + /** Downcast a returned {@code any Greeter} to its concrete Swift struct type. */ |
| 32 | + @Test |
| 33 | + void downcastToConcreteType_succeeds() { |
| 34 | + try (var arena = SwiftArena.ofConfined()) { |
| 35 | + Greeter greeter = MySwiftLibrary.makeEnglishGreeter("World", arena); |
| 36 | + |
| 37 | + Optional<EnglishGreeter> english = greeter.as(EnglishGreeter.class, arena); |
| 38 | + assertTrue(english.isPresent()); |
| 39 | + // `name` is a member of the concrete struct, not visible through the protocol. |
| 40 | + assertEquals("World", english.get().getName()); |
| 41 | + assertEquals("Hello, World!", english.get().greeting()); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** Downcasting to the wrong dynamic type returns empty (a faithful Swift {@code as?}). */ |
| 46 | + @Test |
| 47 | + void downcastToWrongType_isEmpty() { |
| 48 | + try (var arena = SwiftArena.ofConfined()) { |
| 49 | + Greeter greeter = MySwiftLibrary.makeEnglishGreeter("World", arena); |
| 50 | + |
| 51 | + Optional<DanishGreeter> danish = greeter.as(DanishGreeter.class, arena); |
| 52 | + assertTrue(danish.isEmpty()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** {@code some Greeter} (opaque return) downcasts the same as {@code any Greeter}. */ |
| 57 | + @Test |
| 58 | + void downcastOpaqueReturn_succeeds() { |
| 59 | + try (var arena = SwiftArena.ofConfined()) { |
| 60 | + Greeter greeter = MySwiftLibrary.makeOpaqueGreeter("World", arena); |
| 61 | + |
| 62 | + Optional<EnglishGreeter> english = greeter.as(EnglishGreeter.class, arena); |
| 63 | + assertTrue(english.isPresent()); |
| 64 | + assertEquals("World", english.get().getName()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@code as} works on a concrete-typed reference too (the {@code public} variant on |
| 70 | + * the class, not the {@code default} on the protocol interface). |
| 71 | + */ |
| 72 | + @Test |
| 73 | + void downcastFromConcreteReference() { |
| 74 | + try (var arena = SwiftArena.ofConfined()) { |
| 75 | + EnglishGreeter greeter = EnglishGreeter.init("World", arena); |
| 76 | + |
| 77 | + Optional<EnglishGreeter> english = greeter.as(EnglishGreeter.class, arena); |
| 78 | + assertTrue(english.isPresent()); |
| 79 | + assertEquals("World", english.get().getName()); |
| 80 | + |
| 81 | + Optional<DanishGreeter> danish = greeter.as(DanishGreeter.class, arena); |
| 82 | + assertTrue(danish.isEmpty()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * A Java-implemented conformer has no backing Swift value, so {@code as} returns |
| 88 | + * empty rather than crashing (the {@code instanceof JNISwiftInstance} guard). This is |
| 89 | + * the case that only exists with Java callbacks enabled. |
| 90 | + */ |
| 91 | + @Test |
| 92 | + void downcastJavaImplementedConformer_isEmpty() { |
| 93 | + try (var arena = SwiftArena.ofConfined()) { |
| 94 | + Greeter javaGreeter = new Greeter() { |
| 95 | + @Override |
| 96 | + public String greeting() { |
| 97 | + return "Hi from Java"; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String repeated(long count) { |
| 102 | + return "Hi from Java"; |
| 103 | + } |
| 104 | + }; |
| 105 | + assertFalse(javaGreeter instanceof JNISwiftInstance); |
| 106 | + |
| 107 | + Optional<EnglishGreeter> english = javaGreeter.as(EnglishGreeter.class, arena); |
| 108 | + assertTrue(english.isEmpty()); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void downcastResultIsAWorkingInstance() { |
| 114 | + try (var arena = SwiftArena.ofConfined()) { |
| 115 | + Greeter greeter = MySwiftLibrary.makeEnglishGreeter("World", arena); |
| 116 | + |
| 117 | + EnglishGreeter english = greeter.as(EnglishGreeter.class, arena).orElseThrow(); |
| 118 | + assertEquals("Hello, World! Hello, World!", english.repeated(2)); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments