Skip to content

Commit

Permalink
Fixes for subscripts support in AutoMockable template
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-savelev-bumble committed Dec 22, 2023
1 parent a09bdf4 commit b76e38b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
7 changes: 4 additions & 3 deletions Templates/Templates/AutoMockable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ import {{ import }}
{% endif %}
{% endmacro %}

{% macro mockSubscript subscript %}
{% call accessLevel variable.readAccess %}{{ 'final ' if subscript.isFinal }}subscript{% if subscript.isGeneric %}<{% for genericParameter in subscript.genericParameters %}{{ genericParameter.name }}{% if genericParameter.typeName %}: {{ genericParameter.typeName.name }}{% endif %}{{ ', ' if not forloop.last }}{% endfor %}>{% endif %}({% for parameters in subscript.parameters }{{ parameter.asSource }}{{ ', ' if not forloop.last }}{% endfor %}) -> {{ subscript.returnTypeName.name }}{% if subscript.genericRequirements|count != 0 %} where {% for requirement in subscript.genericRequirements %}{{ requirement.leftType.name }} {{ requirement.relationshipSyntax }} {{ requirement.rightType.typeName.name }}{{ ', ' if not forloop.last }}{% endfor %}{% endif %} {
{% macro mockSubscript subscript index %}
//MARK: - Subscript #{{ index }}
{% call accessLevel variable.readAccess %}subscript{% if subscript.isGeneric %}<{% for genericParameter in subscript.genericParameters %}{{ genericParameter.name }}{% if genericParameter.inheritedTypeName %}: {{ genericParameter.inheritedTypeName.name }}{% endif %}{{ ', ' if not forloop.last }}{% endfor %}>{% endif %}({% for parameter in subscript.parameters %}{{ parameter.asSource }}{{ ', ' if not forloop.last }}{% endfor %}) -> {{ subscript.returnTypeName.name }}{% if subscript.genericRequirements|count != 0 %} where {% for requirement in subscript.genericRequirements %}{{ requirement.leftType.name }} {{ requirement.relationshipSyntax }} {{ requirement.rightType.typeName.name }}{{ ', ' if not forloop.last }}{% endfor %}{% endif %} {
{% if subscript.readAccess %} get { fatalError("Subscripts are not fully supported yet") }{% endif %}
{% if subscript.writeAccess %} set { fatalError("Subscripts are not fully supported yet") }{% endif %}
}
Expand Down Expand Up @@ -296,7 +297,7 @@ import {{ import }}
{% endfor %}

{% for subscript in type.allSubscripts|!definedInExtension %}
{% call mockSubscript subscript %}
{% call mockSubscript subscript forloop.counter %}
{% endfor %}
}
{% endif %}{% endfor %}
3 changes: 2 additions & 1 deletion Templates/Tests/Context/AutoMockable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ public protocol ProtocolWithOverrides {
func doSomething(_ data: String) throws -> (([Int], [Any]) -> Void)
}

protocol SubscriptProtocol: AutoMockable {
// sourcery: AutoMockable
protocol SubscriptProtocol {
subscript(arg: Int) -> String { get set }
subscript<T>(arg: T) -> Int { get }
subscript<T: Hashable>(arg: T) -> T? { get set }
Expand Down
20 changes: 20 additions & 0 deletions Templates/Tests/Expected/AutoMockable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,26 @@ class StaticMethodProtocolMock: StaticMethodProtocol {
}

}
class SubscriptProtocolMock: SubscriptProtocol {
//MARK: - Subscript #1
subscript(arg: Int) -> String {
get { fatalError("Subscripts are not fully supported yet") }
set { fatalError("Subscripts are not fully supported yet") }
}
//MARK: - Subscript #2
subscript<T>(arg: T) -> Int {
get { fatalError("Subscripts are not fully supported yet") }
}
//MARK: - Subscript #3
subscript<T: Hashable>(arg: T) -> T? {
get { fatalError("Subscripts are not fully supported yet") }
set { fatalError("Subscripts are not fully supported yet") }
}
//MARK: - Subscript #4
subscript<T>(arg: String) -> T? where T : Cancellable {
get { fatalError("Subscripts are not fully supported yet") }
}
}
class ThrowableProtocolMock: ThrowableProtocol {


Expand Down

0 comments on commit b76e38b

Please sign in to comment.