Skip to content

Commit

Permalink
Fix spy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Kryštof Matěj committed Sep 18, 2023
1 parent b622878 commit bd2f660
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 33 deletions.
8 changes: 4 additions & 4 deletions Application/RubiconApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Mac Developer";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 42;
CURRENT_PROJECT_VERSION = 43;
DEVELOPMENT_ASSET_PATHS = ..;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=macosx*]" = 473264X5JK;
Expand Down Expand Up @@ -344,7 +344,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "3rd Party Mac Developer Application";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 42;
CURRENT_PROJECT_VERSION = 43;
DEVELOPMENT_ASSET_PATHS = ..;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=macosx*]" = 473264X5JK;
Expand Down Expand Up @@ -491,7 +491,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Mac Developer";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 42;
CURRENT_PROJECT_VERSION = 43;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=macosx*]" = 473264X5JK;
GENERATE_INFOPLIST_FILE = YES;
Expand Down Expand Up @@ -525,7 +525,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "3rd Party Mac Developer Application";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 42;
CURRENT_PROJECT_VERSION = 43;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=macosx*]" = 473264X5JK;
GENERATE_INFOPLIST_FILE = YES;
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ final class CarSpy: Car {

var name: String?
var color: Int
var loadThrowBlock: () -> Void?

var loadThrowBlock: (() -> Void)?
var loadReturn: Int
var isFullReturn: Bool
var downloadThrowBlock: () -> Void?
var downloadThrowBlock: (() -> Void)?
var downloadReturn: [String]

var goCount = 0
var load = [Load]()
var isFullCount = 0
Expand Down Expand Up @@ -99,10 +99,10 @@ output:
final class CarStub: Car {
var name: String?
var color: Int
var loadThrowBlock: () -> Void?
var loadThrowBlock: (() -> Void)?
var loadReturn: Int
var isFullReturn: Bool
var downloadThrowBlock: () -> Void?
var downloadThrowBlock: (() -> Void)?
var downloadReturn: [String]

init(color: Int, loadReturn: Int, isFullReturn: Bool, downloadReturn: [String]) {
Expand Down
12 changes: 8 additions & 4 deletions Sources/Rubicon/Generator/SpyGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class SpyGenerator {

content.append(makeVariables(from: protocolType).map(variableGenerator.makeCode))
content.append(makeSpyVariables(from: protocolType))
content.append(initGenerator.makeCode(with: makeVariables(from: protocolType)))
content.append(initGenerator.makeCode(with: makeVariables(from: protocolType) + makeReturnVariables(from: protocolType)))

content += protocolType.functions.map {
makeFunction(from: $0, protocolDeclaration: protocolType)
Expand All @@ -51,7 +51,11 @@ final class SpyGenerator {
}

private func makeVariables(from declaration: ProtocolDeclaration) -> [VarDeclaration] {
return declaration.variables.map(makeSpyVariable) + declaration.functions.flatMap { makeFunctionReturnVariable(from: $0, protocolDeclaration: declaration) }
return declaration.variables.map(makeSpyVariable)
}

private func makeReturnVariables(from declaration: ProtocolDeclaration) -> [VarDeclaration] {
return declaration.functions.flatMap { makeFunctionReturnVariable(from: $0, protocolDeclaration: declaration) }
}

private func makeSpyVariable(from declaration: VarDeclaration) -> VarDeclaration {
Expand All @@ -67,7 +71,7 @@ final class SpyGenerator {
var variables = [VarDeclaration]()

if declaration.isThrowing {
let throwBlockType = TypeDeclaration(name: "() -> Void?", isOptional: true, prefix: [.escaping])
let throwBlockType = TypeDeclaration(name: "(() -> Void)?", isOptional: true, prefix: [.escaping])
variables.append(VarDeclaration(isConstant: false, identifier: name + "ThrowBlock", type: throwBlockType))
}

Expand All @@ -79,7 +83,7 @@ final class SpyGenerator {
}

private func makeSpyVariables(from declaration: ProtocolDeclaration) -> [String] {
return declaration.functions.map { makeSpyVariable(from: $0, protocolDeclaration: declaration) }
return makeReturnVariables(from: declaration).map(variableGenerator.makeCode) + declaration.functions.map { makeSpyVariable(from: $0, protocolDeclaration: declaration) }
}

private func makeSpyVariable(from declaration: FunctionDeclaration, protocolDeclaration: ProtocolDeclaration) -> String {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Rubicon/Generator/StubGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class StubGenerator {
var variables = [VarDeclaration]()

if declaration.isThrowing {
let throwBlockType = TypeDeclaration(name: "() -> Void?", isOptional: true, prefix: [.escaping])
let throwBlockType = TypeDeclaration(name: "(() -> Void)?", isOptional: true, prefix: [.escaping])
variables.append(VarDeclaration(isConstant: false, identifier: name + "ThrowBlock", type: throwBlockType))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ class ArgumentDeclarationParserImpl: ArgumentDeclarationParser {
}

let token = node.firstName
switch token.tokenKind {
case .wildcard:
return nil
default:
return token.text
}
return token.text
}

private func makeName(from node: FunctionParameterSyntax) -> String {
Expand Down
5 changes: 1 addition & 4 deletions Tests/RubiconTests/Generator/SpyGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ final class SpyGeneratorTests: XCTestCase {
])
equal(protocolGeneratorSpy.makeProtocol.first?.content, rows: [
"variable",
"",
"var functionNameCount = 0",
"",
"init",
Expand All @@ -141,7 +140,6 @@ final class SpyGeneratorTests: XCTestCase {
])
equal(protocolGeneratorSpy.makeProtocol.first?.content, rows: [
"variable",
"",
"var functionNameCount = 0",
"",
"init",
Expand All @@ -159,7 +157,7 @@ final class SpyGeneratorTests: XCTestCase {

XCTAssertEqual(variableGeneratorSpy.makeCode.count, 2)
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.identifier, "functionNameThrowBlock")
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.type, .makeStub(name: "() -> Void?", isOptional: true, prefix: [.escaping]))
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.type, .makeStub(name: "(() -> Void)?", isOptional: true, prefix: [.escaping]))
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.isConstant, false)
XCTAssertEqual(variableGeneratorSpy.makeCode.last?.declaration.identifier, "functionNameReturn")
XCTAssertEqual(variableGeneratorSpy.makeCode.last?.declaration.type, returnType)
Expand All @@ -174,7 +172,6 @@ final class SpyGeneratorTests: XCTestCase {
equal(protocolGeneratorSpy.makeProtocol.first?.content, rows: [
"variable",
"variable",
"",
"var functionNameCount = 0",
"",
"init",
Expand Down
2 changes: 1 addition & 1 deletion Tests/RubiconTests/Generator/StubGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ final class StubGeneratorTests: XCTestCase {

XCTAssertEqual(variableGeneratorSpy.makeCode.count, 2)
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.identifier, "functionNameThrowBlock")
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.type, .makeStub(name: "() -> Void?", isOptional: true, prefix: [.escaping]))
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.type, .makeStub(name: "(() -> Void)?", isOptional: true, prefix: [.escaping]))
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.isConstant, false)
XCTAssertEqual(variableGeneratorSpy.makeCode.last?.declaration.identifier, "functionNameReturn")
XCTAssertEqual(variableGeneratorSpy.makeCode.last?.declaration.type, returnType)
Expand Down
10 changes: 5 additions & 5 deletions Tests/RubiconTests/Integration/SpyIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class SpyIntegrationTests: XCTestCase {
@MainActor
func go()
func load(with stuff: Int, label: String) throws -> Int
func isFull(validate: @escaping () -> Void) -> Bool
func isFull(_ validate: @escaping () -> Void) -> Bool
func download() async throws -> [String]
}
"""
Expand All @@ -32,12 +32,12 @@ final class SpyIntegrationTests: XCTestCase {
"",
"-var name: String?",
"-var color: Int",
"-var loadThrowBlock: () -> Void?",
"",
"-var loadThrowBlock: (() -> Void)?",
"-var loadReturn: Int",
"-var isFullReturn: Bool",
"-var downloadThrowBlock: () -> Void?",
"-var downloadThrowBlock: (() -> Void)?",
"-var downloadReturn: [String]",
"",
"-var goCount = 0",
"-var load = [Load]()",
"-var isFull = [IsFull]()",
Expand All @@ -61,7 +61,7 @@ final class SpyIntegrationTests: XCTestCase {
"--return loadReturn",
"-}",
"",
"-func isFull(validate: @escaping () -> Void) -> Bool {",
"-func isFull(_ validate: @escaping () -> Void) -> Bool {",
"--let item = IsFull(validate: validate)",
"--isFull.append(item)",
"--return isFullReturn",
Expand Down
4 changes: 2 additions & 2 deletions Tests/RubiconTests/Integration/StubIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ final class StubIntegrationTests: XCTestCase {
"final class CarStub: Car {",
"-var name: String?",
"-var color: Int",
"-var loadThrowBlock: () -> Void?",
"-var loadThrowBlock: (() -> Void)?",
"-var loadReturn: Int",
"-var isFullReturn: Bool",
"-var downloadThrowBlock: () -> Void?",
"-var downloadThrowBlock: (() -> Void)?",
"-var downloadReturn: [String]",
"",
"-init(color: Int, loadReturn: Int, isFullReturn: Bool, downloadReturn: [String]) {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class ArgumentDeclarationParserTests: XCTestCase {

let declaration = sut.parse(node: node)

XCTAssertNil(declaration.label)
XCTAssertEqual(declaration.label, "_")
XCTAssertEqual(declaration.name, "name")
XCTAssertEqual(declaration.type, .makeStub())
XCTAssertEqual(typeDeclarationParserSpy.parse.count, 1)
Expand Down

0 comments on commit bd2f660

Please sign in to comment.