-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
CtElementAssertTest.java
71 lines (63 loc) · 3.13 KB
/
CtElementAssertTest.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
/**
* Copyright (C) 2006-2018 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.testing;
import org.junit.jupiter.api.Test;
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static spoon.testing.Assert.assertThat;
import static spoon.testing.utils.ModelUtils.buildNoClasspath;
import static spoon.testing.utils.ModelUtils.createFactory;
public class CtElementAssertTest {
public int i;
@Test
public void testEqualityBetweenTwoCtElement() throws Exception {
final CtType<CtElementAssertTest> type = buildNoClasspath(CtElementAssertTest.class).Type().get(CtElementAssertTest.class);
final Factory factory = createFactory();
final CtField<Integer> expected = factory.Core().createField();
expected.setSimpleName("i");
expected.setType(factory.Type().integerPrimitiveType());
expected.addModifier(ModifierKind.PUBLIC);
CtField<?> f = type.getField("i");
assertThat(f).isEqualTo(expected);
}
@Test
public void testEqualityBetweenACtElementAndAString() throws Exception {
final CtType<CtElementAssertTest> type = buildNoClasspath(CtElementAssertTest.class).Type().get(CtElementAssertTest.class);
assertThat(type.getField("i")).isEqualTo("public int i;");
}
@Test
public void testEqualityBetweenTwoCtElementWithTypeDifferent() {
assertThrows(AssertionError.class, ()-> assertThat(createFactory().Core().createAnnotation()).isEqualTo(createFactory().Core().createBlock()));
}
@Test
public void testEqualityBetweenTwoCtElementWithTheSameSignatureButNotTheSameContent() throws Exception {
assertThrows(AssertionError.class, ()-> assertThat(buildNoClasspath(CtElementAssertTest.class).Type().get(CtElementAssertTest.class)).isEqualTo(createFactory().Class().create(CtElementAssertTest.class.getName())));
}
@Test
public void testEqualityBetweenTwoDifferentCtElement() throws Exception {
class String {
}
final Factory build = buildNoClasspath(CtElementAssertTest.class);
final CtFieldAccess<Class<String>> actual = build.Code().createClassAccess(build.Type().<String>get(String.class).getReference());
final CtFieldAccess<Class<java.lang.String>> expected = createFactory().Code().createClassAccess(createFactory().Type().stringType());
assertThrows(AssertionError.class, ()-> assertThat(actual).isEqualTo(expected));
}
}