Randomizable

任何类型的随机值生成器,以减少单元测试中的样板代码
Bitrise Carthage compatible Swift Version license:MIT

特性

要求

安装

Carthage

github "tattn/Randomizable"

Swift Package Manager

.package(url: "https://github.com/tattn/Randomizable.git", from: “1.0.3")

用法

struct 生成随机值

import Randomizable

// conform Decodable to auto-implement Randomizable
struct User: Decodable, Randomizable {
    let id: ID
    let name: String

    struct ID: Decodable, Randomizable {
        let id: Int
    }
}

let user = User.randomValue()
// => User(id: ID(id: 1823), name: "raewfbaw")

自定义随机值

struct User: Decodable, Randomizable {
    let name: String
    let profile: Profile

    struct Profile: Decodable, Randomizable {
        let email: String
        let birthday: Date?

        static func randomValue() -> Profile {
            return Profile(email: "\(String.randomValue())@test.com",
                           birthday: Date?.randomValue())
        }
    }
}

let user = User.randomValue()
// => User(name: "gaoaweja", profile: Profile(email: "iwelasm@test.com"
//                           birthday: 2018-11-02 16:25:17 +0000))

枚举支持

struct A: Decodable, Randomizable {
    let animal: Animal
    enum Animal: String, Decodable, CaseIterable, Randomizable {
        case cat
        case dog
        case rabbit
    }
}

A.randomValue()
// => A(animal: Animal.cat)

配置

struct A: Decodable, Randomizable {
    let int: Int?
    let string: String?
}

let configuration = RandomizableConfiguration()
configuration.forceNil = true

let a = A.randomValue(with: configuration)
a.int // nil
a.string // nil

贡献

  1. Fork 它!
  2. 创建您的特性分支:git checkout -b my-new-feature
  3. 提交您的更改:git commit -am 'Add some feature'
  4. 推送到分支:git push origin my-new-feature
  5. 提交一个 pull request :D

许可证

Randomizable 使用 MIT 许可证发布。 详情请参阅 LICENSE。