From ba97d04089faab51fc27219ea8a026d0fad8f55e Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Mon, 1 Jul 2019 18:14:47 +0200 Subject: [PATCH] Fix few minor code-smells --- .../kotlin/com/github/jcornaz/kwik/Runner.kt | 1 + .../jcornaz/kwik/generator/Collections.kt | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/core/src/commonMain/kotlin/com/github/jcornaz/kwik/Runner.kt b/core/src/commonMain/kotlin/com/github/jcornaz/kwik/Runner.kt index 1f23fa44..298a1771 100644 --- a/core/src/commonMain/kotlin/com/github/jcornaz/kwik/Runner.kt +++ b/core/src/commonMain/kotlin/com/github/jcornaz/kwik/Runner.kt @@ -34,6 +34,7 @@ fun forAll( while (attempts < iterations) { val argument = iterator.next() + @Suppress("SwallowedException") // SkipEvaluation is used to silently skip an evaluation val isSatisfied = try { context.property(argument).also { ++attempts } } catch (skip: SkipEvaluation) { diff --git a/core/src/commonMain/kotlin/com/github/jcornaz/kwik/generator/Collections.kt b/core/src/commonMain/kotlin/com/github/jcornaz/kwik/generator/Collections.kt index 2e2ba6e0..c47a0873 100644 --- a/core/src/commonMain/kotlin/com/github/jcornaz/kwik/generator/Collections.kt +++ b/core/src/commonMain/kotlin/com/github/jcornaz/kwik/generator/Collections.kt @@ -57,8 +57,7 @@ private class ListGenerator( } init { - require(minSize >= 0) { "Invalid size: $minSize" } - require(maxSize >= minSize) { "Invalid max size: $minSize" } + requireValidSizes(minSize, maxSize) } override fun randoms(seed: Long): Sequence> = sequence { @@ -124,8 +123,7 @@ private class SetGenerator( } init { - require(minSize >= 0) { "Invalid size: $minSize" } - require(maxSize >= minSize) { "Invalid max size: $minSize" } + requireValidSizes(minSize, maxSize) } override fun randoms(seed: Long): Sequence> = sequence { @@ -146,7 +144,8 @@ private class SetGenerator( ++extraAttempt } - if (set.size < minSize) throw Exception("Failed to create a set with the requested minimum of element") + if (set.size < minSize) + error("Failed to create a set with the requested minimum of element") yield(set) } @@ -216,8 +215,7 @@ private class MapGenerator( } init { - require(minSize >= 0) { "Invalid size: $minSize" } - require(maxSize >= minSize) { "Invalid max size: $minSize" } + requireValidSizes(minSize, maxSize) } override fun randoms(seed: Long): Sequence> = sequence { @@ -239,9 +237,15 @@ private class MapGenerator( ++extraAttempt } - if (map.size < minSize) throw Exception("Failed to create a set with the requested minimum of element") + if (map.size < minSize) + error("Failed to create a set with the requested minimum of element") yield(map) } } } + +private fun requireValidSizes(minSize: Int, maxSize: Int) { + require(minSize >= 0) { "Invalid min size: $minSize" } + require(maxSize >= minSize) { "Invalid max size: $minSize" } +}