Skip to content

Commit e9cb470

Browse files
hyttijantmoo
authored andcommitted
Added parsing for available points
1 parent 1ef1de2 commit e9cb470

File tree

10 files changed

+297
-12
lines changed

10 files changed

+297
-12
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
package fi.helsinki.cs.tmc.langs.r;
3+
4+
import fi.helsinki.cs.tmc.langs.domain.TestDesc;
5+
6+
import com.fasterxml.jackson.core.type.TypeReference;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.google.common.collect.ImmutableList;
9+
10+
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
19+
20+
class RExerciseDescParser {
21+
22+
private static Path RESULT_FILE = Paths.get(".available_points.json");
23+
private static final TypeReference<Map<String, List<String>>> MAP_TYPE_REFERENCE =
24+
new TypeReference<Map<String, List<String>>>() {};
25+
private Path path;
26+
private ObjectMapper mapper;
27+
28+
public RExerciseDescParser(Path path) {
29+
this.path = path;
30+
this.mapper = new ObjectMapper();
31+
}
32+
33+
public ImmutableList<TestDesc> parse() throws IOException {
34+
List<TestDesc> testDescs = new ArrayList<>();
35+
byte[] json = Files.readAllBytes(path.resolve(RESULT_FILE));
36+
Map<String, List<String>> parse = mapper.readValue(json, MAP_TYPE_REFERENCE);
37+
for (String name : parse.keySet()) {
38+
ImmutableList<String> points = ImmutableList.copyOf(parse.get(name));
39+
testDescs.add(new TestDesc(name, points));
40+
}
41+
return ImmutableList.copyOf(testDescs);
42+
}
43+
44+
}

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package fi.helsinki.cs.tmc.langs.r;
22

3+
34
import fi.helsinki.cs.tmc.langs.AbstractLanguagePlugin;
45
import fi.helsinki.cs.tmc.langs.abstraction.ValidationResult;
56
import fi.helsinki.cs.tmc.langs.domain.ExerciseBuilder;
67
import fi.helsinki.cs.tmc.langs.domain.ExerciseDesc;
78
import fi.helsinki.cs.tmc.langs.domain.RunResult;
9+
import fi.helsinki.cs.tmc.langs.domain.TestDesc;
810
import fi.helsinki.cs.tmc.langs.io.StudentFilePolicy;
911
import fi.helsinki.cs.tmc.langs.io.sandbox.StudentFileAwareSubmissionProcessor;
10-
import fi.helsinki.cs.tmc.langs.io.sandbox.SubmissionProcessor;
1112
import fi.helsinki.cs.tmc.langs.io.zip.StudentFileAwareUnzipper;
1213
import fi.helsinki.cs.tmc.langs.io.zip.StudentFileAwareZipper;
13-
import fi.helsinki.cs.tmc.langs.io.zip.Unzipper;
14-
import fi.helsinki.cs.tmc.langs.io.zip.Zipper;
15-
import fi.helsinki.cs.tmc.langs.python3.Python3TestResultParser;
14+
1615
import fi.helsinki.cs.tmc.langs.utils.ProcessRunner;
1716

17+
1818
import com.google.common.base.Optional;
19+
import com.google.common.collect.ImmutableList;
20+
1921

2022
import org.apache.commons.lang3.ArrayUtils;
2123
import org.apache.commons.lang3.SystemUtils;
@@ -62,7 +64,20 @@ public String getPluginName() {
6264

6365
@Override
6466
public Optional<ExerciseDesc> scanExercise(Path path, String exerciseName) {
65-
return null;
67+
ProcessRunner runner = new ProcessRunner(getAvailablePointsCommand(), path);
68+
try {
69+
runner.call();
70+
} catch (Exception e) {
71+
System.out.println(e);
72+
log.error(CANNOT_SCAN_EXERCISE_MESSAGE, e);
73+
}
74+
try {
75+
ImmutableList<TestDesc> testDescs = new RExerciseDescParser(path).parse();
76+
return Optional.of(new ExerciseDesc(exerciseName, testDescs));
77+
} catch (IOException e) {
78+
log.error(CANNOT_PARSE_EXERCISE_DESCRIPTION_MESSAGE, e);
79+
}
80+
return Optional.absent();
6681
}
6782

6883
@Override
@@ -98,7 +113,14 @@ private String[] getTestCommand() {
98113
}
99114
return ArrayUtils.addAll(rscr, command);
100115
}
101-
116+
117+
private String[] getAvailablePointsCommand() {
118+
String[] rscr = new String[] {"Rscript", "-e"};
119+
String[] command = new String[] {"\"library('tmcRtestrunner');"
120+
+ "getAvailablePoints(\"$PWD\")\""};
121+
return ArrayUtils.addAll(rscr, command);
122+
}
123+
102124
@Override
103125
public void clean(Path path) {
104126

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private TestResult toTestResult(JsonNode node) {
6767
backTrace.add(line.asText());
6868
}
6969

70-
boolean passed = node.get("status").asText().equals("pass");
70+
boolean passed = node.get("status").asText().equals("passed");
7171

7272
return new TestResult(
7373
node.get("name").asText(),

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ public static void main(String[] args) {
2323

2424
//For now, add the path you want to test here fully,
2525
//for example: pathToGithubFolder/tmc-r/example_projects/example_project1
26-
String exampleProjectLocation = "/tmc-r/example_projects/example_project1";
27-
Path path = Paths.get(exampleProjectLocation);
28-
RunResult runRes = runTests(path);
29-
printTestResult(runRes);
30-
RunResult rr;
26+
3127

3228
/* try {
3329
rr = new RTestResultParser(path).parse();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
package fi.helsinki.cs.tmc.langs.r;
3+
4+
import static org.junit.Assert.assertEquals;
5+
6+
import fi.helsinki.cs.tmc.langs.domain.TestDesc;
7+
import fi.helsinki.cs.tmc.langs.utils.TestUtils;
8+
9+
import com.google.common.collect.ImmutableList;
10+
11+
import org.junit.Test;
12+
13+
import java.io.IOException;
14+
import java.nio.file.Path;
15+
16+
17+
18+
public class RExerciseDescParserTest {
19+
private ImmutableList<TestDesc> re;
20+
private Path jsonDir;
21+
22+
public RExerciseDescParserTest() {
23+
jsonDir = TestUtils.getPath(getClass(), "example_json");
24+
try {
25+
re = new RExerciseDescParser(jsonDir).parse();
26+
} catch (IOException e) {
27+
System.out.println("Something wrong: " + e.getMessage());
28+
}
29+
}
30+
31+
@Test
32+
public void testThatParseSeemsToWorkOnExampleJson() {
33+
assertEquals(re.size(),6);
34+
assertEquals(re.get(0).points.size(),2);
35+
assertEquals(re.get(0).name,"Addition works");
36+
assertEquals(re.get(1).points.size(),2);
37+
assertEquals(re.get(1).name,"Multiplication works");
38+
assertEquals(re.get(2).points.size(),1);
39+
assertEquals(re.get(2).name,"Subtraction works");
40+
assertEquals(re.get(3).points.size(),1);
41+
assertEquals(re.get(3).name,"Division works");
42+
assertEquals(re.get(4).points.size(),0);
43+
assertEquals(re.get(4).name, "Test with no points");
44+
assertEquals(re.get(5).points.size(),0);
45+
assertEquals(re.get(5).name, "Dummy test set to fail");
46+
}
47+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
package fi.helsinki.cs.tmc.langs.r;
3+
4+
import org.junit.Test;
5+
6+
public class RPluginTest {
7+
8+
@Test
9+
public void testGetAvailablePointsCommand(){
10+
11+
}
12+
13+
14+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void testThatParseSeemsToWorkOnExampleJson() {
3232
assertEquals(22, rr.testResults.size());
3333

3434
for (TestResult tr : rr.testResults) {
35+
System.out.println(tr);
3536
if (tr.getName().equals("Addition works")) {
3637
assertTrue(tr.isSuccessful());
3738
assertEquals(2, tr.points.size());
@@ -43,4 +44,5 @@ public void testThatParseSeemsToWorkOnExampleJson() {
4344
}
4445
}
4546
}
47+
4648
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{"Addition works": ["r1.1","r1.2"],
2+
"Multiplication works":["r1.3", "r1.4"],
3+
"Subtraction works":["r1.5"],
4+
"Division works":["r1.6"],
5+
"Test with no points":[],
6+
"Dummy test set to fail":[]
7+
}
8+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
##These exercises are taken from the course "Data-analyysi R-ohjelmistolla"
2+
#(https://wiki.helsinki.fi/pages/viewpage.action?pageId=135074576).
3+
##
4+
5+
##Exercise 1
6+
7+
a1 <- 4 + 10
8+
b1 <- 5*a1
9+
c1 <- b1^3
10+
d1 <- exp(b1)
11+
e1 <- d1^(1/10)
12+
f1 <- sin(c1)
13+
res1 <- (b1 + c1 + e1 + f1)
14+
15+
##Exercise 2
16+
17+
a2 <- 51%%7
18+
19+
##Excercise 3
20+
a3 <- 51%%7 + 51%/%7
21+
22+
##Excercise 4
23+
v4_1 <- c(20, 5, -2, 3, 47)
24+
v4_2 <- seq(0, 100, 5)
25+
v4_3 <- c(v4_1, v4_2)
26+
v4_4 <- v4_3[(v4_3 > 3) & (v4_3 < 50)]
27+
v4_5 <- v4_4[(v4_4 %% 10) == 0]
28+
29+
##Exercise 5
30+
v5_1 <- rep(0, 1, 50)
31+
v5_1[c(F, T)] <- 2
32+
33+
sum5_1 <- sum(v5_1)
34+
v5_1
35+
v5_2 <- v5_1
36+
v5_2[c(T, F)] <- 1.2
37+
sum5_2 <- sum(v5_2)
38+
39+
##Excerice 6
40+
41+
A <- matrix(c(3, 5, 6, 1/2, sqrt(5), 16, 0, 2, 0),nrow = 3, ncol = 3, byrow = TRUE)
42+
43+
44+
##Excercise 7
45+
B <- c(1, 1, 0)%*% solve(A)
46+
47+
##Excercise 8
48+
I_3 <- diag(c(1, 1, 1))
49+
A_8 <- A %*%I_3
50+
51+
##Excercise 9
52+
is_eq_matrix <- t(A) == A
53+
is_eq_matrix <- F
54+
55+
##Excercise 10
56+
C_10 <- matrix(c(runif(20, min = 1, max=20)), ncol=4)
57+
v10_1 <- C_10[C_10 < 5]
58+
number10 <- length(v10_1)
59+
60+
D_10 <- C_10
61+
D_10[D_10 < 5] <- 10
62+
63+
E_10 <- D_10
64+
E_10 <- D_10[c(T, T, T, T, F),c(F,T,T, T)]
65+
66+
67+
##Excercise 11
68+
C_11 <- matrix(1:100, ncol=2)
69+
C_11[c(F, T)] <- NA
70+
71+
##Excercise 12
72+
C_12 <- C_11
73+
C_12[is.na(C_12)] <- 0
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
library('testthat')
2+
source('../../R/week1.R')
3+
4+
points_for_all_tests(c("r4"))
5+
6+
help_A <- matrix(c(3, 5, 6, 1/2, sqrt(5), 16, 0, 2, 0),nrow = 3, ncol = 3, byrow = TRUE)
7+
8+
test("Exercise 1 is correct", c("r4.1"), {
9+
expect_equal(a1, 14)
10+
expect_equal(b1, 70)
11+
expect_equal(c1, 343000)
12+
expect_true(abs(e1-1096.63) < 0.1)
13+
expect_true(abs(f1 - 0.7920019) < 0.1)
14+
expect_true(abs(res1 - 344167.4) < 0.1)
15+
})
16+
17+
test("Exercise 2 is correct", c("r4.2"), {
18+
expect_equal(a2, 2)
19+
})
20+
21+
test("Exercise 3 is correct", c("r4.3", "r4.4"), {
22+
expect_equal(a3, 9)
23+
})
24+
25+
test("Exercise 4 is correct", c("r4.5"), {
26+
expect_equal(v4_1, c(20, 5, -2, 3, 47))
27+
expect_equal(v4_2, c(0:20)*5)
28+
expect_equal(v4_3, c(c(20, 5, -2, 3, 47), seq(0, 100, 5)))
29+
expect_equal(v4_4, c(20, 5, 47, 5, 10, 15, 20, 25, 30, 35, 40, 45))
30+
expect_equal(v4_5, c(20, 10, 20, 30, 40))
31+
})
32+
33+
test("Exercise 5 is correct", c("r4.6"), {
34+
t1 <- rep(0, 1, 50)
35+
t1[c(F, T)] <- 2
36+
expect_equal(v5_1, t1)
37+
expect_equal(sum5_1, 50)
38+
expect_equal(sum5_2, 80)
39+
})
40+
41+
test("Exercise 6 is correct", c("r4.7"), {
42+
expect_equal(A, matrix(c(3, 5, 6, 1/2, sqrt(5), 16, 0, 2, 0),nrow = 3, ncol = 3, byrow = TRUE))
43+
})
44+
45+
test("Exercise 7 is correct", c("r4.8"), {
46+
expect_equal(A, help_A)
47+
})
48+
49+
test("Exercise 8 is correct", c("r4.9"), {
50+
expect_equal(B, c(1, 1, 0)%*%solve(help_A))
51+
})
52+
53+
test("Exercise 9 is correct", c("r4.10"), {
54+
expect_equal(I_3, diag(c(1, 1, 1)))
55+
expect_equal(A_8, help_A)
56+
})
57+
58+
test("Exercise 10 is correct", c("r4.11"), {
59+
expect_false(is_eq_matrix)
60+
})
61+
62+
test("Exercise 11 is correct", c("r4.12"), {
63+
expect_true(number10 < 10)
64+
65+
expect_true(dim(E_10)[1] == 4)
66+
expect_true(dim(E_10)[2] == 3)
67+
})
68+
69+
test("Exercise 12 is correct", c("r4.13"), {
70+
M <- matrix(1:100, ncol=2)
71+
M[c(F, T)] <- NA
72+
expect_equal(C_11, M)
73+
})
74+
75+
test("Exercise 13 is correct", c("r4.14"), {
76+
M <- matrix(1:100, ncol=2)
77+
M[c(F, T)] <- 0
78+
expect_equal(C_12, M)
79+
})

0 commit comments

Comments
 (0)