Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class RPlugin extends AbstractLanguagePlugin {
private static final Path TESTTHAT_FOLDER_PATH = Paths.get("testthat");
private static final Path TMC_FOLDER_PATH = Paths.get("tmc");
private static final Path DESCRIPTION_PATH = Paths.get("DESCRIPTION");
private static final Path RHISTORY_PATH = Paths.get(".RHistory");
private static final Path RHISTORY_PATH = Paths.get(".Rhistory");
private static final Path RESULT_R_PATH = Paths.get("result.R");

private static final String CANNOT_RUN_TESTS_MESSAGE = "Failed to run tests.";
Expand Down Expand Up @@ -86,6 +86,9 @@ public boolean isExerciseTypeCorrect(Path path) {
terminal for the first time.

tmc/result.R contains the call to tmcRtestrunner's runTests function.

NOTE: Files.exists does not seem to be able to verify the R and
testthat folder's existence if they are empty.
*/
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fi.helsinki.cs.tmc.langs.domain.RunResult;
import fi.helsinki.cs.tmc.langs.domain.TestResult;
import fi.helsinki.cs.tmc.langs.utils.ProcessRunner;
import fi.helsinki.cs.tmc.langs.utils.TestUtils;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.SystemUtils;
Expand All @@ -20,15 +21,13 @@ public class TestMain {
* @param args Nothing.
*/
public static void main(String[] args) {

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

}

public static void printTestResult(RunResult rr) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,81 @@

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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import fi.helsinki.cs.tmc.langs.io.StudentFilePolicy;
import fi.helsinki.cs.tmc.langs.utils.TestUtils;

import org.junit.Before;
import org.junit.Test;

import java.nio.file.Path;
import java.nio.file.Paths;

public class RPluginTest {

private RPlugin plugin;

@Before
public void setUp() {
plugin = new RPlugin();
}

@Test
public void testGetAvailablePointsCommand(){

}



@Test
public void testGetPluginName() {
assertEquals("r", plugin.getLanguageName());
}

@Test
public void excerciseIsCorrectTypeIfItContainsRFolder() {
Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases");
Path project = testCasesRoot.resolve("R_folder");

assertTrue(plugin.isExerciseTypeCorrect(project));
}

@Test
public void excerciseIsCorrectTypeIfItContainsTestthatFolder() {
Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases");
Path project = testCasesRoot.resolve("testthat_folder");

assertTrue(plugin.isExerciseTypeCorrect(project));
}

@Test
public void excerciseIsCorrectTypeIfItContainsDescription() {
Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases");
Path project = testCasesRoot.resolve("description");

assertTrue(plugin.isExerciseTypeCorrect(project));
}

@Test
public void excerciseIsCorrectTypeIfItContainsRhistory() {
Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases");
Path project = testCasesRoot.resolve("rhistory");

assertTrue(plugin.isExerciseTypeCorrect(project));
}

@Test
public void excerciseIsCorrectTypeIfItContainsResultR() {
Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases");
Path project = testCasesRoot.resolve("result_r");

assertTrue(plugin.isExerciseTypeCorrect(project));
}

@Test
public void getStudentFilePolicyReturnsRStudentFilePolicy() {
StudentFilePolicy policy = plugin.getStudentFilePolicy(Paths.get(""));

assertTrue(policy instanceof RStudentFilePolicy);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package fi.helsinki.cs.tmc.langs.r;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import fi.helsinki.cs.tmc.langs.utils.TestUtils;

import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;


public class RStudentFilePolicyTest {

private Path projectPath;
private RStudentFilePolicy studentFilePolicy;

@Before
public void setUp() {
projectPath = TestUtils.getPath(getClass(), "passing");
studentFilePolicy = new RStudentFilePolicy(projectPath);
}

@Test
public void testFilesInRDirectoryAreStudentFiles() throws IOException {
List<String> studentFiles = new ArrayList();

TestUtils.collectPaths(projectPath, studentFiles, studentFilePolicy);

assertEquals(1, studentFiles.size());
assertTrue(studentFiles.contains("R" + File.separator + "arithmetics.R"));
}

@Test
public void testFilesInTestthatDirectoryAreNotStudentFiles() throws IOException {
List<String> studentFiles = new ArrayList<>();

TestUtils.collectPaths(projectPath, studentFiles, studentFilePolicy);

assertEquals(1, studentFiles.size());
assertFalse(studentFiles.contains(
"test" + File.separatorChar + "testthat"
+ File.separatorChar + "testArithmetics.R"));
}

@Test
public void testResultRInTmcDirectoryIsNotAStudentFile() throws IOException {
List<String> studentFiles = new ArrayList<>();

TestUtils.collectPaths(projectPath, studentFiles, studentFilePolicy);

assertEquals(1, studentFiles.size());
assertFalse(studentFiles.contains("tmc" + File.separatorChar + "result.R"));
}
}
16 changes: 16 additions & 0 deletions tmc-langs-r/src/test/resources/passing/R/arithmetics.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

add <- function(a, b) {
return(a+b)
}

subtract <- function(a, b) {
return(a-b)
}

multiply <- function(a, b) {
return(a*b)
}

divide <- function(a, b) {
return(a/b)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
library('testthat')

source("../../R/arithmetics.R")

pointsForAllTests(c("r1"))

test("Addition works", c("r1.1", "r1.2"), {
expect_equal(add(1, 2), 3)
expect_equal(add(1, 2), 3.0)
expect_equal(add(1, 4), 5)
})

test("Multiplication works", c("r1.3", "r1.4"), {
expect_equal(multiply(1, 2), 2)
expect_equal(multiply(2, 10), 20)
})

test("Subtraction works", c("r1.5"), {
expect_equal(subtract(10, 2), 8)
expect_equal(subtract(0, 0), 0)
expect_equal(subtract(0, 4), -4)
})

test("Division works", c("r1.6"), {
expect_equal(divide(10, 2), 5)
expect_equal(divide(1, 2), 0.5)
})

test("Test with no points", c(), {
expect_equal(1,1)
})

test("Dummy test set to fail", c(), {
expect_true(FALSE)
expect_equal(1,2)
})
4 changes: 4 additions & 0 deletions tmc-langs-r/src/test/resources/passing/tmc/result.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(tmcRtestrunner)

# Note: working directory must be the projects root!
runTests(getwd())
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Package: project1
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends: R (>= 3.2.3)
License: What license is it under?
Encoding: UTF-8
LazyData: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(tmcRtestrunner)

# Note: working directory must be the projects root!
runTests(getwd())
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2+2
hello()
hello <- function() {
print("Hello, world!")
}
hello()