View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package mockit.internal.expectations.argumentMatching;
7   
8   import static org.junit.jupiter.api.Assertions.assertFalse;
9   import static org.junit.jupiter.api.Assertions.assertNotNull;
10  import static org.junit.jupiter.api.Assertions.assertTrue;
11  
12  import java.util.ArrayList;
13  import java.util.List;
14  
15  import org.junit.jupiter.api.Test;
16  
17  final class ArgumentMatcherTest {
18  
19      @Test
20      void nullityMatcherInstanceNotNull() {
21          assertNotNull(NullityMatcher.INSTANCE);
22      }
23  
24      @Test
25      void nullityMatcherMatchesNull() {
26          assertTrue(NullityMatcher.INSTANCE.matches(null));
27      }
28  
29      @Test
30      void nullityMatcherDoesNotMatchNonNull() {
31          assertFalse(NullityMatcher.INSTANCE.matches("not null"));
32      }
33  
34      @Test
35      void nullityMatcherWritesMismatchPhrase() {
36          ArgumentMismatch mismatch = new ArgumentMismatch();
37          ((NullityMatcher) NullityMatcher.INSTANCE).writeMismatchPhrase(mismatch);
38          assertTrue(mismatch.toString().contains("null"));
39      }
40  
41      @Test
42      void nonNullityMatcherInstanceNotNull() {
43          assertNotNull(NonNullityMatcher.INSTANCE);
44      }
45  
46      @Test
47      void nonNullityMatcherMatchesNonNull() {
48          assertTrue(NonNullityMatcher.INSTANCE.matches("hello"));
49      }
50  
51      @Test
52      void nonNullityMatcherDoesNotMatchNull() {
53          assertFalse(NonNullityMatcher.INSTANCE.matches(null));
54      }
55  
56      @Test
57      void nonNullityMatcherWritesMismatchPhrase() {
58          ArgumentMismatch mismatch = new ArgumentMismatch();
59          ((NonNullityMatcher) NonNullityMatcher.INSTANCE).writeMismatchPhrase(mismatch);
60          assertTrue(mismatch.toString().contains("not null"));
61      }
62  
63      @Test
64      void captureMatcherCapturesValue() {
65          List<String> captured = new ArrayList<>();
66          CaptureMatcher<String> matcher = new CaptureMatcher<>(captured);
67          assertTrue(matcher.matches("test-value"));
68          assertTrue(captured.contains("test-value"));
69      }
70  
71      @Test
72      void captureMatcherWithExpectedTypeCaptures() {
73          List<String> captured = new ArrayList<>();
74          CaptureMatcher<String> matcher = new CaptureMatcher<>(captured);
75          matcher.setExpectedType(String.class);
76          assertTrue(matcher.matches("test-value"));
77          assertTrue(captured.contains("test-value"));
78      }
79  
80      @Test
81      void captureMatcherWithExpectedTypeDoesNotCaptureWrongType() {
82          List<String> captured = new ArrayList<>();
83          CaptureMatcher<String> matcher = new CaptureMatcher<>(captured);
84          matcher.setExpectedType(String.class);
85          // Integer doesn't match String type, but matches() still returns true
86          assertTrue(matcher.matches(Integer.valueOf(42)));
87          assertTrue(captured.isEmpty()); // Not captured because wrong type
88      }
89  
90      @Test
91      void captureMatcherSameReturnsFalse() {
92          List<String> captured1 = new ArrayList<>();
93          List<String> captured2 = new ArrayList<>();
94          CaptureMatcher<String> matcher1 = new CaptureMatcher<>(captured1);
95          CaptureMatcher<String> matcher2 = new CaptureMatcher<>(captured2);
96          assertFalse(matcher1.same(matcher2));
97      }
98  
99      @Test
100     void stringContainmentMatcherMatchesContainingString() {
101         StringContainmentMatcher matcher = new StringContainmentMatcher("hello");
102         assertTrue(matcher.matches("say hello world"));
103     }
104 
105     @Test
106     void stringContainmentMatcherDoesNotMatchNonContaining() {
107         StringContainmentMatcher matcher = new StringContainmentMatcher("hello");
108         assertFalse(matcher.matches("goodbye"));
109     }
110 
111     @Test
112     void stringContainmentMatcherWritesMismatchPhrase() {
113         StringContainmentMatcher matcher = new StringContainmentMatcher("hello");
114         ArgumentMismatch mismatch = new ArgumentMismatch();
115         matcher.writeMismatchPhrase(mismatch);
116         assertTrue(mismatch.toString().contains("hello"));
117     }
118 
119     @Test
120     void stringSuffixMatcherMatchesSuffix() {
121         StringSuffixMatcher matcher = new StringSuffixMatcher("world");
122         assertTrue(matcher.matches("hello world"));
123     }
124 
125     @Test
126     void stringSuffixMatcherDoesNotMatchNonSuffix() {
127         StringSuffixMatcher matcher = new StringSuffixMatcher("world");
128         assertFalse(matcher.matches("world hello"));
129     }
130 
131     @Test
132     void stringSuffixMatcherWritesMismatchPhrase() {
133         StringSuffixMatcher matcher = new StringSuffixMatcher("world");
134         ArgumentMismatch mismatch = new ArgumentMismatch();
135         matcher.writeMismatchPhrase(mismatch);
136         assertTrue(mismatch.toString().contains("world"));
137     }
138 }