-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
3,026 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ out/ | |
build/ | ||
logs/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
# kotlin-language-server | ||
kls_database.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# `kotlin-faker-blns` | ||
|
||
[![Maven Central](https://img.shields.io/maven-central/v/io.github.serpro69/kotlin-faker-blns?style=for-the-badge)](https://search.maven.org/artifact/io.github.serpro69/kotlin-faker-blns) | ||
[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/io.github.serpro69/kotlin-faker-blns?label=snapshot-version&server=https%3A%2F%2Foss.sonatype.org&style=for-the-badge&color=yellow)](#downloading) | ||
|
||
`kotlin-faker-blns` module provides extensions for the [big-list-of-naughty-strings](https://github.com/minimaxir/big-list-of-naughty-strings), a list of strings which have a high probability of causing issues when used as user-input data, and can therefore be quite useful in testing. | ||
|
||
## Usage | ||
|
||
Documentation for this extension is available at [serpro69.github.io/kotlin-faker/](https://serpro69.github.io/kotlin-faker/extensions/blns-extension). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
plugins { | ||
`faker-ext-conventions` | ||
} | ||
|
||
dependencies { | ||
compileOnly(projects.core) | ||
implementation(libs.bundles.jackson) | ||
testImplementation(projects.core) // needed for tests since we have compileOnly dependency | ||
testImplementation(libs.bundles.test.kotest) | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
dependsOn(":core:shadowJar") | ||
} |
77 changes: 77 additions & 0 deletions
77
extension/blns/src/main/kotlin/io/github/serpro69/kfaker/blns/Blns.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.github.serpro69.kfaker.blns | ||
|
||
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef | ||
import io.github.serpro69.kfaker.AbstractFaker | ||
import io.github.serpro69.kfaker.Faker | ||
import io.github.serpro69.kfaker.FakerConfig | ||
import io.github.serpro69.kfaker.FakerDsl | ||
import io.github.serpro69.kfaker.fakerConfig | ||
import io.github.serpro69.kfaker.RandomService | ||
|
||
/** | ||
* A kotlin-faker extension that helps to use | ||
* [The Big List of Naughty Strings](https://github.com/minimaxir/big-list-of-naughty-strings) | ||
* for various test-related inputs. | ||
*/ | ||
@Suppress("unused") | ||
class Blns @JvmOverloads constructor(config: FakerConfig = fakerConfig { }) : AbstractFaker(config) { | ||
private val get: (filename: String) -> List<String> = { | ||
val inStr = requireNotNull(javaClass.classLoader.getResourceAsStream(it)) | ||
Mapper.readValue(inStr, jacksonTypeRef()) | ||
} | ||
|
||
/** | ||
* @property all a list of all strings. | ||
*/ | ||
val all: List<String> by lazy { get("blns.json") } | ||
|
||
/** | ||
* @property allBase64 a list of all base64-encoded strings. | ||
*/ | ||
val allBase64: List<String> by lazy { get("blns.base64.json") } | ||
|
||
/** | ||
* Returns a random string of [all] strings (or [allBase64] strings if [base64] is `true`) | ||
*/ | ||
fun random(base64: Boolean = false): String = | ||
if (base64) randomService.randomValue(allBase64) else randomService.randomValue(all) | ||
|
||
/** | ||
* Returns a portion of [all] strings (or [allBase64] strings if [base64] is `true`) | ||
* with pseudo-randomly generated `fromIndex` and (possibly) `toIndex` values. | ||
* | ||
* @param size the desired size of the resulting list. | ||
* If `size <= 0` then `toIndex` will also be randomly-generated. | ||
*/ | ||
fun sublist(size: Int, base64: Boolean = false): List<String> = | ||
if (base64) randomService.randomSublist(allBase64, size) else randomService.randomSublist(all, size) | ||
|
||
/** | ||
* Returns a portion of [all] strings (or [allBase64] strings if [base64] is `true`) | ||
* with pseudo-randomly generated `fromIndex` and (possibly) `toIndex` values. | ||
* | ||
* @param sizeRange the desired size range of the resulting list. | ||
* The `size` of the returned list is the result of calling [RandomService.nextInt] with the given [sizeRange]. | ||
* IF `size <= 0` then `toIndex` will also be randomly-generated. | ||
*/ | ||
fun sublist(sizeRange: IntRange, base64: Boolean = false): List<String> = | ||
if (base64) randomService.randomSublist(allBase64, sizeRange) else randomService.randomSublist(all, sizeRange) | ||
|
||
@FakerDsl | ||
/** | ||
* DSL builder for creating instances of [Blns] | ||
*/ | ||
class Builder internal constructor() : AbstractFaker.Builder<Blns>() { | ||
|
||
/** | ||
* Builds an instance of [Faker] with this [config]. | ||
*/ | ||
override fun build(): Blns = Blns(config) | ||
} | ||
} | ||
|
||
/** | ||
* Applies the [block] function to [Blns.Builder] | ||
* and returns as an instance of [Blns] from that builder. | ||
*/ | ||
fun blns(block: Blns.Builder.() -> Unit): Blns = Blns.Builder().apply(block).build() |
16 changes: 16 additions & 0 deletions
16
extension/blns/src/main/kotlin/io/github/serpro69/kfaker/blns/Mapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.github.serpro69.kfaker.blns | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.KotlinModule | ||
import java.io.InputStream | ||
|
||
internal object Mapper { | ||
private val mapper = ObjectMapper() | ||
|
||
init { | ||
mapper.registerModule(KotlinModule.Builder().build()) | ||
} | ||
|
||
fun <T> readValue(inputStream: InputStream, typeRef: TypeReference<T>): T = mapper.readValue(inputStream, typeRef) | ||
} |
Oops, something went wrong.