-
Notifications
You must be signed in to change notification settings - Fork 0
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 #25 from appunite/0.4.0
0.4.0
- Loading branch information
Showing
9 changed files
with
188 additions
and
6 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
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
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
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,52 @@ | ||
// | ||
// Spied.swift | ||
// Spy | ||
// | ||
// Created by Tomasz Lewandowski on 01/02/2020. | ||
// Copyright © 2020 AppUnite Sp. z o.o. All rights reserved. | ||
// | ||
|
||
@propertyWrapper | ||
public struct Spied<SpyLevel: PSpyLevel, SpyChannel: PSpyChannel, T: PSpyable> { | ||
private var spy: AnySpy<SpyLevel, SpyChannel> | ||
private var value: T | ||
private var level: SpyLevel | ||
private var channel: SpyChannel | ||
private var getMapper: (PSpyable) -> PSpyable | ||
private var setMapper: (PSpyable) -> PSpyable | ||
|
||
public var wrappedValue: T { | ||
get { | ||
spy.log(level: level, channel: channel, message: getMapper(value)) | ||
return value | ||
} | ||
set { | ||
spy.log(level: level, channel: channel, message: setMapper(newValue)) | ||
value = newValue | ||
} | ||
} | ||
|
||
public init(wrappedValue: T, spy: AnySpy<SpyLevel, SpyChannel>, onLevel level: SpyLevel, onChannel channel: SpyChannel) { | ||
self.value = wrappedValue | ||
self.spy = spy | ||
self.level = level | ||
self.channel = channel | ||
self.getMapper = { spyable in "Get: \(spyable.spyMessage)" } | ||
self.setMapper = { spyable in "Set: \(spyable.spyMessage)" } | ||
} | ||
|
||
public init(wrappedValue: T, | ||
spy: AnySpy<SpyLevel, SpyChannel>, | ||
onLevel level: SpyLevel, | ||
onChannel channel: SpyChannel, | ||
getMapper: @escaping (PSpyable) -> PSpyable, | ||
setMapper: @escaping (PSpyable) -> PSpyable) { | ||
self.value = wrappedValue | ||
self.spy = spy | ||
self.level = level | ||
self.channel = channel | ||
self.getMapper = getMapper | ||
self.setMapper = setMapper | ||
} | ||
|
||
} |
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,91 @@ | ||
// | ||
// SpiedTests.swift | ||
// Spy | ||
// | ||
// Created by Tomasz Lewandowski on 03/02/2020. | ||
// Copyright © 2020 AppUnite Sp. z o.o. All rights reserved. | ||
// | ||
|
||
|
||
import XCTest | ||
import Spy | ||
|
||
public final class SpiedTests: XCTestCase { | ||
func testGetter_WhenValueAccessed_ShouldInvokeMapper() { | ||
class SUT { | ||
static var pSpyMock = PSpyMock<SpyLevel, SpyChannel>() | ||
@Spied(spy: pSpyMock.any(), onLevel: .info, onChannel: .foo) | ||
var foo: String = "message" | ||
} | ||
SUT.pSpyMock.logLevelChannelMessageReturnValue = SUT.pSpyMock | ||
let sut = SUT() | ||
print(sut.foo) | ||
XCTAssertTrue(SUT.pSpyMock.logLevelChannelMessageCalled) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.level, .info) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.channel, .foo) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.message as? String, "Get: message") | ||
} | ||
|
||
func testSetter_WhenValueSet_ShouldInvokeMapper() { | ||
class SUT { | ||
static var pSpyMock = PSpyMock<SpyLevel, SpyChannel>() | ||
@Spied(spy: pSpyMock.any(), onLevel: .info, onChannel: .foo) | ||
var foo: String = "message" | ||
} | ||
SUT.pSpyMock.logLevelChannelMessageReturnValue = SUT.pSpyMock | ||
let sut = SUT() | ||
sut.foo = "foo" | ||
XCTAssertTrue(SUT.pSpyMock.logLevelChannelMessageCalled) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.level, .info) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.channel, .foo) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.message as? String, "Set: foo") | ||
} | ||
|
||
func testCustomGetter_WhenValueAccessed_ShouldInvokeMapper() { | ||
class SUT { | ||
static var pSpyMock = PSpyMock<SpyLevel, SpyChannel>() | ||
@Spied(spy: pSpyMock.any(), onLevel: .info, onChannel: .foo, | ||
getMapper: { spyable in | ||
let spyable = "CustomGet \(spyable.spyMessage)" | ||
return spyable | ||
}, | ||
setMapper: { spyable in | ||
let spyable = "CustomSet \(spyable.spyMessage)" | ||
return spyable | ||
}) | ||
var foo: String = "message" | ||
} | ||
SUT.pSpyMock.logLevelChannelMessageReturnValue = SUT.pSpyMock | ||
let sut = SUT() | ||
print(sut.foo) | ||
XCTAssertTrue(SUT.pSpyMock.logLevelChannelMessageCalled) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.level, .info) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.channel, .foo) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.message as? String, "CustomGet message") | ||
} | ||
|
||
func testCustomSetter_WhenValueAccessed_ShouldInvokeMapper() { | ||
class SUT { | ||
static var pSpyMock = PSpyMock<SpyLevel, SpyChannel>() | ||
@Spied(spy: pSpyMock.any(), onLevel: .info, onChannel: .foo, | ||
getMapper: { spyable in | ||
let spyable = "CustomGet \(spyable.spyMessage)" | ||
return spyable | ||
}, | ||
setMapper: { spyable in | ||
let spyable = "CustomSet \(spyable.spyMessage)" | ||
return spyable | ||
}) | ||
var foo: String = "message" | ||
} | ||
SUT.pSpyMock.logLevelChannelMessageReturnValue = SUT.pSpyMock | ||
let sut = SUT() | ||
sut.foo = "foo" | ||
XCTAssertTrue(SUT.pSpyMock.logLevelChannelMessageCalled) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.level, .info) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.channel, .foo) | ||
XCTAssertEqual(SUT.pSpyMock.logLevelChannelMessageReceivedArguments?.message as? String, "CustomSet foo") | ||
} | ||
|
||
} | ||
|