|
| 1 | +// |
| 2 | +// PolymorphicMatchingTypesProvidingTests.swift |
| 3 | +// KarrotCodableKit |
| 4 | +// |
| 5 | +// Created by Elon on 8/4/26. |
| 6 | +// Copyright © 2026 Danggeun Market Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import Testing |
| 11 | + |
| 12 | +import KarrotCodableKit |
| 13 | + |
| 14 | +struct PolymorphicMatchingTypesProvidingTests { |
| 15 | + |
| 16 | + @Test |
| 17 | + func `generated strategy exposes its matching types in declaration order`() { |
| 18 | + // given |
| 19 | + // DummyNotice declares DummyCallout, DummyActionableCallout and DummyDismissibleCallout. |
| 20 | + |
| 21 | + // when |
| 22 | + let identifiers = DummyNoticeCodableStrategy.matchingTypes.map { $0.polymorphicIdentifier } |
| 23 | + |
| 24 | + // then |
| 25 | + #expect(identifiers == ["callout", "actionable-callout", "dismissible-callout"]) |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + func `generated strategy exposes its fallback type`() throws { |
| 30 | + // given |
| 31 | + // DummyNotice declares DummyUndefinedCallout as its fallback. |
| 32 | + |
| 33 | + // when |
| 34 | + let fallbackType = try #require(DummyNoticeCodableStrategy.fallbackType) |
| 35 | + |
| 36 | + // then |
| 37 | + #expect(fallbackType.polymorphicIdentifier == "undefined-callout") |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + func `strategy declared without a fallback type exposes nil`() { |
| 42 | + // given |
| 43 | + // ViewItem declares matching types only, with no fallbackType argument. |
| 44 | + |
| 45 | + // when |
| 46 | + let fallbackType = ViewItemCodableStrategy.fallbackType |
| 47 | + |
| 48 | + // then |
| 49 | + #expect(fallbackType == nil) |
| 50 | + #expect(ViewItemCodableStrategy.matchingTypes.isEmpty == false) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + func `exposed matching types describe what decoding actually resolves`() throws { |
| 55 | + // given |
| 56 | + let jsonData = #""" |
| 57 | + { |
| 58 | + "notice" : { |
| 59 | + "description" : "Your listing is under review", |
| 60 | + "key" : "listing-under-review", |
| 61 | + "title" : "Under review", |
| 62 | + "type" : "dismissible-callout" |
| 63 | + }, |
| 64 | + "notices" : [ |
| 65 | + { |
| 66 | + "description" : "A notice type this client does not know yet", |
| 67 | + "title" : "Sponsored", |
| 68 | + "type" : "sponsored-callout" |
| 69 | + } |
| 70 | + ] |
| 71 | + } |
| 72 | + """# |
| 73 | + |
| 74 | + let declaredIdentifiers = DummyNoticeCodableStrategy.matchingTypes.map { $0.polymorphicIdentifier } |
| 75 | + let fallbackIdentifier = try #require(DummyNoticeCodableStrategy.fallbackType).polymorphicIdentifier |
| 76 | + |
| 77 | + // when |
| 78 | + let response = try JSONDecoder().decode(DummyResponse.self, from: Data(jsonData.utf8)) |
| 79 | + |
| 80 | + // then |
| 81 | + // A declared identifier resolves to the declared type. |
| 82 | + #expect(declaredIdentifiers.contains("dismissible-callout")) |
| 83 | + let notice = try #require(response.notice as? DummyDismissibleCallout) |
| 84 | + #expect(notice.key == "listing-under-review") |
| 85 | + |
| 86 | + // An identifier outside the exposed list resolves to the exposed fallback type. |
| 87 | + #expect(declaredIdentifiers.contains("sponsored-callout") == false) |
| 88 | + #expect(fallbackIdentifier == "undefined-callout") |
| 89 | + let unknownNotice = try #require(response.notices.first as? DummyUndefinedCallout) |
| 90 | + #expect(unknownNotice.description == "A notice type this client does not know yet") |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + func `a generic helper can enumerate any conforming strategy`() { |
| 95 | + // given |
| 96 | + // A caller that only knows the protocol, mirroring an exhaustiveness check in a consumer. |
| 97 | + |
| 98 | + // when |
| 99 | + let noticeIdentifiers = polymorphicIdentifiers(declaredIn: DummyNoticeCodableStrategy.self) |
| 100 | + let viewItemIdentifiers = polymorphicIdentifiers(declaredIn: ViewItemCodableStrategy.self) |
| 101 | + |
| 102 | + // then |
| 103 | + #expect(noticeIdentifiers == ["callout", "actionable-callout", "dismissible-callout"]) |
| 104 | + #expect(viewItemIdentifiers.contains("TITLE_VIEW_ITEM")) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + func `a hand-written strategy keeps decoding without adopting the new protocol`() throws { |
| 109 | + // given |
| 110 | + let jsonData = #""" |
| 111 | + { |
| 112 | + "notice" : { |
| 113 | + "description" : "Welcome to Karrot", |
| 114 | + "icon" : "waving_hand", |
| 115 | + "type" : "callout" |
| 116 | + } |
| 117 | + } |
| 118 | + """# |
| 119 | + |
| 120 | + // when |
| 121 | + let response = try JSONDecoder().decode( |
| 122 | + HandWrittenStrategyDummyResponse.self, |
| 123 | + from: Data(jsonData.utf8), |
| 124 | + ) |
| 125 | + |
| 126 | + // then |
| 127 | + let notice = try #require(response.notice as? DummyCallout) |
| 128 | + #expect(notice.description == "Welcome to Karrot") |
| 129 | + #expect(notice.icon == "waving_hand") |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +private func polymorphicIdentifiers<Strategy: PolymorphicMatchingTypesProviding>( |
| 134 | + declaredIn _: Strategy.Type |
| 135 | +) -> [String] { |
| 136 | + Strategy.matchingTypes.map { $0.polymorphicIdentifier } |
| 137 | +} |
0 commit comments