-
Notifications
You must be signed in to change notification settings - Fork 6
/
PersonMatchersTest.java
130 lines (104 loc) · 3.92 KB
/
PersonMatchersTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* 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
*
* http://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.hobsoft.hamcrest.compose.demo;
import org.hamcrest.StringDescription;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hobsoft.hamcrest.compose.demo.PersonMatchers.personEqualTo;
import static org.junit.Assert.assertThat;
/**
* Tests {@code PersonMatchers}.
*/
public class PersonMatchersTest
{
// ----------------------------------------------------------------------------------------------------------------
// tests
// ----------------------------------------------------------------------------------------------------------------
@Test
public void describeToDescribesExpected()
{
Person person = new Person("x", "y", "z");
StringDescription description = new StringDescription();
personEqualTo(person).describeTo(description);
assertThat(description.toString(), is("a person with title \"x\"\n"
+ " and first name \"y\"\n"
+ " and last name \"z\""));
}
@Test
public void matchesWhenEqualReturnsTrue()
{
Person person1 = new Person("x", "y", "z");
Person person2 = new Person("x", "y", "z");
assertThat(personEqualTo(person1).matches(person2), is(true));
}
@Test
public void matchesWhenTitleUnequalReturnsFalse()
{
Person person1 = new Person("x1", "y", "z");
Person person2 = new Person("x2", "y", "z");
assertThat(personEqualTo(person1).matches(person2), is(false));
}
@Test
public void matchesWhenFirstNameUnequalReturnsFalse()
{
Person person1 = new Person("x", "y1", "z");
Person person2 = new Person("x", "y2", "z");
assertThat(personEqualTo(person1).matches(person2), is(false));
}
@Test
public void matchesWhenLastNameUnequalReturnsFalse()
{
Person person1 = new Person("x", "y", "z1");
Person person2 = new Person("x", "y", "z2");
assertThat(personEqualTo(person1).matches(person2), is(false));
}
@Test
public void describeMismatchWhenTitleUnequalDescribesMismatch()
{
Person person1 = new Person("x1", "y", "z");
Person person2 = new Person("x2", "y", "z");
StringDescription description = new StringDescription();
personEqualTo(person1).describeMismatch(person2, description);
assertThat(description.toString(), is("title was \"x2\""));
}
@Test
public void describeMismatchWhenFirstNameUnequalDescribesMismatch()
{
Person person1 = new Person("x", "y1", "z");
Person person2 = new Person("x", "y2", "z");
StringDescription description = new StringDescription();
personEqualTo(person1).describeMismatch(person2, description);
assertThat(description.toString(), is("first name was \"y2\""));
}
@Test
public void describeMismatchWhenLastNameUnequalDescribesMismatch()
{
Person person1 = new Person("x", "y", "z1");
Person person2 = new Person("x", "y", "z2");
StringDescription description = new StringDescription();
personEqualTo(person1).describeMismatch(person2, description);
assertThat(description.toString(), is("last name was \"z2\""));
}
@Test
public void describeMismatchWhenAllPropertiesUnequalDescribesMismatch()
{
Person person1 = new Person("x1", "y1", "z1");
Person person2 = new Person("x2", "y2", "z2");
StringDescription description = new StringDescription();
personEqualTo(person1).describeMismatch(person2, description);
assertThat(description.toString(), is("title was \"x2\"\n"
+ " and first name was \"y2\"\n"
+ " and last name was \"z2\""));
}
}