Skip to content

Commit efc8ef2

Browse files
Rohitesh-Kumar-Jainwoutersmeenk
authored andcommitted
test: Add tests for the Check class (INRIA#4034)
1 parent 33d3fc3 commit efc8ef2

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package spoon.testing.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.io.TempDir;
5+
6+
import spoon.Launcher;
7+
import spoon.reflect.declaration.CtField;
8+
import spoon.support.reflect.declaration.CtEnumValueImpl;
9+
import spoon.support.reflect.declaration.CtFieldImpl;
10+
11+
import java.io.File;
12+
import java.io.IOException;
13+
14+
import static org.junit.jupiter.api.Assertions.*;
15+
16+
class CheckTest {
17+
18+
@Test
19+
public void testAssertNotNull() {
20+
// contract: assertNotNull throws AssertionError as we pass a null reference, with a default
21+
// error message
22+
23+
String expectedMessage = "Your parameter can't be null.";
24+
25+
AssertionError error = assertThrows(AssertionError.class, () -> {
26+
Check.assertNotNull(null);
27+
});
28+
29+
assertEquals(expectedMessage, error.getMessage());
30+
}
31+
32+
@Test
33+
public void testAssertNotNullWithMessageParameter() {
34+
// contract: assertNotNull throws AssertionError as we pass a null reference, with an
35+
// error message that was passed as an argument
36+
37+
String messageParameter = "testMessage";
38+
39+
AssertionError error = assertThrows(AssertionError.class, () -> {
40+
Check.assertNotNull(messageParameter,null);
41+
});
42+
43+
assertEquals(messageParameter, error.getMessage());
44+
}
45+
46+
@Test
47+
public void testAssertNotNullWithNotNullReference() {
48+
// contract: assertNotNull returns the reference passed without any changes as the reference
49+
// passed was not null
50+
51+
int i = 0;
52+
53+
int j = Check.assertNotNull(i);
54+
55+
assertEquals(i, j);
56+
}
57+
58+
@Test
59+
public void testAssertExistWithoutAnExistingFile(@TempDir File tempDir) {
60+
// contract: assertExists throws AssertionError as a non existing file is passed
61+
62+
File file = getFileWithTestFilePathName(tempDir);
63+
String expectedMessage = "You should specify an existing file.";
64+
65+
AssertionError error = assertThrows(AssertionError.class, () -> {
66+
Check.assertExists(file);
67+
});
68+
69+
assertEquals(expectedMessage, error.getMessage());
70+
}
71+
72+
@Test
73+
public void testAssertExistWithExistingFile(@TempDir File tempDir) throws IOException {
74+
// contract: assertExists the passed file exists, and as it exists it returns the passed file back
75+
76+
File file = getFileWithTestFilePathName(tempDir);
77+
file.createNewFile();
78+
79+
File returnedFile = Check.assertExists(file);
80+
81+
assertEquals(file, returnedFile);
82+
}
83+
84+
@Test
85+
public void testAssertIsSameWithSameElementsIsTrue() {
86+
// contract: assertIsSame returns the assumedActualElement without any changes as the assumedExpectedElement
87+
// and the assumedActualElement were objects of the same class
88+
89+
CtEnumValueImpl<String> assumedExpectedElement = new CtEnumValueImpl();
90+
CtField<Integer> assumedActualElement = new CtEnumValueImpl();
91+
92+
CtField<?> returnedActualElement = Check.assertIsSame(assumedActualElement, assumedExpectedElement);
93+
94+
assertEquals(assumedActualElement, returnedActualElement);
95+
}
96+
97+
@Test
98+
public void testAssertIsSameWithDissimilarElements() {
99+
// contract: assertIsSame throws AssertionError as the assumedExpectedElement and the assumeActualElement
100+
// were not objects of the same class
101+
102+
// arrange
103+
CtEnumValueImpl<String> assumedExpectedElement = new CtEnumValueImpl();
104+
CtField<String> assumedActualElement = new CtFieldImpl();
105+
String expectedMessage = String.format(
106+
"Actual value is typed by %1$s and expected is typed by %2$s, these objects should be the same type.",
107+
assumedActualElement.getClass().getName(), assumedExpectedElement.getClass().getName()
108+
);
109+
110+
// act
111+
AssertionError error = assertThrows(AssertionError.class, () -> {
112+
Check.assertIsSame(assumedActualElement, assumedExpectedElement);
113+
});
114+
115+
// assert
116+
assertEquals(expectedMessage, error.getMessage());
117+
}
118+
119+
private static File getFileWithTestFilePathName(File tempDir) {
120+
Launcher launcher = new Launcher();
121+
launcher.setSourceOutputDirectory(tempDir.getAbsolutePath());
122+
return new File (launcher.getModelBuilder().getSourceOutputDirectory() + "testFile.txt");
123+
}
124+
}

0 commit comments

Comments
 (0)