mockolo icon indicating copy to clipboard operation
mockolo copied to clipboard

Support Actor Protocol

Open tsuzukihashi opened this issue 1 year ago • 6 comments

Could you support to actor protocol? I would like to automatically generate an "Actor" compliant protocol as follows.

/// @mockable
protocol Hoge: Actor {
  func get(hoge: String) async -> Result<String, Error>
  var count: Int { get }
}

↓ Generated

// NOTE: change actor
actor HogeMock: Hoge {
  init() {}
  init(count: Int = 0) {
    self.count = count
  }

  private(set) var getCallCount = 0
  var getArgValues = [String]()
  // NOTE: add @MainActor
  @MainActor
  var getHandler: ((String) async -> (Result<String, Error>))?
  func get(hoge: String) async -> Result<String, Error> {
    getCallCount += 1
    getArgValues.append(hoge)
    // NOTE: add await
    if let getHandler = await getHandler {
      return await getHandler(hoge)
    }
    fatalError("getHandler returns can't have a default value thus its handler must be set")
  }

  private(set) var countSetCallCount = 0
  var count: Int = 0 { didSet { countSetCallCount += 1 } }
  // NOTE: add set func
  func setCount(_ value: Int) {
    self.count = value
  }
}

Thank you.

tsuzukihashi avatar Mar 03 '23 08:03 tsuzukihashi