• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

appmattus/kotlinfixture: Fixtures for Kotlin providing generated values for unit ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

appmattus/kotlinfixture

开源软件地址(OpenSource Url):

https://github.com/appmattus/kotlinfixture

开源编程语言(OpenSource Language):

Kotlin 99.4%

开源软件介绍(OpenSource Introduction):

KotlinFixture

Maven Central CI status Coverage status License

A tool to generate well-defined, but essentially random, input following the idea of constrained non-determinism.

Getting started

Include the following dependency in your build.gradle.kts file:

build.gradle.kts
testImplementation("com.appmattus.fixture:fixture:<latest-version>")

Simply create a fixture and invoke it with the type to be generated:

val fixture = kotlinFixture()

// Generate a list of strings
val aListOfStrings = fixture<List<String>>()

// Nulls are supported
val sometimesNull = fixture<Int?>()

// Create instances of classes
// Optional parameters will be randomly used or overridden
data class ADataClass(val value: String = "default")
val aClass = fixture<ADataClass>()

// Abstract classes will pick a sub-class at random
// This could be a Byte, Double, Long, Float, Int or Short
val anyNumber = fixture<Number>()

// Pick randomly from a list
val randomStringFromTheList = fixture(listOf("Cat", "Dog", "Horse"))
val anotherRandomIntFromAList = fixture(1..5)

You can also generate an infinite sequence of a type, which you can then filter:

val fixture = kotlinFixture()

val intSequence = fixture.asSequence<Int>()

// Standard Kotlin sequence functions can then be applied before using
// the sequence through an iterator for access to the next() function.

// For example, you can filter values
val oddIterator = intSequence.filter { it.absoluteValue.rem(2) == 1 }.iterator()
val oddNumber = oddIterator.next()
val anotherOddNumber = oddIterator.next()

// Or, ensure it returns only distinct values
enum class XYZ { X, Y, Z }
val enumIterator = fixture.asSequence<XYZ>().distinct().iterator()
val aDistinctValue = enumIterator.next()
val anotherDistinctValue = enumIterator.next()
⚠️

The sequence can hang indefinitely if the applied operators prevent the generation of new values. For example:

  • 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.

  • filter that can never be fulfilled e.g. filter { false }

Configuration options

Everything can be customised, see configuration options for more details.

Advanced engine customisation is also possible if the above options are not enough.

Kotest integration: property based testing

The library provides KotlinFixture powered property based testing for Kotest.

See Kotest integration for more details.

Java Faker integration: pretty data

Generate values with a closer match to real data using Java Faker.

See Java Faker integration for more details.

Generex integration: regex to random string

To generate a random string from a regular expression, look no further than the Generex integration.

See Generex integration for more details.

Please take a look at the feature comparison with related projects.

Contributing

Please fork this repository and contribute back using pull requests.

All contributions, large or small, major features, bug fixes, additional language translations, unit/integration tests are welcome.

License

License

Copyright 2021 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 https://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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap