Skip to content

Commit 31b9ab2

Browse files
committed
Initial configuration for the project and RTestResultParser
1 parent 55e4dfb commit 31b9ab2

File tree

6 files changed

+388
-0
lines changed

6 files changed

+388
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237

238238
<module>tmc-langs-util</module>
239239
<module>tmc-langs-cli</module>
240+
<module>tmc-langs-r</module>
240241
</modules>
241242

242243
<profiles>

tmc-langs-r/pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>fi.helsinki.cs.tmc</groupId>
6+
<artifactId>tmc-langs</artifactId>
7+
<version>0.7.7-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>tmc-langs-r</artifactId>
10+
<version>0.7.7-SNAPSHOT</version>
11+
<packaging>jar</packaging>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>fi.helsinki.cs.tmc</groupId>
16+
<artifactId>tmc-langs-framework</artifactId>
17+
<version>0.7.7-SNAPSHOT</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>fi.helsinki.cs.tmc</groupId>
21+
<artifactId>tmc-langs-util</artifactId>
22+
<version>0.7.7-SNAPSHOT</version>
23+
</dependency>
24+
</dependencies>
25+
26+
</project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package fi.helsinki.cs.tmc.langs.r;
2+
3+
import fi.helsinki.cs.tmc.langs.domain.RunResult;
4+
import fi.helsinki.cs.tmc.langs.domain.TestResult;
5+
6+
import com.fasterxml.jackson.databind.JsonNode;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.google.common.collect.ImmutableList;
9+
import com.google.common.collect.ImmutableMap;
10+
11+
import java.io.IOException;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.nio.file.Paths;
15+
import java.util.ArrayList;
16+
import java.util.HashMap;
17+
import java.util.List;
18+
19+
public class RTestResultParser {
20+
21+
Path path;
22+
private ObjectMapper mapper;
23+
24+
private static Path RESULT_FILE = Paths.get(".results.json");
25+
26+
public RTestResultParser(Path path) {
27+
this.path = path;
28+
this.mapper = new ObjectMapper();
29+
}
30+
31+
RunResult parse() throws IOException {
32+
List<TestResult> testResults = getTestResults();
33+
34+
RunResult.Status status = RunResult.Status.PASSED;
35+
for (TestResult result : testResults) {
36+
if (!result.isSuccessful()) {
37+
status = RunResult.Status.TESTS_FAILED;
38+
}
39+
}
40+
41+
ImmutableList<TestResult> immutableResults = ImmutableList.copyOf(testResults);
42+
ImmutableMap<String, byte[]> logs = ImmutableMap.copyOf(new HashMap<String, byte[]>());
43+
return new RunResult(status, immutableResults, logs);
44+
}
45+
46+
private List<TestResult> getTestResults() throws IOException {
47+
byte[] json = Files.readAllBytes(path.resolve(RESULT_FILE));
48+
49+
List<TestResult> results = new ArrayList<>();
50+
51+
JsonNode tree = mapper.readTree(json);
52+
for (JsonNode node : tree) {
53+
results.add(toTestResult(node));
54+
}
55+
56+
return results;
57+
}
58+
59+
private TestResult toTestResult(JsonNode node) {
60+
List<String> points = new ArrayList<>();
61+
for (JsonNode point : node.get("points")) {
62+
points.add(point.asText());
63+
}
64+
65+
List<String> backTrace = new ArrayList<>();
66+
for (JsonNode line : node.get("backtrace")) {
67+
backTrace.add(line.asText());
68+
}
69+
70+
boolean passed = node.get("status").asText().equals("passed");
71+
72+
return new TestResult(
73+
node.get("name").asText(),
74+
passed,
75+
ImmutableList.copyOf(points),
76+
node.get("message").toString(),
77+
ImmutableList.copyOf(backTrace));
78+
}
79+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package fi.helsinki.cs.tmc.langs.r;
2+
3+
import fi.helsinki.cs.tmc.langs.domain.RunResult;
4+
import fi.helsinki.cs.tmc.langs.domain.TestResult;
5+
6+
import java.io.IOException;
7+
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
11+
public class TestMain {
12+
13+
/**
14+
* Just testing.
15+
* @param args Nothing.
16+
*/
17+
public static void main(String[] args) {
18+
19+
Path path = Paths.get(".");
20+
21+
RunResult rr;
22+
23+
try {
24+
rr = new RTestResultParser(path).parse();
25+
for (TestResult tr : rr.testResults) {
26+
System.out.println(tr.toString());
27+
}
28+
} catch (IOException e) {
29+
System.out.println("Something wrong: " + e.getMessage());
30+
}
31+
}
32+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package fi.helsinki.cs.tmc.langs.r;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import fi.helsinki.cs.tmc.langs.domain.RunResult;
7+
import fi.helsinki.cs.tmc.langs.domain.TestResult;
8+
import fi.helsinki.cs.tmc.langs.utils.TestUtils;
9+
10+
import org.junit.Test;
11+
12+
import java.io.IOException;
13+
import java.nio.file.Path;
14+
15+
public class RTestResultParserTest {
16+
17+
private RunResult rr;
18+
private Path jsonDir;
19+
20+
public RTestResultParserTest() {
21+
jsonDir = TestUtils.getPath(getClass(), "example_json");
22+
try {
23+
rr = new RTestResultParser(jsonDir).parse();
24+
} catch (IOException e) {
25+
System.out.println("Something wrong: " + e.getMessage());
26+
}
27+
}
28+
29+
@Test
30+
public void testThatParseSeemsToWorkOnExampleJson() {
31+
assertEquals(RunResult.Status.TESTS_FAILED, rr.status);
32+
assertEquals(22, rr.testResults.size());
33+
34+
for (TestResult tr : rr.testResults) {
35+
if (tr.getName().equals("Addition works")) {
36+
assertTrue(tr.isSuccessful());
37+
assertEquals(2, tr.points.size());
38+
assertTrue(tr.points.contains("r1.1"));
39+
assertTrue(tr.points.contains("r1.2"));
40+
}
41+
if (!tr.isSuccessful()) {
42+
assertEquals("Dummy test set to fail", tr.getName());
43+
}
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)