Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have computed var properties also use the queue to add invocations safely #334

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/SwiftyMocky/Mock.swifttemplate
Original file line number Diff line number Diff line change
Expand Up @@ -1500,14 +1500,14 @@ class VariableWrapper {
var getter: String {
let staticModifier = variable.isStatic ? "\(scope)." : ""
let returnValue = variable.isOptional ? "optionalGivenGetterValue(.\(propertyCaseGetName), \"\(noStubDefinedMessage)\")" : "givenGetterValue(.\(propertyCaseGetName), \"\(noStubDefinedMessage)\")"
return "\n\t\tget {\t\(staticModifier)invocations.append(.\(propertyCaseGetName)); return \(staticModifier)\(privatePrototypeName) ?? \(returnValue) }"
return "\n\t\tget {\t\(staticModifier)self.queue.sync { invocations.append(.\(propertyCaseGetName)) }; return \(staticModifier)\(privatePrototypeName) ?? \(returnValue) }"
}
var setter: String {
let staticModifier = variable.isStatic ? "\(scope)." : ""
if readonly {
return ""
} else {
return "\n\t\tset {\t\(staticModifier)invocations.append(.\(propertyCaseSetName)(.value(newValue))); \(variable.isStatic ? "\(scope)." : "")\(privatePrototypeName) = newValue }"
return "\n\t\tset {\t\(staticModifier)self.queue.sync { invocations.append(.\(propertyCaseSetName)(.value(newValue))) }; \(variable.isStatic ? "\(scope)." : "")\(privatePrototypeName) = newValue }"
}
}
var prototype: String {
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftyPrototype/Prototype.swifttemplate
Original file line number Diff line number Diff line change
Expand Up @@ -1499,14 +1499,14 @@ class VariableWrapper {
var getter: String {
let staticModifier = variable.isStatic ? "\(scope)." : ""
let returnValue = variable.isOptional ? "optionalGivenGetterValue(.\(propertyCaseGetName), \"\(noStubDefinedMessage)\")" : "givenGetterValue(.\(propertyCaseGetName), \"\(noStubDefinedMessage)\")"
return "\n\t\tget {\t\(staticModifier)invocations.append(.\(propertyCaseGetName)); return \(staticModifier)\(privatePrototypeName) ?? \(returnValue) }"
return "\n\t\tget {\t\(staticModifier)self.queue.sync { invocations.append(.\(propertyCaseGetName)) }; return \(staticModifier)\(privatePrototypeName) ?? \(returnValue) }"
}
var setter: String {
let staticModifier = variable.isStatic ? "\(scope)." : ""
if readonly {
return ""
} else {
return "\n\t\tset {\t\(staticModifier)invocations.append(.\(propertyCaseSetName)(.value(newValue))); \(variable.isStatic ? "\(scope)." : "")\(privatePrototypeName) = newValue }"
return "\n\t\tset {\t\(staticModifier)self.queue.sync { invocations.append(.\(propertyCaseSetName)(.value(newValue))) }; \(variable.isStatic ? "\(scope)." : "")\(privatePrototypeName) = newValue }"
}
}
var prototype: String {
Expand Down
24 changes: 24 additions & 0 deletions SwiftyMocky-Tests/Shared/Example 1/SimpleProtocolsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,30 @@ class SimpleProtocolsTests: XCTestCase {
XCTAssertEqual(mock.propertyImplicit, 7)
}

func test_simpleProtocol_with_properties_threadSafe() {
let mock = SimpleProtocolWithPropertiesMock()

// Can use explicit values, or Count convenience static members as Count
// Check property getters invocations
Verify(mock, 0, .property)

Verify(mock, .never, .property)
// Check property setters invocations
Verify(mock, .never, .property(set: .any))

// We should set all initial values for non optional parameters and implicitly unwrapped optional parameters
mock.property = "test"
Given(mock, .propertyGetOnly(getter: "get only ;)"))
Given(mock, .propertyOptional(getter: nil))
mock.propertyImplicit = 1

// Try accessing the property on many threads simultaneously.
DispatchQueue.concurrentPerform(iterations: 10_000) { i in
let _ = mock.property
}

}

func test_simpleProtocol_with_both() {
let mock = SimpleProtocolWithBothMethodsAndPropertiesMock()

Expand Down
108 changes: 54 additions & 54 deletions SwiftyMocky-Tests/iOS/Mocks/Mock.15.generated.swift

Large diffs are not rendered by default.

106 changes: 53 additions & 53 deletions SwiftyMocky-Tests/iOS/Mocks/Mock.generated.swift

Large diffs are not rendered by default.

106 changes: 53 additions & 53 deletions SwiftyMocky-Tests/macOS/Mocks/Mock.generated.swift

Large diffs are not rendered by default.

106 changes: 53 additions & 53 deletions SwiftyMocky-Tests/tvOS/Mocks/Mock.generated.swift

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Templates/VariableWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class VariableWrapper {
var getter: String {
let staticModifier = variable.isStatic ? "\(scope)." : ""
let returnValue = variable.isOptional ? "optionalGivenGetterValue(.\(propertyCaseGetName), \"\(noStubDefinedMessage)\")" : "givenGetterValue(.\(propertyCaseGetName), \"\(noStubDefinedMessage)\")"
return "\n\t\tget {\t\(staticModifier)invocations.append(.\(propertyCaseGetName)); return \(staticModifier)\(privatePrototypeName) ?? \(returnValue) }"
return "\n\t\tget {\t\(staticModifier)self.queue.sync { invocations.append(.\(propertyCaseGetName)) }; return \(staticModifier)\(privatePrototypeName) ?? \(returnValue) }"
}
var setter: String {
let staticModifier = variable.isStatic ? "\(scope)." : ""
if readonly {
return ""
} else {
return "\n\t\tset {\t\(staticModifier)invocations.append(.\(propertyCaseSetName)(.value(newValue))); \(variable.isStatic ? "\(scope)." : "")\(privatePrototypeName) = newValue }"
return "\n\t\tset {\t\(staticModifier)self.queue.sync { invocations.append(.\(propertyCaseSetName)(.value(newValue))) }; \(variable.isStatic ? "\(scope)." : "")\(privatePrototypeName) = newValue }"
}
}
var prototype: String {
Expand Down
Loading