|
| 1 | +// |
| 2 | +// PropertiesListTests.swift |
| 3 | +// PrincipleMacros |
| 4 | +// |
| 5 | +// Created by Kamil Strzelecki on 15/05/2025. |
| 6 | +// Copyright © 2025 Kamil Strzelecki. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +@testable import PrincipleMacros |
| 10 | +import SwiftSyntaxMacroExpansion |
| 11 | +import Testing |
| 12 | + |
| 13 | +internal struct PropertiesListTests { |
| 14 | + |
| 15 | + private let list: PropertiesList |
| 16 | + |
| 17 | + init() throws { |
| 18 | + let decl: DeclSyntax = """ |
| 19 | + class Test { |
| 20 | +
|
| 21 | + let letProp: Int? = 0 |
| 22 | + var varProp = "" |
| 23 | + lazy var lazyVarProp = Optional<Int>.none |
| 24 | +
|
| 25 | + static let staticLetProp = String(data: Data(), encoding: .utf8) |
| 26 | + static var staticVarProp = Int?.some(0) |
| 27 | + class var classVarProp: String { "" } |
| 28 | +
|
| 29 | + var computedProp: Int? { 0 } |
| 30 | + var computedSettableProp: Optional<Int> { |
| 31 | + get { 0 } |
| 32 | + set {} |
| 33 | + } |
| 34 | + } |
| 35 | + """ |
| 36 | + |
| 37 | + let classDecl = try #require(ClassDeclSyntax(decl)) |
| 38 | + let context = BasicMacroExpansionContext() |
| 39 | + self.list = PropertiesParser.parse(memberBlock: classDecl.memberBlock, in: context) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + func testImmutable() throws { |
| 44 | + #expect( |
| 45 | + list.immutable.map(\.trimmedName.description) == [ |
| 46 | + "letProp", |
| 47 | + "staticLetProp", |
| 48 | + "classVarProp", |
| 49 | + "computedProp" |
| 50 | + ] |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + func testMutable() throws { |
| 56 | + #expect( |
| 57 | + list.mutable.map(\.trimmedName.description) == [ |
| 58 | + "varProp", |
| 59 | + "lazyVarProp", |
| 60 | + "staticVarProp", |
| 61 | + "computedSettableProp" |
| 62 | + ] |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + func testInstance() throws { |
| 68 | + #expect( |
| 69 | + list.instance.map(\.trimmedName.description) == [ |
| 70 | + "letProp", |
| 71 | + "varProp", |
| 72 | + "lazyVarProp", |
| 73 | + "computedProp", |
| 74 | + "computedSettableProp" |
| 75 | + ] |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + func testType() throws { |
| 81 | + #expect( |
| 82 | + list.type.map(\.trimmedName.description) == [ |
| 83 | + "staticLetProp", |
| 84 | + "staticVarProp", |
| 85 | + "classVarProp" |
| 86 | + ] |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + func testStored() throws { |
| 92 | + #expect( |
| 93 | + list.stored.map(\.trimmedName.description) == [ |
| 94 | + "letProp", |
| 95 | + "varProp", |
| 96 | + "lazyVarProp", |
| 97 | + "staticLetProp", |
| 98 | + "staticVarProp" |
| 99 | + ] |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + func testComputed() throws { |
| 105 | + #expect( |
| 106 | + list.computed.map(\.trimmedName.description) == [ |
| 107 | + "classVarProp", |
| 108 | + "computedProp", |
| 109 | + "computedSettableProp" |
| 110 | + ] |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + func testUniqueInferredTypes() throws { |
| 116 | + #expect( |
| 117 | + list.uniqueInferredTypes.map(\.description) == [ |
| 118 | + "Optional<Int>", |
| 119 | + "String" |
| 120 | + ] |
| 121 | + ) |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + func testWithInferredType() throws { |
| 126 | + #expect( |
| 127 | + list.withInferredType(like: "Int?").map(\.trimmedName.description) == [ |
| 128 | + "letProp", |
| 129 | + "lazyVarProp", |
| 130 | + "staticVarProp", |
| 131 | + "computedProp", |
| 132 | + "computedSettableProp" |
| 133 | + ] |
| 134 | + ) |
| 135 | + #expect( |
| 136 | + list.withInferredType(like: "String").map(\.trimmedName.description) == [ |
| 137 | + "varProp", |
| 138 | + "staticLetProp", |
| 139 | + "classVarProp" |
| 140 | + ] |
| 141 | + ) |
| 142 | + } |
| 143 | +} |
0 commit comments