Skip to content

Commit

Permalink
Merge branch 'main' into changes_for_plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrosario committed Jul 1, 2024
2 parents d399dec + 906d33e commit f228a0c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ data class NumberPattern(
if (sampleData.toStringLiteral().length > maxLength)
return mismatchResult("number with maxLength $maxLength", sampleData, resolver.mismatchMessages)

val sampleNumber = BigDecimal(sampleData.number.toDouble())
val sampleNumber = BigDecimal(sampleData.number.toString())

val minOp = if (exclusiveMinimum) ">" else ">="
if (!eval(sampleNumber, minOp, minimum))
Expand All @@ -85,7 +85,8 @@ data class NumberPattern(
} else
minimum
val max = if (maximum == HIGHEST_DECIMAL) largestValue else maximum
return NumberValue(SecureRandom().nextDouble(min.toDouble(), max.toDouble()))
if (isDoubleFormat) return NumberValue(SecureRandom().nextDouble(min.toDouble(), max.toDouble()))
return NumberValue(SecureRandom().nextInt(min.toInt(), max.toInt()))
}

private fun randomNumber(minLength: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,34 @@ internal class NumberPatternTest {
}
}

@Test
fun `should generate an Int within min and max bounds if isDoubleFormat is false and the min and max constraints are set`() {
val generatedValues = (0..5).map {
NumberPattern(
minimum = BigDecimal(0),
maximum = BigDecimal(10),
isDoubleFormat = false
).generate(Resolver())
}
assertThat(generatedValues).allSatisfy {
it as NumberValue
assertThat(it.number is Int).isTrue()
assertThat(it.number.toInt()).isGreaterThanOrEqualTo(0)
assertThat(it.number.toInt()).isLessThanOrEqualTo(10)
}
}

@Test
fun `should generate an Int if isDoubleFormat is false and the min and max constraints are set`() {
val generatedNumber = NumberPattern(
minimum = BigDecimal(0),
maximum = BigDecimal(10),
isDoubleFormat = false
).generate(Resolver()) as NumberValue

assertThat(generatedNumber.number is Int).isTrue()
}

@Test
fun `should not match when number is shorter than minLength`() {
val result = NumberPattern(minLength = 4).matches(NumberValue(123), Resolver())
Expand All @@ -216,6 +244,14 @@ internal class NumberPatternTest {
assertThat(generated).isEqualTo(NumberValue(10))
}

@Test
fun `test`() {
val pointZeroOne = ".01"
val minimumPointZeroOne = NumberPattern(minimum = BigDecimal(pointZeroOne))
val result = minimumPointZeroOne.matches(NumberValue(convertToNumber(pointZeroOne)), Resolver())
assertThat(result.isSuccess()).withFailMessage(result.reportString()).isTrue()
}

@Test
@Tag(GENERATION)
fun `negative values should be generated`() {
Expand Down

0 comments on commit f228a0c

Please sign in to comment.