|
| 1 | +import Fakes |
| 2 | +import Nimble |
| 3 | +import XCTest |
| 4 | + |
| 5 | +final class SettablePropertySpyTests: XCTestCase { |
| 6 | + func testGettingPropertyWhenTypesMatch() { |
| 7 | + struct AnObject { |
| 8 | + @SettablePropertySpy(1) |
| 9 | + var value: Int |
| 10 | + } |
| 11 | + |
| 12 | + let object = AnObject() |
| 13 | + |
| 14 | + expect(object.value).to(equal(1)) |
| 15 | + // because we called it, we should expect for the getter spy to be called |
| 16 | + expect(object.$value.getter).to(beCalled()) |
| 17 | + |
| 18 | + // We never interacted with the setter, so it shouldn't have been called. |
| 19 | + expect(object.$value.setter).toNot(beCalled()) |
| 20 | + } |
| 21 | + |
| 22 | + func testSettingPropertyWhenTypesMatch() { |
| 23 | + struct AnObject { |
| 24 | + @SettablePropertySpy(1) |
| 25 | + var value: Int |
| 26 | + } |
| 27 | + |
| 28 | + var object = AnObject() |
| 29 | + object.value = 3 |
| 30 | + |
| 31 | + expect(object.$value.getter).toNot(beCalled()) |
| 32 | + expect(object.$value.setter).to(beCalled(3)) |
| 33 | + |
| 34 | + // the returned value should now be updated with the new value |
| 35 | + expect(object.value).to(equal(3)) |
| 36 | + |
| 37 | + // and because we called the getter, the getter spy should be called. |
| 38 | + expect(object.$value.getter).to(beCalled()) |
| 39 | + } |
| 40 | + |
| 41 | + func testGettingPropertyProtocolInheritence() { |
| 42 | + struct ImplementedProtocol: SomeProtocol { |
| 43 | + var value: Int = 1 |
| 44 | + } |
| 45 | + |
| 46 | + struct AnObject { |
| 47 | + @SettablePropertySpy(ImplementedProtocol(value: 2)) |
| 48 | + var value: SomeProtocol |
| 49 | + } |
| 50 | + |
| 51 | + let object = AnObject() |
| 52 | + |
| 53 | + expect(object.value).to(beAKindOf(ImplementedProtocol.self)) |
| 54 | + // because we called it, we should expect for the getter spy to be called |
| 55 | + expect(object.$value.getter).to(beCalled()) |
| 56 | + |
| 57 | + // We never interacted with the setter, so it shouldn't have been called. |
| 58 | + expect(object.$value.setter).toNot(beCalled()) |
| 59 | + } |
| 60 | + |
| 61 | + func testSettingPropertyProtocolInheritence() { |
| 62 | + struct ImplementedProtocol: SomeProtocol, Equatable { |
| 63 | + var value: Int = 1 |
| 64 | + } |
| 65 | + |
| 66 | + struct AnObject { |
| 67 | + @SettablePropertySpy(ImplementedProtocol()) |
| 68 | + var value: SomeProtocol |
| 69 | + } |
| 70 | + |
| 71 | + var object = AnObject() |
| 72 | + object.value = ImplementedProtocol(value: 2) |
| 73 | + |
| 74 | + expect(object.$value.getter).toNot(beCalled()) |
| 75 | + expect(object.$value.setter).to(beCalled(satisfyAllOf( |
| 76 | + beAKindOf(ImplementedProtocol.self), |
| 77 | + map(\.value, equal(2)) |
| 78 | + ))) |
| 79 | + |
| 80 | + // the returned value should now be updated with the new value |
| 81 | + expect(object.value).to(satisfyAllOf( |
| 82 | + beAKindOf(ImplementedProtocol.self), |
| 83 | + map(\.value, equal(2)) |
| 84 | + )) |
| 85 | + // and because we called the getter, the getter spy should be called. |
| 86 | + expect(object.$value.getter).to(beCalled(times: 1)) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +final class PropertySpyTests: XCTestCase { |
| 91 | + func testGettingPropertyWhenTypesMatch() { |
| 92 | + struct AnObject { |
| 93 | + @PropertySpy(1) |
| 94 | + var value: Int |
| 95 | + } |
| 96 | + |
| 97 | + let object = AnObject() |
| 98 | + |
| 99 | + expect(object.value).to(equal(1)) |
| 100 | + // because we called it, we should expect for the getter spy to be called |
| 101 | + expect(object.$value).to(beCalled()) |
| 102 | + } |
| 103 | + |
| 104 | + func testGettingPropertyProtocolInheritence() { |
| 105 | + struct ImplementedProtocol: SomeProtocol { |
| 106 | + var value: Int = 1 |
| 107 | + } |
| 108 | + |
| 109 | + struct ObjectUsingProtocol { |
| 110 | + @PropertySpy(ImplementedProtocol(value: 2)) |
| 111 | + var value: SomeProtocol |
| 112 | + } |
| 113 | + |
| 114 | + struct ObjectUsingDirectInstance { |
| 115 | + @PropertySpy(ImplementedProtocol(value: 2), as: { $0 }) |
| 116 | + var value: SomeProtocol |
| 117 | + } |
| 118 | + |
| 119 | + let object = ObjectUsingProtocol() |
| 120 | + |
| 121 | + expect(object.value).to(beAnInstanceOf(ImplementedProtocol.self)) |
| 122 | + // because we called it, we should expect for the getter spy to be called |
| 123 | + expect(object.$value).to(beCalled()) |
| 124 | + expect(object.$value).to(beAnInstanceOf(Spy<Void, SomeProtocol>.self)) |
| 125 | + |
| 126 | + let otherObject = ObjectUsingDirectInstance() |
| 127 | + |
| 128 | + expect(otherObject.$value).to(beAnInstanceOf(Spy<Void, ImplementedProtocol>.self)) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +protocol SomeProtocol { |
| 133 | + var value: Int { get } |
| 134 | +} |
0 commit comments