-
Notifications
You must be signed in to change notification settings - Fork 45
/
FormAssert.java
125 lines (105 loc) · 4.14 KB
/
FormAssert.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
/*******************************************************************************
* Copyright (c) 2019, 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.forms.tests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.sirius.components.forms.tests.FormAssertions.assertThat;
import java.util.ArrayList;
import java.util.List;
import org.assertj.core.api.AbstractAssert;
import org.eclipse.sirius.components.forms.AbstractWidget;
import org.eclipse.sirius.components.forms.Form;
import org.eclipse.sirius.components.forms.Group;
import org.eclipse.sirius.components.forms.Page;
/**
* Custom assertion class used to performs some tests on a form.
*
* @author lfasani
*/
public class FormAssert extends AbstractAssert<FormAssert, Form> {
public FormAssert(Form form) {
super(form, FormAssert.class);
}
/**
* Asserts that the actual form has the same properties as the given form (without taking into account the position
* and size of any elements.
*
* @param form
* The given form
* @param idPolicy
* Indicates if the identifiers of the form elements should match too
* @return The current form assert
*/
public FormAssert matchesRecursively(Form form, IdPolicy idPolicy) {
this.matches(form, idPolicy);
if (this.actual != null && form != null) {
assertThat(this.actual.getPages()).hasSize(form.getPages().size());
if (this.actual.getPages().size() == form.getPages().size()) {
int size = this.actual.getPages().size();
for (int i = 0; i < size; i++) {
Page actualPage = this.actual.getPages().get(i);
Page page = form.getPages().get(i);
assertThat(actualPage).matchesRecursively(page, idPolicy);
}
}
}
return this;
}
/**
* Asserts that the actual form has the same properties as the given form.
*
* @param form
* The given form
* @param idPolicy
* Indicates if the identifiers of the form elements should match too
* @return The current form assert
*/
public FormAssert matches(Form form, IdPolicy idPolicy) {
if (form != null) {
this.isNotNull();
if (idPolicy == IdPolicy.WITH_ID) {
assertThat(this.actual.getId()).isEqualTo(form.getId());
}
assertThat(this.actual.getLabel()).isEqualTo(form.getLabel());
} else {
this.isNull();
}
return this;
}
public void isValid() {
this.isNotNull();
List<String> ids = new ArrayList<>();
this.actual.getPages().forEach(page -> this.visitPageId(ids, page));
}
private void visitPageId(List<String> ids, Page page) {
if (ids.contains(page.getId())) {
this.failWithMessage("The id of the page <%s> already exists in the form", page.getId());
}
ids.add(page.getId());
page.getGroups().forEach(group -> this.visitGroupId(ids, group));
}
private void visitGroupId(List<String> ids, Group group) {
if (ids.contains(group.getId())) {
this.failWithMessage("The id of the group <%s> already exists in the form", group.getId());
}
ids.add(group.getId());
group.getWidgets().forEach(widget -> this.visitWidgetId(ids, widget));
}
private void visitWidgetId(List<String> ids, AbstractWidget widget) {
if (widget != null) {
if (ids.contains(widget.getId())) {
this.failWithMessage("The id of the widget <%s> already exists in the form", widget.getId());
}
ids.add(widget.getId());
}
}
}