Skip to content

Commit fc19d17

Browse files
committed
RTestResultParser parses updated .results.json format and creates a specific kind of RunTest if sourcing has failed
1 parent 8d1e9e0 commit fc19d17

File tree

15 files changed

+297
-257
lines changed

15 files changed

+297
-257
lines changed

tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/RExerciseDescParser.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
import java.nio.file.Paths;
1414
import java.util.ArrayList;
1515
import java.util.List;
16-
16+
import java.util.Map;
1717

1818

1919
class RExerciseDescParser {
2020

2121
private static Path RESULT_FILE = Paths.get(".available_points.json");
22-
private static final TypeReference<List<RResult>> MAP_TYPE_REFERENCE =
23-
new TypeReference<List<RResult>>() {};
22+
private static final TypeReference<Map<String, List<String>>> MAP_TYPE_REFERENCE =
23+
new TypeReference<Map<String, List<String>>>() {};
2424
private Path path;
2525
private ObjectMapper mapper;
2626

@@ -30,14 +30,16 @@ public RExerciseDescParser(Path path) {
3030
}
3131

3232
public ImmutableList<TestDesc> parse() throws IOException {
33-
3433
List<TestDesc> testDescs = new ArrayList<>();
34+
3535
byte[] json = Files.readAllBytes(path.resolve(RESULT_FILE));
36-
List<RResult> parse = mapper.readValue(json, MAP_TYPE_REFERENCE);
37-
for (RResult result : parse) {
38-
ImmutableList<String> points = ImmutableList.copyOf(result.getPoints());
39-
testDescs.add(new TestDesc(result.getName(), points));
36+
Map<String, List<String>> parse = mapper.readValue(json, MAP_TYPE_REFERENCE);
37+
38+
for (String name : parse.keySet()) {
39+
ImmutableList<String> points = ImmutableList.copyOf(parse.get(name));
40+
testDescs.add(new TestDesc(name, points));
4041
}
42+
4143
return ImmutableList.copyOf(testDescs);
4244
}
4345

tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/RResult.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/RTestResultParser.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ RunResult parse() throws IOException {
3737
status = RunResult.Status.TESTS_FAILED;
3838
}
3939
}
40+
if (!testResults.isEmpty() && testResults.get(0).getName().equals("COMPILATION FAILED")) {
41+
status = RunResult.Status.COMPILE_FAILED;
42+
}
4043

4144
ImmutableList<TestResult> immutableResults = ImmutableList.copyOf(testResults);
4245
ImmutableMap<String, byte[]> logs = ImmutableMap.copyOf(new HashMap<String, byte[]>());
@@ -45,9 +48,24 @@ RunResult parse() throws IOException {
4548

4649
private List<TestResult> getTestResults() throws IOException {
4750
byte[] json = Files.readAllBytes(path.resolve(RESULT_FILE));
48-
4951
List<TestResult> results = new ArrayList<>();
5052

53+
JsonNode runStatus = mapper.readTree(json).get("runStatus");
54+
if (!runStatus.toString().equals("\"success\"")) {
55+
List<String> backTrace = new ArrayList<>();
56+
for (JsonNode line : mapper.readTree(json).get("backtrace")) {
57+
backTrace.add(line.asText());
58+
}
59+
60+
List<String> dummy = new ArrayList();
61+
results.add(new TestResult(
62+
"COMPILATION FAILED",
63+
false,
64+
ImmutableList.copyOf(dummy),
65+
"Something wrong with source code",
66+
ImmutableList.copyOf(backTrace)));
67+
}
68+
5169
JsonNode tree = mapper.readTree(json).get("testResults");
5270
for (JsonNode node : tree) {
5371
results.add(toTestResult(node));

tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/TestMain.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ public class TestMain {
1616
public static void main(String[] args) {
1717
//For now, add the path you want to test here fully,
1818
//for example: pathToGithubFolder/tmc-r/example_projects/example_project1
19-
/**String exampleProjectLocation = "/path/to/r-project"
20-
+ "/example_projects/example_project1";
21-
Path path = Paths.get(exampleProjectLocation);
22-
RPlugin rplugin = new RPlugin();
23-
RunResult runRes = rplugin.runTests(path);
24-
printTestResult(runRes);**/
19+
// String exampleProjectLocation = "/path/to/r-project"
20+
// + "/example_projects/example_project1";
21+
// Path path = Paths.get(exampleProjectLocation);
22+
// RPlugin rplugin = new RPlugin();
23+
// RunResult runRes = rplugin.runTests(path);
24+
//
25+
// printTestResult(runRes);
26+
//
27+
// System.out.println(runRes.status);
2528
}
2629

2730
public static void printTestResult(RunResult rr) {

tmc-langs-r/src/test/java/fi/helsinki/cs/tmc/langs/r/RPluginTest.java

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import static org.junit.Assert.assertTrue;
77

88
import fi.helsinki.cs.tmc.langs.domain.RunResult;
9-
import fi.helsinki.cs.tmc.langs.domain.TestDesc;
109
import fi.helsinki.cs.tmc.langs.domain.TestResult;
1110
import fi.helsinki.cs.tmc.langs.io.StudentFilePolicy;
1211
import fi.helsinki.cs.tmc.langs.utils.TestUtils;
@@ -24,16 +23,6 @@
2423
import java.nio.file.Path;
2524
import java.nio.file.Paths;
2625

27-
28-
29-
30-
31-
32-
33-
34-
35-
36-
3726
public class RPluginTest {
3827

3928
private RPlugin plugin;
@@ -48,9 +37,14 @@ public void tearDown() {
4837
Path testDir = TestUtils.getPath(getClass(), "project1");
4938
File resultsJson = new File(testDir.toAbsolutePath().toString() + "/.results.json");
5039
resultsJson.delete();
51-
File availablePointsJson = new File(testDir.toAbsolutePath().toString()
52-
+ "/.available_points.json");
53-
availablePointsJson.delete();
40+
41+
testDir = TestUtils.getPath(getClass(), "simple_source_code_error");
42+
resultsJson = new File(testDir.toAbsolutePath().toString() + "/.results.json");
43+
resultsJson.delete();
44+
45+
testDir = TestUtils.getPath(getClass(), "simple_all_tests_pass");
46+
resultsJson = new File(testDir.toAbsolutePath().toString() + "/.results.json");
47+
resultsJson.delete();
5448
}
5549

5650
@Test
@@ -96,50 +90,62 @@ public void testScanExercise() {
9690
}
9791

9892
@Test
99-
public void testScanExerciseInTheWrongPlace() {
100-
Path testDir = TestUtils.getPath(getClass(), "project1");
101-
plugin.scanExercise(testDir, "ar.R");
102-
Path availablePointsJson = testDir.resolve(".available_points.json");
103-
ImmutableList<TestDesc> re = null;
104-
try {
105-
re = new RExerciseDescParser(availablePointsJson).parse();
106-
} catch (IOException e) {
107-
System.out.println("Something wrong: " + e.getMessage());
108-
}
109-
assertTrue(re == null);
93+
public void runTestsCreatesAJson() {
94+
Path testDir = TestUtils.getPath(getClass(), "passing");
95+
plugin.runTests(testDir);
96+
File resultsJson = new File(testDir.toAbsolutePath().toString() + "/.results.json");
97+
98+
assertTrue(resultsJson.exists());
11099
}
111100

112101
@Test
113-
public void testRunTests() {
114-
Path testDir = TestUtils.getPath(getClass(), "project1");
115-
RunResult runRes = plugin.runTests(testDir);
116-
ImmutableList<TestResult> re = runRes.testResults;
117-
assertEquals(re.size(),22);
118-
assertEquals(re.get(0).getName(),"Addition works");
119-
assertTrue(re.get(1).isSuccessful());
120-
assertEquals(re.get(1).getName(),"Multiplication works");
121-
assertTrue(re.get(2).isSuccessful());
122-
assertEquals(re.get(2).getName(),"Subtraction works");
123-
assertTrue(re.get(3).isSuccessful());
124-
assertEquals(re.get(3).getName(),"Division works");
125-
assertTrue(re.get(4).isSuccessful());
126-
assertEquals(re.get(4).getName(), "Test with no points");
127-
assertFalse(re.get(5).isSuccessful());
128-
assertEquals(re.get(5).getName(), "Dummy test set to fail");
129-
assertTrue(re.get(6).isSuccessful());
130-
assertEquals(re.get(6).getName(), "Matrix transpose with [[1,2]] works");
131-
assertTrue(re.get(7).isSuccessful());
132-
assertEquals(re.get(7).getName(), "Matrix transpose with [[1,2],[3,4]] works");
133-
assertTrue(re.get(8).isSuccessful());
134-
assertEquals(re.get(8).getName(), "Constant string works");
135-
for (int i = 1;i <= 13;i++) {
136-
assertEquals(re.get(8 + i).getName(), "Exercise " + i + " is correct");
137-
assertTrue(re.get(8 + i).isSuccessful());
102+
public void runTestsCreatesJsonWithCorrectStatus() {
103+
Path testDir = TestUtils.getPath(getClass(), "passing");
104+
RunResult res = plugin.runTests(testDir);
105+
106+
assertEquals(RunResult.Status.TESTS_FAILED, res.status);
107+
}
108+
109+
@Test
110+
public void runTestsCreatesJsonWithCorrectNumberOfResults() {
111+
Path testDir = TestUtils.getPath(getClass(), "passing");
112+
RunResult res = plugin.runTests(testDir);
113+
114+
assertEquals(19, res.testResults.size());
115+
}
138116

117+
@Test
118+
public void testResultsFromRunTestsHaveCorrectStatuses() {
119+
Path testDir = TestUtils.getPath(getClass(), "passing");
120+
RunResult res = plugin.runTests(testDir);
121+
122+
for (TestResult tr : res.testResults) {
123+
if (!tr.getName().equals("Dummy test set to fail")) {
124+
assertTrue(tr.isSuccessful());
125+
} else {
126+
assertFalse(tr.isSuccessful());
127+
}
139128
}
140-
File resultsJson = new File(testDir.toAbsolutePath().toString() + "/.results.json");
129+
}
141130

142-
assertTrue(resultsJson.exists());
131+
@Test
132+
public void runTestsWorksWithErronousSourceCode() {
133+
Path testDir = TestUtils.getPath(getClass(), "simple_source_code_error");
134+
RunResult res = plugin.runTests(testDir);
135+
136+
assertEquals(RunResult.Status.COMPILE_FAILED, res.status);
137+
assertEquals(1, res.testResults.size());
138+
}
139+
140+
@Test
141+
public void runTestsHasCorrectStatusesWhenAllTestsPass() {
142+
Path testDir = TestUtils.getPath(getClass(), "simple_all_tests_pass");
143+
RunResult res = plugin.runTests(testDir);
144+
145+
assertEquals(RunResult.Status.PASSED, res.status);
146+
for (TestResult tr : res.testResults) {
147+
assertTrue(tr.isSuccessful());
148+
}
143149
}
144150

145151
@Test

0 commit comments

Comments
 (0)