|
| 1 | +import XCTest |
| 2 | +import Foundation |
| 3 | + |
| 4 | +@testable import SwiftMockGenerator |
| 5 | + |
| 6 | +final class DummyGeneratorCoverageTests: XCTestCase { |
| 7 | + |
| 8 | + // MARK: - generateMockDefinition Coverage Tests |
| 9 | + |
| 10 | + func testDummyGenerator_givenProtocol_whenGeneratingMockDefinition_thenRemovesHeader() throws { |
| 11 | + // Given |
| 12 | + let sut = DummyGenerator() |
| 13 | + let protocolElement = ProtocolElement(name: "TestProtocol") |
| 14 | + let annotation = MockAnnotation( |
| 15 | + type: .dummy, |
| 16 | + element: .protocol(protocolElement), |
| 17 | + location: SourceLocation(line: 1, column: 1, file: "test.swift") |
| 18 | + ) |
| 19 | + |
| 20 | + // When |
| 21 | + let result = try sut.generateMockDefinition(for: annotation.element, annotation: annotation) |
| 22 | + |
| 23 | + // Then |
| 24 | + XCTAssertFalse(result.isEmpty) |
| 25 | + XCTAssertFalse(result.contains("// MARK: - Generated Dummy")) |
| 26 | + XCTAssertTrue(result.contains("class TestProtocolDummy")) |
| 27 | + } |
| 28 | + |
| 29 | + // MARK: - Property Generation Coverage Tests |
| 30 | + |
| 31 | + func testDummyGenerator_givenStaticProperty_whenGenerating_thenIncludesStaticModifier() throws { |
| 32 | + // Given |
| 33 | + let sut = DummyGenerator() |
| 34 | + let property = PropertyElement( |
| 35 | + name: "staticProp", |
| 36 | + type: "Bool", |
| 37 | + isStatic: true, |
| 38 | + hasGetter: true, |
| 39 | + hasSetter: true |
| 40 | + ) |
| 41 | + let protocolElement = ProtocolElement( |
| 42 | + name: "TestProtocol", |
| 43 | + properties: [property] |
| 44 | + ) |
| 45 | + let annotation = MockAnnotation( |
| 46 | + type: .dummy, |
| 47 | + element: .protocol(protocolElement), |
| 48 | + location: SourceLocation(line: 1, column: 1, file: "test.swift") |
| 49 | + ) |
| 50 | + |
| 51 | + // When |
| 52 | + let result = try sut.generateMock(for: annotation.element, annotation: annotation) |
| 53 | + |
| 54 | + // Then |
| 55 | + XCTAssertTrue(result.contains("static")) |
| 56 | + XCTAssertTrue(result.contains("staticProp")) |
| 57 | + } |
| 58 | + |
| 59 | + // MARK: - Method Generation Coverage Tests |
| 60 | + |
| 61 | + func testDummyGenerator_givenMethodWithReturnType_whenGenerating_thenReturnsMinimalValue() throws { |
| 62 | + // Given |
| 63 | + let sut = DummyGenerator() |
| 64 | + let method = MethodElement( |
| 65 | + name: "returningMethod", |
| 66 | + parameters: [], |
| 67 | + returnType: "Int" |
| 68 | + ) |
| 69 | + let protocolElement = ProtocolElement( |
| 70 | + name: "TestProtocol", |
| 71 | + methods: [method] |
| 72 | + ) |
| 73 | + let annotation = MockAnnotation( |
| 74 | + type: .dummy, |
| 75 | + element: .protocol(protocolElement), |
| 76 | + location: SourceLocation(line: 1, column: 1, file: "test.swift") |
| 77 | + ) |
| 78 | + |
| 79 | + // When |
| 80 | + let result = try sut.generateMock(for: annotation.element, annotation: annotation) |
| 81 | + |
| 82 | + // Then |
| 83 | + XCTAssertTrue(result.contains("return")) |
| 84 | + XCTAssertTrue(result.contains("returningMethod")) |
| 85 | + } |
| 86 | + |
| 87 | + // MARK: - Initializer Generation Coverage Tests |
| 88 | + |
| 89 | + func testDummyGenerator_givenInitializer_whenGenerating_thenIncludesDummyComment() throws { |
| 90 | + // Given |
| 91 | + let sut = DummyGenerator() |
| 92 | + let initializer = InitializerElement( |
| 93 | + parameters: [ParameterElement(internalName: "value", type: "String")] |
| 94 | + ) |
| 95 | + let classElement = ClassElement( |
| 96 | + name: "TestClass", |
| 97 | + initializers: [initializer] |
| 98 | + ) |
| 99 | + let annotation = MockAnnotation( |
| 100 | + type: .dummy, |
| 101 | + element: .class(classElement), |
| 102 | + location: SourceLocation(line: 1, column: 1, file: "test.swift") |
| 103 | + ) |
| 104 | + |
| 105 | + // When |
| 106 | + let result = try sut.generateMock(for: annotation.element, annotation: annotation) |
| 107 | + |
| 108 | + // Then |
| 109 | + XCTAssertTrue(result.contains("Dummy implementation")) |
| 110 | + } |
| 111 | + |
| 112 | + // MARK: - Minimal Value Generation Coverage Tests |
| 113 | + |
| 114 | + func testDummyGenerator_givenOptionalTypes_whenGeneratingMinimalValues_thenReturnsNil() throws { |
| 115 | + // Given |
| 116 | + let sut = DummyGenerator() |
| 117 | + let types = ["String?", "Int?", "Bool?", "User?"] |
| 118 | + |
| 119 | + // When & Then |
| 120 | + for type in types { |
| 121 | + let method = MethodElement( |
| 122 | + name: "get\(type.replacingOccurrences(of: "?", with: ""))", |
| 123 | + returnType: type |
| 124 | + ) |
| 125 | + let protocolElement = ProtocolElement( |
| 126 | + name: "TestProtocol", |
| 127 | + methods: [method] |
| 128 | + ) |
| 129 | + let annotation = MockAnnotation( |
| 130 | + type: .dummy, |
| 131 | + element: .protocol(protocolElement), |
| 132 | + location: SourceLocation(line: 1, column: 1, file: "test.swift") |
| 133 | + ) |
| 134 | + |
| 135 | + let result = try sut.generateMock(for: annotation.element, annotation: annotation) |
| 136 | + XCTAssertTrue(result.contains("nil")) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + func testDummyGenerator_givenGenericTypes_whenGeneratingMinimalValues_thenUsesTypeInitializer() throws { |
| 141 | + // Given |
| 142 | + let sut = DummyGenerator() |
| 143 | + let types = ["Array<Int>", "Dictionary<String, Int>", "Optional<String>"] |
| 144 | + |
| 145 | + // When & Then |
| 146 | + for type in types { |
| 147 | + let method = MethodElement( |
| 148 | + name: "getValue", |
| 149 | + returnType: type |
| 150 | + ) |
| 151 | + let protocolElement = ProtocolElement( |
| 152 | + name: "TestProtocol", |
| 153 | + methods: [method] |
| 154 | + ) |
| 155 | + let annotation = MockAnnotation( |
| 156 | + type: .dummy, |
| 157 | + element: .protocol(protocolElement), |
| 158 | + location: SourceLocation(line: 1, column: 1, file: "test.swift") |
| 159 | + ) |
| 160 | + |
| 161 | + let result = try sut.generateMock(for: annotation.element, annotation: annotation) |
| 162 | + // Should generate some form of default value |
| 163 | + XCTAssertFalse(result.isEmpty) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // MARK: - Class Generation Coverage Tests |
| 168 | + |
| 169 | + func testDummyGenerator_givenClassWithInheritance_whenGenerating_thenIncludesInheritance() throws { |
| 170 | + // Given |
| 171 | + let sut = DummyGenerator() |
| 172 | + let classElement = ClassElement( |
| 173 | + name: "TestClass", |
| 174 | + inheritance: ["ParentClass", "Protocol1"] |
| 175 | + ) |
| 176 | + let annotation = MockAnnotation( |
| 177 | + type: .dummy, |
| 178 | + element: .class(classElement), |
| 179 | + location: SourceLocation(line: 1, column: 1, file: "test.swift") |
| 180 | + ) |
| 181 | + |
| 182 | + // When |
| 183 | + let result = try sut.generateMock(for: annotation.element, annotation: annotation) |
| 184 | + |
| 185 | + // Then |
| 186 | + XCTAssertTrue(result.contains("TestClass")) |
| 187 | + XCTAssertTrue(result.contains("ParentClass") || result.contains("Protocol1")) |
| 188 | + } |
| 189 | + |
| 190 | + // MARK: - Function Generation Coverage Tests |
| 191 | + |
| 192 | + func testDummyGenerator_givenAsyncFunctionWithUseResult_whenGenerating_thenUsesReturnValue() throws { |
| 193 | + // Given |
| 194 | + let sut = DummyGenerator() |
| 195 | + let function = FunctionElement( |
| 196 | + name: "asyncFunction", |
| 197 | + parameters: [], |
| 198 | + returnType: "String", |
| 199 | + isAsync: true, |
| 200 | + isThrowing: true |
| 201 | + ) |
| 202 | + let annotation = MockAnnotation( |
| 203 | + type: .dummy, |
| 204 | + element: .function(function), |
| 205 | + location: SourceLocation(line: 1, column: 1, file: "test.swift") |
| 206 | + ) |
| 207 | + |
| 208 | + // When |
| 209 | + let result = try sut.generateMock(for: annotation.element, annotation: annotation, useResult: true) |
| 210 | + |
| 211 | + // Then |
| 212 | + // For functions, DummyGenerator uses returnValue.get() but doesn't generate the property |
| 213 | + // This test verifies the function body uses returnValue |
| 214 | + XCTAssertTrue(result.contains("returnValue") || result.contains("return")) |
| 215 | + XCTAssertTrue(result.contains("asyncFunction")) |
| 216 | + } |
| 217 | + |
| 218 | + // MARK: - Return Value Property Coverage Tests |
| 219 | + |
| 220 | + func testDummyGenerator_givenVoidMethodWithUseResult_whenGenerating_thenDoesNotCreateReturnValue() throws { |
| 221 | + // Given |
| 222 | + let sut = DummyGenerator() |
| 223 | + let method = MethodElement( |
| 224 | + name: "voidMethod", |
| 225 | + returnType: nil, |
| 226 | + isAsync: true |
| 227 | + ) |
| 228 | + let protocolElement = ProtocolElement( |
| 229 | + name: "TestProtocol", |
| 230 | + methods: [method] |
| 231 | + ) |
| 232 | + let annotation = MockAnnotation( |
| 233 | + type: .dummy, |
| 234 | + element: .protocol(protocolElement), |
| 235 | + location: SourceLocation(line: 1, column: 1, file: "test.swift") |
| 236 | + ) |
| 237 | + |
| 238 | + // When |
| 239 | + let result = try sut.generateMock(for: annotation.element, annotation: annotation, useResult: true) |
| 240 | + |
| 241 | + // Then |
| 242 | + XCTAssertFalse(result.contains("voidMethodReturnValue")) |
| 243 | + } |
| 244 | + |
| 245 | + // MARK: - Access Level Coverage Tests |
| 246 | + |
| 247 | + func testDummyGenerator_givenDifferentAccessLevels_whenGenerating_thenRespectsAccessLevels() throws { |
| 248 | + // Given |
| 249 | + let sut = DummyGenerator() |
| 250 | + let accessLevels: [AccessLevel] = [.private, .fileprivate, .internal, .public, .open] |
| 251 | + |
| 252 | + // When & Then |
| 253 | + for accessLevel in accessLevels { |
| 254 | + let protocolElement = ProtocolElement( |
| 255 | + name: "\(accessLevel.rawValue)Protocol", |
| 256 | + accessLevel: accessLevel |
| 257 | + ) |
| 258 | + let annotation = MockAnnotation( |
| 259 | + type: .dummy, |
| 260 | + element: .protocol(protocolElement), |
| 261 | + location: SourceLocation(line: 1, column: 1, file: "test.swift") |
| 262 | + ) |
| 263 | + |
| 264 | + let result = try sut.generateMock(for: annotation.element, annotation: annotation, useResult: false) |
| 265 | + |
| 266 | + if accessLevel != .internal { |
| 267 | + XCTAssertTrue(result.contains(accessLevel.rawValue), "Failed for \(accessLevel)") |
| 268 | + } |
| 269 | + } |
| 270 | + } |
| 271 | +} |
| 272 | + |
0 commit comments