View Javadoc
1   /**
2    * Powerunit - A JDK1.8 test framework
3    * Copyright (C) 2014 Mathieu Boretti.
4    *
5    * This file is part of Powerunit
6    *
7    * Powerunit is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * Powerunit is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package ch.powerunit.matchers.impl;
21  
22  import java.util.function.BiFunction;
23  import java.util.stream.Stream;
24  import java.util.stream.Stream.Builder;
25  
26  import org.hamcrest.Matcher;
27  import org.hamcrest.StringDescription;
28  
29  import ch.powerunit.Parameter;
30  import ch.powerunit.Parameters;
31  import ch.powerunit.Test;
32  import ch.powerunit.TestDelegator;
33  import ch.powerunit.TestSuite;
34  import ch.powerunit.matchers.MatcherTester;
35  import ch.powerunit.matchers.lang.MatcherAssertion;
36  
37  /**
38   * @author borettim
39   * @since 0.3.0
40   */
41  @TestDelegator
42  public final class MatcherTesterImpl<T extends Matcher<?>> implements TestSuite {
43  	@Parameters
44  	public static <T extends Matcher<?>> Stream<Object[]> getParameter(MatcherTester<T> input) {
45  		Builder<Object[]> b = Stream.builder();
46  		for (MatcherAssertion<?> ma : input.getAssertions()) {
47  			MatcherAssertionImpl<?> c = (MatcherAssertionImpl<?>) ma;
48  			b.accept(new Object[] { ma, c.getMatcher(), c.getExpected(),
49  					descriptionAsString(c.getExpected()), c.getNullRejected(),
50  					c.getNullRejectedMessage(),
51  					descriptionAsString(c.getNullRejectedMessage()), null,
52  					null, null, null });
53  			if (c.getAcceptedValues() != null) {
54  				for (Object o : c.getAcceptedValues()) {
55  					b.accept(new Object[] { ma, c.getMatcher(), null, null,
56  							null, null, null, o, null, null, null });
57  				}
58  			}
59  			if (c.getRejectedValue() != null) {
60  				for (MatcherAssertion.Reject o : c.getRejectedValue()) {
61  					RejectImpl r = (RejectImpl) o;
62  					b.accept(new Object[] { ma, c.getMatcher(), null, null,
63  							null, null, null, null, r.getValue(),
64  							r.getMessageValidation(),
65  							descriptionAsString(r.getMessageValidation()) });
66  				}
67  			}
68  		}
69  		return b.build()
70  				.map(TestSuite.DSL.addFieldToEachEntry(input.getMatcherClass()))
71  				.map(TestSuite.DSL
72  						.<BiFunction<String, Object[], Boolean>> addFieldToEachEntry(MatcherTesterImpl::filter));
73  	}
74  
75  	@Parameter(0)
76  	public MatcherAssertionImpl<T> assertion;
77  
78  	@Parameter(1)
79  	public Matcher<T> target;
80  
81  	@Parameter(2)
82  	public Matcher<String> expectedDescription;
83  
84  	@Parameter(3)
85  	public String expectedDescriptionAsString;
86  
87  	@Parameter(4)
88  	public Boolean nullRejected;
89  
90  	@Parameter(5)
91  	public Matcher<String> expectedRejectDescription;
92  
93  	@Parameter(6)
94  	public String expectedRejectDescriptionAsString;
95  
96  	@Parameter(7)
97  	public Object acceptedValue;
98  
99  	@Parameter(8)
100 	public Object rejectedValue;
101 
102 	@Parameter(9)
103 	public Matcher<String> rejectMessage;
104 
105 	@Parameter(10)
106 	public String rejectMesageAsString;
107 
108 	@Parameter(11)
109 	public Class<T> matcherClass;
110 
111 	@Parameter(value = 12, filter = true)
112 	public BiFunction<String, Object[], Boolean> filter;
113 
114 	private static String descriptionAsString(Matcher<?> input) {
115 		if (input == null) {
116 			return null;
117 		}
118 		StringDescription description = new StringDescription();
119 		input.describeTo(description);
120 		return description.toString();
121 	}
122 
123 	private static boolean filter(String methodName, Object parameter[]) {
124 		if ("testMatcherForExpectedValue".equals(methodName)
125 				&& parameter[2] == null) {
126 			return false;
127 		}
128 		if ("testMatcherForNullIsRejected".equals(methodName)
129 				&& (parameter[4] == null || (Boolean) parameter[4] == false)) {
130 			return false;
131 		}
132 		if ("testMatcherForNullIsAccepted".equals(methodName)
133 				&& (parameter[4] == null || (Boolean) parameter[4] == true)) {
134 			return false;
135 		}
136 		if ("testMatcherForValueIsAccepted".equals(methodName)
137 				&& parameter[7] == null) {
138 			return false;
139 		}
140 		if ("testMatcherForValueIsRejected".equals(methodName)
141 				&& parameter[8] == null) {
142 			return false;
143 		}
144 		return true;
145 	}
146 
147 	@Test(name = "Validate that for the matcher of class %12$s the expectedDescription is correct (%2$s then %4$s)")
148 	public void testMatcherForExpectedValue() {
149 		StringDescription d = new StringDescription();
150 		assertion.getMatcher().describeTo(d);
151 		assertThat("Validate the description of the matcher ", d.toString())
152 				.is(expectedDescription);
153 	}
154 
155 	@Test(name = "Validate that null is rejected for the matcher of class %12$s (%2$s then %7$s)")
156 	public void testMatcherForNullIsRejected() {
157 		assertThat("Validate null is rejected by this matcher",
158 				assertion.getMatcher().matches(null)).is(false);
159 		StringDescription d = new StringDescription();
160 		assertion.getMatcher().describeMismatch(null, d);
161 		assertThat("Validate the mismatch description for this matcher",
162 				d.toString()).is(expectedRejectDescription);
163 
164 	}
165 
166 	@Test(name = "Validate that null is accepted for the matcher of class %12$s (%2$s)")
167 	public void testMatcherForNullIsAccepted() {
168 		assertThat("Validate null is accepted by this matcher",
169 				assertion.getMatcher().matches(null)).is(true);
170 	}
171 
172 	@Test(name = "Validate that a value (%8$s) is accepted for the matcher of class %12$s (%2$s)")
173 	public void testMatcherForValueIsAccepted() {
174 		assertThat(
175 				"Validate " + acceptedValue + " is accepted by this matcher",
176 				assertion.getMatcher().matches(acceptedValue)).is(true);
177 	}
178 
179 	@Test(name = "Validate that a value (%9$s) is rejected for the matcher of class %12$s (%2$s then %11$s)")
180 	public void testMatcherForValueIsRejected() {
181 		assertThat(
182 				"Validate " + rejectedValue + " is rejected by this matcher",
183 				assertion.getMatcher().matches(rejectedValue)).is(false);
184 		StringDescription d = new StringDescription();
185 		assertion.getMatcher().describeMismatch(rejectedValue, d);
186 		assertThat("Validate the mismatch description for this matcher",
187 				d.toString()).is(rejectMessage);
188 
189 	}
190 }