-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from uber/es-macro
Support members wrapped in #if macro
- Loading branch information
Showing
6 changed files
with
212 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// Copyright (c) 2018. Uber Technologies | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
final class IfMacroModel: Model { | ||
var name: String | ||
var offset: Int64 | ||
var type: Type | ||
let entities: [Model] | ||
|
||
var modelType: ModelType { | ||
return .macro | ||
} | ||
|
||
var fullName: String { | ||
return entities.map {$0.fullName}.joined(separator: "_") | ||
} | ||
|
||
init(name: String, | ||
offset: Int64, | ||
entities: [Model]) { | ||
self.name = name | ||
self.entities = entities | ||
self.offset = offset | ||
self.type = Type(name) | ||
} | ||
|
||
func render(with identifier: String, typeKeys: [String: String]? = nil) -> String? { | ||
return applyMacroTemplate(name: name, typeKeys: typeKeys, entities: entities) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Copyright (c) 2018. Uber Technologies | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
func applyMacroTemplate(name: String, | ||
typeKeys: [String: String]?, | ||
entities: [Model]) -> String { | ||
let rendered = entities | ||
.compactMap {$0.render(with: $0.name, typeKeys: typeKeys) } | ||
.joined(separator: "\n") | ||
|
||
let template = """ | ||
#if \(name) | ||
\(rendered) | ||
#endif | ||
""" | ||
return template | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import MockoloFramework | ||
|
||
|
||
let macro = | ||
""" | ||
/// \(String.mockAnnotation) | ||
protocol PresentableListener: class { | ||
func run() | ||
#if DEBUG | ||
func showDebugMode() | ||
#endif | ||
} | ||
""" | ||
|
||
let macroMock = """ | ||
class PresentableListenerMock: PresentableListener { | ||
private var _doneInit = false | ||
init() { _doneInit = true } | ||
var runCallCount = 0 | ||
var runHandler: (() -> ())? | ||
func run() { | ||
runCallCount += 1 | ||
if let runHandler = runHandler { | ||
runHandler() | ||
} | ||
} | ||
#if DEBUG | ||
var showDebugModeCallCount = 0 | ||
var showDebugModeHandler: (() -> ())? | ||
func showDebugMode() { | ||
showDebugModeCallCount += 1 | ||
if let showDebugModeHandler = showDebugModeHandler { | ||
showDebugModeHandler() | ||
} | ||
} | ||
#endif | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Foundation | ||
|
||
class MacroTests: MockoloTestCase { | ||
func testMacro() { | ||
verify(srcContent: macro, | ||
dstContent: macroMock, | ||
parser: .swiftSyntax) | ||
} | ||
} |