[Blotter-commits] r1723 - in pkg/quantstrat: R inst/tests tests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Dec 11 15:53:34 CET 2015


Author: bodanker
Date: 2015-12-11 15:53:33 +0100 (Fri, 11 Dec 2015)
New Revision: 1723

Added:
   pkg/quantstrat/inst/tests/test_paramsets.R
Modified:
   pkg/quantstrat/R/paramsets.R
   pkg/quantstrat/tests/run-all.R
Log:
Warn if replacing paramset distribution/constraint

add.distribution and add.distribution.constraint would silently replace
any distribution/constraint for the same strategy/paramset with the
same label. Keep current behavior, but throw warning to avoid surprise.

Also change test_package to only test paramset warnings, since test_bee
and test_blotter_break both currently fail, and testthat apparently
stops all tests after the first failure when running under R CMD check.


Modified: pkg/quantstrat/R/paramsets.R
===================================================================
--- pkg/quantstrat/R/paramsets.R	2015-12-11 14:44:38 UTC (rev 1722)
+++ pkg/quantstrat/R/paramsets.R	2015-12-11 14:53:33 UTC (rev 1723)
@@ -231,8 +231,12 @@
         store <- TRUE
     }
 
-    if(!is.null(strategy$paramsets[[paramset.label]]))
+    if(!is.null(strategy$paramsets[[paramset.label]])) {
         strategy$paramsets[[paramset.label]] <- NULL
+    } else {
+        warning("strategy ", sQuote(strategy$name), " does not have a paramset ",
+                sQuote(paramset.label), " to delete. Aborting.", immediate.=TRUE)
+    }
 
     if(store)
     {
@@ -279,6 +283,13 @@
     if(!(paramset.label %in% names(strategy$paramsets)))
         strategy <- create.paramset(strategy, paramset.label)
 
+    if(label %in% names(strategy$paramsets[[paramset.label]]$distributions)) {
+        fmt <- paste("add.distribution replacing previously defined",
+                     "distribution %s in paramset %s for strategy %s.")
+        msg <- sprintf(fmt, sQuote(label), sQuote(paramset.label), sQuote(strategy$name))
+        warning(msg, immediate.=TRUE, call.=FALSE)
+    }
+
     strategy$paramsets[[paramset.label]]$distributions[[label]] <- new_distribution
 
     if(store)
@@ -323,6 +334,13 @@
     if(!(paramset.label %in% names(strategy$paramsets)))
         strategy <- create.paramset(strategy, paramset.label)
 
+    if(label %in% names(strategy$paramsets[[paramset.label]]$constraints)) {
+        fmt <- paste("add.distribution.constraint replacing previously defined",
+                     "constraint %s in paramset %s for strategy %s.")
+        msg <- sprintf(fmt, sQuote(label), sQuote(paramset.label), sQuote(strategy$name))
+        warning(msg, immediate.=TRUE, call.=FALSE)
+    }
+
     strategy$paramsets[[paramset.label]]$constraints[[label]] <- new_constraint
 
     if(store)

Added: pkg/quantstrat/inst/tests/test_paramsets.R
===================================================================
--- pkg/quantstrat/inst/tests/test_paramsets.R	                        (rev 0)
+++ pkg/quantstrat/inst/tests/test_paramsets.R	2015-12-11 14:53:33 UTC (rev 1723)
@@ -0,0 +1,46 @@
+require(testthat)
+context("paramset warnings")
+
+options(in_test=TRUE)
+
+require(quantstrat)
+strategy.st <- "paramset_test"
+strategy(strategy.st, store=TRUE)
+.timespans <- c("T08:00/T10:00")
+
+add.distribution(strategy.st,
+	paramset.label = 'Timespan',
+	component.type = 'enter',
+	component.label = 'EnterLONG',
+	variable = list(timespan = .timespans),
+	label = 'Timespan')
+
+test_that("Replacing paramset distribution warns", expect_warning({
+    add.distribution(strategy.st,
+	    paramset.label = 'Timespan',
+	    component.type = 'enter',
+	    component.label = 'EnterLONG',
+	    variable = list(timespan = .timespans),
+	    label = 'Timespan')
+}))
+
+add.distribution.constraint(strategy.st,
+    paramset.label = 'Timespan',
+    distribution.label.1 = 'EnterLong',
+    distribution.label.2 = 'EnterShort',
+    operator = '==',
+    label = 'Timespan')
+
+test_that("Replacing paramset constraint warns", expect_warning({
+    add.distribution.constraint(strategy.st,
+	    paramset.label = 'Timespan',
+	    distribution.label.1 = 'EnterLong',
+	    distribution.label.2 = 'EnterShort',
+	    operator = '==',
+	    label = 'Timespan')
+}))
+
+test_that("Deleting unknown paramset warns", expect_warning({
+  delete.paramset(strategy.st, "Timespans")
+}))
+

Modified: pkg/quantstrat/tests/run-all.R
===================================================================
--- pkg/quantstrat/tests/run-all.R	2015-12-11 14:44:38 UTC (rev 1722)
+++ pkg/quantstrat/tests/run-all.R	2015-12-11 14:53:33 UTC (rev 1723)
@@ -1,5 +1,5 @@
 require(testthat)
 require(quantstrat)
 
-try(test_package("quantstrat"))
+try(test_package("quantstrat", filter="paramsets"))
 



More information about the Blotter-commits mailing list