From ffb6b3969947e2ed58d43d82ddd1f70726e7c701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20Br=C3=BCnings?= Date: Wed, 24 Jan 2024 15:26:17 +0100 Subject: [PATCH] Fix null handling for collection conditions (#1858) --- docs/release_notes.adoc | 1 + .../spockframework/runtime/SpockRuntime.java | 32 +++++--- .../condition/ConditionEvaluation.groovy | 82 +++++++++++++++---- ...lection_conditions_work_with_nulls_[0].txt | 7 ++ ...lection_conditions_work_with_nulls_[1].txt | 7 ++ ...lection_conditions_work_with_nulls_[2].txt | 7 ++ ...lection_conditions_work_with_nulls_[3].txt | 7 ++ ...lection_conditions_work_with_nulls_[0].txt | 7 ++ ...lection_conditions_work_with_nulls_[1].txt | 7 ++ ...lection_conditions_work_with_nulls_[2].txt | 7 ++ ...lection_conditions_work_with_nulls_[3].txt | 7 ++ 11 files changed, 144 insertions(+), 27 deletions(-) create mode 100644 spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[0].txt create mode 100644 spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[1].txt create mode 100644 spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[2].txt create mode 100644 spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[3].txt create mode 100644 spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[0].txt create mode 100644 spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[1].txt create mode 100644 spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[2].txt create mode 100644 spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[3].txt diff --git a/docs/release_notes.adoc b/docs/release_notes.adoc index bb6fc9ce87..f58c8ab3d1 100644 --- a/docs/release_notes.adoc +++ b/docs/release_notes.adoc @@ -11,6 +11,7 @@ include::include.adoc[] === Misc * Add support for <> +* Fix bad error message for collection conditions, when one of the operands is `null` == 2.4-M1 (2022-11-30) diff --git a/spock-core/src/main/java/org/spockframework/runtime/SpockRuntime.java b/spock-core/src/main/java/org/spockframework/runtime/SpockRuntime.java index 477a104709..79658a74d8 100644 --- a/spock-core/src/main/java/org/spockframework/runtime/SpockRuntime.java +++ b/spock-core/src/main/java/org/spockframework/runtime/SpockRuntime.java @@ -1,17 +1,16 @@ /* - * Copyright 2009 the original author or authors. + * Copyright 2023 the original author or authors. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * https://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package org.spockframework.runtime; @@ -269,9 +268,16 @@ void verify(ErrorCollector errorCollector, @Nullable List values, @Nulla // we have a mismatch, so add false for the result // due to the way the ValueRecorder works, we have two (n/a) values at the end, so we need to skip them values.add(values.size() - 2, false); + } else if (left == null || right == null) { + values.set(idxActual, left); + values.set(idxExpected, right); + if (left == null && right == null) { + return; + } + values.set(3, false); } else { - Pattern pattern = Pattern.compile(right.toString()); - java.util.regex.Matcher matcher = pattern.matcher(left.toString()); + Pattern pattern = Pattern.compile(String.valueOf(right)); + java.util.regex.Matcher matcher = pattern.matcher(String.valueOf(left)); values.set(idxActual, left); values.set(idxExpected, right); diff --git a/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionEvaluation.groovy b/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionEvaluation.groovy index 205c8ac5c6..a9d8bbe307 100644 --- a/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionEvaluation.groovy +++ b/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionEvaluation.groovy @@ -1,17 +1,16 @@ /* - * Copyright 2008 the original author or authors. + * Copyright 2023 the original author or authors. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * https://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ package org.spockframework.smoke.condition @@ -20,13 +19,14 @@ import org.opentest4j.AssertionFailedError import org.spockframework.EmbeddedSpecification import org.spockframework.runtime.ConditionFailedWithExceptionError import org.spockframework.runtime.ConditionNotSatisfiedError - -import spock.lang.Issue +import org.spockframework.specs.extension.Snapshot +import org.spockframework.specs.extension.Snapshotter import spock.lang.FailsWith +import spock.lang.Issue +import static java.lang.Integer.MAX_VALUE import static java.lang.Math.max import static java.lang.Math.min -import static java.lang.Integer.MAX_VALUE import static java.lang.Thread.State.BLOCKED /** @@ -389,6 +389,60 @@ class ConditionEvaluation extends EmbeddedSpecification { [aType, bType] << ['int[]', 'Integer[]', 'List', 'Queue', 'Deque'].with { [it, it] }.combinations() } + def "collection conditions work with nulls"(@Snapshot Snapshotter snapshotter) { + when: + runner.runFeatureBody(""" + given: + def a = ${aType} + def b = ${bType} + expect: + a =~ b + """) + then: + ConditionNotSatisfiedError e = thrown() + snapshotter.assertThat(e.message).matchesSnapshot() + + where: + aType | bType + '"str"' | 'null' + '[1]' | 'null' + 'null' | '[1]' + 'null' | '"str"' + } + + def "strict collection conditions work with nulls"(@Snapshot Snapshotter snapshotter) { + when: + runner.runFeatureBody(""" + given: + def a = ${aType} + def b = ${bType} + expect: + a ==~ b + """) + then: + ConditionNotSatisfiedError e = thrown() + snapshotter.assertThat(e.message).matchesSnapshot() + + where: + aType | bType + '"str"' | 'null' + '[1]' | 'null' + 'null' | '[1]' + 'null' | '"str"' + } + + def "collection conditions work with null equality"() { + given: + def a = null + def b = null + + expect: + null ==~ null + null =~ null + a ==~ b + a =~ b + } + @FailsWith(ConditionFailedWithExceptionError) def "regular implicit condition"() { expect: diff --git a/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[0].txt b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[0].txt new file mode 100644 index 0000000000..b261a3aef5 --- /dev/null +++ b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[0].txt @@ -0,0 +1,7 @@ +Condition not satisfied: + +a =~ b +| | | +| | null +| false +str diff --git a/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[1].txt b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[1].txt new file mode 100644 index 0000000000..21eb6dc5bc --- /dev/null +++ b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[1].txt @@ -0,0 +1,7 @@ +Condition not satisfied: + +a =~ b +| | | +| | null +| false +[1] diff --git a/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[2].txt b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[2].txt new file mode 100644 index 0000000000..810092dc49 --- /dev/null +++ b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[2].txt @@ -0,0 +1,7 @@ +Condition not satisfied: + +a =~ b +| | | +| | [1] +| false +null diff --git a/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[3].txt b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[3].txt new file mode 100644 index 0000000000..7ed0fbb04d --- /dev/null +++ b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__collection_conditions_work_with_nulls_[3].txt @@ -0,0 +1,7 @@ +Condition not satisfied: + +a =~ b +| | | +| | str +| false +null diff --git a/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[0].txt b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[0].txt new file mode 100644 index 0000000000..681f74b083 --- /dev/null +++ b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[0].txt @@ -0,0 +1,7 @@ +Condition not satisfied: + +a ==~ b +| | | +| | null +| false +str diff --git a/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[1].txt b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[1].txt new file mode 100644 index 0000000000..3843293d3b --- /dev/null +++ b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[1].txt @@ -0,0 +1,7 @@ +Condition not satisfied: + +a ==~ b +| | | +| | null +| false +[1] diff --git a/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[2].txt b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[2].txt new file mode 100644 index 0000000000..81d82ecb7d --- /dev/null +++ b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[2].txt @@ -0,0 +1,7 @@ +Condition not satisfied: + +a ==~ b +| | | +| | [1] +| false +null diff --git a/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[3].txt b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[3].txt new file mode 100644 index 0000000000..95a9b51b7a --- /dev/null +++ b/spock-specs/src/test/resources/org/spockframework/smoke/condition/ConditionEvaluation__strict_collection_conditions_work_with_nulls_[3].txt @@ -0,0 +1,7 @@ +Condition not satisfied: + +a ==~ b +| | | +| | str +| false +null