Skip to content

Commit

Permalink
Fix null handling for collection conditions (#1858)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 authored Jan 24, 2024
1 parent 5458b32 commit ffb6b39
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 27 deletions.
1 change: 1 addition & 0 deletions docs/release_notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ include::include.adoc[]

=== Misc
* Add support for <<extensions.adoc#extension-store,keeping state in extensions>>
* Fix bad error message for collection conditions, when one of the operands is `null`

== 2.4-M1 (2022-11-30)

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -269,9 +268,16 @@ void verify(ErrorCollector errorCollector, @Nullable List<Object> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

/**
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Condition not satisfied:

a =~ b
| | |
| | null
| false
str
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Condition not satisfied:

a =~ b
| | |
| | null
| false
[1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Condition not satisfied:

a =~ b
| | |
| | [1]
| false
null
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Condition not satisfied:

a =~ b
| | |
| | str
| false
null
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Condition not satisfied:

a ==~ b
| | |
| | null
| false
str
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Condition not satisfied:

a ==~ b
| | |
| | null
| false
[1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Condition not satisfied:

a ==~ b
| | |
| | [1]
| false
null
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Condition not satisfied:

a ==~ b
| | |
| | str
| false
null

0 comments on commit ffb6b39

Please sign in to comment.