From eee363d388d36e04728f233d3e69c258dd943aa0 Mon Sep 17 00:00:00 2001 From: Matthew Dolan Date: Tue, 11 Feb 2020 09:41:24 +0000 Subject: [PATCH] Add ability to create new fixtures from existing ones modifying config (#37) --- .idea/jarRepositories.xml | 35 ++++ README.md | 23 ++- build.gradle.kts | 8 +- fixture-android-tests/build.gradle.kts | 4 +- fixture-kotlintest/build.gradle.kts | 4 +- fixture/build.gradle.kts | 10 +- .../appmattus/kotlinfixture/KotlinFixture.kt | 6 +- .../kotlinfixture/config/Generator.kt | 5 +- .../kotlinfixture/config/GeneratorBoolean.kt | 20 ++ .../kotlinfixture/config/GeneratorDouble.kt | 20 ++ .../kotlinfixture/config/GeneratorFloat.kt | 20 ++ .../kotlinfixture/config/GeneratorInt.kt | 20 ++ .../kotlinfixture/config/GeneratorLong.kt | 20 ++ .../kotlinfixture/config/GeneratorUInt.kt | 22 ++ .../kotlinfixture/config/GeneratorULong.kt | 22 ++ .../appmattus/kotlinfixture/FixtureTest.kt | 96 ++++++++- .../config/GeneratorShrugTest.kt | 195 ++++++++++++++++++ .../kotlinfixture/config/GeneratorTest.kt | 53 +++++ 18 files changed, 566 insertions(+), 17 deletions(-) create mode 100644 .idea/jarRepositories.xml create mode 100644 fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorBoolean.kt create mode 100644 fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorDouble.kt create mode 100644 fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorFloat.kt create mode 100644 fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorInt.kt create mode 100644 fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorLong.kt create mode 100644 fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorUInt.kt create mode 100644 fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorULong.kt create mode 100644 fixture/src/test/kotlin/com/appmattus/kotlinfixture/config/GeneratorShrugTest.kt create mode 100644 fixture/src/test/kotlin/com/appmattus/kotlinfixture/config/GeneratorTest.kt diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 00000000..247f71d9 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 721e2d6e..8d45b376 100644 --- a/README.md +++ b/README.md @@ -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 { 3 } +} + +val fixture = baseFixture.new { + factory { 100L } +} + +// Prints 100 +println(fixture()) +// Prints 3 +println(fixture()) +``` + #### repeatCount 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: ```kotlin val fixture = kotlinFixture { + // Generate using ranges (and iterables) + factory { range(1..10) } + // Generate between two dates factory { between(startDate, endDate) } } @@ -263,7 +284,7 @@ fixture { classOverride(NeverOptionalStrategy) // You can override the strategy for a property of a class - propertyOverride(AnotherObject:property, RandomlyOptionalStrategy) + propertyOverride(AnotherObject::property, RandomlyOptionalStrategy) } } ``` diff --git a/build.gradle.kts b/build.gradle.kts index f41dc99b..8205c491 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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. @@ -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" } @@ -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") } } @@ -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 { diff --git a/fixture-android-tests/build.gradle.kts b/fixture-android-tests/build.gradle.kts index 06716216..fbdb86c7 100644 --- a/fixture-android-tests/build.gradle.kts +++ b/fixture-android-tests/build.gradle.kts @@ -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. @@ -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") diff --git a/fixture-kotlintest/build.gradle.kts b/fixture-kotlintest/build.gradle.kts index 003a1d40..42056ee9 100644 --- a/fixture-kotlintest/build.gradle.kts +++ b/fixture-kotlintest/build.gradle.kts @@ -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. @@ -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") diff --git a/fixture/build.gradle.kts b/fixture/build.gradle.kts index 64f19e61..9c7d72e3 100644 --- a/fixture/build.gradle.kts +++ b/fixture/build.gradle.kts @@ -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. @@ -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") diff --git a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/KotlinFixture.kt b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/KotlinFixture.kt index 3cedfdd3..beae8b22 100644 --- a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/KotlinFixture.kt +++ b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/KotlinFixture.kt @@ -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. @@ -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()) diff --git a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/Generator.kt b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/Generator.kt index 34f6a82c..3fd9c0fa 100644 --- a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/Generator.kt +++ b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/Generator.kt @@ -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. @@ -25,3 +25,6 @@ interface Generator { } internal typealias GeneratorFun = Generator.() -> Any? + +fun Generator.range(range: Iterable) = + range.shuffled(random).firstOrNull() ?: throw NoSuchElementException("Range is empty") diff --git a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorBoolean.kt b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorBoolean.kt new file mode 100644 index 00000000..1a1fc7f0 --- /dev/null +++ b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorBoolean.kt @@ -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 = random.nextBoolean() diff --git a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorDouble.kt b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorDouble.kt new file mode 100644 index 00000000..b36fde36 --- /dev/null +++ b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorDouble.kt @@ -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 = random.nextDouble() diff --git a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorFloat.kt b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorFloat.kt new file mode 100644 index 00000000..e2bff28b --- /dev/null +++ b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorFloat.kt @@ -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 = random.nextFloat() diff --git a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorInt.kt b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorInt.kt new file mode 100644 index 00000000..e1998d3a --- /dev/null +++ b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorInt.kt @@ -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 = random.nextInt() diff --git a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorLong.kt b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorLong.kt new file mode 100644 index 00000000..89b2350b --- /dev/null +++ b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorLong.kt @@ -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 = random.nextLong() diff --git a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorUInt.kt b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorUInt.kt new file mode 100644 index 00000000..b6fbeef4 --- /dev/null +++ b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorUInt.kt @@ -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 = random.nextUInt() diff --git a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorULong.kt b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorULong.kt new file mode 100644 index 00000000..619ee636 --- /dev/null +++ b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/config/GeneratorULong.kt @@ -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 = random.nextULong() diff --git a/fixture/src/test/kotlin/com/appmattus/kotlinfixture/FixtureTest.kt b/fixture/src/test/kotlin/com/appmattus/kotlinfixture/FixtureTest.kt index e1c6871d..66bc5d89 100644 --- a/fixture/src/test/kotlin/com/appmattus/kotlinfixture/FixtureTest.kt +++ b/fixture/src/test/kotlin/com/appmattus/kotlinfixture/FixtureTest.kt @@ -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. @@ -165,6 +165,21 @@ class FixtureTest { } } + @Test + fun `subclass can be overridden in new when already overridden in initialisation`() { + val baseFixture = kotlinFixture { + subType() + } + + repeat(100) { + val fixture = baseFixture.new { + subType() + } + val result = fixture() + assertEquals(SubclassB::class, result::class) + } + } + @Test fun `can override instance in initialisation`() { val fixture = kotlinFixture { @@ -222,6 +237,20 @@ class FixtureTest { assertEquals(30, result) } + @Test + fun `can override instance in new when overridden in initialisation`() { + val baseFixture = kotlinFixture { + factory { 10 } + } + + val fixture = baseFixture.new { + factory { 30 } + } + + val result = fixture() + assertEquals(30, result) + } + data class KotlinClass(val readOnly: String, private var private: String) { var member: String? = null val alsoReadOnly: String? = null @@ -260,6 +289,19 @@ class FixtureTest { assertEquals("b", instance.readOnly) } + @Test + fun `constructor property can be overridden in new when already overridden in initialisation`() { + val baseFixture = kotlinFixture { + property(KotlinClass::readOnly) { "a" } + } + val fixture = baseFixture.new { + property(KotlinClass::readOnly) { "b" } + } + + val instance = fixture() + assertEquals("b", instance.readOnly) + } + @Test fun `private constructor property can be set in fixture initialisation`() { val fixture = kotlinFixture { @@ -292,6 +334,19 @@ class FixtureTest { assertEquals("b", instance.getPrivate()) } + @Test + fun `private constructor property can be overridden in new when already overridden`() { + val baseFixture = kotlinFixture { + property("private") { "a" } + } + val fixture = baseFixture.new { + property("private") { "b" } + } + + val instance = fixture() + assertEquals("b", instance.getPrivate()) + } + @Test fun `member property can be set in fixture initialisation`() { val fixture = kotlinFixture { @@ -324,6 +379,19 @@ class FixtureTest { assertEquals("b", instance.member) } + @Test + fun `member property can be overridden in new when already overridden in initialisation`() { + val baseFixture = kotlinFixture { + property(KotlinClass::member) { "a" } + } + val fixture = baseFixture.new { + property(KotlinClass::member) { "b" } + } + + val instance = fixture() + assertEquals("b", instance.member) + } + @Test fun `read only property cannot be set in fixture initialisation`() { assertFailsWith { @@ -376,6 +444,19 @@ class FixtureTest { assertEquals("b", instance.constructor) } + @Test + fun `java constructor property can be overridden in new when already overridden in initialisation`() { + val baseFixture = kotlinFixture { + property("arg0") { "a" } + } + val fixture = baseFixture.new { + property("arg0") { "b" } + } + + val instance = fixture() + assertEquals("b", instance.constructor) + } + @Test fun `java member property can be set in fixture initialisation`() { val fixture = kotlinFixture { @@ -407,4 +488,17 @@ class FixtureTest { } assertEquals("b", instance.mutable) } + + @Test + fun `java member property can be overridden in new when already overridden in initialisation`() { + val baseFixture = kotlinFixture { + property(FixtureTestJavaClass::setMutable) { "a" } + } + val fixture = baseFixture.new { + property(FixtureTestJavaClass::setMutable) { "b" } + } + + val instance = fixture() + assertEquals("b", instance.mutable) + } } diff --git a/fixture/src/test/kotlin/com/appmattus/kotlinfixture/config/GeneratorShrugTest.kt b/fixture/src/test/kotlin/com/appmattus/kotlinfixture/config/GeneratorShrugTest.kt new file mode 100644 index 00000000..2e3e3d6d --- /dev/null +++ b/fixture/src/test/kotlin/com/appmattus/kotlinfixture/config/GeneratorShrugTest.kt @@ -0,0 +1,195 @@ +/* + * 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 com.appmattus.kotlinfixture.assertIsRandom +import com.appmattus.kotlinfixture.kotlinFixture +import org.junit.Test +import kotlin.random.Random +import kotlin.test.assertEquals + +@Suppress("EXPERIMENTAL_API_USAGE") +class GeneratorShrugTest { + + // Boolean, Double, Float, Int, Long, UInt, ULong + + data class TestClass( + val boolean: Boolean, + val double: Double, + val float: Float, + val int: Int, + val long: Long, + val uInt: UInt, + val uLong: ULong + ) + + val fixture = kotlinFixture { + factory { false } + factory { 0.0 } + factory { 0f } + factory { 0 } + factory { 0L } + factory { 0.toUInt() } + factory { 0.toULong() } + } + + @Test + fun `Default generators always return the same content`() { + val original = fixture() + + repeat(100) { + assertEquals(original.toString(), fixture().toString()) + } + } + + @Test + fun `Random values returned for Boolean shrug`() { + assertIsRandom { + fixture { + factory { `¯\_(ツ)_/¯`() } + }.boolean + } + } + + private fun ConfigurationBuilder.seededRandom() { + random = Random(0) + } + + @Test + fun `Boolean shrug uses seeded random`() { + val fixture = fixture.new { + factory { `¯\_(ツ)_/¯`() } + } + + @Suppress("RemoveExplicitTypeArguments") + assertEquals(fixture { seededRandom() }, fixture { seededRandom() }) + } + + @Test + fun `Random values returned for Double shrug`() { + assertIsRandom { + fixture { + factory { `¯\_(ツ)_/¯`() } + }.double + } + } + + @Test + fun `Double shrug uses seeded random`() { + val fixture = fixture.new { + factory { `¯\_(ツ)_/¯`() } + } + + @Suppress("RemoveExplicitTypeArguments") + assertEquals(fixture { seededRandom() }, fixture { seededRandom() }) + } + + @Test + fun `Random values returned for Float shrug`() { + assertIsRandom { + fixture { + factory { `¯\_(ツ)_/¯`() } + }.float + } + } + + @Test + fun `Float shrug uses seeded random`() { + val fixture = fixture.new { + factory { `¯\_(ツ)_/¯`() } + } + + @Suppress("RemoveExplicitTypeArguments") + assertEquals(fixture { seededRandom() }, fixture { seededRandom() }) + } + + @Test + fun `Random values returned for Int shrug`() { + assertIsRandom { + fixture { + factory { `¯\_(ツ)_/¯`() } + }.int + } + } + + @Test + fun `Int shrug uses seeded random`() { + val fixture = fixture.new { + factory { `¯\_(ツ)_/¯`() } + } + + @Suppress("RemoveExplicitTypeArguments") + assertEquals(fixture { seededRandom() }, fixture { seededRandom() }) + } + + @Test + fun `Random values returned for Long shrug`() { + assertIsRandom { + fixture { + factory { `¯\_(ツ)_/¯`() } + }.long + } + } + + @Test + fun `Long shrug uses seeded random`() { + val fixture = fixture.new { + factory { `¯\_(ツ)_/¯`() } + } + + @Suppress("RemoveExplicitTypeArguments") + assertEquals(fixture { seededRandom() }, fixture { seededRandom() }) + } + + @Test + fun `Random values returned for UInt shrug`() { + assertIsRandom { + fixture { + factory { `¯\_(ツ)_/¯`() } + }.uInt + } + } + + @Test + fun `UInt shrug uses seeded random`() { + val fixture = fixture.new { + factory { `¯\_(ツ)_/¯`() } + } + + @Suppress("RemoveExplicitTypeArguments") + assertEquals(fixture { seededRandom() }, fixture { seededRandom() }) + } + + @Test + fun `Random values returned for ULong shrug`() { + assertIsRandom { + fixture { + factory { `¯\_(ツ)_/¯`() } + }.uLong + } + } + + @Test + fun `ULong shrug uses seeded random`() { + val fixture = fixture.new { + factory { `¯\_(ツ)_/¯`() } + } + + @Suppress("RemoveExplicitTypeArguments") + assertEquals(fixture { seededRandom() }, fixture { seededRandom() }) + } +} diff --git a/fixture/src/test/kotlin/com/appmattus/kotlinfixture/config/GeneratorTest.kt b/fixture/src/test/kotlin/com/appmattus/kotlinfixture/config/GeneratorTest.kt new file mode 100644 index 00000000..117b4f3e --- /dev/null +++ b/fixture/src/test/kotlin/com/appmattus/kotlinfixture/config/GeneratorTest.kt @@ -0,0 +1,53 @@ +/* + * 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 com.appmattus.kotlinfixture.Fixture +import com.appmattus.kotlinfixture.assertIsRandom +import org.junit.Test +import kotlin.random.Random +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith + +class GeneratorTest { + + private class IntGenerator(override val random: Random = Random) : Generator { + override val fixture = Fixture(Configuration()) + } + + @Test + fun `Empty range throws exception`() { + assertFailsWith { + IntGenerator().range(emptyList()) + } + } + + @Test + fun `Random values returned from range`() { + assertIsRandom { + IntGenerator().range(1..100) + } + } + + @Test + fun `Range uses seeded random`() { + val value1 = IntGenerator(random = Random(0)).range(1..100) + val value2 = IntGenerator(random = Random(0)).range(1..100) + + assertEquals(value1, value2) + } +}