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

Fix stub naming #63

Merged
merged 1 commit into from
Mar 28, 2024
Merged
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/Rubicon/Generator/StubGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ final class StubGenerator {
self.initGenerator = initGenerator
}

func generate(from protocolType: ProtocolDeclaration, stubSuffix: String = "Stub") -> String {
func generate(from protocolType: ProtocolDeclaration, nameSuffix: String) -> String {
let content = generateBody(from: protocolType)
return protocolGenerator.makeProtocol(
from: protocolType,
stub: stubSuffix,
stub: nameSuffix,
content: content
).joined(separator: "\n") + "\n"
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Rubicon/Integration/Rubicon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public final class Rubicon {
)
}

public func makeStub(code: String, accessLevel: AccessLevel, indentStep: String) -> [String] {
public func makeStub(code: String, accessLevel: AccessLevel, indentStep: String, nameSuffix: String = "Stub") -> [String] {
let parser = makeParser()
let generator = makeStubGenerator(accessLevel: accessLevel, indentStep: indentStep)
do {
let protocols = try parser.parse(text: code)
return protocols.map { generator.generate(from: $0) }
return protocols.map { generator.generate(from: $0, nameSuffix: nameSuffix) }
} catch {
return []
}
Expand Down
24 changes: 9 additions & 15 deletions Tests/RubiconTests/Generator/StubGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,21 @@ final class StubGeneratorTests: XCTestCase {
}

func test_givenEmptyProtocol_whenGenerate_thenGenerateCode() {
let result = sut.generate(from: .makeStub())
let result = sut.generate(from: .makeStub(), nameSuffix: "Suffix")

XCTAssertEqual(protocolGeneratorSpy.makeProtocol.count, 1)
XCTAssertEqual(protocolGeneratorSpy.makeProtocol.first?.declaration, .makeStub())
XCTAssertEqual(protocolGeneratorSpy.makeProtocol.first?.stub, "Stub")
XCTAssertEqual(protocolGeneratorSpy.makeProtocol.first?.stub, "Suffix")
equal(protocolGeneratorSpy.makeProtocol.first?.content, rows: [
"init",
])
XCTAssertEqual(result, "result\n")
}

func test_givenEmptyProtocolWithCustomSuffix_whenGenerate_thenGenerateCode() {
let result = sut.generate(from: .makeStub(), stubSuffix: "Mock")

XCTAssertEqual(protocolGeneratorSpy.makeProtocol.first?.stub, "Mock")
}

func test_givenProtocolWithVariable_whenGenerate_thenGenerateStub() {
let protocolDeclaration = ProtocolDeclaration.makeStub(variables: [.makeStub()])

_ = sut.generate(from: protocolDeclaration)
_ = sut.generate(from: protocolDeclaration, nameSuffix: "Stub")

XCTAssertEqual(variableGeneratorSpy.makeCode.count, 1)
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration, .makeStub())
Expand All @@ -63,15 +57,15 @@ final class StubGeneratorTests: XCTestCase {
func test_givenProtocolWithConstant_whenGenerate_thenGenerateStub() {
let protocolDeclaration = ProtocolDeclaration.makeStub(variables: [.makeStub(isConstant: true)])

_ = sut.generate(from: protocolDeclaration)
_ = sut.generate(from: protocolDeclaration, nameSuffix: "Stub")

XCTAssertEqual(initGeneratorSpy.makeCode.first?.variables.first?.isConstant, false)
}

func test_givenProtocolWithVariables_whenGenerate_thenGenerateStub() {
let protocolDeclaration = ProtocolDeclaration.makeStub(variables: [.makeStub(), .makeStub()])

_ = sut.generate(from: protocolDeclaration)
_ = sut.generate(from: protocolDeclaration, nameSuffix: "Stub")

XCTAssertEqual(variableGeneratorSpy.makeCode.count, 2)
equal(protocolGeneratorSpy.makeProtocol.first?.content, rows: [
Expand All @@ -85,7 +79,7 @@ final class StubGeneratorTests: XCTestCase {
func test_givenProtocolWithFunctionWithoutReturn_whenGenerate_thenGenerateStub() {
let protocolDeclaration = ProtocolDeclaration.makeStub(functions: [.makeStub()])

_ = sut.generate(from: protocolDeclaration)
_ = sut.generate(from: protocolDeclaration, nameSuffix: "Stub")

equal(protocolGeneratorSpy.makeProtocol.first?.content, rows: [
"init",
Expand All @@ -100,7 +94,7 @@ final class StubGeneratorTests: XCTestCase {
func test_givenProtocolWithFunctionWithtReturn_whenGenerate_thenGenerateStub() {
let protocolDeclaration = ProtocolDeclaration.makeStub(functions: [.makeStub(returnType: .makeStub())])

_ = sut.generate(from: protocolDeclaration)
_ = sut.generate(from: protocolDeclaration, nameSuffix: "Stub")

XCTAssertEqual(variableGeneratorSpy.makeCode.count, 1)
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.identifier, "functionNameReturn")
Expand All @@ -124,7 +118,7 @@ final class StubGeneratorTests: XCTestCase {
let returnType = TypeDeclaration.makeStub(isOptional: true)
let protocolDeclaration = ProtocolDeclaration.makeStub(functions: [.makeStub(returnType: returnType)])

_ = sut.generate(from: protocolDeclaration)
_ = sut.generate(from: protocolDeclaration, nameSuffix: "Stub")

XCTAssertEqual(variableGeneratorSpy.makeCode.count, 1)
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.identifier, "functionNameReturn")
Expand All @@ -147,7 +141,7 @@ final class StubGeneratorTests: XCTestCase {
let functionDeclaration = FunctionDeclaration.makeStub(isThrowing: true, returnType: returnType)
let protocolDeclaration = ProtocolDeclaration.makeStub(functions: [functionDeclaration])

_ = sut.generate(from: protocolDeclaration)
_ = sut.generate(from: protocolDeclaration, nameSuffix: "Stub")

XCTAssertEqual(variableGeneratorSpy.makeCode.count, 2)
XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.identifier, "functionNameThrowBlock")
Expand Down
Loading