Skip to content

Commit 8defe69

Browse files
authored
Support for javafaker library (#47)
1 parent 60c8afc commit 8defe69

25 files changed

+1502
-3
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ testImplementation("com.appmattus.fixture:fixture:<latest-version>")
1414

1515
// Add for Kotest integration
1616
testImplementation("com.appmattus.fixture:fixture-kotest:<latest-version>")
17+
18+
// Add for Java Faker integration
19+
testImplementation("com.appmattus.fixture:fixture-javafaker:<latest-version>")
1720
```
1821

1922
Simply create a fixture and invoke it with the type to be generated:
@@ -415,6 +418,53 @@ fixture.checkAll { person1: Person, person2: Person ->
415418
}
416419
```
417420

421+
## Java Faker support
422+
423+
The [Java Faker](http://dius.github.io/java-faker/) library generates
424+
fake data, useful if you need to generate objects with pretty data.
425+
426+
Including the `fixture-javafaker` dependency in your project adds a
427+
`javaFakerStrategy` which will use
428+
[Java Faker](http://dius.github.io/java-faker/) to populate named
429+
properties such as `name`, `city` and `phoneNumber`. A full list of
430+
supported properties and how they map to
431+
[Java Faker](http://dius.github.io/java-faker/) can be found in
432+
[JavaFakerConfiguration](fixture-javafaker/src/main/kotlin/com/appmattus/kotlinfixture/decorator/fake/javafaker/JavaFakerConfiguration.kt).
433+
434+
Additionally, the `javaFakerStrategy` function allows you to override
435+
faker settings such as `locale`.
436+
437+
```kotlin
438+
439+
val fixture = kotlinFixture {
440+
javaFakerStrategy()
441+
}
442+
443+
data class Person(val name: String, val age: Long)
444+
445+
println(fixture<Person>()) // Person(name=Keneth Bartoletti, age=54)
446+
```
447+
448+
### Regex to String generation
449+
450+
The module also introduces the ability to generate a random string from
451+
a Regex, with no need to enable the faker functionality:
452+
453+
```kotlin
454+
data class DataClass(val index: String, val value: String)
455+
456+
val indexRegex = "[a-z][0-9]".toRegex()
457+
val valueRegex = "[A-Z]{3}".toRegex()
458+
459+
val fixture = kotlinFixture {
460+
factory<String> { regexify(indexRegex) }
461+
462+
property(DataClass::value) { regexify(valueRegex) }
463+
}
464+
465+
println(fixture<DataClass>()) // DataClass(index=m3, value=CGJ)
466+
```
467+
418468
## Contributing
419469

420470
Please fork this repository and contribute back using

detekt-config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ build:
44
style:
55
MagicNumber:
66
excludes: "**/test/**,**/androidTest/**,**/*.Test.kt,**/*.Spec.kt,**/*.Spek.kt,**/*.gradle.kts"
7+
MaxLineLength:
8+
active: false
79

810
formatting:
911
StringTemplate:

fixture-javafaker/build.gradle.kts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2020 Appmattus Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
18+
19+
plugins {
20+
kotlin("jvm")
21+
id("com.android.lint")
22+
}
23+
24+
apply(from = "$rootDir/bintray.gradle.kts")
25+
apply(from = "$rootDir/gradle/scripts/jacoco.gradle.kts")
26+
27+
dependencies {
28+
api(kotlin("stdlib-jdk8"))
29+
api(project(":fixture"))
30+
api("com.github.javafaker:javafaker:1.0.2")
31+
32+
testImplementation("junit:junit:4.13")
33+
testImplementation(kotlin("test"))
34+
testImplementation(kotlin("test-junit"))
35+
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
36+
37+
testImplementation(kotlin("reflect"))
38+
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.7")
39+
}
40+
41+
lintOptions {
42+
isAbortOnError = true
43+
isWarningsAsErrors = true
44+
htmlOutput = file("${buildDir}/reports/lint-results.html")
45+
xmlOutput = file("${buildDir}/reports/lint-results.xml")
46+
}
47+
48+
java {
49+
sourceCompatibility = JavaVersion.VERSION_1_8
50+
targetCompatibility = JavaVersion.VERSION_1_8
51+
}
52+
53+
tasks.withType<KotlinCompile> {
54+
kotlinOptions.jvmTarget = "1.8"
55+
}
56+
57+
// Fix lack of source code when publishing pure Kotlin projects
58+
// See https://github.com/novoda/bintray-release/issues/262
59+
tasks.whenTaskAdded {
60+
if (name == "generateSourcesJarForMavenPublication") {
61+
this as Jar
62+
from(sourceSets.main.get().allSource)
63+
}
64+
}
65+
66+
tasks.getByName("check").finalizedBy(rootProject.tasks.getByName("detekt"))
67+
tasks.getByName("check").finalizedBy(rootProject.tasks.getByName("markdownlint"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2020 Appmattus Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.appmattus.kotlinfixture.decorator.fake.javafaker
18+
19+
import com.appmattus.kotlinfixture.config.Generator
20+
import com.github.javafaker.Faker
21+
import kotlin.random.asJavaRandom
22+
23+
@Suppress("SpellCheckingInspection")
24+
fun Generator<String>.regexify(regex: String): String {
25+
return Faker(random.asJavaRandom()).regexify(regex)
26+
}
27+
28+
@Suppress("SpellCheckingInspection")
29+
fun Generator<String>.regexify(regex: Regex): String {
30+
return regexify(regex.pattern)
31+
}

0 commit comments

Comments
 (0)