scalacheck-faker

Fake data generation using ScalaCheck Arbitrary instances

License

License

MIT
Categories

Categories

Scala Languages
GroupId

GroupId

io.github.etspaceman
ArtifactId

ArtifactId

scalacheck-faker_2.12
Last Version

Last Version

7.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

scalacheck-faker
Fake data generation using ScalaCheck Arbitrary instances
Project URL

Project URL

https://github.com/etspaceman/scalacheck-faker
Project Organization

Project Organization

io.github.etspaceman
Source Code Management

Source Code Management

https://github.com/etspaceman/scalacheck-faker

Download scalacheck-faker_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.etspaceman/scalacheck-faker_2.12/ -->
<dependency>
    <groupId>io.github.etspaceman</groupId>
    <artifactId>scalacheck-faker_2.12</artifactId>
    <version>7.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.etspaceman/scalacheck-faker_2.12/
implementation 'io.github.etspaceman:scalacheck-faker_2.12:7.0.0'
// https://jarcasting.com/artifacts/io.github.etspaceman/scalacheck-faker_2.12/
implementation ("io.github.etspaceman:scalacheck-faker_2.12:7.0.0")
'io.github.etspaceman:scalacheck-faker_2.12:jar:7.0.0'
<dependency org="io.github.etspaceman" name="scalacheck-faker_2.12" rev="7.0.0">
  <artifact name="scalacheck-faker_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.etspaceman', module='scalacheck-faker_2.12', version='7.0.0')
)
libraryDependencies += "io.github.etspaceman" % "scalacheck-faker_2.12" % "7.0.0"
[io.github.etspaceman/scalacheck-faker_2.12 "7.0.0"]

Dependencies

compile (7)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.13
org.scalacheck : scalacheck_2.12 jar 1.15.4
com.typesafe : config jar 1.4.1
com.github.pureconfig : pureconfig_2.12 jar 0.14.1
org.apache.commons : commons-lang3 jar 3.12.0
io.github.wolfendale : scalacheck-gen-regexp_2.12 jar 0.1.3
io.estatico : newtype_2.12 jar 0.4.4

test (2)

Group / Artifact Type Version
org.scalatest : scalatest_2.12 jar 3.2.8
org.scalatestplus : scalacheck-1-14_2.12 jar 3.2.2.0

Project Modules

There are no modules declared in this project.

scalacheck-faker

Maven Central codecov Scala Steward badge Join the chat at https://gitter.im/etspaceman/scalacheck-faker

Fake Data Generation using ScalaCheck

This library ports over common Faker routines from popular implementations, such as Java Faker into a Scala library.

Arbitraries

Data generators are provided in the form of ScalaCheck Arbitraries. In the below example, we use the instances to construct an Arbitrary instance for our own class.

import org.scalacheck._

import faker._
// Import the default resource-loader implicits
import faker.ResourceLoader.Implicits._

final case class MyClass(firstName: String, lastName: String, emailAddress: String, ipAddress: String)

object MyClass {
  implicit val myClassArbitrary: Arbitrary[MyClass] = Arbitrary(
    for {
      firstName <- Arbitrary.arbitrary[name.FirstName]
      lastName <- Arbitrary.arbitrary[name.LastName]
      email <- Arbitrary.arbitrary[internet.EmailAddress]
      ip <- Arbitrary.arbitrary[internet.IpV4Address]
    }  yield MyClass(firstName.value, lastName.value, email.value, ip.value)
  )
}

The above uses import faker.ResourceLoader.Implicits._, which provides the default ResourceLoader for the Arbitrary instances. This ResourceLoader will evaluate the Faker instances against your system's default Locale value. You can override this value in your application's JVM through JVM args.

If you have a need to use a separate locale than the system default, you can create your own instance:

import java.util.Locale

import org.scalacheck._

import faker._
import faker.ResourceLoader

object UKResourceLoader {
  implicit val UKResourceLoader: ResourceLoader = ResourceLoader.en_GB
}

Faker Class

Another option for generating data is the Faker class. This class provides a similar feel to other Faker libraries.

import java.time.Instant
import java.util.Locale

import org.scalacheck.rng.Seed

import faker._

val firstName: String = Faker.default.firstName()
val nowInstant: Instant = Faker.default.nowInstant()

// You can also leverage a Faker instance using a manually-designated Locale:
val ukFirstName: String = Faker.en_GB.firstName()

// Sometimes you need to be able to manually supply the seed for the random number generator.
// You can do that below:
val firstNameSeeded: String = Faker.default.firstName(Seed(2L))

Un-implemented instances

Some locales may cause Faker to return dummy values for certain instances. For example, there are no StateLike instances for the UK (en_GB) Locale.

Some instances may not be implemented due to the support not being present in some other Faker library. If you see some instances that should exist for a locale, and have values that would be relevant for it, please either open an Issue or PR with the proposed implementation.

Versions

Version
7.0.0
6.0.0
5.0.11
5.0.10
5.0.9
5.0.8
5.0.7
5.0.6
5.0.5
5.0.4
5.0.3
5.0.2
5.0.1
5.0.0
4.0.1
4.0.0
3.0.1
3.0.0
2.0.0
1.0.1
1.0.0
0.0.5
0.0.4