Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kryštof Matěj committed Sep 26, 2023
2 parents 87c6c33 + 2a28edd commit 414b2cf
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Application/RubiconApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Mac Developer";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 43;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=macosx*]" = 473264X5JK;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -505,7 +505,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 3.0.0;
MARKETING_VERSION = 3.1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.cleverlance.rubicon;
PRODUCT_NAME = "Rubicon for Xcode";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -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 = 43;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=macosx*]" = 473264X5JK;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -539,7 +539,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 3.0.0;
MARKETING_VERSION = 3.1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.cleverlance.rubicon;
PRODUCT_NAME = "Rubicon for Xcode";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
12 changes: 11 additions & 1 deletion Sources/Rubicon/Generator/ProtocolGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ final class ProtocolGeneratorImpl: ProtocolGenerator {

func makeProtocol(from declaration: ProtocolDeclaration, stub: String, content: [String]) -> [String] {
let content = content.map(indentationGenerator.indenting)
let parentClause = makeParentClause(from: declaration, stub: stub)
let header = declaration.name + stub + parentClause + declaration.name
return [
"\(accessLevelGenerator.makeClassAccessLevel())final class \(declaration.name)\(stub): \(declaration.name) {"
"\(accessLevelGenerator.makeClassAccessLevel())final class \(header) {"
] + content + [
"}",
]
}

private func makeParentClause(from declaration: ProtocolDeclaration, stub: String) -> String {
if let parent = declaration.parents.first, declaration.parents.count == 1 {
return ": \(parent)\(stub), "
} else {
return ": "
}
}
}
10 changes: 9 additions & 1 deletion Sources/Rubicon/Syntactic analysis/ProtocolParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,21 @@ private class ProtocolVisitor: SyntaxVisitor {
result.append(
ProtocolDeclaration(
name: node.name.text,
parents: [],
parents: node.inheritanceClause?.inheritedTypes.compactMap(parseParent) ?? [],
variables: varsVisitor.execute(node: node),
functions: functionsVisitor.execute(node: node)
)
)
return .visitChildren
}

private func parseParent(from inheridedTypeSyntax: InheritedTypeSyntax) -> String? {
guard let type = inheridedTypeSyntax.type.as(IdentifierTypeSyntax.self) else {
return nil
}

return type.name.text
}
}

private class FunctionsVisitor: SyntaxVisitor {
Expand Down
3 changes: 2 additions & 1 deletion Tests/RubiconTests/Generator/DummyGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ extension TypeDeclaration {

extension ProtocolDeclaration {
static func makeStub(
parents: [String] = [],
variables: [VarDeclaration] = [],
functions: [FunctionDeclaration] = []
) -> ProtocolDeclaration {
return ProtocolDeclaration(
name: "Name",
parents: [],
parents: parents,
variables: variables,
functions: functions
)
Expand Down
22 changes: 22 additions & 0 deletions Tests/RubiconTests/Generator/ProtocolGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ final class ProtocolGeneratorTests: XCTestCase {
])
XCTAssertEqual(accessLevelGeneratorSpy.makeClassAccessLevelCount, 1)
}

func test_givenProtocolSingleParent_whenGenerate_thenGenerateCode() {
let code = sut.makeProtocol(from: .makeStub(parents: ["Parent"]), stub: "Dummy", content: ["content"])

equal(code, rows: [
"accessLevel final class NameDummy: ParentDummy, Name {",
"-content",
"}",
])
XCTAssertEqual(accessLevelGeneratorSpy.makeClassAccessLevelCount, 1)
}

func test_givenProtocolMultipleParent_whenGenerate_thenGenerateCode() {
let code = sut.makeProtocol(from: .makeStub(parents: ["Parent1", "Parent2"]), stub: "Dummy", content: ["content"])

equal(code, rows: [
"accessLevel final class NameDummy: Name {",
"-content",
"}",
])
XCTAssertEqual(accessLevelGeneratorSpy.makeClassAccessLevelCount, 1)
}
}

func equal(string: String?, rows: [String], line: UInt = #line, file: StaticString = #file) {
Expand Down
4 changes: 2 additions & 2 deletions Tests/RubiconTests/Integration/SpyIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest
final class SpyIntegrationTests: XCTestCase {
func test_givenProtocol_whenMakeDummy_thenReturnStub() {
let code = """
protocol Car {
protocol Car: Vehicle {
var name: String? { get }
var color: Int { get set }
Expand All @@ -20,7 +20,7 @@ final class SpyIntegrationTests: XCTestCase {
let result = sut.makeSpy(code: code, accessLevel: .internal, indentStep: "-")

equal(string: result.first ?? "", rows: [
"final class CarSpy: Car {",
"final class CarSpy: VehicleSpy, Car {",
"-struct Load {",
"--let stuff: Int",
"--let label: String",
Expand Down
24 changes: 24 additions & 0 deletions Tests/RubiconTests/Syntactic analysis/ProtocolParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ final class ProtocolParserTests: XCTestCase {
XCTAssertEqual(varParserSpy.parse.count, 2)
XCTAssertEqual(varParserSpy.parse.first?.node.description, "\n var a: Int { get set }")
}

func test_givenProtocolWithInheritance_whenParse_thenReturnProtocol() throws {
let text = """
protocol A: B {
}
"""

let protocols = try sut.parse(text: text)

XCTAssertEqual(protocols.first?.name, "A")
XCTAssertEqual(protocols.first?.parents, ["B"])
}

func test_givenProtocolWithMultipleInheritance_whenParse_thenReturnProtocol() throws {
let text = """
protocol A: B, C {
}
"""

let protocols = try sut.parse(text: text)

XCTAssertEqual(protocols.first?.name, "A")
XCTAssertEqual(protocols.first?.parents, ["B", "C"])
}
}

final class FunctionDeclarationParserSpy: FunctionDeclarationParser {
Expand Down

0 comments on commit 414b2cf

Please sign in to comment.