add tests to sample package; fix load/unload behavior
authorBenjamin Auder <benjamin.a@mailoo.org>
Sun, 1 Feb 2015 12:46:28 +0000 (13:46 +0100)
committerBenjamin Auder <benjamin.a@mailoo.org>
Sun, 1 Feb 2015 12:46:28 +0000 (13:46 +0100)
.gitignore
R/load.R
R/runRtests.R
R/zzz.R [new file with mode: 0644]
pkgTest/NAMESPACE
pkgTest/R/tests/t.a.R [new file with mode: 0644]
pkgTest/R/zzz.R
pkgTest/src/sources/d.c [moved from pkgTest/src/d.c with 100% similarity]
pkgTest/src/tests/t.d.c [new file with mode: 0644]

index 5393fd3..a6e78fd 100644 (file)
@@ -1,2 +1,3 @@
-/pkgTest/src/d.o
+/pkgTest/src/sources/d.o
+/pkgTest/src/tests/t.d.o
 /pkgTest/src/pkgTest.so
index 2d6d122..646f60d 100644 (file)
--- a/R/load.R
+++ b/R/load.R
 
     if (file.exists(file.path(path,"src"))) {
         # C code -- Warning: src/tests folder should not be listed
-        cFiles = list.files( file.path(path,"src"), pattern="\\.[cChH]$",
-            full.names=TRUE, recursive=TRUE, no..=TRUE )
-        cFiles = lapply(cFiles, function(x) {
-            if (nchar(x)>=5 && substr(x,1,5)=="tests") return ("")
-            return (x)
-        })
-        
+        cFiles = c(
+        list.files(file.path(path,"src","sources"),pattern="\\.[cChH]$",
+            full.names=TRUE, recursive=TRUE, no..=TRUE),
+        list.files(file.path(path,"src","adapters"),pattern="\\.[cChH]$",
+            full.names=TRUE, recursive=TRUE, no..=TRUE))
+
         # Create folder R_HOME_USER/pkgdev/pkgs/pkgName/src (if not existing)
         dir.create(file.path(pkdev_path,"pkgs",pkgName,"src"), showWarnings=FALSE)
         
index b649ead..2350a8e 100644 (file)
@@ -3,9 +3,9 @@
 
     # Initial step: list every potential unit test under path/R/tests.
     allFuncNames = .parseRunitTests(file.path(path,"R","tests"))
-    
+
     # Filter functions names matching prefix
-    funcNames = grep( paste("^test\\.",prefix,sep=''), allFuncNames, value=TRUE )
+    funcNames = grep( paste("^test_",prefix,sep=''), allFuncNames, value=TRUE )
     if (length(funcNames) == 0) return #shortcut: nothing to do...
     
     # If show==TRUE, display every potential test starting with prefix, and exit
         # If the file is not a source, skip
         if ( length( grep("\\.[RrSsq]$", fileName) ) == 0) next
         
-        # Every test function has a name starting with "test."
+        # Every test function has a name starting with "test_"
         matches = grep(
-            "^[ \t]*test\\.[a-zA-Z0-9_]*[ \t]*(=|<-)[ \t]*function.*",
+            "^[ \t]*test_[a-zA-Z0-9_]*[ \t]*(=|<-)[ \t]*function.*",
             scan(fileName, what="character", sep='\n', quiet=TRUE),
             value = TRUE)
         
         # We matched more to be 100% sure we got test functions, but need to strip now
         funcNames = c(funcNames, sub(
-            "^[ \t]*(test\\.[a-zA-Z0-9_]*)[ \t]*(=|<-)[ \t]*function.*",
+            "^[ \t]*(test_[a-zA-Z0-9_]*)[ \t]*(=|<-)[ \t]*function.*",
             "\\1",
             matches))
     }
diff --git a/R/zzz.R b/R/zzz.R
new file mode 100644 (file)
index 0000000..6c2d872
--- /dev/null
+++ b/R/zzz.R
@@ -0,0 +1,10 @@
+#called by library(pkgdev)
+.onAttach = function(libname, pkgname) {
+       #wipe all previous "loaded" tags
+       R_HOME_USER = Sys.getenv("R_HOME_USER")
+       if (R_HOME_USER != "") {
+               pkgdevPackagesPath = file.path( Sys.getenv("R_HOME_USER"),"pkgdev","pkgs" )
+               for (package in list.dirs(pkgdevPackagesPath, recursive=FALSE))
+                       file.remove(file.path(package,"loaded"))
+       }
+}
index c969d76..b9d040d 100755 (executable)
@@ -1,4 +1,4 @@
 # Export all user-level R functions
-export (a, b, c, .Last.lib)
+export (a, b, c)
 
 useDynLib(pkgTest)
diff --git a/pkgTest/R/tests/t.a.R b/pkgTest/R/tests/t.a.R
new file mode 100644 (file)
index 0000000..970ef5d
--- /dev/null
@@ -0,0 +1,7 @@
+test_a = function() {
+       a()
+}
+
+test_c = function() {
+       c()
+}
index 415c03a..ce4432e 100644 (file)
@@ -1,5 +1,5 @@
 #called when package is detached ( detach("package:pkg_name") )
-.Last.lib = function(path)
+.onDetach = function(libpath)
 {
-       library.dynam.unload("pkgTest", path)
+       library.dynam.unload("pkgTest", libpath)
 }
similarity index 100%
rename from pkgTest/src/d.c
rename to pkgTest/src/sources/d.c
diff --git a/pkgTest/src/tests/t.d.c b/pkgTest/src/tests/t.d.c
new file mode 100644 (file)
index 0000000..63939a9
--- /dev/null
@@ -0,0 +1,3 @@
+void test_d() {
+       d();
+}