From 613121657fcace79a21399c3a52585faaffaa24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kry=C5=A1tof=20Mat=C4=9Bj?= Date: Thu, 16 May 2024 15:25:09 +0200 Subject: [PATCH] Add default values to array, dictionary and set --- Sources/Rubicon/Domain/TypeDeclaration.swift | 10 +++- .../Generator/DefaultValueGenerator.swift | 13 ++++- Sources/Rubicon/Generator/InitGenerator.swift | 10 ++-- Sources/Rubicon/Generator/SpyGenerator.swift | 8 ++- .../Generator/StructStubGenerator.swift | 2 +- Sources/Rubicon/Generator/StubGenerator.swift | 8 ++- .../TypeDeclarationParser.swift | 44 +++++++++++++++-- .../DefaultValueGeneratorTests.swift | 20 +++++++- .../Generator/DummyGeneratorTests.swift | 10 ++-- .../Generator/InitGeneratorTests.swift | 8 +-- .../Generator/SpyGeneratorTests.swift | 10 ++-- .../Generator/StubGeneratorTests.swift | 10 ++-- .../Generator/TypeGeneratorTests.swift | 4 +- .../StructStubIntegrationTests.swift | 9 ++-- .../TypeDeclarationParserTests.swift | 49 ++++++++++++++----- 15 files changed, 165 insertions(+), 50 deletions(-) diff --git a/Sources/Rubicon/Domain/TypeDeclaration.swift b/Sources/Rubicon/Domain/TypeDeclaration.swift index 0c1857c..a789e52 100644 --- a/Sources/Rubicon/Domain/TypeDeclaration.swift +++ b/Sources/Rubicon/Domain/TypeDeclaration.swift @@ -13,7 +13,15 @@ struct TypeDeclaration: Equatable { case autoclosure = "@autoclosure" } + enum ComposedType { + case plain + case array + case dictionary + case optional + case set + } + var name: String - var isOptional: Bool let prefix: [Prefix] + let composedType: ComposedType } diff --git a/Sources/Rubicon/Generator/DefaultValueGenerator.swift b/Sources/Rubicon/Generator/DefaultValueGenerator.swift index 032d1c5..5f137c2 100644 --- a/Sources/Rubicon/Generator/DefaultValueGenerator.swift +++ b/Sources/Rubicon/Generator/DefaultValueGenerator.swift @@ -18,10 +18,21 @@ final class DefaultValueGeneratorImpl: DefaultValueGenerator { return customValue } - guard !varDeclaration.type.isOptional else { + switch varDeclaration.type.composedType { + case .plain: + return makeDefaultValueByType(varDeclaration: varDeclaration) + case .array: + return "[]" + case .dictionary: + return "[:]" + case .optional: return "nil" + case .set: + return "[]" } + } + private func makeDefaultValueByType(varDeclaration: VarDeclaration) -> String { switch varDeclaration.type.name { case "String": return "\"\(varDeclaration.identifier)\"" diff --git a/Sources/Rubicon/Generator/InitGenerator.swift b/Sources/Rubicon/Generator/InitGenerator.swift index 3fc4973..047ed6d 100644 --- a/Sources/Rubicon/Generator/InitGenerator.swift +++ b/Sources/Rubicon/Generator/InitGenerator.swift @@ -44,9 +44,13 @@ final class InitGeneratorImpl: InitGenerator { } private func makeInitArgument(from variable: VarDeclaration, isAddingDefaultValueToOptionalsEnabled: Bool) -> String { - let defaultValue = isAddingDefaultValueToOptionalsEnabled && variable.type.isOptional ? "nil" : nil - let prefix = variable.type.isOptional ? [] : variable.type.prefix - let type = TypeDeclaration(name: variable.type.name, isOptional: variable.type.isOptional, prefix: prefix) + let defaultValue = isAddingDefaultValueToOptionalsEnabled && variable.type.composedType == .optional ? "nil" : nil + let prefix = variable.type.composedType == .optional ? [] : variable.type.prefix + let type = TypeDeclaration( + name: variable.type.name, + prefix: prefix, + composedType: variable.type.composedType + ) let declaration = ArgumentDeclaration(name: variable.identifier, type: type, defaultValue: defaultValue) return argumentGenerator.makeCode(from: declaration) } diff --git a/Sources/Rubicon/Generator/SpyGenerator.swift b/Sources/Rubicon/Generator/SpyGenerator.swift index 3baf8a9..10622de 100644 --- a/Sources/Rubicon/Generator/SpyGenerator.swift +++ b/Sources/Rubicon/Generator/SpyGenerator.swift @@ -46,7 +46,7 @@ final class SpyGenerator { content.append(makeVariables(from: protocolType).map(variableGenerator.makeCode)) content.append(makeSpyVariables(from: protocolType)) let initVariables = makeVariables(from: protocolType) + makeReturnVariables(from: protocolType) - let nonOptionalInitVariables = initVariables.filter { !$0.type.isOptional } + let nonOptionalInitVariables = initVariables.filter { $0.type.composedType != .optional } content.append(initGenerator.makeCode( with: isInitWithOptionalsEnabled ? initVariables : nonOptionalInitVariables, isAddingDefaultValueToOptionalsEnabled: isInitWithOptionalsEnabled @@ -81,7 +81,11 @@ final class SpyGenerator { var variables = [VarDeclaration]() if declaration.isThrowing { - let throwBlockType = TypeDeclaration(name: "(() throws -> Void)?", isOptional: true, prefix: [.escaping]) + let throwBlockType = TypeDeclaration( + name: "(() throws -> Void)?", + prefix: [.escaping], + composedType: .optional + ) variables.append(VarDeclaration(isConstant: false, identifier: name + "ThrowBlock", type: throwBlockType)) } diff --git a/Sources/Rubicon/Generator/StructStubGenerator.swift b/Sources/Rubicon/Generator/StructStubGenerator.swift index d4dcacc..9c6fbc9 100644 --- a/Sources/Rubicon/Generator/StructStubGenerator.swift +++ b/Sources/Rubicon/Generator/StructStubGenerator.swift @@ -36,7 +36,7 @@ final class StructStubGeneratorImpl: StructStubGenerator { isThrowing: false, isAsync: false, isStatic: true, - returnType: TypeDeclaration(name: structType.name, isOptional: false, prefix: []) + returnType: TypeDeclaration(name: structType.name, prefix: [], composedType: .plain) ) return functionGenerator.makeCode( from: functionDeclaration, diff --git a/Sources/Rubicon/Generator/StubGenerator.swift b/Sources/Rubicon/Generator/StubGenerator.swift index 28a1366..bbd9db3 100644 --- a/Sources/Rubicon/Generator/StubGenerator.swift +++ b/Sources/Rubicon/Generator/StubGenerator.swift @@ -35,7 +35,7 @@ final class StubGenerator { content.append(makeVariables(from: protocolType).map(variableGenerator.makeCode)) let initVariables = makeVariables(from: protocolType) - let nonOptionalInitVariables = initVariables.filter { !$0.type.isOptional } + let nonOptionalInitVariables = initVariables.filter { $0.type.composedType != .optional } content.append(initGenerator.makeCode( with: isInitWithOptionalsEnabled ? initVariables : nonOptionalInitVariables, isAddingDefaultValueToOptionalsEnabled: isInitWithOptionalsEnabled @@ -66,7 +66,11 @@ final class StubGenerator { var variables = [VarDeclaration]() if declaration.isThrowing { - let throwBlockType = TypeDeclaration(name: "(() throws -> Void)?", isOptional: true, prefix: [.escaping]) + let throwBlockType = TypeDeclaration( + name: "(() throws -> Void)?", + prefix: [.escaping], + composedType: .optional + ) variables.append(VarDeclaration(isConstant: false, identifier: name + "ThrowBlock", type: throwBlockType)) } diff --git a/Sources/Rubicon/Syntactic analysis/TypeDeclarationParser.swift b/Sources/Rubicon/Syntactic analysis/TypeDeclarationParser.swift index f3bfa32..1ff627e 100644 --- a/Sources/Rubicon/Syntactic analysis/TypeDeclarationParser.swift +++ b/Sources/Rubicon/Syntactic analysis/TypeDeclarationParser.swift @@ -14,25 +14,61 @@ final class TypeDeclarationParserImpl: TypeDeclarationParser { if let attributedType = node.as(AttributedTypeSyntax.self) { return TypeDeclaration( name: attributedType.baseType.description.trimmingCharacters(in: .whitespacesAndNewlines), - isOptional: isOptional(node: node), - prefix: attributedType.attributes.compactMap(makePrefix(from:)) + prefix: attributedType.attributes.compactMap(makePrefix(from:)), + composedType: parseComposedType(from: node) ) } else { return TypeDeclaration( name: node.description.trimmingCharacters(in: .whitespacesAndNewlines), - isOptional: isOptional(node: node), - prefix: [] + prefix: [], + composedType: parseComposedType(from: node) ) } } + private func parseComposedType(from node: TypeSyntax) -> TypeDeclaration.ComposedType { + if isOptional(node: node) { + return .optional + } + + if node.is(DictionaryTypeSyntax.self) { + return .dictionary + } + + if isArray(node: node) { + return .array + } + + if isSet(node: node) { + return .set + } + + return .plain + } + private func isOptional(node: TypeSyntax) -> Bool { let isSimpleOptional = node.as(OptionalTypeSyntax.self) != nil let isClosureOptional = node.as(FunctionTypeSyntax.self)?.returnClause.type.as(OptionalTypeSyntax.self) != nil return isSimpleOptional || isClosureOptional } + private func isArray(node: TypeSyntax) -> Bool { + if let identifierTypeSyntax = node.as(IdentifierTypeSyntax.self) { + return identifierTypeSyntax.name.text == "Array" + } else { + return node.is(ArrayTypeSyntax.self) + } + } + + private func isSet(node: TypeSyntax) -> Bool { + if let identifierTypeSyntax = node.as(IdentifierTypeSyntax.self) { + return identifierTypeSyntax.name.text == "Set" + } else { + return false + } + } + private func makePrefix(from node: AttributeListSyntax.Element) -> TypeDeclaration.Prefix? { switch node { case .attribute(let attributeSyntax): diff --git a/Tests/RubiconTests/Generator/DefaultValueGeneratorTests.swift b/Tests/RubiconTests/Generator/DefaultValueGeneratorTests.swift index edc9e8b..828eda9 100644 --- a/Tests/RubiconTests/Generator/DefaultValueGeneratorTests.swift +++ b/Tests/RubiconTests/Generator/DefaultValueGeneratorTests.swift @@ -22,11 +22,29 @@ final class DefaultValueGeneratorTests: XCTestCase { } func test_givenOptionalType_whenMakeDefaultValue_thenReturnNil() { - let value = sut.makeDefaultValue(for: .makeStub(type: .makeStub(isOptional: true))) + let value = sut.makeDefaultValue(for: .makeStub(type: .makeStub(composedType: .optional))) XCTAssertEqual(value, "nil") } + func test_givenArrayType_whenMakeDefaultValue_thenReturnNil() { + let value = sut.makeDefaultValue(for: .makeStub(type: .makeStub(composedType: .array))) + + XCTAssertEqual(value, "[]") + } + + func test_givenDictionaryType_whenMakeDefaultValue_thenReturnNil() { + let value = sut.makeDefaultValue(for: .makeStub(type: .makeStub(composedType: .dictionary))) + + XCTAssertEqual(value, "[:]") + } + + func test_givenSetType_whenMakeDefaultValue_thenReturnNil() { + let value = sut.makeDefaultValue(for: .makeStub(type: .makeStub(composedType: .set))) + + XCTAssertEqual(value, "[]") + } + func test_givenIntType_whenMakeDefaultValue_thenReturnZero() { let value = sut.makeDefaultValue(for: .makeStub(identifier: "name", type: .makeStub(name: "Int"))) diff --git a/Tests/RubiconTests/Generator/DummyGeneratorTests.swift b/Tests/RubiconTests/Generator/DummyGeneratorTests.swift index 03f4226..0e2ec77 100644 --- a/Tests/RubiconTests/Generator/DummyGeneratorTests.swift +++ b/Tests/RubiconTests/Generator/DummyGeneratorTests.swift @@ -7,7 +7,7 @@ final class DummyGeneratorTests: XCTestCase { private var functionGeneratorSpy: FunctionGeneratorSpy! private var initGeneratorSpy: InitGeneratorSpy! private var sut: DummyGenerator! - private let type = TypeDeclaration.makeStub(name: "Color", isOptional: false) + private let type = TypeDeclaration.makeStub(name: "Color") override func setUp() { super.setUp() @@ -84,13 +84,13 @@ final class DummyGeneratorTests: XCTestCase { extension TypeDeclaration { static func makeStub( name: String = "Int", - isOptional: Bool = false, - prefix: [Prefix] = [] + prefix: [Prefix] = [], + composedType: ComposedType = .plain ) -> TypeDeclaration { return TypeDeclaration( name: name, - isOptional: isOptional, - prefix: prefix + prefix: prefix, + composedType: composedType ) } } diff --git a/Tests/RubiconTests/Generator/InitGeneratorTests.swift b/Tests/RubiconTests/Generator/InitGeneratorTests.swift index d0a43f5..edf7b67 100644 --- a/Tests/RubiconTests/Generator/InitGeneratorTests.swift +++ b/Tests/RubiconTests/Generator/InitGeneratorTests.swift @@ -6,7 +6,7 @@ final class InitGeneratorTests: XCTestCase { private var accessLevelGeneratorSpy: AccessLevelGeneratorSpy! private var indentationGeneratorStub: IndentationGeneratorStub! private var sut: InitGenerator! - private let type = TypeDeclaration.makeStub(name: "Color", isOptional: false) + private let type = TypeDeclaration.makeStub(name: "Color") override func setUp() { super.setUp() @@ -64,7 +64,7 @@ final class InitGeneratorTests: XCTestCase { let result = sut.makeCode( with: [ .makeStub(type: .makeStub(prefix: [.escaping])), - .makeStub(type: .makeStub(isOptional: true, prefix: [.escaping])) + .makeStub(type: .makeStub(prefix: [.escaping], composedType: .optional)) ], isAddingDefaultValueToOptionalsEnabled: false ) @@ -77,14 +77,14 @@ final class InitGeneratorTests: XCTestCase { ]) XCTAssertEqual(argumentGeneratorSpy.makeCode.count, 2) XCTAssertEqual(argumentGeneratorSpy.makeCode.first?.declaration, .makeStub(label: nil, name: "identifier", type: .makeStub(prefix: [.escaping]))) - XCTAssertEqual(argumentGeneratorSpy.makeCode.last?.declaration, .makeStub(label: nil, name: "identifier", type: .makeStub(isOptional: true, prefix: []))) + XCTAssertEqual(argumentGeneratorSpy.makeCode.last?.declaration, .makeStub(label: nil, name: "identifier", type: .makeStub(prefix: [], composedType: .optional))) } func test_givenOptionalVariableAndIsAddingDefaultValueToOptionalsEnabled_whenMakeCode_thenMakeInit() { initialize(accessLevel: .public) let result = sut.makeCode( - with: [.makeStub(), .makeStub(type: .makeStub(isOptional: true))], + with: [.makeStub(), .makeStub(type: .makeStub(composedType: .optional))], isAddingDefaultValueToOptionalsEnabled: true ) diff --git a/Tests/RubiconTests/Generator/SpyGeneratorTests.swift b/Tests/RubiconTests/Generator/SpyGeneratorTests.swift index c932ff0..e3268a5 100644 --- a/Tests/RubiconTests/Generator/SpyGeneratorTests.swift +++ b/Tests/RubiconTests/Generator/SpyGeneratorTests.swift @@ -10,7 +10,7 @@ final class SpyGeneratorTests: XCTestCase { private var structGeneratorSpy: StructGeneratorSpy! private var accessLevelGeneratorSpy: AccessLevelGeneratorSpy! private var sut: SpyGenerator! - private let type = TypeDeclaration.makeStub(name: "Color", isOptional: false) + private let type = TypeDeclaration.makeStub(name: "Color") override func setUp() { super.setUp() @@ -127,7 +127,7 @@ final class SpyGeneratorTests: XCTestCase { } func test_givenProtocolWithFunctionWithtOptionalReturn_whenGenerate_thenGenerateSpy() { - let returnType = TypeDeclaration.makeStub(isOptional: true) + let returnType = TypeDeclaration.makeStub(composedType: .optional) let protocolDeclaration = ProtocolDeclaration.makeStub(functions: [.makeStub(returnType: returnType)]) _ = sut.generate(from: protocolDeclaration, isInitWithOptionalsEnabled: false) @@ -153,7 +153,7 @@ final class SpyGeneratorTests: XCTestCase { } func test_givenProtocolWithThrowingFunction_whenGenerate_thenGenerateSpy() { - let returnType = TypeDeclaration.makeStub(isOptional: true) + let returnType = TypeDeclaration.makeStub(composedType: .optional) let functionDeclaration = FunctionDeclaration.makeStub(isThrowing: true, returnType: returnType) let protocolDeclaration = ProtocolDeclaration.makeStub(functions: [functionDeclaration]) @@ -161,7 +161,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: "(() throws -> Void)?", isOptional: true, prefix: [.escaping])) + XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.type, .makeStub(name: "(() throws -> Void)?", prefix: [.escaping], composedType: .optional)) XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.isConstant, false) XCTAssertEqual(variableGeneratorSpy.makeCode.last?.declaration.identifier, "functionNameReturn") XCTAssertEqual(variableGeneratorSpy.makeCode.last?.declaration.type, returnType) @@ -219,7 +219,7 @@ final class SpyGeneratorTests: XCTestCase { let protocolDeclaration = ProtocolDeclaration.makeStub( variables: [ .makeStub(), - .makeStub(type: .makeStub(isOptional: true)) + .makeStub(type: .makeStub(composedType: .optional)) ] ) diff --git a/Tests/RubiconTests/Generator/StubGeneratorTests.swift b/Tests/RubiconTests/Generator/StubGeneratorTests.swift index bfdbf4d..9bedc5a 100644 --- a/Tests/RubiconTests/Generator/StubGeneratorTests.swift +++ b/Tests/RubiconTests/Generator/StubGeneratorTests.swift @@ -8,7 +8,7 @@ final class StubGeneratorTests: XCTestCase { private var functionNameGeneratorSpy: FunctionNameGeneratorSpy! private var initGeneratorSpy: InitGeneratorSpy! private var sut: StubGenerator! - private let type = TypeDeclaration.makeStub(name: "Color", isOptional: false) + private let type = TypeDeclaration.makeStub(name: "Color") override func setUp() { super.setUp() @@ -116,7 +116,7 @@ final class StubGeneratorTests: XCTestCase { } func test_givenProtocolWithFunctionWithtOptionalReturn_whenGenerate_thenGenerateStub() { - let returnType = TypeDeclaration.makeStub(isOptional: true) + let returnType = TypeDeclaration.makeStub(composedType: .optional) let protocolDeclaration = ProtocolDeclaration.makeStub(functions: [.makeStub(returnType: returnType)]) _ = sut.generate(from: protocolDeclaration, nameSuffix: "Stub", isInitWithOptionalsEnabled: false) @@ -138,7 +138,7 @@ final class StubGeneratorTests: XCTestCase { } func test_givenProtocolWithThrowingFunction_whenGenerate_thenGenerateStub() { - let returnType = TypeDeclaration.makeStub(isOptional: true) + let returnType = TypeDeclaration.makeStub(composedType: .optional) let functionDeclaration = FunctionDeclaration.makeStub(isThrowing: true, returnType: returnType) let protocolDeclaration = ProtocolDeclaration.makeStub(functions: [functionDeclaration]) @@ -146,7 +146,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: "(() throws -> Void)?", isOptional: true, prefix: [.escaping])) + XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.type, .makeStub(name: "(() throws -> Void)?", prefix: [.escaping], composedType: .optional)) XCTAssertEqual(variableGeneratorSpy.makeCode.first?.declaration.isConstant, false) XCTAssertEqual(variableGeneratorSpy.makeCode.last?.declaration.identifier, "functionNameReturn") XCTAssertEqual(variableGeneratorSpy.makeCode.last?.declaration.type, returnType) @@ -171,7 +171,7 @@ final class StubGeneratorTests: XCTestCase { let protocolDeclaration = ProtocolDeclaration.makeStub( variables: [ .makeStub(), - .makeStub(type: .makeStub(isOptional: true)) + .makeStub(type: .makeStub(composedType: .optional)) ] ) diff --git a/Tests/RubiconTests/Generator/TypeGeneratorTests.swift b/Tests/RubiconTests/Generator/TypeGeneratorTests.swift index 0576a87..b90975b 100644 --- a/Tests/RubiconTests/Generator/TypeGeneratorTests.swift +++ b/Tests/RubiconTests/Generator/TypeGeneratorTests.swift @@ -26,7 +26,7 @@ final class TypGeneratorTests: XCTestCase { } func test_givenOptional_whenMakeVariable_thenGenerateCode() { - let typeDeclaration = TypeDeclaration.makeStub(name: "() -> Void?", isOptional: true) + let typeDeclaration = TypeDeclaration.makeStub(name: "() -> Void?", composedType: .optional) let code = sut.makeVariableCode(from: typeDeclaration) @@ -50,7 +50,7 @@ final class TypGeneratorTests: XCTestCase { } func test_givenOptional_whenMakeArgument_thenGenerateCode() { - let typeDeclaration = TypeDeclaration.makeStub(name: "() -> Void?", isOptional: true) + let typeDeclaration = TypeDeclaration.makeStub(name: "() -> Void?", composedType: .optional) let code = sut.makeArgumentCode(from: typeDeclaration) diff --git a/Tests/RubiconTests/Integration/StructStubIntegrationTests.swift b/Tests/RubiconTests/Integration/StructStubIntegrationTests.swift index e3851e4..bb5ea23 100644 --- a/Tests/RubiconTests/Integration/StructStubIntegrationTests.swift +++ b/Tests/RubiconTests/Integration/StructStubIntegrationTests.swift @@ -10,6 +10,7 @@ final class StructStubIntegrationTests: XCTestCase { let isRed: Bool let seats: [Seat] let driver: Driver + let trunk: Trunk """ let sut = Rubicon() @@ -22,15 +23,17 @@ final class StructStubIntegrationTests: XCTestCase { "--lenght: Int = 0,", "--name: String = \"name\",", "--isRed: Bool = false,", - "--seats: [Seat] = .makeStub(),", - "--driver: Driver = Carl", + "--seats: [Seat] = [],", + "--driver: Driver = Carl,", + "--trunk: Trunk = .makeStub()", "-) -> Car {", "--return Car(", "---lenght: lenght,", "---name: name,", "---isRed: isRed,", "---seats: seats,", - "---driver: driver", + "---driver: driver,", + "---trunk: trunk", "--)", "-}", "}", diff --git a/Tests/RubiconTests/Syntactic analysis/TypeDeclarationParserTests.swift b/Tests/RubiconTests/Syntactic analysis/TypeDeclarationParserTests.swift index 6ea9cc3..8b4b5fe 100644 --- a/Tests/RubiconTests/Syntactic analysis/TypeDeclarationParserTests.swift +++ b/Tests/RubiconTests/Syntactic analysis/TypeDeclarationParserTests.swift @@ -22,7 +22,7 @@ final class TypeDeclarationParserTests: XCTestCase { let declaration = sut.parse(node: node) XCTAssertEqual(declaration.name, "T") - XCTAssertEqual(declaration.isOptional, false) + XCTAssertEqual(declaration.composedType, .plain) XCTAssertEqual(declaration.prefix, []) } @@ -32,7 +32,7 @@ final class TypeDeclarationParserTests: XCTestCase { let declaration = sut.parse(node: node) XCTAssertEqual(declaration.name, "T?") - XCTAssertEqual(declaration.isOptional, true) + XCTAssertEqual(declaration.composedType, .optional) } func test_givenForcedUpwrappedType_whenParse_thenReturnDeclaration() throws { @@ -41,7 +41,7 @@ final class TypeDeclarationParserTests: XCTestCase { let declaration = sut.parse(node: node) XCTAssertEqual(declaration.name, "T!") - XCTAssertEqual(declaration.isOptional, false) + XCTAssertEqual(declaration.composedType, .plain) } func test_givenClosure_whenParse_thenReturnDeclaration() throws { @@ -50,7 +50,7 @@ final class TypeDeclarationParserTests: XCTestCase { let declaration = sut.parse(node: node) XCTAssertEqual(declaration.name, "() -> Void") - XCTAssertEqual(declaration.isOptional, false) + XCTAssertEqual(declaration.composedType, .plain) } func test_givenOptionalClosure_whenParse_thenReturnDeclaration() throws { @@ -59,7 +59,7 @@ final class TypeDeclarationParserTests: XCTestCase { let declaration = sut.parse(node: node) XCTAssertEqual(declaration.name, "() -> Void?") - XCTAssertEqual(declaration.isOptional, true) + XCTAssertEqual(declaration.composedType, .optional) } func test_givenSomeType_whenParse_thenReturnDeclaration() throws { @@ -68,7 +68,7 @@ final class TypeDeclarationParserTests: XCTestCase { let declaration = sut.parse(node: node) XCTAssertEqual(declaration.name, "some T") - XCTAssertEqual(declaration.isOptional, false) + XCTAssertEqual(declaration.composedType, .plain) } func test_givenAnyType_whenParse_thenReturnDeclaration() throws { @@ -77,7 +77,7 @@ final class TypeDeclarationParserTests: XCTestCase { let declaration = sut.parse(node: node) XCTAssertEqual(declaration.name, "any T") - XCTAssertEqual(declaration.isOptional, false) + XCTAssertEqual(declaration.composedType, .plain) } func test_givenDictionaryType_whenParse_thenReturnDeclaration() throws { @@ -86,7 +86,34 @@ final class TypeDeclarationParserTests: XCTestCase { let declaration = sut.parse(node: node) XCTAssertEqual(declaration.name, "[A: B]") - XCTAssertEqual(declaration.isOptional, false) + XCTAssertEqual(declaration.composedType, .dictionary) + } + + func test_givenArrayType_whenParse_thenReturnDeclaration() throws { + let node = try parse(string: "[A]") + + let declaration = sut.parse(node: node) + + XCTAssertEqual(declaration.name, "[A]") + XCTAssertEqual(declaration.composedType, .array) + } + + func test_givenArraySecondVariantType_whenParse_thenReturnDeclaration() throws { + let node = try parse(string: "Array") + + let declaration = sut.parse(node: node) + + XCTAssertEqual(declaration.name, "Array") + XCTAssertEqual(declaration.composedType, .array) + } + + func test_givenSetType_whenParse_thenReturnDeclaration() throws { + let node = try parse(string: "Set") + + let declaration = sut.parse(node: node) + + XCTAssertEqual(declaration.name, "Set") + XCTAssertEqual(declaration.composedType, .set) } func test_givenGenericType_whenParse_thenReturnDeclaration() throws { @@ -95,7 +122,7 @@ final class TypeDeclarationParserTests: XCTestCase { let declaration = sut.parse(node: node) XCTAssertEqual(declaration.name, "A") - XCTAssertEqual(declaration.isOptional, false) + XCTAssertEqual(declaration.composedType, .plain) } func test_givenOptionalGenericType_whenParse_thenReturnDeclaration() throws { @@ -104,7 +131,7 @@ final class TypeDeclarationParserTests: XCTestCase { let declaration = sut.parse(node: node) XCTAssertEqual(declaration.name, "A?") - XCTAssertEqual(declaration.isOptional, true) + XCTAssertEqual(declaration.composedType, .optional) } func test_givenEscapingClosure_whenParse_thenReturnDeclaration() throws { @@ -113,7 +140,7 @@ final class TypeDeclarationParserTests: XCTestCase { let declaration = sut.parse(node: node) XCTAssertEqual(declaration.name, "Block") - XCTAssertEqual(declaration.isOptional, false) + XCTAssertEqual(declaration.composedType, .plain) XCTAssertEqual(declaration.prefix, [.escaping]) }