Skip to content

Commit 36c0c29

Browse files
committed
Refactor chunk extend validations messages.
1 parent bc5d681 commit 36c0c29

File tree

5 files changed

+79
-48
lines changed

5 files changed

+79
-48
lines changed

src/main/java/net/itarray/automotion/internal/ResponsiveUIChunkValidatorBase.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public ResponsiveUIChunkValidatorBase doNotOverlap() {
135135
*/
136136
@Override
137137
public ResponsiveUIChunkValidatorBase haveEqualSize() {
138-
validateSameSize(rootElements);
138+
validateSameSize(asNumberedList(rootElements));
139139
return this;
140140
}
141141

@@ -146,7 +146,7 @@ public ResponsiveUIChunkValidatorBase haveEqualSize() {
146146
*/
147147
@Override
148148
public ResponsiveUIChunkValidatorBase haveEqualWidth() {
149-
validateSameWidth(rootElements);
149+
validateSameWidth(asNumberedList(rootElements));
150150
return this;
151151
}
152152

@@ -157,7 +157,7 @@ public ResponsiveUIChunkValidatorBase haveEqualWidth() {
157157
*/
158158
@Override
159159
public ResponsiveUIChunkValidatorBase haveEqualHeight() {
160-
validateSameHeight(rootElements);
160+
validateSameHeight(asNumberedList(rootElements));
161161
return this;
162162
}
163163

@@ -371,9 +371,9 @@ private void validateSameWidth(List<UIElement> elements) {
371371
for (int i = 0; i < elements.size() - 1; i++) {
372372
UIElement element = elements.get(i);
373373
UIElement elementToCompare = elements.get(i + 1);
374-
if (!element.hasSameWidthAs(elementToCompare)) {
375-
errors.add(String.format("Element #%d has different width. Element width is: [%s, %s]", (i + 1), element.getWidth(), element.getHeight()), element);
376-
errors.add(String.format("Element #%d has different width. Element width is: [%s, %s]", (i + 2), elementToCompare.getWidth(), elementToCompare.getHeight()), elementToCompare);
374+
if (!element.hasSameWidthAs(elementToCompare, getContext())) {
375+
errors.add(String.format("Element %s has different width than element %s.", element.getQuotedName(), elementToCompare.getQuotedName()), element);
376+
errors.add(String.format("Element %s has different width than element %s.", elementToCompare.getQuotedName(), element.getQuotedName()), elementToCompare);
377377
}
378378
}
379379
}
@@ -382,9 +382,9 @@ private void validateSameHeight(List<UIElement> elements) {
382382
for (int i = 0; i < elements.size() - 1; i++) {
383383
UIElement element = elements.get(i);
384384
UIElement elementToCompare = elements.get(i + 1);
385-
if (!element.hasSameHeightAs(elementToCompare)) {
386-
errors.add(String.format("Element #%d has different height. Element height is: [%s, %s]", (i + 1), element.getWidth(), element.getHeight()), element);
387-
errors.add(String.format("Element #%d has different height. Element height is: [%s, %s]", (i + 2), elementToCompare.getWidth(), elementToCompare.getHeight()), elementToCompare);
385+
if (!element.hasSameHeightAs(elementToCompare, getContext())) {
386+
errors.add(String.format("Element %s has different height than element %s.", element.getQuotedName(), elementToCompare.getQuotedName()), element);
387+
errors.add(String.format("Element %s has different height than element %s.", elementToCompare.getQuotedName(), element.getQuotedName()), elementToCompare);
388388
}
389389
}
390390
}
@@ -393,9 +393,10 @@ private void validateSameSize(List<UIElement> elements) {
393393
for (int i = 0; i < elements.size() - 1; i++) {
394394
UIElement element = elements.get(i);
395395
UIElement elementToCompare = elements.get(i + 1);
396-
if (!element.hasSameSizeAs(elementToCompare)) {
397-
errors.add(String.format("Element #%d has different size. Element size is: [%s, %s]", (i + 1), element.getWidth(), element.getHeight()), element);
398-
errors.add(String.format("Element #%d has different size. Element size is: [%s, %s]", (i + 2), elementToCompare.getWidth(), elementToCompare.getHeight()), elementToCompare);
396+
if (!element.hasSameSizeAs(elementToCompare, getContext())) {
397+
// todo: one error message + visual feedback
398+
errors.add(String.format("Element %s has different size than element %s.", element.getQuotedName(), elementToCompare.getQuotedName()), element);
399+
errors.add(String.format("Element %s has different size than element %s.", elementToCompare.getQuotedName(), element.getQuotedName()), elementToCompare);
399400
}
400401

401402
}
@@ -406,7 +407,7 @@ private void validateHaveDifferentSizes(List<UIElement> elements) {
406407
UIElement element = elements.get(firstIndex);
407408
for (int secondIndex = firstIndex+1; secondIndex < elements.size(); secondIndex++) {
408409
UIElement elementToCompare = elements.get(secondIndex);
409-
if (element.hasSameSizeAs(elementToCompare)) {
410+
if (element.hasSameSizeAs(elementToCompare, getContext())) {
410411
errors.add(String.format("Element #%d has same size. Element size is: [%s, %s]", (firstIndex + 1), element.getWidth(), element.getHeight()), element);
411412
errors.add(String.format("Element #%d has same size. Element size is: [%s, %s]", (secondIndex + 1), elementToCompare.getWidth(), elementToCompare.getHeight()), elementToCompare);
412413
}
@@ -419,7 +420,7 @@ private void validateHaveDifferentWidths(List<UIElement> elements) {
419420
UIElement element = elements.get(firstIndex);
420421
for (int secondIndex = firstIndex+1; secondIndex < elements.size(); secondIndex++) {
421422
UIElement elementToCompare = elements.get(secondIndex);
422-
if (element.hasSameWidthAs(elementToCompare)) {
423+
if (element.hasSameWidthAs(elementToCompare, getContext())) {
423424
errors.add(String.format("Element #%d has same width. Element width is: [%s, %s]", (firstIndex + 1), element.getWidth(), element.getHeight()), element);
424425
errors.add(String.format("Element #%d has same width. Element width is: [%s, %s]", (secondIndex + 2), elementToCompare.getWidth(), elementToCompare.getHeight()), elementToCompare);
425426
}
@@ -432,7 +433,7 @@ private void validateNotSameHeight(List<UIElement> elements) {
432433
UIElement element = elements.get(firstIndex);
433434
for (int secondIndex = firstIndex+1; secondIndex < elements.size(); secondIndex++) {
434435
UIElement elementToCompare = elements.get(secondIndex);
435-
if (element.hasSameHeightAs(elementToCompare)) {
436+
if (element.hasSameHeightAs(elementToCompare, getContext())) {
436437
errors.add(String.format("Element #%d has same height. Element height is: [%s, %s]", (firstIndex + 1), element.getWidth(), element.getHeight()), element);
437438
errors.add(String.format("Element #%d has same height. Element height is: [%s, %s]", (secondIndex + 2), elementToCompare.getWidth(), elementToCompare.getHeight()), elementToCompare);
438439
}

src/main/java/net/itarray/automotion/internal/UIElement.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static List<UIElement> asNumberedList(List<UIElement> elements) {
6666
ArrayList<UIElement> numbered = new ArrayList<>(elements.size());
6767
for (int i = 0; i < elements.size(); i++) {
6868
UIElement element = elements.get(i);
69-
numbered.add(new UIElement(String.format("#%d", i+1), element.rectangle, element.cssSource, false));
69+
numbered.add(new UIElement(String.format("#%d:%s", i+1, element.rectangle), element.rectangle, element.cssSource, false));
7070
}
7171
return numbered;
7272
}
@@ -149,20 +149,23 @@ public boolean hasEqualBottomOffsetAs(UIElement other) {
149149
return hasEqualBegin(other, UP);
150150
}
151151

152-
private <V extends MetricSpace<V>> boolean hasEqualExtendAs(UIElement other, ExtendGiving<V> direction) {
152+
private <V extends MetricSpace<V>> boolean hasEqualExtendAs(UIElement other, ExtendGiving<V> direction, Context context) {
153+
Expression<Boolean> equal = Expression.equalTo(
154+
ElementPropertyExpression.extend(direction, this),
155+
ElementPropertyExpression.extend(direction, this));
153156
return getExtend(direction).equals(other.getExtend(direction));
154157
}
155158

156-
public boolean hasSameWidthAs(UIElement other) {
157-
return hasEqualExtendAs(other, RIGHT);
159+
public boolean hasSameWidthAs(UIElement other, Context context) {
160+
return hasEqualExtendAs(other, RIGHT, context);
158161
}
159162

160-
public boolean hasSameHeightAs(UIElement other) {
161-
return hasEqualExtendAs(other, DOWN);
163+
public boolean hasSameHeightAs(UIElement other, Context context) {
164+
return hasEqualExtendAs(other, DOWN, context);
162165
}
163166

164-
public boolean hasSameSizeAs(UIElement other) {
165-
return hasEqualExtendAs(other, Rectangle.ORIGIN_CORNER);
167+
public boolean hasSameSizeAs(UIElement other, Context context) {
168+
return hasEqualExtendAs(other, Rectangle.ORIGIN_CORNER, context);
166169
}
167170

168171
public boolean overlaps(UIElement other) {
@@ -273,20 +276,20 @@ public void validateEqualEnd(Direction direction, UIElement element, Errors erro
273276
}
274277
}
275278

276-
public void validateSameSize(UIElement element, Errors errors) {
277-
validateSameExtend(ORIGIN_CORNER, element, errors);
279+
public void validateSameSize(UIElement element, Context context, Errors errors) {
280+
validateSameExtend(ORIGIN_CORNER, element, context, errors);
278281
}
279282

280-
public void validateSameHeight(UIElement element, Errors errors) {
281-
validateSameExtend(DOWN, element, errors);
283+
public void validateSameHeight(UIElement element, Context context, Errors errors) {
284+
validateSameExtend(DOWN, element, context, errors);
282285
}
283286

284-
public void validateSameWidth(UIElement element, Errors errors) {
285-
validateSameExtend(RIGHT, element, errors);
287+
public void validateSameWidth(UIElement element, Context context, Errors errors) {
288+
validateSameExtend(RIGHT, element, context, errors);
286289
}
287290

288-
public <V extends MetricSpace<V>> void validateSameExtend(ExtendGiving<V> direction, UIElement element, Errors errors) {
289-
if (!hasEqualExtendAs(element, direction)) {
291+
public <V extends MetricSpace<V>> void validateSameExtend(ExtendGiving<V> direction, UIElement element, Context context, Errors errors) {
292+
if (!hasEqualExtendAs(element, direction, context)) {
290293
errors.add(
291294
String.format("Element %s has not the same %s as element %s. %s of %s is %s. %s of element is %s",
292295
getQuotedName(),
@@ -301,8 +304,8 @@ public <V extends MetricSpace<V>> void validateSameExtend(ExtendGiving<V> direct
301304
}
302305
}
303306

304-
public <V extends MetricSpace<V>> void validateNotSameExtend(ExtendGiving<V> direction, UIElement element, Errors errors) {
305-
if (hasEqualExtendAs(element, direction)) {
307+
public <V extends MetricSpace<V>> void validateNotSameExtend(ExtendGiving<V> direction, UIElement element, Context context, Errors errors) {
308+
if (hasEqualExtendAs(element, direction, context)) {
306309
errors.add(
307310
String.format("Element %s has the same %s as element %s. %s of %s is %s. %s of element is %s",
308311
getQuotedName(),
@@ -317,8 +320,8 @@ public <V extends MetricSpace<V>> void validateNotSameExtend(ExtendGiving<V> dir
317320
}
318321
}
319322

320-
public void validateNotSameSize(UIElement element, Errors errors) {
321-
validateNotSameExtend(ORIGIN_CORNER, element, errors);
323+
public void validateNotSameSize(UIElement element, Context context, Errors errors) {
324+
validateNotSameExtend(ORIGIN_CORNER, element, context, errors);
322325
}
323326

324327

src/main/java/net/itarray/automotion/internal/UIValidatorBase.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package net.itarray.automotion.internal;
22

33
import net.itarray.automotion.internal.geometry.Direction;
4-
import net.itarray.automotion.internal.geometry.Rectangle;
54
import net.itarray.automotion.internal.geometry.Scalar;
6-
import net.itarray.automotion.internal.properties.Context;
75
import net.itarray.automotion.internal.properties.PixelConstant;
86
import net.itarray.automotion.validation.UIElementValidator;
97
import net.itarray.automotion.validation.UISnapshot;
@@ -312,7 +310,7 @@ public UIValidatorBase isBottomAlignedWith(List<WebElement> elements) {
312310
*/
313311
@Override
314312
public UIValidatorBase hasEqualWidthAs(WebElement element, String readableName) {
315-
rootElement.validateSameWidth(asElement(element, readableName), errors);
313+
rootElement.validateSameWidth(asElement(element, readableName), getContext(), errors);
316314
return this;
317315
}
318316

@@ -325,7 +323,7 @@ public UIValidatorBase hasEqualWidthAs(WebElement element, String readableName)
325323
@Override
326324
public UIValidatorBase hasEqualWidthAs(List<WebElement> elements) {
327325
for (WebElement element : elements) {
328-
rootElement.validateSameWidth(asElement(element), errors);
326+
rootElement.validateSameWidth(asElement(element), getContext(), errors);
329327
}
330328
return this;
331329
}
@@ -344,7 +342,7 @@ public UIValidatorBase hasWidth(Condition<Scalar> condition) {
344342
*/
345343
@Override
346344
public UIValidatorBase hasEqualHeightAs(WebElement element, String readableName) {
347-
rootElement.validateSameHeight(asElement(element, readableName), errors);
345+
rootElement.validateSameHeight(asElement(element, readableName), getContext(), errors);
348346
return this;
349347
}
350348

@@ -357,7 +355,7 @@ public UIValidatorBase hasEqualHeightAs(WebElement element, String readableName)
357355
@Override
358356
public UIValidatorBase hasEqualHeightAs(List<WebElement> elements) {
359357
for (WebElement element : elements) {
360-
rootElement.validateSameHeight(asElement(element), errors);
358+
rootElement.validateSameHeight(asElement(element), getContext(), errors);
361359
}
362360
return this;
363361
}
@@ -376,7 +374,7 @@ public UIValidatorBase hasHeight(Condition<Scalar> condition) {
376374
*/
377375
@Override
378376
public UIValidatorBase hasEqualSizeAs(WebElement element, String readableName) {
379-
rootElement.validateSameSize(asElement(element, readableName), errors);
377+
rootElement.validateSameSize(asElement(element, readableName), getContext(), errors);
380378
return this;
381379
}
382380

@@ -389,7 +387,7 @@ public UIValidatorBase hasEqualSizeAs(WebElement element, String readableName) {
389387
@Override
390388
public UIValidatorBase hasEqualSizeAs(List<WebElement> elements) {
391389
for (WebElement element : elements) {
392-
rootElement.validateSameSize(asElement(element), errors);
390+
rootElement.validateSameSize(asElement(element), getContext(), errors);
393391
}
394392
return this;
395393
}
@@ -573,7 +571,7 @@ public UIValidatorBase isInsideOf(WebElement containerElement, String readableCo
573571
}
574572

575573
private void validateNotSameSize(UIElement element) {
576-
rootElement.validateNotSameSize(element, errors);
574+
rootElement.validateNotSameSize(element, getContext(), errors);
577575
}
578576

579577
@Override

src/main/java/net/itarray/automotion/internal/geometry/Rectangle.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ public boolean contains(Rectangle other) {
7272
return contains(Direction.RIGHT, other) && contains(Direction.DOWN, other);
7373
}
7474

75+
@Override
76+
public String toString() {
77+
Vector extend = corner.minus(origin);
78+
return String.format("[(%s,%s) - %sx%s]", origin.getX(), origin.getY(), extend.getX(), extend.getY());
79+
}
7580
}

src/test/java/net/itarray/automotion/tests/errors/ErrorMessagesRegressionTest.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void areLeftAligned() {
8585
createChunkValidator(createElement(105, 200, 500, 400)).areLeftAligned();
8686
Errors errors = base.getErrors();
8787
assertThat(errors.getLastMessage())
88-
.isEqualTo("Element #1 has not the same left offset as element #2");
88+
.isEqualTo("Element #1:[(100,200) - 400x200] has not the same left offset as element #2:[(105,200) - 395x200]");
8989
}
9090

9191
@Test
@@ -109,7 +109,7 @@ public void areRightAligned() {
109109
createChunkValidator(createElement(100, 200, 505, 400)).areRightAligned();
110110
Errors errors = base.getErrors();
111111
assertThat(errors.getLastMessage())
112-
.isEqualTo("Element #1 has not the same right offset as element #2");
112+
.isEqualTo("Element #1:[(100,200) - 400x200] has not the same right offset as element #2:[(100,200) - 405x200]");
113113
}
114114

115115
@Test
@@ -133,7 +133,7 @@ public void areTopAligned() {
133133
createChunkValidator(createElement(100, 205, 500, 400)).areTopAligned();
134134
Errors errors = base.getErrors();
135135
assertThat(errors.getLastMessage())
136-
.isEqualTo("Element #1 has not the same top offset as element #2");
136+
.isEqualTo("Element #1:[(100,200) - 400x200] has not the same top offset as element #2:[(100,205) - 400x195]");
137137
}
138138

139139
@Test
@@ -157,7 +157,7 @@ public void areBottomAligned() {
157157
createChunkValidator(createElement(100, 200, 500, 405)).areBottomAligned();
158158
Errors errors = base.getErrors();
159159
assertThat(errors.getLastMessage())
160-
.isEqualTo("Element #1 has not the same bottom offset as element #2");
160+
.isEqualTo("Element #1:[(100,200) - 400x200] has not the same bottom offset as element #2:[(100,200) - 400x205]");
161161
}
162162

163163
@Test
@@ -240,6 +240,14 @@ public void sameWidthAsWithList() {
240240
.isEqualTo("Element 'under test' has not the same width as element 'with properties: tag=[null], id=[null], class=[null], text=[], coord=[100,200], size=[405,205]'. Width of 'under test' is 400px. Width of element is 405px");
241241
}
242242

243+
@Test
244+
public void haveEqualWidthChunk() {
245+
createChunkValidator(createElement(100, 200, 505, 405)).haveEqualWidth();
246+
Errors errors = base.getErrors();
247+
assertThat(errors.getLastMessage())
248+
.isEqualTo("Element #2:[(100,200) - 405x205] has different width than element #1:[(100,200) - 400x200].");
249+
}
250+
243251
@Test
244252
public void sameHeightAs() {
245253
createElementValidator().hasEqualHeightAs(createElement(100, 200, 505, 405), "specifying");
@@ -256,6 +264,14 @@ public void sameHeightAsWithList() {
256264
.isEqualTo("Element 'under test' has not the same height as element 'with properties: tag=[null], id=[null], class=[null], text=[], coord=[100,200], size=[405,205]'. Height of 'under test' is 200px. Height of element is 205px");
257265
}
258266

267+
@Test
268+
public void haveEqualHeightChunk() {
269+
createChunkValidator(createElement(100, 200, 505, 405)).haveEqualHeight();
270+
Errors errors = base.getErrors();
271+
assertThat(errors.getLastMessage())
272+
.isEqualTo("Element #2:[(100,200) - 405x205] has different height than element #1:[(100,200) - 400x200].");
273+
}
274+
259275
@Test
260276
public void sameSizeAs() {
261277
createElementValidator().hasEqualSizeAs(createElement(100, 200, 505, 405), "specifying");
@@ -264,6 +280,14 @@ public void sameSizeAs() {
264280
.isEqualTo("Element 'under test' has not the same size as element 'specifying'. Size of 'under test' is 400px x 200px. Size of element is 405px x 205px");
265281
}
266282

283+
@Test
284+
public void haveEqualSizeChunk() {
285+
createChunkValidator(createElement(100, 200, 505, 405)).haveEqualSize();
286+
Errors errors = base.getErrors();
287+
assertThat(errors.getLastMessage())
288+
.isEqualTo("Element #2:[(100,200) - 405x205] has different size than element #1:[(100,200) - 400x200].");
289+
}
290+
267291
@Test
268292
public void sameSizeAsWithList() {
269293
createElementValidator().hasEqualSizeAs(singletonList(createElement(100, 200, 505, 405)));

0 commit comments

Comments
 (0)