Skip to content

Commit

Permalink
Make fixture verification a bit strict (#280)
Browse files Browse the repository at this point in the history
* Remove adding unchecked sendable

* Add requireSendable context parameter

* sendable support in method model

* a bit refactoring

* Fix unnecessary param name and type splitting

* Stop using implicit arguments and context

* process argument history type

* Add final when Sendable required

* Fix state handler type

* sendable handler call

* Add @sendable to closure type

* generate concurrency helpers

* Beautify generated code
  • Loading branch information
sidepelican authored Dec 7, 2024
1 parent 1fb44ed commit 9fced08
Show file tree
Hide file tree
Showing 37 changed files with 250 additions and 418 deletions.
2 changes: 1 addition & 1 deletion Sources/MockoloFramework/Templates/NominalTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ extension NominalModel {
var template = ""

if !extraVarsToDecl.isEmpty {
template += "\(1.tab)\(extraVarsToDecl)\n"
template += "\(extraVarsToDecl)\n"
}

if needBlankInit {
Expand Down
6 changes: 3 additions & 3 deletions Sources/MockoloFramework/Templates/VariableTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension VariableModel {

var assignVal = ""
if !shouldOverride, let val = underlyingVarDefaultVal {
assignVal = "= \(val)"
assignVal = " = \(val)"
}

let privateSetSpace = allowSetCallCount ? "" : "\(String.privateSet) "
Expand Down Expand Up @@ -89,7 +89,7 @@ extension VariableModel {
template = """
\(setCallCountVarDecl)
\(1.tab)\(propertyWrapper)\(staticSpace)private var \(underlyingName): \(underlyingType) \(assignVal)\(accessorBlock)
\(1.tab)\(propertyWrapper)\(staticSpace)private var \(underlyingName): \(underlyingType)\(assignVal)\(accessorBlock)
\(1.tab)\(acl)\(staticSpace)\(overrideStr)\(modifierTypeStr)var \(name): \(type.typeName) {
\(2.tab)get { return \(underlyingName) }
\(2.tab)set { \(underlyingName) = newValue }
Expand All @@ -99,7 +99,7 @@ extension VariableModel {
template = """
\(setCallCountVarDecl)
\(1.tab)\(propertyWrapper)\(acl)\(staticSpace)\(overrideStr)\(modifierTypeStr)var \(name): \(type.typeName) \(assignVal)\(accessorBlock)
\(1.tab)\(propertyWrapper)\(acl)\(staticSpace)\(overrideStr)\(modifierTypeStr)var \(name): \(type.typeName)\(assignVal)\(accessorBlock)
"""
}

Expand Down
50 changes: 25 additions & 25 deletions Tests/MockoloTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,31 +113,31 @@ class MockoloTestCase: XCTestCase {
}

try generate(sourceDirs: [],
sourceFiles: srcFilePaths,
parser: SourceParser(),
exclusionSuffixes: ["Mocks", "Tests"],
mockFilePaths: mockFilePaths,
annotation: String.mockAnnotation,
header: header,
macro: "MOCK",
declType: declType,
useTemplateFunc: useTemplateFunc,
useMockObservable: useMockObservable,
allowSetCallCount: allowSetCallCount,
enableFuncArgsHistory: enableFuncArgsHistory,
disableCombineDefaultValues: disableCombineDefaultValues,
mockFinal: mockFinal,
testableImports: testableImports,
customImports: [],
excludeImports: [],
to: dstFilePath,
loggingLevel: 3,
concurrencyLimit: concurrencyLimit,
onCompletion: { ret in
let output = (try? String(contentsOf: URL(fileURLWithPath: self.defaultDstFilePath), encoding: .utf8)) ?? ""
let outputContents = output.components(separatedBy: .whitespacesAndNewlines).filter{!$0.isEmpty}
let fixtureContents = dstContent.components(separatedBy: .whitespacesAndNewlines).filter{!$0.isEmpty}
XCTAssert(outputContents.contains(subArray: fixtureContents), "output:\n" + output)
sourceFiles: srcFilePaths,
parser: SourceParser(),
exclusionSuffixes: ["Mocks", "Tests"],
mockFilePaths: mockFilePaths,
annotation: String.mockAnnotation,
header: header,
macro: "MOCK",
declType: declType,
useTemplateFunc: useTemplateFunc,
useMockObservable: useMockObservable,
allowSetCallCount: allowSetCallCount,
enableFuncArgsHistory: enableFuncArgsHistory,
disableCombineDefaultValues: disableCombineDefaultValues,
mockFinal: mockFinal,
testableImports: testableImports,
customImports: [],
excludeImports: [],
to: dstFilePath,
loggingLevel: 3,
concurrencyLimit: concurrencyLimit,
onCompletion: { ret in
let output = (try? String(contentsOf: URL(fileURLWithPath: self.defaultDstFilePath), encoding: .utf8)) ?? ""
let outputContents = output.components(separatedBy: .newlines).filter { !$0.isEmpty && !$0.allSatisfy(\.isWhitespace) }
let fixtureContents = dstContent.components(separatedBy: .newlines).filter { !$0.isEmpty && !$0.allSatisfy(\.isWhitespace) }
XCTAssert(outputContents.contains(subArray: fixtureContents), "output:\n" + output)
})
}
}
Expand Down
6 changes: 2 additions & 4 deletions Tests/TestActor/FixtureGlobalActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class RootControllerMock: RootController {
self._viewController = viewController
}
private var _viewController: UIViewController!
private var _viewController: UIViewController!
var viewController: UIViewController {
get { return _viewController }
set { _viewController = newValue }
Expand All @@ -34,7 +32,7 @@ class RootBuildableMock: RootBuildable {
private(set) var buildCallCount = 0
var buildHandler: (() -> RootController)?
func build() -> RootController {
func build() -> RootController {
buildCallCount += 1
if let buildHandler = buildHandler {
return buildHandler()
Expand Down
30 changes: 15 additions & 15 deletions Tests/TestArgumentsHistory/FixtureArgumentsHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FooMock: Foo {
private(set) var fooFuncCallCount = 0
var fooFuncArgValues = [Int]()
var fooFuncHandler: ((Int) -> ())?
func fooFunc(val: Int) {
func fooFunc(val: Int) {
fooFuncCallCount += 1
fooFuncArgValues.append(val)
if let fooFuncHandler = fooFuncHandler {
Expand All @@ -29,7 +29,7 @@ class FooMock: Foo {
private(set) var barFuncCallCount = 0
var barFuncArgValues = [[Int]]()
var barFuncHandler: (([Int]) -> ())?
func barFunc(for: [Int]) {
func barFunc(for: [Int]) {
barFuncCallCount += 1
barFuncArgValues.append(`for`)
if let barFuncHandler = barFuncHandler {
Expand All @@ -41,7 +41,7 @@ class FooMock: Foo {
private(set) var bazFuncCallCount = 0
var bazFuncArgValues = [(String, Float)]()
var bazFuncHandler: ((String, Float) -> ())?
func bazFunc(arg: String, default: Float) {
func bazFunc(arg: String, default: Float) {
bazFuncCallCount += 1
bazFuncArgValues.append((arg, `default`))
if let bazFuncHandler = bazFuncHandler {
Expand All @@ -61,7 +61,7 @@ class FooMock: Foo {
private(set) var fooFuncCallCount = 0
var fooFuncArgValues = [Int]()
var fooFuncHandler: ((Int) -> ())?
func fooFunc(val: Int) {
func fooFunc(val: Int) {
fooFuncCallCount += 1
fooFuncArgValues.append(val)
if let fooFuncHandler = fooFuncHandler {
Expand All @@ -72,7 +72,7 @@ class FooMock: Foo {
private(set) var barFuncCallCount = 0
var barFuncHandler: (([Int]) -> ())?
func barFunc(for: [Int]) {
func barFunc(for: [Int]) {
barFuncCallCount += 1
if let barFuncHandler = barFuncHandler {
barFuncHandler(`for`)
Expand All @@ -83,7 +83,7 @@ class FooMock: Foo {
private(set) var bazFuncCallCount = 0
var bazFuncArgValues = [(String, Float)]()
var bazFuncHandler: ((String, Float) -> ())?
func bazFunc(arg: String, default: Float) {
func bazFunc(arg: String, default: Float) {
bazFuncCallCount += 1
bazFuncArgValues.append((arg, `default`))
if let bazFuncHandler = bazFuncHandler {
Expand Down Expand Up @@ -236,7 +236,7 @@ class FooMock: Foo {
private(set) var fooFuncVal1CallCount = 0
var fooFuncVal1ArgValues = [Int]()
var fooFuncVal1Handler: ((Int) -> ())?
func fooFunc(val1: Int) {
func fooFunc(val1: Int) {
fooFuncVal1CallCount += 1
fooFuncVal1ArgValues.append(val1)
Expand All @@ -249,7 +249,7 @@ class FooMock: Foo {
private(set) var fooFuncVal1StringCallCount = 0
var fooFuncVal1StringArgValues = [String]()
var fooFuncVal1StringHandler: ((String) -> ())?
func fooFunc(val1: String) {
func fooFunc(val1: String) {
fooFuncVal1StringCallCount += 1
fooFuncVal1StringArgValues.append(val1)
Expand All @@ -261,7 +261,7 @@ class FooMock: Foo {
private(set) var fooFuncVal2CallCount = 0
var fooFuncVal2ArgValues = [Int]()
var fooFuncVal2Handler: ((Int) -> ())?
func fooFunc(val2: Int) {
func fooFunc(val2: Int) {
fooFuncVal2CallCount += 1
fooFuncVal2ArgValues.append(val2)
Expand Down Expand Up @@ -323,16 +323,17 @@ let argumentsHistoryInoutCaseMock = """
class FooMock: Foo {
init() { }
private(set) var fooFuncCallCount = 0
private(set) var fooFuncCallCount = 0
var fooFuncArgValues = [Int]()
var fooFuncHandler: ((inout Int) -> ())?
func fooFunc(val: inout Int) {
fooFuncCallCount += 1
fooFuncArgValues.append(val)
if let fooFuncHandler = fooFuncHandler {
fooFuncHandler(&val)
}
}
private(set) var barFuncCallCount = 0
Expand All @@ -341,10 +342,10 @@ class FooMock: Foo {
func barFunc(into val: inout Int) {
barFuncCallCount += 1
barFuncArgValues.append(val)
if let barFuncHandler = barFuncHandler {
barFuncHandler(&val)
barFuncHandler(&val)
}
}
}
"""
Expand Down Expand Up @@ -433,8 +434,7 @@ protocol Foo {

let argumentsHistoryAutoclosureCaseMock = """
class FooMock: Foo {
init() {
}
init() { }
private(set) var fooFuncCallCount = 0
var fooFuncHandler: ((@autoclosure () -> Int) -> ())?
Expand Down
8 changes: 3 additions & 5 deletions Tests/TestClassMocking/FixtureMockableClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class LowMock: Low {
private(set) var barCallCount = 0
var barHandler: (() -> ())?
override func bar() {
override func bar() {
barCallCount += 1
if let barHandler = barHandler {
barHandler()
Expand Down Expand Up @@ -176,14 +176,13 @@ public class LowMock: Low {
private(set) var whatSetCallCount = 0
override var what: Float { didSet { whatSetCallCount += 1 } }
private(set) var barCallCount = 0
private(set) var barCallCount = 0
var barHandler: (() -> ())?
override func bar() {
override func bar() {
barCallCount += 1
if let barHandler = barHandler {
barHandler()
}
}
private(set) var fooCallCount = 0
Expand All @@ -206,5 +205,4 @@ public class LowMock: Low {
}
}
}
"""
7 changes: 3 additions & 4 deletions Tests/TestCombine/FixtureCombine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class FooPublishedMock: FooPublished {
public private(set) var myCustomTypePublisherSubject = PassthroughSubject<MyCustomType, Error>()
public private(set) var myNonOptionalSetCallCount = 0
@CustomPropertyWrapper private var _myNonOptional: NonOptional! { didSet { myNonOptionalSetCallCount += 1 } }
@CustomPropertyWrapper private var _myNonOptional: NonOptional! { didSet { myNonOptionalSetCallCount += 1 } }
public var myNonOptional: NonOptional {
get { return _myNonOptional }
set { _myNonOptional = newValue }
Expand Down Expand Up @@ -210,13 +210,12 @@ public class ChildMock: Child {
self.myStringInBase = myStringInBase
}
public var myStringPublisher: AnyPublisher<String?, Never> { return self.$myStringInBase.map { $0 }.setFailureType(to: Never.self).eraseToAnyPublisher() }
public var myStringPublisher: AnyPublisher<String?, Never> { return self.$myStringInBase.map { $0 }.setFailureType(to: Never.self).eraseToAnyPublisher() }
public var dictionaryPublisher: AnyPublisher<Dictionary<String, String>, Never> { return self.dictionaryPublisherSubject.eraseToAnyPublisher() }
public private(set) var dictionaryPublisherSubject = CurrentValueSubject<Dictionary<String, String>, Never>(Dictionary<String, String>())
public private(set) var myStringInBaseSetCallCount = 0
@Published public var myStringInBase: String = "" { didSet { myStringInBaseSetCallCount += 1 } }
@Published public var myStringInBase: String = "" { didSet { myStringInBaseSetCallCount += 1 } }
}
"""

Expand Down
4 changes: 2 additions & 2 deletions Tests/TestDocComments/FixtureDocComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class DocProtocolMock: DocProtocol {
public private(set) var fooCallCount = 0
public var fooHandler: ((Bool, Int) -> ())?
public func foo(arg: Bool, tag: Int) {
public func foo(arg: Bool, tag: Int) {
fooCallCount += 1
if let fooHandler = fooHandler {
fooHandler(arg, tag)
Expand All @@ -77,7 +77,7 @@ public class DocProtocolMock: DocProtocol {
public private(set) var barCallCount = 0
public var barHandler: ((String, Float) -> ())?
public func bar(name: String, more: Float) {
public func bar(name: String, more: Float) {
barCallCount += 1
if let barHandler = barHandler {
barHandler(name, more)
Expand Down
9 changes: 6 additions & 3 deletions Tests/TestEmojis/EmojiTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import Foundation

class EmojiTests: MockoloTestCase {

func testEmojis() {
verify(srcContent: emojiVars,
dstContent: emojiVarsMock)
Expand All @@ -12,4 +9,10 @@ class EmojiTests: MockoloTestCase {
mockContent: emojiParentMock,
dstContent: emojiCombMock)
}

func testKrJp() {
verify(srcContent: krJp,
mockContent: krJpParentMock,
dstContent: krJpMock)
}
}
Loading

0 comments on commit 9fced08

Please sign in to comment.