Skip to content

Commit

Permalink
Fixing incorrect optional description bug for effected semantic types (
Browse files Browse the repository at this point in the history
…#36)

* Fixing incorrect optional description bug for effected semantic types
  • Loading branch information
mobrien-ghost authored Mar 10, 2024
1 parent e1fc376 commit a55babf
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public struct ArrayDecl: Hashable, Equatable, CustomStringConvertible {
// MARK: - CustomStringConvertible

public var description: String {
resolver.node.description.trimmed
let base = resolver.node.description.trimmed
return base + (isOptional ? "?" : "")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ public struct Closure: DeclarationComponent {
public init(node: FunctionTypeSyntax) {
resolver = ClosureSemanticsResolver(node: node)
}

// MARK: - CustomStringConvertible

public var description: String {
let base = node.description.trimmed
return isOptional ? "(\(base))?" : base
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public struct DictionaryDecl: Hashable, Equatable, CustomStringConvertible {
// MARK: - CustomStringConvertible

public var description: String {
resolver.node.description.trimmed
let base = resolver.node.description.trimmed
return base + (isOptional ? "?" : "")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public struct Result: Hashable, Equatable, CustomStringConvertible {
// MARK: - CustomStringConvertible

public var description: String {
resolver.node.description.trimmed
let base = resolver.node.description.trimmed
return base + (isOptional ? "?" : "")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public struct SetDecl: Hashable, Equatable, CustomStringConvertible {
// MARK: - CustomStringConvertible

public var description: String {
resolver.node.description.trimmed
let base = resolver.node.description.trimmed
return base + (isOptional ? "?" : "")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public struct Tuple: Hashable, Equatable, CustomStringConvertible {
// MARK: - CustomStringConvertible

public var description: String {
resolver.node.description.trimmed
let base = elements.signatureInputString()
return isOptional ? "\(base)?" : base
}
}
43 changes: 36 additions & 7 deletions Tests/SyntaxSparrowTests/Declarations/FunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ final class FunctionTests: XCTestCase {
func asyncAwaitMethodThrowing() throws async {}
func asyncAwaitMethodThrowingReturning() throws async -> String {}
func arrayShorthand(persons: [(name: String, age: Int?)]?) {}
func arrayIdentifier(names: Array<String>) {}
func arrayIdentifier(names: Array<String?>) {}
func arrayIdentifier(names: Array<String?>?) {}
func setIdentifier(names: Set<String>) {}
func dictionaryShorthand(persons: [String: (name: String, age: Int?)]?) {}
func dictionaryIdentifier(names: Dictionary<String, (name: String, age: Int?)>?) {}
Expand All @@ -330,7 +331,7 @@ final class FunctionTests: XCTestCase {
XCTAssertTrue(instanceUnderTest.isStale)
instanceUnderTest.collectChildren()
XCTAssertFalse(instanceUnderTest.isStale)
XCTAssertEqual(instanceUnderTest.functions.count, 26)
XCTAssertEqual(instanceUnderTest.functions.count, 27)

// No Parameters
// func noParameters() throws {}
Expand Down Expand Up @@ -577,6 +578,7 @@ final class FunctionTests: XCTestCase {
XCTAssertEqual(tuple.elements[1].name, "age")
XCTAssertNil(tuple.elements[1].secondName)
XCTAssertTrue(tuple.elements[1].isOptional)
XCTAssertEqual(tuple.description, "(name: String, age: Int?)?")
} else {
XCTFail("function.signature.input[0] type should be tuple")
}
Expand Down Expand Up @@ -609,6 +611,7 @@ final class FunctionTests: XCTestCase {
XCTAssertFalse(closure.isOptional)
XCTAssertTrue(closure.isEscaping)
XCTAssertFalse(closure.isAutoEscaping)
XCTAssertEqual(closure.description, "(Int) -> Void")
} else {
XCTFail("function.signature.input[0] type should be closure")
}
Expand Down Expand Up @@ -640,6 +643,7 @@ final class FunctionTests: XCTestCase {
XCTAssertTrue(closure.isOptional)
XCTAssertTrue(closure.isEscaping)
XCTAssertTrue(closure.isAutoEscaping)
XCTAssertEqual(closure.description, "((Int) -> Void)?")
} else {
XCTFail("function.signature.input[0] type should be closure")
}
Expand Down Expand Up @@ -669,13 +673,15 @@ final class FunctionTests: XCTestCase {
XCTAssertTrue(closure.isOptional)
XCTAssertTrue(closure.isEscaping)
XCTAssertTrue(closure.isAutoEscaping)
XCTAssertEqual(closure.description, "((name: String, age: Int) -> String?)?")
if case let EntityType.tuple(tuple) = closure.input {
XCTAssertEqual(tuple.elements.count, 2)
XCTAssertEqual(tuple.elements[0].name, "name")
XCTAssertEqual(tuple.elements[0].type, .simple("String"))
XCTAssertEqual(tuple.elements[1].name, "age")
XCTAssertEqual(tuple.elements[1].type, .simple("Int"))
XCTAssertFalse(tuple.isOptional)
XCTAssertEqual(tuple.description, "(name: String, age: Int)")
} else {
XCTFail("closure input type should be tuple")
}
Expand Down Expand Up @@ -706,6 +712,7 @@ final class FunctionTests: XCTestCase {
XCTAssertEqual(result.successType, .simple("String"))
XCTAssertEqual(result.failureType, .simple("Error"))
XCTAssertFalse(result.isOptional)
XCTAssertEqual(result.description, "Result<String, Error>")
} else {
XCTFail("function.signature.input[0] type should be Result")
}
Expand Down Expand Up @@ -733,6 +740,7 @@ final class FunctionTests: XCTestCase {
XCTAssertEqual(result.successType, .simple("String"))
XCTAssertEqual(result.failureType, .simple("Error"))
XCTAssertTrue(result.isOptional)
XCTAssertEqual(result.description, "Result<String, Error>?")
} else {
XCTFail("function.signature.input[0] type should be Result")
}
Expand Down Expand Up @@ -831,12 +839,13 @@ final class FunctionTests: XCTestCase {
XCTAssertEqual(tuple.elements[1].name, "age")
XCTAssertEqual(tuple.elements[1].type, .simple("Int?"))
XCTAssertFalse(tuple.isOptional)
XCTAssertEqual(array.description, "[(name: String, age: Int?)]?")
}
} else {
XCTFail("function.signature.input[0] type should be Array")
}

// func arrayIdentifier(names: Array<String>) {}
// func arrayIdentifier(names: Array<String?>) {}
function = instanceUnderTest.functions[22]
XCTAssertEqual(function.keyword, "func")
XCTAssertEqual(function.identifier, "arrayIdentifier")
Expand All @@ -847,14 +856,34 @@ final class FunctionTests: XCTestCase {
if case let EntityType.array(array) = function.signature.input[0].type {
XCTAssertEqual(array.declType, .generic)
XCTAssertFalse(array.isOptional)
XCTAssertEqual(array.elementType, .simple("String"))
XCTAssertEqual(array.elementType, .simple("String?"))
XCTAssertEqual(array.elementType.description, "String?")
XCTAssertEqual(array.description, "Array<String?>")
} else {
XCTFail("function.signature.input[0] type should be Array")
}

// func setIdentifier(names: Set<String>) {}
// func arrayIdentifier(names: Array<String?>?) {}
function = instanceUnderTest.functions[23]
XCTAssertEqual(function.keyword, "func")
XCTAssertEqual(function.identifier, "arrayIdentifier")
XCTAssertNil(function.signature.effectSpecifiers?.throwsSpecifier)
XCTAssertNil(function.signature.effectSpecifiers?.asyncSpecifier)
XCTAssertEqual(function.signature.input.count, 1)

if case let EntityType.array(array) = function.signature.input[0].type {
XCTAssertEqual(array.declType, .generic)
XCTAssertTrue(array.isOptional)
XCTAssertEqual(array.elementType, .simple("String?"))
XCTAssertEqual(array.elementType.description, "String?")
XCTAssertEqual(array.description, "Array<String?>?")
} else {
XCTFail("function.signature.input[0] type should be Array")
}

// func setIdentifier(names: Set<String>) {}
function = instanceUnderTest.functions[24]
XCTAssertEqual(function.keyword, "func")
XCTAssertEqual(function.identifier, "setIdentifier")
XCTAssertNil(function.signature.effectSpecifiers?.throwsSpecifier)
XCTAssertNil(function.signature.effectSpecifiers?.asyncSpecifier)
Expand All @@ -868,7 +897,7 @@ final class FunctionTests: XCTestCase {
}

// func dictionaryShorthand(persons: [String: (name: String, age: Int?)]?) {}
function = instanceUnderTest.functions[24]
function = instanceUnderTest.functions[25]
XCTAssertEqual(function.keyword, "func")
XCTAssertEqual(function.identifier, "dictionaryShorthand")
XCTAssertNil(function.signature.effectSpecifiers?.throwsSpecifier)
Expand All @@ -892,7 +921,7 @@ final class FunctionTests: XCTestCase {
}

// func dictionaryIdentifier(names: Dictionary<String, (name: String, age: Int?)>?) {}
function = instanceUnderTest.functions[25]
function = instanceUnderTest.functions[26]
XCTAssertEqual(function.keyword, "func")
XCTAssertEqual(function.identifier, "dictionaryIdentifier")
XCTAssertNil(function.signature.effectSpecifiers?.throwsSpecifier)
Expand Down
6 changes: 5 additions & 1 deletion Tests/SyntaxSparrowTests/Declarations/VariableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ final class VariableTests: XCTestCase {
XCTAssertEqual(variable.description, "var handler: Void? = {}")
}

func test_variable_typealiasType_optionalVariants_willResolveExpectedValues() {
func test_variable_tupleType_optionalVariants_willResolveExpectedValues() {
let source = #"""
var person: (name: String, age: Int?) = ("name", 20)
var person: (name: String, age: Int?)? = ("name", 20)
Expand All @@ -321,6 +321,7 @@ final class VariableTests: XCTestCase {
XCTAssertEqual(tuple.elements[1].name, "age")
XCTAssertEqual(tuple.elements[1].type, .simple("Int?"))
XCTAssertFalse(tuple.isOptional)
XCTAssertEqual(tuple.description, "(name: String, age: Int?)")
} else {
XCTFail("variable type should be tuple")
}
Expand All @@ -339,6 +340,7 @@ final class VariableTests: XCTestCase {
XCTAssertEqual(tuple.elements[1].name, "age")
XCTAssertEqual(tuple.elements[1].type, .simple("Int?"))
XCTAssertTrue(tuple.isOptional)
XCTAssertEqual(tuple.description, "(name: String, age: Int?)?")
} else {
XCTFail("variable type should be tuple")
}
Expand Down Expand Up @@ -366,6 +368,7 @@ final class VariableTests: XCTestCase {
XCTAssertEqual(closure.input, .simple("String"))
XCTAssertEqual(closure.output, .simple("Int?"))
XCTAssertFalse(closure.isOptional)
XCTAssertEqual(closure.description, "(String) -> Int?")
} else {
XCTFail("variable type should be closure")
}
Expand All @@ -381,6 +384,7 @@ final class VariableTests: XCTestCase {
XCTAssertEqual(closure.input, .simple("String"))
XCTAssertEqual(closure.output, .simple("Int?"))
XCTAssertTrue(closure.isOptional)
XCTAssertEqual(closure.description, "((String) -> Int?)?")
} else {
XCTFail("variable type should be closure")
}
Expand Down

0 comments on commit a55babf

Please sign in to comment.