Skip to content

Commit eee363d

Browse files
authored
Add ability to create new fixtures from existing ones modifying config (#37)
1 parent 15428a3 commit eee363d

File tree

18 files changed

+566
-17
lines changed

18 files changed

+566
-17
lines changed

.idea/jarRepositories.xml

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ val anotherRandomIntFromAList = fixture(1..5)
4646
The default configuration can be overridden when creating the fixture object or
4747
when creating a particular implementation.
4848

49+
It is possible to create a new fixture based on an existing one, which allows
50+
the addition of configuration changes:
51+
52+
```kotlin
53+
val baseFixture = kotlinFixture {
54+
factory<Int> { 3 }
55+
}
56+
57+
val fixture = baseFixture.new {
58+
factory<Long> { 100L }
59+
}
60+
61+
// Prints 100
62+
println(fixture<Long>())
63+
// Prints 3
64+
println(fixture<Int>())
65+
```
66+
4967
#### repeatCount
5068

5169
Used to determine the length used for lists and maps.
@@ -129,6 +147,9 @@ This can be overridden using `factory` which has some built in constructs:
129147

130148
```kotlin
131149
val fixture = kotlinFixture {
150+
// Generate using ranges (and iterables)
151+
factory<Int> { range(1..10) }
152+
132153
// Generate between two dates
133154
factory<Date> { between(startDate, endDate) }
134155
}
@@ -263,7 +284,7 @@ fixture<AnObject> {
263284
classOverride<AnotherObject>(NeverOptionalStrategy)
264285

265286
// You can override the strategy for a property of a class
266-
propertyOverride(AnotherObject:property, RandomlyOptionalStrategy)
287+
propertyOverride(AnotherObject::property, RandomlyOptionalStrategy)
267288
}
268289
}
269290
```

build.gradle.kts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@ import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
1919

2020
plugins {
2121
id("com.github.ben-manes.versions") version "0.27.0"
22-
id("io.gitlab.arturbosch.detekt") version "1.2.1"
22+
id("io.gitlab.arturbosch.detekt") version "1.5.1"
2323
id("com.appmattus.markdown") version "0.6.0"
2424
}
2525

@@ -30,7 +30,7 @@ buildscript {
3030
}
3131
dependencies {
3232
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61")
33-
classpath("com.android.tools.build:gradle:3.5.2")
33+
classpath("com.android.tools.build:gradle:3.5.3")
3434
}
3535
}
3636

@@ -67,7 +67,7 @@ tasks.withType(DependencyUpdatesTask::class.java).all {
6767
}
6868

6969
dependencies {
70-
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.2.1")
70+
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.5.1")
7171
}
7272

7373
detekt {

fixture-android-tests/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,7 +59,7 @@ dependencies {
5959
testImplementation("androidx.test.ext:junit:1.1.1")
6060
testImplementation("org.robolectric:robolectric:4.3.1")
6161

62-
testImplementation("junit:junit:4.12")
62+
testImplementation("junit:junit:4.13")
6363
testImplementation(kotlin("test"))
6464
testImplementation(kotlin("test-junit"))
6565
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")

fixture-kotlintest/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@ dependencies {
2929
api(project(":fixture"))
3030
implementation("io.kotlintest:kotlintest-runner-junit5:3.4.2")
3131

32-
testImplementation("junit:junit:4.12")
32+
testImplementation("junit:junit:4.13")
3333
testImplementation(kotlin("test"))
3434
testImplementation(kotlin("test-junit"))
3535
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")

fixture/build.gradle.kts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,18 +26,18 @@ apply(from = "$rootDir/gradle/scripts/jacoco.gradle.kts")
2626

2727
dependencies {
2828
implementation(kotlin("stdlib-jdk8"))
29-
implementation("io.github.classgraph:classgraph:4.8.58")
29+
implementation("io.github.classgraph:classgraph:4.8.62")
3030
implementation(kotlin("reflect"))
3131

3232
compileOnly("joda-time:joda-time:2.10.5")
3333
testImplementation("joda-time:joda-time:2.10.5")
3434

35-
compileOnly("org.threeten:threetenbp:1.4.0")
36-
testImplementation("org.threeten:threetenbp:1.4.0")
35+
compileOnly("org.threeten:threetenbp:1.4.1")
36+
testImplementation("org.threeten:threetenbp:1.4.1")
3737

3838
compileOnly(files("${System.getenv("ANDROID_HOME")}/platforms/android-29/android.jar"))
3939

40-
testImplementation("junit:junit:4.12")
40+
testImplementation("junit:junit:4.13")
4141
testImplementation(kotlin("test"))
4242
testImplementation(kotlin("test-junit"))
4343
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")

fixture/src/main/kotlin/com/appmattus/kotlinfixture/KotlinFixture.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,10 @@ class Fixture(val fixtureConfiguration: Configuration) {
5353
}
5454
return result
5555
}
56+
57+
fun new(configuration: ConfigurationBuilder.() -> Unit = {}): Fixture {
58+
return Fixture(ConfigurationBuilder(fixtureConfiguration).apply(configuration).build())
59+
}
5660
}
5761

5862
fun kotlinFixture(init: ConfigurationBuilder.() -> Unit = {}) = Fixture(ConfigurationBuilder().apply(init).build())

fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/Generator.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Appmattus Limited
2+
* Copyright 2020 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,3 +25,6 @@ interface Generator<T> {
2525
}
2626

2727
internal typealias GeneratorFun = Generator<Any?>.() -> Any?
28+
29+
fun <T> Generator<T>.range(range: Iterable<T>) =
30+
range.shuffled(random).firstOrNull() ?: throw NoSuchElementException("Range is empty")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.config
18+
19+
@Suppress("FunctionName", "NonAsciiCharacters")
20+
fun Generator<Boolean>.`¯\_(ツ)_/¯`(): Boolean = random.nextBoolean()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.config
18+
19+
@Suppress("FunctionName", "NonAsciiCharacters")
20+
fun Generator<Double>.`¯\_(ツ)_/¯`(): Double = random.nextDouble()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.config
18+
19+
@Suppress("FunctionName", "NonAsciiCharacters")
20+
fun Generator<Float>.`¯\_(ツ)_/¯`(): Float = random.nextFloat()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.config
18+
19+
@Suppress("FunctionName", "NonAsciiCharacters")
20+
fun Generator<Int>.`¯\_(ツ)_/¯`(): Int = random.nextInt()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.config
18+
19+
@Suppress("FunctionName", "NonAsciiCharacters")
20+
fun Generator<Long>.`¯\_(ツ)_/¯`(): Long = random.nextLong()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.config
18+
19+
import kotlin.random.nextUInt
20+
21+
@Suppress("EXPERIMENTAL_API_USAGE", "FunctionName", "NonAsciiCharacters")
22+
fun Generator<UInt>.`¯\_(ツ)_/¯`(): UInt = random.nextUInt()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.config
18+
19+
import kotlin.random.nextULong
20+
21+
@Suppress("EXPERIMENTAL_API_USAGE", "FunctionName", "NonAsciiCharacters")
22+
fun Generator<ULong>.`¯\_(ツ)_/¯`(): ULong = random.nextULong()

0 commit comments

Comments
 (0)