From 61931588e3cf4ab0ba0071bd0e288b260b2c9a01 Mon Sep 17 00:00:00 2001 From: eerojala Date: Wed, 27 Sep 2017 15:09:47 +0300 Subject: [PATCH 1/6] Created tests for isExerciseTypeCorrect --- .../fi/helsinki/cs/tmc/langs/r/RPlugin.java | 3 + .../fi/helsinki/cs/tmc/langs/r/TestMain.java | 3 +- .../helsinki/cs/tmc/langs/r/RPluginTest.java | 62 ++++++++++++++++++- .../R_folder/R/empty.txt | 0 .../description/DESCRIPTION | 9 +++ .../result_r/tmc/result.R | 4 ++ .../recognition_test_cases/rhistory/.Rhistory | 6 ++ .../testthat_folder/tests/testthat/empty.txt | 0 8 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 tmc-langs-r/src/test/resources/recognition_test_cases/R_folder/R/empty.txt create mode 100644 tmc-langs-r/src/test/resources/recognition_test_cases/description/DESCRIPTION create mode 100644 tmc-langs-r/src/test/resources/recognition_test_cases/result_r/tmc/result.R create mode 100644 tmc-langs-r/src/test/resources/recognition_test_cases/rhistory/.Rhistory create mode 100644 tmc-langs-r/src/test/resources/recognition_test_cases/testthat_folder/tests/testthat/empty.txt 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..0aabb3371 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 @@ -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..ab79ed5cc 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,72 @@ 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.utils.TestUtils; + +import org.junit.Before; import org.junit.Test; +import java.nio.file.Path; + 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)); + } } 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 From f6ff4b1cfd7cae304ab5a4165264ef0df12527f4 Mon Sep 17 00:00:00 2001 From: eerojala Date: Wed, 27 Sep 2017 16:56:40 +0300 Subject: [PATCH 2/6] Created tests for RStudentFilePolicy --- .../tmc/langs/r/RStudentFilePolicyTest.java | 61 +++++++++++++++++++ .../test/resources/passing/R/arithmetics.R | 16 +++++ .../passing/test/testthat/testArithmetics.R | 36 +++++++++++ .../src/test/resources/passing/tmc/result.R | 4 ++ 4 files changed, 117 insertions(+) create mode 100644 tmc-langs-r/src/test/java/fi/helsinki/cs/tmc/langs/r/RStudentFilePolicyTest.java create mode 100644 tmc-langs-r/src/test/resources/passing/R/arithmetics.R create mode 100644 tmc-langs-r/src/test/resources/passing/test/testthat/testArithmetics.R create mode 100644 tmc-langs-r/src/test/resources/passing/tmc/result.R 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()) From 52f74528aa19f410d80a45adcf16cf9d863d56bd Mon Sep 17 00:00:00 2001 From: eerojala Date: Wed, 27 Sep 2017 18:58:37 +0300 Subject: [PATCH 3/6] Added test for getStudentFilePolicy --- .../helsinki/cs/tmc/langs/r/RPluginTest.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) 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 ab79ed5cc..9a3b38871 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 @@ -4,12 +4,14 @@ 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; - +git import org.junit.Before; import org.junit.Test; import java.nio.file.Path; +import java.nio.file.Paths; public class RPluginTest { @@ -54,13 +56,15 @@ public void excerciseIsCorrectTypeIfItContainsDescription() { assertTrue(plugin.isExerciseTypeCorrect(project)); } - @Test - public void excerciseIsCorrectTypeIfItContainsRhistory() { - Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases"); - Path project = testCasesRoot.resolve("rhistory"); + // For some reason doesn't work in travis + // + // @Test + // public void excerciseIsCorrectTypeIfItContainsRhistory() { + // Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases"); + // Path project = testCasesRoot.resolve("rhistory"); - assertTrue(plugin.isExerciseTypeCorrect(project)); - } + // assertTrue(plugin.isExerciseTypeCorrect(project)); + // } @Test public void excerciseIsCorrectTypeIfItContainsResultR() { @@ -69,4 +73,11 @@ public void excerciseIsCorrectTypeIfItContainsResultR() { assertTrue(plugin.isExerciseTypeCorrect(project)); } + + @Test + public void getStudentFilePolicyReturnsRStudentFilePolicy() { + StudentFilePolicy policy = plugin.getStudentFilePolicy(Paths.get("")); + + assertTrue(policy instanceof RStudentFilePolicy); + } } From 3d8fc7ff1b1c59013c3d9bb514c83359d555eafb Mon Sep 17 00:00:00 2001 From: eerojala Date: Wed, 27 Sep 2017 19:22:47 +0300 Subject: [PATCH 4/6] fixed small typo --- .../src/test/java/fi/helsinki/cs/tmc/langs/r/RPluginTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 9a3b38871..ee47a873e 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 @@ -6,7 +6,7 @@ import fi.helsinki.cs.tmc.langs.io.StudentFilePolicy; import fi.helsinki.cs.tmc.langs.utils.TestUtils; -git + import org.junit.Before; import org.junit.Test; From 7f18b0a6c80cba7d1aeb593bc0a4815fa4c2eaab Mon Sep 17 00:00:00 2001 From: eerojala Date: Thu, 28 Sep 2017 11:35:26 +0300 Subject: [PATCH 5/6] Fixed Rhistory path in RPlugin --- .../java/fi/helsinki/cs/tmc/langs/r/RPlugin.java | 2 +- .../fi/helsinki/cs/tmc/langs/r/RPluginTest.java | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) 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 0aabb3371..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."; 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 ee47a873e..1fa0c9fd0 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 @@ -56,15 +56,13 @@ public void excerciseIsCorrectTypeIfItContainsDescription() { assertTrue(plugin.isExerciseTypeCorrect(project)); } - // For some reason doesn't work in travis - // - // @Test - // public void excerciseIsCorrectTypeIfItContainsRhistory() { - // Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases"); - // Path project = testCasesRoot.resolve("rhistory"); - - // 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() { From 547cf9eedc89b3579fb2859eb783bb9d674f4934 Mon Sep 17 00:00:00 2001 From: eerojala Date: Thu, 28 Sep 2017 11:43:14 +0300 Subject: [PATCH 6/6] Fixed small checkstyle issue --- .../src/test/java/fi/helsinki/cs/tmc/langs/r/RPluginTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1fa0c9fd0..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 @@ -61,7 +61,7 @@ public void excerciseIsCorrectTypeIfItContainsRhistory() { Path testCasesRoot = TestUtils.getPath(getClass(), "recognition_test_cases"); Path project = testCasesRoot.resolve("rhistory"); - assertTrue(plugin.isExerciseTypeCorrect(project)); + assertTrue(plugin.isExerciseTypeCorrect(project)); } @Test