|
| 1 | +@testable import SimplexArchitecture |
| 2 | +@testable import SimplexArchitectureMacrosPlugin |
| 3 | +import SwiftUI |
| 4 | +import XCTest |
| 5 | +import MacroTesting |
| 6 | + |
| 7 | +final class ScopeStateMacroTests: XCTestCase { |
| 8 | + override func invokeTest() { |
| 9 | + withMacroTesting( |
| 10 | + macros: ["ScopeState": ScopeState.self] |
| 11 | + ) { |
| 12 | + super.invokeTest() |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + func testDiagnostic() { |
| 17 | + assertMacro { |
| 18 | + """ |
| 19 | + @ScopeState |
| 20 | + public actor Test { |
| 21 | + } |
| 22 | + """ |
| 23 | + } matches: { |
| 24 | + """ |
| 25 | + @ScopeState |
| 26 | + ┬────────── |
| 27 | + ├─ 🛑 'ScopeState' macro can only be applied to struct or class |
| 28 | + ╰─ 🛑 'ScopeState' macro can only be applied to struct or class |
| 29 | + public actor Test { |
| 30 | + } |
| 31 | + """ |
| 32 | + } |
| 33 | + |
| 34 | + assertMacro { |
| 35 | + """ |
| 36 | + @ScopeState |
| 37 | + public protocol Test { |
| 38 | + } |
| 39 | + """ |
| 40 | + } matches: { |
| 41 | + """ |
| 42 | + @ScopeState |
| 43 | + ┬────────── |
| 44 | + ├─ 🛑 'ScopeState' macro can only be applied to struct or class |
| 45 | + ╰─ 🛑 'ScopeState' macro can only be applied to struct or class |
| 46 | + public protocol Test { |
| 47 | + } |
| 48 | + """ |
| 49 | + } |
| 50 | + |
| 51 | + assertMacro { |
| 52 | + """ |
| 53 | + @ScopeState |
| 54 | + public extension Test { |
| 55 | + } |
| 56 | + """ |
| 57 | + } matches: { |
| 58 | + """ |
| 59 | + @ScopeState |
| 60 | + ┬────────── |
| 61 | + ├─ 🛑 'ScopeState' macro can only be applied to struct or class |
| 62 | + ╰─ 🛑 'ScopeState' macro can only be applied to struct or class |
| 63 | + public extension Test { |
| 64 | + } |
| 65 | + """ |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + func testPublicExpansion() { |
| 70 | + assertMacro { |
| 71 | + """ |
| 72 | + @ScopeState |
| 73 | + public struct TestView: View { |
| 74 | + @State var count = 0 |
| 75 | + let store: Store<TestReducer> |
| 76 | +
|
| 77 | + init(store: Store<TestReducer> = Store(reducer: TestReducer(), initialReducerState: .init())) { |
| 78 | + self.store = store |
| 79 | + } |
| 80 | +
|
| 81 | + var body: some View { |
| 82 | + EmptyView() |
| 83 | + } |
| 84 | + } |
| 85 | + """ |
| 86 | + } matches: { |
| 87 | + #""" |
| 88 | + public struct TestView: View { |
| 89 | + @State var count = 0 |
| 90 | + let store: Store<TestReducer> |
| 91 | +
|
| 92 | + init(store: Store<TestReducer> = Store(reducer: TestReducer(), initialReducerState: .init())) { |
| 93 | + self.store = store |
| 94 | + } |
| 95 | +
|
| 96 | + var body: some View { |
| 97 | + EmptyView() |
| 98 | + } |
| 99 | +
|
| 100 | + public struct States: StatesProtocol { |
| 101 | + var count = 0 |
| 102 | + public static let keyPathMap: [PartialKeyPath<States>: PartialKeyPath<TestView>] = [\.count: \.count] |
| 103 | + } |
| 104 | + } |
| 105 | +
|
| 106 | + extension TestView: ActionSendable { |
| 107 | + } |
| 108 | + """# |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + func testInternalExpansion() { |
| 113 | + assertMacro { |
| 114 | + """ |
| 115 | + @ScopeState |
| 116 | + struct TestView: View { |
| 117 | + @State var count = 0 |
| 118 | + let store: Store<TestReducer> |
| 119 | +
|
| 120 | + init(store: Store<TestReducer> = Store(reducer: TestReducer(), initialReducerState: .init())) { |
| 121 | + self.store = store |
| 122 | + } |
| 123 | +
|
| 124 | + var body: some View { |
| 125 | + EmptyView() |
| 126 | + } |
| 127 | + } |
| 128 | + """ |
| 129 | + } matches: { |
| 130 | + #""" |
| 131 | + struct TestView: View { |
| 132 | + @State var count = 0 |
| 133 | + let store: Store<TestReducer> |
| 134 | +
|
| 135 | + init(store: Store<TestReducer> = Store(reducer: TestReducer(), initialReducerState: .init())) { |
| 136 | + self.store = store |
| 137 | + } |
| 138 | +
|
| 139 | + var body: some View { |
| 140 | + EmptyView() |
| 141 | + } |
| 142 | +
|
| 143 | + internal struct States: StatesProtocol { |
| 144 | + var count = 0 |
| 145 | + internal static let keyPathMap: [PartialKeyPath<States>: PartialKeyPath<TestView>] = [\.count: \.count] |
| 146 | + } |
| 147 | + } |
| 148 | +
|
| 149 | + extension TestView: ActionSendable { |
| 150 | + } |
| 151 | + """# |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + func testFileprivateExpansion() { |
| 157 | + assertMacro { |
| 158 | + """ |
| 159 | + @ScopeState |
| 160 | + fileprivate struct TestView: View { |
| 161 | + @State fileprivate var count = 0 |
| 162 | + let store: Store<TestReducer> |
| 163 | +
|
| 164 | + init(store: Store<TestReducer> = Store(reducer: TestReducer(), initialReducerState: .init())) { |
| 165 | + self.store = store |
| 166 | + } |
| 167 | +
|
| 168 | + var body: some View { |
| 169 | + EmptyView() |
| 170 | + } |
| 171 | + } |
| 172 | + """ |
| 173 | + } matches: { |
| 174 | + #""" |
| 175 | + fileprivate struct TestView: View { |
| 176 | + @State fileprivate var count = 0 |
| 177 | + let store: Store<TestReducer> |
| 178 | +
|
| 179 | + init(store: Store<TestReducer> = Store(reducer: TestReducer(), initialReducerState: .init())) { |
| 180 | + self.store = store |
| 181 | + } |
| 182 | +
|
| 183 | + var body: some View { |
| 184 | + EmptyView() |
| 185 | + } |
| 186 | +
|
| 187 | + internal struct States: StatesProtocol { |
| 188 | + var count = 0 |
| 189 | + internal static let keyPathMap: [PartialKeyPath<States>: PartialKeyPath<TestView>] = [\.count: \.count] |
| 190 | + } |
| 191 | + } |
| 192 | +
|
| 193 | + extension TestView: ActionSendable { |
| 194 | + } |
| 195 | + """# |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + func testPrivateExpansion() { |
| 200 | + assertMacro { |
| 201 | + """ |
| 202 | + @ScopeState |
| 203 | + private struct TestView: View { |
| 204 | + @State private var count = 0 |
| 205 | + let store: Store<TestReducer> |
| 206 | +
|
| 207 | + init(store: Store<TestReducer> = Store(reducer: TestReducer(), initialReducerState: .init())) { |
| 208 | + self.store = store |
| 209 | + } |
| 210 | +
|
| 211 | + var body: some View { |
| 212 | + EmptyView() |
| 213 | + } |
| 214 | + } |
| 215 | + """ |
| 216 | + } matches: { |
| 217 | + #""" |
| 218 | + private struct TestView: View { |
| 219 | + @State private var count = 0 |
| 220 | + let store: Store<TestReducer> |
| 221 | +
|
| 222 | + init(store: Store<TestReducer> = Store(reducer: TestReducer(), initialReducerState: .init())) { |
| 223 | + self.store = store |
| 224 | + } |
| 225 | +
|
| 226 | + var body: some View { |
| 227 | + EmptyView() |
| 228 | + } |
| 229 | +
|
| 230 | + internal struct States: StatesProtocol { |
| 231 | + var count = 0 |
| 232 | + internal static let keyPathMap: [PartialKeyPath<States>: PartialKeyPath<TestView>] = [\.count: \.count] |
| 233 | + } |
| 234 | + } |
| 235 | +
|
| 236 | + extension TestView: ActionSendable { |
| 237 | + } |
| 238 | + """# |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments