Skip to content

Commit

Permalink
Use kotest APIs more.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimY4 committed Sep 6, 2023
1 parent 9703d9d commit c13b963
Showing 1 changed file with 62 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,84 +16,91 @@

package com.github.simy4.coregex.kotest;

import static io.kotest.property.PropertyTest1Kt.forAll;

import io.kotest.property.Arb;
import io.kotest.property.RandomSource;
import io.kotest.property.arbitrary.CollectionsKt;
import java.net.InetAddress;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import kotlin.Unit;
import kotlin.coroutines.EmptyCoroutineContext;
import kotlin.sequences.SequencesKt;
import kotlinx.coroutines.BuildersKt;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class CoregexArbitraryTest extends Assertions {
@Test
void shouldGenerateMatchingUUIDString() {
var randomSource = RandomSource.Companion.seeded(ThreadLocalRandom.current().nextLong());
assertTrue(
SequencesKt.all(
SequencesKt.take(
CoregexArbitrary.of(
"[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}")
.samples(randomSource),
100),
uuid -> uuid.getValue().equals(UUID.fromString(uuid.getValue()).toString())));
void shouldGenerateMatchingUUIDString() throws InterruptedException {
BuildersKt.runBlocking(
EmptyCoroutineContext.INSTANCE,
(scope, c1) -> {
forAll(
CoregexArbitrary.of(
"[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}"),
(context, uuid, c2) -> uuid.equals(UUID.fromString(uuid).toString()),
c1);
return Unit.INSTANCE;
});
}

@Test
void shouldGenerateMatchingIPv4String() {
var randomSource = RandomSource.Companion.seeded(ThreadLocalRandom.current().nextLong());
SequencesKt.forEach(
SequencesKt.take(
CoregexArbitrary.of(
"((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])")
.samples(randomSource),
100),
ipv4 -> {
String[] expected = ipv4.getValue().split("\\.");
String[] actual =
assertDoesNotThrow(() -> InetAddress.getByName(ipv4.getValue()))
.getHostAddress()
.split("\\.");
assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; i++) {
assertEquals(Integer.parseInt(expected[i]), Integer.parseInt(actual[i]));
}
void shouldGenerateMatchingIPv4String() throws InterruptedException {
BuildersKt.runBlocking(
EmptyCoroutineContext.INSTANCE,
(scope, c1) -> {
forAll(
CoregexArbitrary.of(
"((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])"),
(context, ipv4, c2) -> {
var expected = ipv4.split("\\.");
var actual =
assertDoesNotThrow(() -> InetAddress.getByName(ipv4))
.getHostAddress()
.split("\\.");
return expected.length == actual.length
&& SequencesKt.all(
SequencesKt.zip(
SequencesKt.sequenceOf(expected), SequencesKt.sequenceOf(actual)),
pair ->
Integer.parseInt(pair.getFirst())
== Integer.parseInt(pair.getSecond()));
},
c1);
return Unit.INSTANCE;
});
}

@Test
void shouldGenerateMatchingIsoDateString() {
void shouldGenerateMatchingIsoDateString() throws InterruptedException {
var formatter = DateTimeFormatter.ISO_INSTANT;
var randomSource = RandomSource.Companion.seeded(ThreadLocalRandom.current().nextLong());
assertTrue(
SequencesKt.all(
SequencesKt.take(
CoregexArbitrary.of(
"[12]\\d{3}-(?:0[1-9]|1[012])-(?:0[1-9]|1\\d|2[0-8])T(?:1\\d|2[0-3]):[0-5]\\d:[0-5]\\d(\\.\\d{2}[1-9])?Z")
.samples(randomSource),
100),
iso8601Date ->
iso8601Date
.getValue()
.equals(formatter.format(formatter.parse(iso8601Date.getValue())))));
BuildersKt.runBlocking(
EmptyCoroutineContext.INSTANCE,
(scope, c1) -> {
forAll(
CoregexArbitrary.of(
"[12]\\d{3}-(?:0[1-9]|1[012])-(?:0[1-9]|1\\d|2[0-8])T(?:1\\d|2[0-3]):[0-5]\\d:[0-5]\\d(\\.\\d{2}[1-9])?Z"),
(context, iso8601Date, c2) ->
iso8601Date.equals(formatter.format(formatter.parse(iso8601Date))),
c1);
return Unit.INSTANCE;
});
}

@Test
void shouldGenerateUniqueStrings() {
var randomSource = RandomSource.Companion.seeded(ThreadLocalRandom.current().nextLong());
assertTrue(
SequencesKt.all(
SequencesKt.take(
CollectionsKt.list(Arb.Companion, CoregexArbitrary.of("[a-zA-Z0-9]{32,}"))
.samples(randomSource),
100),
strings ->
strings.getValue().stream()
.allMatch(
s -> s.length() >= 32 && s.chars().allMatch(Character::isLetterOrDigit))));
void shouldGenerateUniqueStrings() throws InterruptedException {
BuildersKt.runBlocking(
EmptyCoroutineContext.INSTANCE,
(scope, c1) -> {
forAll(
CollectionsKt.list(Arb.Companion, CoregexArbitrary.of("[a-zA-Z0-9]{32,}")),
(context, strings, c2) ->
strings.stream()
.allMatch(
s -> s.length() >= 32 && s.chars().allMatch(Character::isLetterOrDigit)),
c1);
return Unit.INSTANCE;
});
}
}

0 comments on commit c13b963

Please sign in to comment.