Skip to content

Commit 768103c

Browse files
eerojalatmoo
authored andcommitted
Running tests works and added tests
1 parent 4594ae6 commit 768103c

File tree

16 files changed

+316
-52
lines changed

16 files changed

+316
-52
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ env:
1919

2020
before_install:
2121
- curl -L https://static.rust-lang.org/rustup.sh | sh -s -- --channel=stable --yes --prefix=$PWD --disable-sudo
22+
- sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9
23+
- echo "deb https://cloud.r-project.org/bin/linux/ubuntu trusty/" | sudo tee -a /etc/apt/sources.list
24+
- sudo apt-get update -qq
25+
- sudo apt-get install r-base -y
26+
- sudo chmod 277 /usr/local/lib/R/site-library
27+
- Rscript -e 'install.packages(c("devtools","testthat"),repos="http://cran.us.r-project.org")'
28+
- Rscript -e 'devtools::install_github("RTMC/tmc-r-tester/tmcRtestrunner")'
2229
- export PATH=$PATH:$PWD/bin
2330
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD/lib
2431
- mkdir -p $HOME/bin && ln -s $(which python3.4) $HOME/bin/python3 && export PATH="$HOME/bin:$PATH"
2532
- mvn install -Dmaven.test.skip=true
33+
2634
script:
2735
- mvn clean test
2836
- mvn checkstyle:check

tmc-langs-r/getAvailablePoints.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
#Currently this script needs to be run at project root!
3+
4+
Rscript -e "library(tmcRtestrunner);get_available_points(\"$PWD\")"

tmc-langs-r/runTests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
#Currently this script needs to be run at project root!
3+
/usr/bin/Rscript -e "library(tmcRtestrunner);run_tests_with_default(TRUE)"

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class RPlugin extends AbstractLanguagePlugin{
4040
private static final Path TESTTHAT_FOLDER_PATH = Paths.get("testthat");
4141
private static final Path TMC_FOLDER_PATH = Paths.get("tmc");
4242
private static final Path DESCRIPTION_PATH = Paths.get("DESCRIPTION");
43-
private static final Path RHISTORY_PATH = Paths.get(".RHistory");
43+
private static final Path RHISTORY_PATH = Paths.get(".Rhistory");
4444
private static final Path RESULT_R_PATH = Paths.get("result.R");
4545

4646

@@ -87,6 +87,9 @@ public boolean isExerciseTypeCorrect(Path path) {
8787
terminal for the first time.
8888
8989
tmc/result.R contains the call to tmcRtestrunner's runTests function.
90+
91+
NOTE: Files.exists does not seem to be able to verify the R and
92+
testthat folder's existence if they are empty.
9093
*/
9194
}
9295

@@ -95,15 +98,14 @@ protected StudentFilePolicy getStudentFilePolicy(Path projectPath) {
9598
return new RStudentFilePolicy(projectPath);
9699
}
97100

98-
99101
@Override
100102
public String getPluginName() {
101103
return "r";
102104
}
103105

104106
@Override
105107
public Optional<ExerciseDesc> scanExercise(Path path, String exerciseName) {
106-
ProcessRunner runner = new ProcessRunner(getAvailablePointsCommand(), path);
108+
ProcessRunner runner = new ProcessRunner(this.getAvailablePointsCommand(), path);
107109
try {
108110
runner.call();
109111
} catch (Exception e) {
@@ -143,21 +145,32 @@ public ValidationResult checkCodeStyle(Path path, Locale messageLocale) throws U
143145
return null;
144146
}
145147

146-
private String[] getTestCommand() {
147-
String[] rscr = new String[] {"Rscript", "-e"};
148+
public String[] getTestCommand() {
149+
150+
String[] rscr;
148151
String[] command;
149152
if (SystemUtils.IS_OS_WINDOWS) {
150-
command = new String[] {"\"library('tmcRtestrunner');runTestsWithDefault(TRUE)\""};
153+
rscr = new String[] {"Rscript", "-e"};
154+
command = new String[] {"\"library('tmcRtestrunner');run_tests_with_default(TRUE)\""};
151155
} else {
152-
command = new String[] {"\"library(tmcRtestrunner);runTests(\"$PWD\", print=TRUE)\""};
156+
rscr = new String[] {"bash"};
157+
command = new String[] {Paths.get("").toAbsolutePath().toString() + "/runTests.sh"};
153158
}
154159
return ArrayUtils.addAll(rscr, command);
155160
}
156161

157-
private String[] getAvailablePointsCommand() {
158-
String[] rscr = new String[] {"Rscript", "-e"};
159-
String[] command = new String[] {"\"library(tmcRtestrunner);"
160-
+ "getAvailablePoints(\"$PWD\")\""};
162+
public String[] getAvailablePointsCommand() {
163+
String[] rscr;
164+
String[] command;
165+
if (SystemUtils.IS_OS_WINDOWS) {
166+
rscr = new String[] {"Rscript", "-e"};
167+
command = new String[] {"\"library(tmcRtestrunner);"
168+
+ "run_available_points(\"$PWD\")\""};
169+
} else {
170+
rscr = new String[] {"bash"};
171+
command = new String[] {Paths.get("").toAbsolutePath().toString()
172+
+ "/getAvailablePoints.sh"};
173+
}
161174
return ArrayUtils.addAll(rscr, command);
162175
}
163176

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

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
import fi.helsinki.cs.tmc.langs.domain.RunResult;
44
import fi.helsinki.cs.tmc.langs.domain.TestResult;
5-
import fi.helsinki.cs.tmc.langs.utils.ProcessRunner;
6-
7-
import org.apache.commons.lang3.ArrayUtils;
8-
import org.apache.commons.lang3.SystemUtils;
9-
10-
import java.io.IOException;
115

126
import java.nio.file.Path;
137
import java.nio.file.Paths;
@@ -20,15 +14,14 @@ public class TestMain {
2014
* @param args Nothing.
2115
*/
2216
public static void main(String[] args) {
23-
2417
//For now, add the path you want to test here fully,
2518
//for example: pathToGithubFolder/tmc-r/example_projects/example_project1
26-
String exampleProjectLocation = "/example_projects/example_project1";
19+
/**String exampleProjectLocation = "/path/to/r-project"
20+
+ "/example_projects/example_project1";
2721
Path path = Paths.get(exampleProjectLocation);
28-
RunResult runRes = runTests(path);
29-
printTestResult(runRes);
30-
RunResult rr;
31-
22+
RPlugin rplugin = new RPlugin();
23+
RunResult runRes = rplugin.runTests(path);
24+
printTestResult(runRes);**/
3225
}
3326

3427
public static void printTestResult(RunResult rr) {
@@ -38,31 +31,4 @@ public static void printTestResult(RunResult rr) {
3831
}
3932

4033

41-
public static RunResult runTests(Path path) {
42-
43-
ProcessRunner runner = new ProcessRunner(getTestCommand(), path);
44-
try {
45-
runner.call();
46-
} catch (Exception e) {
47-
System.out.println("Something wrong: " + e.getMessage());
48-
}
49-
50-
try {
51-
return new RTestResultParser(path).parse();
52-
} catch (IOException e) {
53-
System.out.println("Something wrong: " + e.getMessage());
54-
}
55-
return null;
56-
}
57-
58-
private static String[] getTestCommand() {
59-
String[] rscr = new String[]{"Rscript", "-e"};
60-
String[] command;
61-
if (SystemUtils.IS_OS_WINDOWS) {
62-
command = new String[] {"\"library('tmcRtestrunner');runTestsWithDefault(TRUE)\""};
63-
} else {
64-
command = new String[] {"\"library(tmcRtestrunner);runTests(\"$PWD\", print=TRUE)\""};
65-
}
66-
return ArrayUtils.addAll(rscr, command);
67-
}
6834
}
Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,135 @@
11

22
package fi.helsinki.cs.tmc.langs.r;
33

4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import fi.helsinki.cs.tmc.langs.io.StudentFilePolicy;
8+
import fi.helsinki.cs.tmc.langs.utils.TestUtils;
9+
10+
import org.apache.commons.lang3.SystemUtils;
11+
import org.junit.After;
12+
import org.junit.Assert;
13+
import org.junit.Before;
414
import org.junit.Test;
515

16+
import java.io.File;
17+
import java.nio.file.Path;
18+
import java.nio.file.Paths;
19+
20+
21+
22+
623
public class RPluginTest {
724

25+
private RPlugin plugin;
26+
27+
@Before
28+
public void setUp() {
29+
plugin = new RPlugin();
30+
}
31+
32+
@After
33+
public void tearDown() {
34+
Path testDir = TestUtils.getPath(getClass(), "passing");
35+
File resultsJson = new File(testDir.toAbsolutePath().toString() + "/.results.json");
36+
resultsJson.delete();
37+
}
38+
839
@Test
9-
public void testGetAvailablePointsCommand(){
10-
40+
public void testGetTestCommand() {
41+
if (SystemUtils.IS_OS_WINDOWS) {
42+
String[] expectedCommand = new String[]{"Rscript", "-e",
43+
"\"library('tmcRtestrunner');run_tests_with_default(TRUE)\""};
44+
45+
Assert.assertArrayEquals(expectedCommand,plugin.getTestCommand());
46+
} else if (SystemUtils.IS_OS_LINUX) {
47+
String[] expectedCommand = new String[]{"bash",
48+
Paths.get("").toAbsolutePath().toString() + "/runTests.sh"};
49+
50+
Assert.assertArrayEquals(expectedCommand,plugin.getTestCommand());
51+
}
1152
}
1253

54+
@Test
55+
public void testGetAvailablePointsCommand() {
56+
if (SystemUtils.IS_OS_WINDOWS) {
57+
String[] expectedCommand = new String[]{"Rscript", "-e","\"library('tmcRtestrunner');"
58+
+ "get_available_points(\"$PWD\")\""};
59+
60+
Assert.assertArrayEquals(expectedCommand,plugin.getAvailablePointsCommand());
61+
} else if (SystemUtils.IS_OS_LINUX) {
62+
String[] expectedCommand = new String[]{"bash",
63+
Paths.get("").toAbsolutePath().toString() + "/getAvailablePoints.sh"};
64+
65+
Assert.assertArrayEquals(expectedCommand,plugin.getAvailablePointsCommand());
66+
}
67+
}
68+
69+
@Test
70+
public void testGetPluginName() {
71+
assertEquals("r", plugin.getLanguageName());
72+
}
73+
74+
@Test
75+
public void testScanExercise() {
76+
Path testDir = TestUtils.getPath(getClass(), "passing");
77+
plugin.scanExercise(testDir, "arithmetics.R");
78+
}
79+
80+
@Test
81+
public void testRunTests() {
82+
Path testDir = TestUtils.getPath(getClass(), "passing");
83+
plugin.runTests(testDir);
84+
File resultsJson = new File(testDir.toAbsolutePath().toString() + "/.results.json");
85+
86+
assertTrue(resultsJson.exists());
87+
}
1388

89+
@Test
90+
public void excerciseIsCorrectTypeIfItContainsRFolder() {
91+
Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases");
92+
Path project = testCasesRoot.resolve("R_folder");
93+
94+
assertTrue(plugin.isExerciseTypeCorrect(project));
95+
}
96+
97+
@Test
98+
public void excerciseIsCorrectTypeIfItContainsTestthatFolder() {
99+
Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases");
100+
Path project = testCasesRoot.resolve("testthat_folder");
101+
102+
assertTrue(plugin.isExerciseTypeCorrect(project));
103+
}
104+
105+
@Test
106+
public void excerciseIsCorrectTypeIfItContainsDescription() {
107+
Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases");
108+
Path project = testCasesRoot.resolve("description");
109+
110+
assertTrue(plugin.isExerciseTypeCorrect(project));
111+
}
112+
113+
@Test
114+
public void excerciseIsCorrectTypeIfItContainsRhistory() {
115+
Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases");
116+
Path project = testCasesRoot.resolve("rhistory");
117+
118+
assertTrue(plugin.isExerciseTypeCorrect(project));
119+
}
120+
121+
@Test
122+
public void excerciseIsCorrectTypeIfItContainsResultR() {
123+
Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases");
124+
Path project = testCasesRoot.resolve("result_r");
125+
126+
assertTrue(plugin.isExerciseTypeCorrect(project));
127+
}
128+
129+
@Test
130+
public void getStudentFilePolicyReturnsRStudentFilePolicy() {
131+
StudentFilePolicy policy = plugin.getStudentFilePolicy(Paths.get(""));
132+
133+
assertTrue(policy instanceof RStudentFilePolicy);
134+
}
14135
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package fi.helsinki.cs.tmc.langs.r;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import fi.helsinki.cs.tmc.langs.utils.TestUtils;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.nio.file.Path;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
19+
public class RStudentFilePolicyTest {
20+
21+
private Path projectPath;
22+
private RStudentFilePolicy studentFilePolicy;
23+
24+
@Before
25+
public void setUp() {
26+
projectPath = TestUtils.getPath(getClass(), "passing");
27+
studentFilePolicy = new RStudentFilePolicy(projectPath);
28+
}
29+
30+
@Test
31+
public void testFilesInRDirectoryAreStudentFiles() throws IOException {
32+
List<String> studentFiles = new ArrayList();
33+
34+
TestUtils.collectPaths(projectPath, studentFiles, studentFilePolicy);
35+
36+
assertEquals(2, studentFiles.size());
37+
assertTrue(studentFiles.contains("R" + File.separator + "arithmetics.R"));
38+
}
39+
40+
@Test
41+
public void testFilesInTestthatDirectoryAreNotStudentFiles() throws IOException {
42+
List<String> studentFiles = new ArrayList<>();
43+
44+
TestUtils.collectPaths(projectPath, studentFiles, studentFilePolicy);
45+
46+
assertEquals(2, studentFiles.size());
47+
assertFalse(studentFiles.contains(
48+
"test" + File.separatorChar + "testthat"
49+
+ File.separatorChar + "testArithmetics.R"));
50+
}
51+
52+
@Test
53+
public void testResultRInTmcDirectoryIsNotAStudentFile() throws IOException {
54+
List<String> studentFiles = new ArrayList<>();
55+
56+
TestUtils.collectPaths(projectPath, studentFiles, studentFilePolicy);
57+
58+
assertEquals(2, studentFiles.size());
59+
assertFalse(studentFiles.contains("tmc" + File.separatorChar + "result.R"));
60+
}
61+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
add <- function(a, b) {
3+
return(a+b)
4+
}
5+
6+
subtract <- function(a, b) {
7+
return(a-b)
8+
}
9+
10+
multiply <- function(a, b) {
11+
return(a*b)
12+
}
13+
14+
divide <- function(a, b) {
15+
return(a/b)
16+
}

0 commit comments

Comments
 (0)