Skip to content

Commit

Permalink
Add ability to create new fixtures from existing ones modifying config (
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmook authored Feb 11, 2020
1 parent 15428a3 commit eee363d
Show file tree
Hide file tree
Showing 18 changed files with 566 additions and 17 deletions.
35 changes: 35 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ val anotherRandomIntFromAList = fixture(1..5)
The default configuration can be overridden when creating the fixture object or
when creating a particular implementation.

It is possible to create a new fixture based on an existing one, which allows
the addition of configuration changes:

```kotlin
val baseFixture = kotlinFixture {
factory<Int> { 3 }
}

val fixture = baseFixture.new {
factory<Long> { 100L }
}

// Prints 100
println(fixture<Long>())
// Prints 3
println(fixture<Int>())
```

#### repeatCount

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

```kotlin
val fixture = kotlinFixture {
// Generate using ranges (and iterables)
factory<Int> { range(1..10) }

// Generate between two dates
factory<Date> { between(startDate, endDate) }
}
Expand Down Expand Up @@ -263,7 +284,7 @@ fixture<AnObject> {
classOverride<AnotherObject>(NeverOptionalStrategy)

// You can override the strategy for a property of a class
propertyOverride(AnotherObject:property, RandomlyOptionalStrategy)
propertyOverride(AnotherObject::property, RandomlyOptionalStrategy)
}
}
```
Expand Down
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Appmattus Limited
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,7 @@ import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask

plugins {
id("com.github.ben-manes.versions") version "0.27.0"
id("io.gitlab.arturbosch.detekt") version "1.2.1"
id("io.gitlab.arturbosch.detekt") version "1.5.1"
id("com.appmattus.markdown") version "0.6.0"
}

Expand All @@ -30,7 +30,7 @@ buildscript {
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61")
classpath("com.android.tools.build:gradle:3.5.2")
classpath("com.android.tools.build:gradle:3.5.3")
}
}

Expand Down Expand Up @@ -67,7 +67,7 @@ tasks.withType(DependencyUpdatesTask::class.java).all {
}

dependencies {
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.2.1")
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.5.1")
}

detekt {
Expand Down
4 changes: 2 additions & 2 deletions fixture-android-tests/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Appmattus Limited
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,7 +59,7 @@ dependencies {
testImplementation("androidx.test.ext:junit:1.1.1")
testImplementation("org.robolectric:robolectric:4.3.1")

testImplementation("junit:junit:4.12")
testImplementation("junit:junit:4.13")
testImplementation(kotlin("test"))
testImplementation(kotlin("test-junit"))
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
Expand Down
4 changes: 2 additions & 2 deletions fixture-kotlintest/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Appmattus Limited
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@ dependencies {
api(project(":fixture"))
implementation("io.kotlintest:kotlintest-runner-junit5:3.4.2")

testImplementation("junit:junit:4.12")
testImplementation("junit:junit:4.13")
testImplementation(kotlin("test"))
testImplementation(kotlin("test-junit"))
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
Expand Down
10 changes: 5 additions & 5 deletions fixture/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Appmattus Limited
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,18 +26,18 @@ apply(from = "$rootDir/gradle/scripts/jacoco.gradle.kts")

dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("io.github.classgraph:classgraph:4.8.58")
implementation("io.github.classgraph:classgraph:4.8.62")
implementation(kotlin("reflect"))

compileOnly("joda-time:joda-time:2.10.5")
testImplementation("joda-time:joda-time:2.10.5")

compileOnly("org.threeten:threetenbp:1.4.0")
testImplementation("org.threeten:threetenbp:1.4.0")
compileOnly("org.threeten:threetenbp:1.4.1")
testImplementation("org.threeten:threetenbp:1.4.1")

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

testImplementation("junit:junit:4.12")
testImplementation("junit:junit:4.13")
testImplementation(kotlin("test"))
testImplementation(kotlin("test-junit"))
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Appmattus Limited
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,6 +53,10 @@ class Fixture(val fixtureConfiguration: Configuration) {
}
return result
}

fun new(configuration: ConfigurationBuilder.() -> Unit = {}): Fixture {
return Fixture(ConfigurationBuilder(fixtureConfiguration).apply(configuration).build())
}
}

fun kotlinFixture(init: ConfigurationBuilder.() -> Unit = {}) = Fixture(ConfigurationBuilder().apply(init).build())
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Appmattus Limited
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,3 +25,6 @@ interface Generator<T> {
}

internal typealias GeneratorFun = Generator<Any?>.() -> Any?

fun <T> Generator<T>.range(range: Iterable<T>) =
range.shuffled(random).firstOrNull() ?: throw NoSuchElementException("Range is empty")
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.kotlinfixture.config

@Suppress("FunctionName", "NonAsciiCharacters")
fun Generator<Boolean>.`¯\_(ツ)_/¯`(): Boolean = random.nextBoolean()
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.kotlinfixture.config

@Suppress("FunctionName", "NonAsciiCharacters")
fun Generator<Double>.`¯\_(ツ)_/¯`(): Double = random.nextDouble()
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.kotlinfixture.config

@Suppress("FunctionName", "NonAsciiCharacters")
fun Generator<Float>.`¯\_(ツ)_/¯`(): Float = random.nextFloat()
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.kotlinfixture.config

@Suppress("FunctionName", "NonAsciiCharacters")
fun Generator<Int>.`¯\_(ツ)_/¯`(): Int = random.nextInt()
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.kotlinfixture.config

@Suppress("FunctionName", "NonAsciiCharacters")
fun Generator<Long>.`¯\_(ツ)_/¯`(): Long = random.nextLong()
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.kotlinfixture.config

import kotlin.random.nextUInt

@Suppress("EXPERIMENTAL_API_USAGE", "FunctionName", "NonAsciiCharacters")
fun Generator<UInt>.`¯\_(ツ)_/¯`(): UInt = random.nextUInt()
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2020 Appmattus Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.appmattus.kotlinfixture.config

import kotlin.random.nextULong

@Suppress("EXPERIMENTAL_API_USAGE", "FunctionName", "NonAsciiCharacters")
fun Generator<ULong>.`¯\_(ツ)_/¯`(): ULong = random.nextULong()
Loading

0 comments on commit eee363d

Please sign in to comment.