|
| 1 | += KotlinFixture |
| 2 | +Appmattus Limited <info@appmattus.com> |
| 3 | +:toc: preamble |
| 4 | +:toc-title: Contents |
| 5 | +:homepage: https://github.com/appmattus/kotlinfixture |
| 6 | +ifdef::env-github[] |
| 7 | +:tip-caption: :bulb: |
| 8 | +:note-caption: :information_source: |
| 9 | +:important-caption: :heavy_exclamation_mark: |
| 10 | +:caution-caption: :fire: |
| 11 | +:warning-caption: :warning: |
| 12 | +endif::[] |
| 13 | +:link-appmattus: https://github.com/appmattus/kotlinfixture[KotlinFixture] |
| 14 | + |
| 15 | +https://bintray.com/appmattus/maven/fixture/_latestVersion[image:https://api.bintray.com/packages/appmattus/maven/fixture/images/download.svg[Download]] |
| 16 | +https://github.com/appmattus/kotlinfixture/actions[image:https://github.com/appmattus/kotlinfixture/workflows/CI/badge.svg[CI status]] |
| 17 | +https://codecov.io/gh/appmattus/kotlinfixture[image:https://codecov.io/gh/appmattus/kotlinfixture/branch/master/graph/badge.svg[Coverage status]] |
| 18 | +link:LICENSE.md[image:https://img.shields.io/badge/License-Apache%202.0-blue.svg[License]] |
| 19 | +
|
| 20 | +A tool to generate well-defined, but essentially random, input following the |
| 21 | +idea of constrained non-determinism. |
| 22 | +
|
| 23 | +== Getting started |
| 24 | +
|
| 25 | +Include the following dependency in your `build.gradle.kts` file: |
| 26 | +
|
| 27 | +[source,kotlin] |
| 28 | +._build.gradle.kts_ |
| 29 | +---- |
| 30 | +testImplementation("com.appmattus.fixture:fixture:<latest-version>") |
| 31 | +---- |
| 32 | + |
| 33 | +Simply create a fixture and invoke it with the type to be generated: |
| 34 | + |
| 35 | +[source,kotlin] |
| 36 | +---- |
| 37 | +val fixture = kotlinFixture() |
| 38 | +
|
| 39 | +// Generate a list of strings |
| 40 | +val aListOfStrings = fixture<List<String>>() |
| 41 | +
|
| 42 | +// Nulls are supported |
| 43 | +val sometimesNull = fixture<Int?>() |
| 44 | +
|
| 45 | +// Create instances of classes |
| 46 | +// Optional parameters will be randomly used or overridden |
| 47 | +data class ADataClass(val value: String = "default") |
| 48 | +val aClass = fixture<ADataClass>() |
| 49 | +
|
| 50 | +// Abstract classes will pick a sub-class at random |
| 51 | +// This could be a Byte, Double, Long, Float, Int or Short |
| 52 | +val anyNumber = fixture<Number>() |
| 53 | +
|
| 54 | +// Pick randomly from a list |
| 55 | +val randomStringFromTheList = fixture(listOf("Cat", "Dog", "Horse")) |
| 56 | +val anotherRandomIntFromAList = fixture(1..5) |
| 57 | +---- |
| 58 | + |
| 59 | +You can also generate an infinite sequence of a type, which you can then |
| 60 | +filter: |
| 61 | + |
| 62 | +[source,kotlin] |
| 63 | +---- |
| 64 | +val fixture = kotlinFixture() |
| 65 | +
|
| 66 | +val intSequence = fixture.asSequence<Int>() |
| 67 | +
|
| 68 | +// Standard Kotlin sequence functions can then be applied before using |
| 69 | +// the sequence through an iterator for access to the next() function. |
| 70 | +
|
| 71 | +// For example, you can filter values |
| 72 | +val oddIterator = intSequence.filter { it.absoluteValue.rem(2) == 1 }.iterator() |
| 73 | +val oddNumber = oddIterator.next() |
| 74 | +val anotherOddNumber = oddIterator.next() |
| 75 | +
|
| 76 | +// Or, ensure it returns only distinct values |
| 77 | +enum class XYZ { X, Y, Z } |
| 78 | +val enumIterator = fixture.asSequence<XYZ>().distinct().iterator() |
| 79 | +val aDistinctValue = enumIterator.next() |
| 80 | +val anotherDistinctValue = enumIterator.next() |
| 81 | +---- |
| 82 | + |
| 83 | +[WARNING] |
| 84 | +==== |
| 85 | +The sequence can hang indefinitely if the applied operators prevent the generation of new values. For example: |
| 86 | +
|
| 87 | +* `distinct` will hang if we exhaust all available values. A good practice is to add a `take(count)` which will throw a `NoSuchElementException` if we try to generate more values. |
| 88 | +* `filter` that can never be fulfilled e.g. `filter { false }` |
| 89 | +==== |
| 90 | + |
| 91 | +== Configuration options |
| 92 | + |
| 93 | +Everything can be customised, see link:fixture/configuration-options.adoc[configuration options] for more details. |
| 94 | + |
| 95 | +== Kotest integration: property based testing |
| 96 | + |
| 97 | +The library provides {link-appmattus} powered property based testing for https://github.com/kotest/kotest/[Kotest]. |
| 98 | + |
| 99 | +See link:fixture-kotest/README.adoc[Kotest integration] for more details. |
| 100 | + |
| 101 | +== Java Faker integration: pretty data |
| 102 | + |
| 103 | +Generate values with a closer match to real data using http://dius.github.io/java-faker/[Java Faker]. |
| 104 | + |
| 105 | +See link:fixture-javafaker/README.adoc[Java Faker integration] for more details. |
| 106 | + |
| 107 | +== Generex integration: regex to random string |
| 108 | + |
| 109 | +To generate a random string from a Regex, look no further than the Generex integration. |
| 110 | + |
| 111 | +See link:fixture-generex/README.adoc[Generex integration] for more details. |
| 112 | + |
| 113 | +== Related projects |
| 114 | + |
| 115 | +* Marcello Galhardo's https://github.com/marcellogalhardo/kotlin-fixture[Kotlin.Fixture]. |
| 116 | +* FlexTrade's https://github.com/FlexTradeUKLtd/kfixture[KFixture] wrapper for https://github.com/FlexTradeUKLtd/jfixture[JFixture]. |
| 117 | +* Jeasy's https://github.com/j-easy/easy-random[Easy Random], |
| 118 | + |
| 119 | +Please take a look at the feature link:fixture/comparison.adoc[comparison with related projects]. |
| 120 | + |
| 121 | +== Contributing |
| 122 | + |
| 123 | +Please fork this repository and contribute back using |
| 124 | +https://github.com/appmattus/kotlinfixture/pulls[pull requests]. |
| 125 | + |
| 126 | +All contributions, large or small, major features, bug fixes, additional |
| 127 | +language translations, unit/integration tests are welcome. |
| 128 | + |
| 129 | +== License |
| 130 | + |
| 131 | +link:LICENSE.md[image:https://img.shields.io/badge/License-Apache%202.0-blue.svg[License]] |
| 132 | + |
| 133 | +Copyright 2020 Appmattus Limited |
| 134 | + |
| 135 | +Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 136 | +not use this file except in compliance with the License. You may obtain |
| 137 | +a copy of the License at |
| 138 | +https://www.apache.org/licenses/LICENSE-2.0[https://www.apache.org/licenses/LICENSE-2.0]. |
| 139 | + |
| 140 | +Unless required by applicable law or agreed to in writing, software |
| 141 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 142 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 143 | +See the License for the specific language governing permissions and |
| 144 | +limitations under the License. |
0 commit comments