Skip to content

Commit

Permalink
Avoid duplicated variable under if-macro syntax (#279)
Browse files Browse the repository at this point in the history
* Use uniqueModel for IfMacroModel subModels before rendering
so that we can avoid naming duplication after generation

* Remove wrapping by `XCTExpectFailure`

* Remove XCTest import

* Fix test fixture
  • Loading branch information
fummicc1 authored Dec 3, 2024
1 parent 0d66daa commit 954b952
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
6 changes: 3 additions & 3 deletions Sources/MockoloFramework/Models/IfMacroModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
final class IfMacroModel: Model {
let name: String
let offset: Int64
let entities: [Model]
let entities: [(String, Model)]

var modelType: ModelType {
return .macro
}

var fullName: String {
return entities.map {$0.fullName}.joined(separator: "_")
return entities.map {$0.0}.joined(separator: "_")
}

init(name: String,
offset: Int64,
entities: [Model]) {
entities: [(String, Model)]) {
self.name = name
self.entities = entities
self.offset = offset
Expand Down
10 changes: 8 additions & 2 deletions Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,14 @@ extension IfConfigDeclSyntax {
}
}
}

let macroModel = IfMacroModel(name: name, offset: self.offset, entities: subModels)

let uniqueSubModels = uniqueEntities(
in: subModels,
exclude: [:],
fullnames: []
).map({ $0 })

let macroModel = IfMacroModel(name: name, offset: self.offset, entities: uniqueSubModels)
return (macroModel, attrDesc, hasInit)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/MockoloFramework/Templates/IfMacroTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ extension IfMacroModel {
func applyMacroTemplate(name: String,
context: RenderContext,
arguments: GenerationArguments,
entities: [Model]) -> String {
entities: [(String, Model)]) -> String {
let rendered = entities
.compactMap { model in
model.render(
model.1.render(
context: .init(
overloadingResolvedName: model.name, // FIXME: the name is not resolving overload
overloadingResolvedName: model.0,
enclosingType: context.enclosingType,
annotatedTypeKind: context.annotatedTypeKind
),
Expand Down
15 changes: 11 additions & 4 deletions Tests/TestMacros/FixtureMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,27 @@ protocol PresentableListener: AnyObject {
"""

let macroInFuncWithOverloadMock = """
class PresentableListenerMock: PresentableListener {
init() { }
#if DEBUG
private(set) var runCallCount = 0
var runHandler: ((Int) -> ())?
func run(value: Int) {
var runHandler: ((Int) -> ())?
func run(value: Int) {
runCallCount += 1
if let runHandler = runHandler {
runHandler(value)
}
}
private(set) var runValueCallCount = 0
var runValueHandler: ((String) -> ())?
func run(value: String) {
var runValueHandler: ((String) -> ())?
func run(value: String) {
runValueCallCount += 1
if let runValueHandler = runValueHandler {
runValueHandler(value)
Expand All @@ -305,4 +311,5 @@ class PresentableListenerMock: PresentableListener {
}
#endif
}
"""
10 changes: 2 additions & 8 deletions Tests/TestMacros/MacroTests.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import XCTest

final class MacroTests: MockoloTestCase {
func testMacroInFunc() {
verify(srcContent: macroInFunc,
dstContent: macroInFuncMock)
}

#if os(macOS)
func testMacroInFuncWithOverload() {
XCTExpectFailure("Resolving overloading in #if is broken.") {
verify(srcContent: macroInFuncWithOverload,
dstContent: macroInFuncWithOverloadMock)
}
verify(srcContent: macroInFuncWithOverload,
dstContent: macroInFuncWithOverloadMock)
}
#endif

func testMacroImports() {
verify(srcContent: macroImports,
Expand Down

0 comments on commit 954b952

Please sign in to comment.