Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate an elaborate set of negative tests for ListPattern #1337

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion core/src/main/kotlin/io/specmatic/core/pattern/ListPattern.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,21 @@ data class ListPattern(override val pattern: Pattern, override val typeAlias: St
}
}

override fun negativeBasedOn(row: Row, resolver: Resolver, config: NegativePatternConfiguration): Sequence<ReturnValue<Pattern>> = sequenceOf(HasValue(NullPattern))
override fun negativeBasedOn(row: Row, resolver: Resolver, config: NegativePatternConfiguration): Sequence<ReturnValue<Pattern>> {
return pattern.negativeBasedOn(row, resolver, config)
.filterIsInstance<HasValue<Pattern>>()
.filter {
it.value !is NullPattern
}
.flatMap { negativePatternValue ->
listOf(
negativePatternValue,
negativePatternValue.ifValue { pattern ->
ListPattern(pattern) as Pattern
}
)
}.plus(HasValue(NullPattern))
}

override fun parse(value: String, resolver: Resolver): Value = parsedJSONArray(value, resolver.mismatchMessages)

Expand Down
3 changes: 3 additions & 0 deletions core/src/main/kotlin/io/specmatic/core/pattern/Row.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ data class Row(
if(requestBodyJSONExample == null)
return this

if(requestBodyJSONExample.jsonObject is JSONArrayValue)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joelrosario , require your help here. For now we have introduced this condition to make a test pass. We are not much sure if this is right. We'll discuss.

return withNoJSONObjectExample()

if(requestBodyJSONExample.jsonObject !is JSONObjectValue)
throw ContractException("Example provided is a JSON array, which can't contain key $key")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ class DefaultValuesInOpenapiSpecification {
})

assertThat(testTypes).containsExactlyInAnyOrder(
"salary is present",
"salary is present",
"name mutated to null",
"name mutated to number",
"name mutated to boolean",
Expand All @@ -177,7 +179,15 @@ class DefaultValuesInOpenapiSpecification {
"age mutated to string",
"salary mutated to boolean",
"salary mutated to string",
"salary_history mutated to boolean",
"salary_history[item] mutated to boolean",
"salary_history mutated to string",
"salary_history[item] mutated to string",
"salary_history mutated to null",
"years_employed mutated to boolean",
"years_employed[item] mutated to boolean",
"years_employed mutated to string",
"years_employed[item] mutated to string",
"years_employed mutated to null",
"name mutated to null",
"name mutated to number",
Expand All @@ -187,9 +197,11 @@ class DefaultValuesInOpenapiSpecification {
"age mutated to string",
"salary mutated to boolean",
"salary mutated to string",
"years_employed mutated to null",
"salary is present",
"salary is present"
"years_employed mutated to boolean",
"years_employed[item] mutated to boolean",
"years_employed mutated to string",
"years_employed[item] mutated to string",
"years_employed mutated to null"
)
assertThat(results.results).hasSize(testTypes.size)
}
Expand Down
7 changes: 4 additions & 3 deletions core/src/test/kotlin/integration_tests/ResiliencyTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ class GenerativeTests {
components:
schemas:
Address:
type: object
properties:
street:
type: "string"
Expand All @@ -1074,7 +1075,7 @@ class GenerativeTests {

val requestBodiesSeen = mutableListOf<Value>()

val results = specification.enableGenerativeTesting().executeTests(object : TestExecutor {
val results = specification.enableGenerativeTesting(onlyPositive = true).executeTests(object : TestExecutor {
override fun execute(request: HttpRequest): HttpResponse {
println(request.body)
requestBodiesSeen.add(request.body)
Expand All @@ -1086,7 +1087,7 @@ class GenerativeTests {
})
println(results.report())

assertThat(requestBodiesSeen).hasSize(3)
assertThat(requestBodiesSeen).hasSize(2)
}

@Test
Expand Down Expand Up @@ -1153,7 +1154,7 @@ class GenerativeTests {
}
})

assertThat(results.results).hasSize(9)
assertThat(results.results).hasSize(13)
}

private fun runGenerativeTests(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ Feature: Recursive test
val negativePatterns =
ListPattern(StringPattern()).negativeBasedOn(Row(), Resolver()).map { it.value }.toList()
assertThat(negativePatterns.map { it.typeName }).containsExactlyInAnyOrder(
"null"
"null",
"number",
"boolean",
"list of number",
"list of boolean"
)
}

Expand Down