diff --git a/tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/RPlugin.java b/tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/RPlugin.java index 8b93a00c5..e9ea94acb 100644 --- a/tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/RPlugin.java +++ b/tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/RPlugin.java @@ -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."; @@ -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. */ } diff --git a/tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/TestMain.java b/tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/TestMain.java index 82b00e175..cc193ec63 100644 --- a/tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/TestMain.java +++ b/tmc-langs-r/src/main/java/fi/helsinki/cs/tmc/langs/r/TestMain.java @@ -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; @@ -20,7 +21,6 @@ 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"; @@ -28,7 +28,6 @@ public static void main(String[] args) { RunResult runRes = runTests(path); printTestResult(runRes); RunResult rr; - } public static void printTestResult(RunResult rr) { diff --git a/tmc-langs-r/src/test/java/fi/helsinki/cs/tmc/langs/r/RPluginTest.java b/tmc-langs-r/src/test/java/fi/helsinki/cs/tmc/langs/r/RPluginTest.java index 4a0882319..f007905ca 100644 --- a/tmc-langs-r/src/test/java/fi/helsinki/cs/tmc/langs/r/RPluginTest.java +++ b/tmc-langs-r/src/test/java/fi/helsinki/cs/tmc/langs/r/RPluginTest.java @@ -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); + } } diff --git a/tmc-langs-r/src/test/java/fi/helsinki/cs/tmc/langs/r/RStudentFilePolicyTest.java b/tmc-langs-r/src/test/java/fi/helsinki/cs/tmc/langs/r/RStudentFilePolicyTest.java new file mode 100644 index 000000000..05c91dcfd --- /dev/null +++ b/tmc-langs-r/src/test/java/fi/helsinki/cs/tmc/langs/r/RStudentFilePolicyTest.java @@ -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 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 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 studentFiles = new ArrayList<>(); + + TestUtils.collectPaths(projectPath, studentFiles, studentFilePolicy); + + assertEquals(1, studentFiles.size()); + assertFalse(studentFiles.contains("tmc" + File.separatorChar + "result.R")); + } +} \ No newline at end of file diff --git a/tmc-langs-r/src/test/resources/passing/R/arithmetics.R b/tmc-langs-r/src/test/resources/passing/R/arithmetics.R new file mode 100644 index 000000000..fe5f31853 --- /dev/null +++ b/tmc-langs-r/src/test/resources/passing/R/arithmetics.R @@ -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) +} diff --git a/tmc-langs-r/src/test/resources/passing/test/testthat/testArithmetics.R b/tmc-langs-r/src/test/resources/passing/test/testthat/testArithmetics.R new file mode 100644 index 000000000..f889db744 --- /dev/null +++ b/tmc-langs-r/src/test/resources/passing/test/testthat/testArithmetics.R @@ -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) +}) diff --git a/tmc-langs-r/src/test/resources/passing/tmc/result.R b/tmc-langs-r/src/test/resources/passing/tmc/result.R new file mode 100644 index 000000000..73f5a9196 --- /dev/null +++ b/tmc-langs-r/src/test/resources/passing/tmc/result.R @@ -0,0 +1,4 @@ +library(tmcRtestrunner) + +# Note: working directory must be the projects root! +runTests(getwd()) diff --git a/tmc-langs-r/src/test/resources/recognition_test_cases/R_folder/R/empty.txt b/tmc-langs-r/src/test/resources/recognition_test_cases/R_folder/R/empty.txt new file mode 100644 index 000000000..e69de29bb diff --git a/tmc-langs-r/src/test/resources/recognition_test_cases/description/DESCRIPTION b/tmc-langs-r/src/test/resources/recognition_test_cases/description/DESCRIPTION new file mode 100644 index 000000000..329bf0b74 --- /dev/null +++ b/tmc-langs-r/src/test/resources/recognition_test_cases/description/DESCRIPTION @@ -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 diff --git a/tmc-langs-r/src/test/resources/recognition_test_cases/result_r/tmc/result.R b/tmc-langs-r/src/test/resources/recognition_test_cases/result_r/tmc/result.R new file mode 100644 index 000000000..73f5a9196 --- /dev/null +++ b/tmc-langs-r/src/test/resources/recognition_test_cases/result_r/tmc/result.R @@ -0,0 +1,4 @@ +library(tmcRtestrunner) + +# Note: working directory must be the projects root! +runTests(getwd()) diff --git a/tmc-langs-r/src/test/resources/recognition_test_cases/rhistory/.Rhistory b/tmc-langs-r/src/test/resources/recognition_test_cases/rhistory/.Rhistory new file mode 100644 index 000000000..7c6881529 --- /dev/null +++ b/tmc-langs-r/src/test/resources/recognition_test_cases/rhistory/.Rhistory @@ -0,0 +1,6 @@ +2+2 +hello() +hello <- function() { +print("Hello, world!") +} +hello() diff --git a/tmc-langs-r/src/test/resources/recognition_test_cases/testthat_folder/tests/testthat/empty.txt b/tmc-langs-r/src/test/resources/recognition_test_cases/testthat_folder/tests/testthat/empty.txt new file mode 100644 index 000000000..e69de29bb