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+ }
0 commit comments