Randomizable

1.0.0

Any type random value generator to reduce boilerplates for unittest
tattn/Randomizable

What's New

2018-12-07T06:23:00Z

Randomizable

Any type random value generator to reduce boilerplates for unittest

Features

  • Generate random value for any type
  • Customize rules for random

Requirements

  • Xcode 10.x
  • Swift 4.x
  • iOS 9.0+

Installation

Carthage

github "tattn/Randomizable"

Usage

Generate random value for 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")

Customize random value

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))

Enum support

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)

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

Randomizable is released under the MIT license. See LICENSE for details.

Description

  • Swift Tools
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Oct 21 2024 17:12:46 GMT-0900 (Hawaii-Aleutian Daylight Time)