|
| 1 | +protocol BaseProtocol {} |
| 2 | + |
| 3 | +extension BaseProtocol { |
| 4 | + /// Say hello |
| 5 | + func hello() { } |
| 6 | +} |
| 7 | + |
| 8 | +protocol InheritedProtocol: BaseProtocol {} |
| 9 | + |
| 10 | +extension InheritedProtocol { |
| 11 | + func hello() { } |
| 12 | +} |
| 13 | + |
| 14 | +func testInheritBetweenExtensions(x: MyStruct) { |
| 15 | + struct MyStruct: InheritedProtocol {} |
| 16 | + |
| 17 | + // RUN: %sourcekitd-test -req=cursor -req-opts=retrieve_symbol_graph=1 -pos=%(line + 1):14 %s -- %s | %FileCheck %s --check-prefix BETWEEN_EXTENSIONS |
| 18 | + MyStruct().hello() |
| 19 | + // BETWEEN_EXTENSIONS: DOC COMMENT |
| 20 | + // BETWEEN_EXTENSIONS-NEXT: Say hello |
| 21 | + // BETWEEN_EXTENSIONS-NEXT: DOC COMMENT XML |
| 22 | + // BETWEEN_EXTENSIONS: SYMBOL GRAPH BEGIN |
| 23 | + // BETWEEN_EXTENSIONS: Say hello |
| 24 | + // BETWEEN_EXTENSIONS: SYMBOL GRAPH END |
| 25 | +} |
| 26 | + |
| 27 | +func testInheritFromExtensionToStruct(x: MyStruct) { |
| 28 | + struct MyStruct: BaseProtocol { |
| 29 | + func hello() {} |
| 30 | + } |
| 31 | + |
| 32 | + // RUN: %sourcekitd-test -req=cursor -req-opts=retrieve_symbol_graph=1 -pos=%(line + 1):14 %s -- %s | %FileCheck %s --check-prefix EXTENSION_TO_STRUCT |
| 33 | + MyStruct().hello() |
| 34 | + // EXTENSION_TO_STRUCT: DOC COMMENT |
| 35 | + // EXTENSION_TO_STRUCT-NEXT: Say hello |
| 36 | + // EXTENSION_TO_STRUCT-NEXT: DOC COMMENT XML |
| 37 | + // EXTENSION_TO_STRUCT: SYMBOL GRAPH BEGIN |
| 38 | + // EXTENSION_TO_STRUCT: Say hello |
| 39 | + // EXTENSION_TO_STRUCT: SYMBOL GRAPH END |
| 40 | +} |
| 41 | + |
| 42 | +protocol ProtocolWithDefaultedRequirement { |
| 43 | + /// Doc for the requirement |
| 44 | + func hello() |
| 45 | +} |
| 46 | + |
| 47 | +extension ProtocolWithDefaultedRequirement { |
| 48 | + /// Doc for the default implementation |
| 49 | + func hello() {} |
| 50 | +} |
| 51 | + |
| 52 | +func testProtocolWithDefaultedRequirement(foo: any ProtocolWithDefaultedRequirement) { |
| 53 | + // RUN: %sourcekitd-test -req=cursor -req-opts=retrieve_symbol_graph=1 -pos=%(line + 1):7 %s -- %s | %FileCheck %s --check-prefix DEFAULTED_REQUIREMENT |
| 54 | + foo.hello() |
| 55 | + // DEFAULTED_REQUIREMENT: DOC COMMENT |
| 56 | + // DEFAULTED_REQUIREMENT-NEXT: Doc for the requirement |
| 57 | + // DEFAULTED_REQUIREMENT-NEXT: DOC COMMENT XML |
| 58 | + // DEFAULTED_REQUIREMENT: SYMBOL GRAPH BEGIN |
| 59 | + // DEFAULTED_REQUIREMENT: Doc for the requirement |
| 60 | + // DEFAULTED_REQUIREMENT: SYMBOL GRAPH END |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +func testDontInheritFromFunctionWithDifferentType() { |
| 65 | + struct MyStruct: InheritedProtocol { |
| 66 | + func hello() -> String { return "Hello" } |
| 67 | + } |
| 68 | + // RUN: %sourcekitd-test -req=cursor -pos=%(line + 1):30 %s -- %s | %FileCheck %s --check-prefix FROM_FUNCTION_WITH_DIFFERENT_TYPE |
| 69 | + let x: String = MyStruct().hello() |
| 70 | + // FROM_FUNCTION_WITH_DIFFERENT_TYPE: DOC COMMENT |
| 71 | + // FROM_FUNCTION_WITH_DIFFERENT_TYPE-NEXT: DOC COMMENT XML |
| 72 | +} |
| 73 | + |
| 74 | +protocol ProtocolWithThrowingFunction { |
| 75 | + /// Say hello |
| 76 | + func hello() throws |
| 77 | +} |
| 78 | + |
| 79 | +func testInheritEvenIfThrowingNonThrowingMismatches() { |
| 80 | + struct MyStruct: ProtocolWithThrowingFunction { |
| 81 | + func hello() { } |
| 82 | + } |
| 83 | + // RUN: %sourcekitd-test -req=cursor -pos=%(line + 1):14 %s -- %s | %FileCheck %s --check-prefix SATISFY_THROWING_WITH_NON_THROWING |
| 84 | + MyStruct().hello() |
| 85 | + // SATISFY_THROWING_WITH_NON_THROWING: DOC COMMENT |
| 86 | + // SATISFY_THROWING_WITH_NON_THROWING-NEXT: Say hello |
| 87 | + // SATISFY_THROWING_WITH_NON_THROWING-NEXT: DOC COMMENT XML |
| 88 | +} |
| 89 | + |
| 90 | +protocol ProtocolWithAccessor { |
| 91 | + /// The greeting |
| 92 | + var greeting: String { get } |
| 93 | +} |
| 94 | + |
| 95 | +func testSatisfyAccessorRequirement() { |
| 96 | + struct MyStruct: ProtocolWithAccessor { |
| 97 | + var greeting: String = "hello" |
| 98 | + } |
| 99 | + // RUN: %sourcekitd-test -req=cursor -pos=%(line + 1):14 %s -- %s | %FileCheck %s --check-prefix ACCESSOR_REQUIREMENT |
| 100 | + MyStruct().greeting |
| 101 | + // ACCESSOR_REQUIREMENT: DOC COMMENT |
| 102 | + // ACCESSOR_REQUIREMENT-NEXT: The greeting |
| 103 | + // ACCESSOR_REQUIREMENT-NEXT: DOC COMMENT XML |
| 104 | +} |
| 105 | + |
| 106 | +protocol ProtocolWithMethodReturningOptional {} |
| 107 | +extension ProtocolWithMethodReturningOptional { |
| 108 | + /// hello |
| 109 | + func hello() -> String? { nil } |
| 110 | +} |
| 111 | + |
| 112 | +func testInheritFromLessSpecificOverridden() { |
| 113 | + struct MyStruct: ProtocolWithMethodReturningOptional { |
| 114 | + func hello() -> String { "hello" } |
| 115 | + } |
| 116 | + |
| 117 | + // RUN: %sourcekitd-test -req=cursor -pos=%(line + 1):14 %s -- %s | %FileCheck %s --check-prefix INHERIT_FROM_LESS_SPECIFIC_OVERRIDDEN |
| 118 | + MyStruct().hello() |
| 119 | + // INHERIT_FROM_LESS_SPECIFIC_OVERRIDDEN: DOC COMMENT |
| 120 | + // INHERIT_FROM_LESS_SPECIFIC_OVERRIDDEN-NEXT: hello |
| 121 | + // INHERIT_FROM_LESS_SPECIFIC_OVERRIDDEN-NEXT: DOC COMMENT XML |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +protocol ProtocolWithStaticMethod {} |
| 126 | +extension ProtocolWithStaticMethod { |
| 127 | + /// hello |
| 128 | + static func hello() {} |
| 129 | +} |
| 130 | + |
| 131 | +func testDontInheritFromStaticToNonStatic() { |
| 132 | + struct MyStruct: ProtocolWithStaticMethod { |
| 133 | + func hello() {} |
| 134 | + } |
| 135 | + |
| 136 | + // RUN: %sourcekitd-test -req=cursor -pos=%(line + 1):14 %s -- %s | %FileCheck %s --check-prefix DONT_INHERIT_FROM_STATIC_TO_NON_STATIC |
| 137 | + MyStruct().hello() |
| 138 | + // DONT_INHERIT_FROM_STATIC_TO_NON_STATIC: DOC COMMENT |
| 139 | + // DONT_INHERIT_FROM_STATIC_TO_NON_STATIC-NEXT: DOC COMMENT XML |
| 140 | +} |
| 141 | + |
| 142 | +protocol ProtocolWithStaticMethodReturningSelf { |
| 143 | + /// hello |
| 144 | + static func hello(_ greeting: String) -> Self {} |
| 145 | +} |
| 146 | + |
| 147 | +func testInheritStaticFuncToEnumCase() { |
| 148 | + enum MyEnum: ProtocolWithStaticMethodReturningSelf { |
| 149 | + case hello(String) |
| 150 | + } |
| 151 | + |
| 152 | + // RUN: %sourcekitd-test -req=cursor -pos=%(line + 1):10 %s -- %s | %FileCheck %s --check-prefix INHERIT_FROM_STATIC_FUNC_TO_ENUM_CASE |
| 153 | + MyEnum.hello |
| 154 | + // INHERIT_FROM_STATIC_FUNC_TO_ENUM_CASE: DOC COMMENT |
| 155 | + // INHERIT_FROM_STATIC_FUNC_TO_ENUM_CASE-NEXT: hello |
| 156 | + // INHERIT_FROM_STATIC_FUNC_TO_ENUM_CASE-NEXT: DOC COMMENT XML |
| 157 | +} |
0 commit comments