Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About the EasyPredicate design #4

Open
ZhipingYang opened this issue Aug 1, 2019 · 2 comments
Open

About the EasyPredicate design #4

ZhipingYang opened this issue Aug 1, 2019 · 2 comments
Assignees
Labels
Code Design better architecture to discuss

Comments

@ZhipingYang
Copy link
Owner

ZhipingYang commented Aug 1, 2019

PredicateRawValue is RawValue type of EasyPredicate

current

public enum PredicateRawValue: RawRepresentable {
    public var rawValue: String { return xxx }
    public init?(rawValue: String) {
        self = .custom(regular: rawValue)
    }
    case bool(key: PredicateKey.bool, comparison: Comparison, value: Bool)
    case string(key: PredicateKey.string, comparison: Comparison, value: String)
    case type(value: XCUIElement.ElementType)
    case custom(regular: String)
}

the thinking of new design

enum PredicateRawValue<Key: RawRepresentable, Value: RawRepresentable>: RawRepresentable where Key.RawValue == String {
    var rawValue: String { return xxx }
    init?(rawValue: String) {
        self = .custom(regular: rawValue)
    }
    case keyValue(key: Key, comparison: Comparison, value: Value)
    case custom(regular: String) // unrelated with Key & Value define, shit 💩
}
@ZhipingYang ZhipingYang added the Code Design better architecture to discuss label Aug 1, 2019
@ZhipingYang ZhipingYang self-assigned this Aug 1, 2019
@ZhipingYang
Copy link
Owner Author

ZhipingYang commented Aug 1, 2019

the older EasyPredicate

public enum EasyPredicate: RawRepresentable {
    
    case exists(_ exists: Bool)
    case isEnabled(_ isEnabled: Bool)
    case isHittable(_ isHittable: Bool)
    case isSelected(_ isSelected: Bool)
    case label(_ comparison: Comparison, _ value: String)
    case identifier(_ identifier: String)
    case type(_ type: XCUIElement.ElementType)
    case other(_ ragular: String)
    
    public init?(rawValue: PredicateRawValue) {
        switch rawValue {
        case .bool(let key, _, let value):
            switch key {
            case .exists:       self = .exists(value)
            case .isEnabled:    self = .isEnabled(value)
            case .isSelected:   self = .isSelected(value)
            case .isHittable:   self = .isHittable(value)
            }
        case .type(let value):  self = .type(value)
        case .string(let key, let comparison, let value):
            switch key {
            case .label:        self = .label(comparison, value)
            case .identifier:   self = .identifier(value)
            }
        case .custom(let regular): self = .other(regular)
        }
    }
    
    public var rawValue: PredicateRawValue {
        switch self {
        case .exists(let value):
            return .bool(key: .exists, comparison: .equals, value: value)
        case .isEnabled(let value):
            return .bool(key: .isEnabled, comparison: .equals, value: value)
        case .isHittable(let value):
            return .bool(key: .isHittable, comparison: .equals, value: value)
        case .isSelected(let value):
            return .bool(key: .isSelected, comparison: .equals, value: value)
        case .label(let comparison, let value):
            return .string(key: .label, comparison: comparison, value: value)
        case .identifier(let value):
            return .string(key: .identifier, comparison: .equals, value: value)
        case .type(let value):
            return .type(value: value)
        case .other(let value):
            return .custom(regular: value)
        }
    }
}

the thinking of new design

struct EasyPredicate {

    typealias BoolRawValue = PredicateRawValue<PredicateKey.bool, Bool>
    typealias StringRawValue = PredicateRawValue<PredicateKey.string, String>
    typealias TypeRawValue = PredicateRawValue<PredicateKey.type, XCUIElement.ElementType>
    
    static func exists(_ exists: Bool) -> BoolRawValue {
        return BoolRawValue.keyValue(key: .exists, comparison: .equals, value: exists)
    }
    static func isEnabled(_ isEnabled: Bool) -> BoolRawValue {
        return BoolRawValue.keyValue(key: .isEnabled, comparison: .equals, value: isEnabled)
    }
    static func isHittable(_ isHittable: Bool) -> BoolRawValue {
        return BoolRawValue.keyValue(key: .isHittable, comparison: .equals, value: isHittable)
    }
    static func isSelected(_ isSelected: Bool) -> BoolRawValue {
        return BoolRawValue.keyValue(key: .isSelected, comparison: .equals, value: isSelected)
    }
    static func type(_ comparison: Comparison, _ value: XCUIElement.ElementType) -> TypeRawValue {
        return TypeRawValue.keyValue(key: .elementType, comparison: comparison, value: value)
    }
    static func label(_ comparison: Comparison, _ value: String) -> StringRawValue {
        return StringRawValue.keyValue(key: .label, comparison: comparison, value: value)
    }
    static func identifier( _ value: String) -> StringRawValue {
        return StringRawValue.keyValue(key: PredicateKey.string.label, comparison: .equals, value: value)
    }
    static func ragular( _ ragular: String) -> StringRawValue {
        return StringRawValue.custom(regular: ragular)
    }
}

new design requires extensions as below:

extension String: RawRepresentable {
    public var rawValue: String { return self }
    public init?(rawValue: String) {
        self = rawValue
    }
}
extension Bool: RawRepresentable {
    public var rawValue: Bool { return self }
    public init?(rawValue: Bool) {
        self = rawValue
    }
}
extension XCUIElement.ElementType: RawRepresentable {
    public var rawValue: XCUIElement.ElementType { return self }
    public init?(rawValue: XCUIElement.ElementType) {
        self = rawValue
    }
}

@ZhipingYang
Copy link
Owner Author

ZhipingYang commented Aug 1, 2019

About the new design of PredicateRawValue's part is perfect.
But when we going down to the EasyPredicate's design,
Emm... 🤮🤮🤮🤮

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Design better architecture to discuss
Projects
None yet
Development

No branches or pull requests

1 participant