Skip to content

How combine two functions as the one in AccessibilityIdentifier #5

@ZhipingYang

Description

@ZhipingYang

😤

Here is two extensions PrettyRawRepresentable+helpers.swift and RawRepresentable+helpers.swift which are support different situation on enum.
But the most unacceptable thing is that their API is almost the same

enum WayOne: String, PrettyRawRepresentable {
    case theWay
}
enum WayTwo: String {
    case theWay
}
wayOneView >>> WayOne.theWay
wayTwoView >>> WayTwo.theWay

// wayOneView.accessibilityIdentifier == "WayOne_theWay"
// called in PrettyRawRepresentable+helpers.swift
let element_1: XCUIElement = WayOne.theWay.element 

// wayTwoView.accessibilityIdentifier == "theWay"
// called in RawRepresentable+helpers.swift
let element_2: XCUIElement = WayTwo.theWay.element

current

public protocol PrettyRawRepresentable: RawRepresentable where RawValue == String {
    var prettyRawValue: RawValue { get }
}

public extension PrettyRawRepresentable {
    var prettyRawValue: String {
        let paths = String(reflecting: self).split(separator: ".").dropFirst()
        if String(paths.last ?? "") != rawValue {
            return rawValue
        }
        return paths.joined(separator: "_")
    }
}

infix operator >>>
public func >>> <T: RawRepresentable>(lhs: UIAccessibilityIdentification?, rhs: T) where T.RawValue == String {
    lhs?.accessibilityIdentifier = rhs.rawValue
}
public func >>> <T: PrettyRawRepresentable>(lhs: UIAccessibilityIdentification?, rhs: T) {
    lhs?.accessibilityIdentifier = rhs.prettyRawValue
}

wants the design

only use one function

public protocol PrettyRawRepresentable: RawRepresentable where RawValue == String {
    var rawValue: RawValue { get }
}

public extension PrettyRawRepresentable {
    var rawValue: String {
        // how todo
    }
}

infix operator >>>
public func >>> <T: RawRepresentable>(lhs: UIAccessibilityIdentification?, rhs: T) where T.RawValue == String {
    lhs?.accessibilityIdentifier = rhs.rawValue
}

Metadata

Metadata

Assignees

Labels

Code Designbetter architecture to discuss

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions