Skip to content

Commit f429f3b

Browse files
matchers: add sameInstance (#1553)
1 parent 552444e commit f429f3b

File tree

7 files changed

+237
-0
lines changed

7 files changed

+237
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2023 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.testingisdocumenting.webtau.expectation.equality
18+
19+
import org.junit.Test
20+
21+
import static org.testingisdocumenting.webtau.Matchers.sameInstance
22+
23+
class SameInstanceMatcherGroovyTest {
24+
@Test
25+
void matches() {
26+
def value = [1, 2, 3]
27+
def anotherValue = value
28+
// should-be
29+
value.shouldBe sameInstance(anotherValue)
30+
// should-be
31+
}
32+
33+
@Test
34+
void negativeMatches() {
35+
def value = [1, 2, 3]
36+
def anotherValue = [1, 2, 3]
37+
// should-not-be
38+
value.shouldNotBe sameInstance(anotherValue)
39+
// should-not-be
40+
}
41+
}

webtau-core/src/main/java/org/testingisdocumenting/webtau/Matchers.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,18 @@ public static AnyOfMatcher anyOf(Collection<Object> expected) {
293293
return new AnyOfMatcher(expected);
294294
}
295295

296+
/**
297+
* Same instance
298+
* <pre>
299+
* actual(value).shouldBe(sameInstance(anotherValue));
300+
* </pre>
301+
* @param expected expected instance
302+
* @return matcher instance
303+
*/
304+
public static SameInstanceMatcher sameInstance(Object expected) {
305+
return new SameInstanceMatcher(expected);
306+
}
307+
296308
/**
297309
* Throw exception <code>code</code> matcher.
298310
* <pre>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2023 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.testingisdocumenting.webtau.expectation.equality;
18+
19+
import org.testingisdocumenting.webtau.data.ValuePath;
20+
import org.testingisdocumenting.webtau.data.render.PrettyPrintable;
21+
import org.testingisdocumenting.webtau.data.render.PrettyPrinter;
22+
import org.testingisdocumenting.webtau.expectation.ValueMatcher;
23+
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
24+
25+
import static org.testingisdocumenting.webtau.WebTauCore.*;
26+
27+
public class SameInstanceMatcher implements ValueMatcher, PrettyPrintable {
28+
private final Object expected;
29+
30+
public SameInstanceMatcher(Object expected) {
31+
this.expected = expected;
32+
}
33+
34+
@Override
35+
public TokenizedMessage matchingTokenizedMessage(ValuePath actualPath, Object actual) {
36+
return tokenizedMessage().matcher("to be the same instance as").valueFirstLinesOnly(expected);
37+
}
38+
39+
@Override
40+
public TokenizedMessage matchedTokenizedMessage(ValuePath actualPath, Object actual) {
41+
return tokenizedMessage().matcher("is the same instance as").valueFirstLinesOnly(expected);
42+
}
43+
44+
@Override
45+
public TokenizedMessage mismatchedTokenizedMessage(ValuePath actualPath, Object actual) {
46+
return tokenizedMessage().error("different instance than").valueFirstLinesOnly(expected);
47+
}
48+
49+
@Override
50+
public boolean matches(ValuePath actualPath, Object actual) {
51+
return actual == expected;
52+
}
53+
54+
@Override
55+
public TokenizedMessage negativeMatchingTokenizedMessage(ValuePath actualPath, Object actual) {
56+
return tokenizedMessage().matcher("to be different instance from").valueFirstLinesOnly(expected);
57+
}
58+
59+
@Override
60+
public TokenizedMessage negativeMatchedTokenizedMessage(ValuePath actualPath, Object actual) {
61+
return tokenizedMessage().matcher("is different instance from").valueFirstLinesOnly(expected);
62+
}
63+
64+
@Override
65+
public TokenizedMessage negativeMismatchedTokenizedMessage(ValuePath actualPath, Object actual) {
66+
return tokenizedMessage().error("is the same instance as").valueFirstLinesOnly(expected);
67+
}
68+
69+
@Override
70+
public boolean negativeMatches(ValuePath actualPath, Object actual) {
71+
return expected != actual;
72+
}
73+
74+
@Override
75+
public void prettyPrint(PrettyPrinter printer) {
76+
printer.printDelimiter("<");
77+
printer.print(PrettyPrinter.CLASSIFIER_COLOR, "sameInstance ");
78+
printer.printObject(expected);
79+
printer.printDelimiter(">");
80+
}
81+
82+
@Override
83+
public String toString() {
84+
return PrettyPrinter.renderAsTextWithoutColors(this);
85+
}
86+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2023 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.testingisdocumenting.webtau.expectation.equality;
18+
19+
20+
import org.junit.Test;
21+
import org.testingisdocumenting.webtau.testutils.TestConsoleOutput;
22+
23+
import java.util.Arrays;
24+
25+
import static org.testingisdocumenting.webtau.Matchers.*;
26+
27+
public class SameInstanceMatcherJavaTest {
28+
@Test
29+
public void matches() {
30+
TestConsoleOutput.runAndValidateOutput("""
31+
. [value] is the same instance as [1, 2, 3] (Xms)""", () -> {
32+
Object value = Arrays.asList(1, 2, 3);
33+
Object anotherValue = value;
34+
// should-be
35+
actual(value).shouldBe(sameInstance(anotherValue));
36+
// should-be
37+
});
38+
}
39+
40+
@Test
41+
public void negativeMatches() {
42+
TestConsoleOutput.runAndValidateOutput("""
43+
. [value] is different instance from [1, 2, 3] (Xms)""", () -> {
44+
Object value = Arrays.asList(1, 2, 3);
45+
Object anotherValue = Arrays.asList(1, 2, 3);
46+
// should-not-be
47+
actual(value).shouldNotBe(sameInstance(anotherValue));
48+
// should-not-be
49+
});
50+
}
51+
52+
@Test
53+
public void mismatch() {
54+
TestConsoleOutput.runExpectExceptionAndValidateOutput(AssertionError.class, """
55+
X failed expecting [value] to be the same instance as [1, 2, 3]:
56+
different instance than [1, 2, 3] (Xms)
57+
\s
58+
[1, 2, 3]""", () -> {
59+
Object value = Arrays.asList(1, 2, 3);
60+
Object anotherValue = Arrays.asList(1, 2, 3);
61+
actual(value).shouldBe(sameInstance(anotherValue));
62+
});
63+
}
64+
65+
@Test
66+
public void negativeMismatch() {
67+
TestConsoleOutput.runExpectExceptionAndValidateOutput(AssertionError.class, """
68+
X failed expecting [value] to be different instance from [1, 2, 3]:
69+
is the same instance as [1, 2, 3] (Xms)""", () -> {
70+
Object value = Arrays.asList(1, 2, 3);
71+
Object anotherValue = value;
72+
actual(value).shouldNotBe(sameInstance(anotherValue));
73+
});
74+
}
75+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
identifier: {validationPath: "org/testingisdocumenting/webtau/expectation/equality/SameInstanceMatcherJavaTest.java"}
3+
---
4+
5+
Use `:identifier: sameInstance` to check if two objects are the same:
6+
7+
```tabs
8+
Groovy:
9+
:include-file: org/testingisdocumenting/webtau/expectation/equality/SameInstanceMatcherGroovyTest.groovy {
10+
title: "same instance",
11+
surroundedBy: ["should-be", "should-not-be"]
12+
}
13+
:include-markdown: import-ref.md
14+
15+
Java:
16+
:include-file: org/testingisdocumenting/webtau/expectation/equality/SameInstanceMatcherJavaTest.java {
17+
title: "same instance",
18+
surroundedBy: ["should-be", "should-not-be"]
19+
}
20+
:include-markdown: import-ref.md
21+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: [sameInstance](matchers/same-instance) matcher

webtau-docs/znai/toc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ matchers
102102
any-of
103103
contain-exactly
104104
any-value
105+
same-instance
105106
custom-compare-to-handler
106107
value-change
107108
import-and-dependencies

0 commit comments

Comments
 (0)