Skip to content

Commit

Permalink
Add unit tests for .xts() checking index class and timezone
Browse files Browse the repository at this point in the history
As documented in #249, v0.11 introduced changes to .xts() which altered
behaviour compared to v0.10-2.  As a first step to fixing this
regression, this commit adds unit tests for all possible permutations
for setting the index class and timezone in .xts().

The tests check accessing the class in 3 different ways:
    1. Using tclass()
    2. Using indexClass()
    3. Checking the 'tclass' attribute on the index object

Similarly the tests check accessing timezone by:
    1. Using tzone()
    2. Using indexTZ()
    3. Checking the '.indexTZ' attribute on the index object

These tests are designed to document the behaviour as of v0.10-2.  As
such they pass on v0.10-2 but fail for v0.11.
  • Loading branch information
TomAndrews committed Jul 27, 2018
1 parent d112844 commit beb4267
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions inst/unitTests/runit.xts.R
Expand Up @@ -50,3 +50,48 @@ test..xts_dimnames_in_dots <- function() {
y <- xts(1:5, index(x), dimnames = list(NULL, "x"))
checkEquals(x, y)
}

checkXtsClass <- function(xts, class) {
checkEquals(tclass(xts), class)
checkEquals(indexClass(xts), class)
checkEquals(attr(attr(xts, "index"), "tclass"), class)
}

### Check that .indexCLASS takes precedence over tclass when both specified
test..xts_class <- function() {
checkXtsClass(.xts(1, 1), c("POSIXct", "POSIXt"))
checkXtsClass(.xts(1, 1, tclass="timeDate"), "timeDate")
checkXtsClass(.xts(1, 1, .indexCLASS="Date"), "Date")
checkXtsClass(.xts(1, 1, tclass="timeDate", .indexCLASS="Date"), "Date")

## also check that tclass is ignored if specified as part of index
checkXtsClass(.xts(1, structure(1, tzone="",tclass="yearmon")), c("POSIXct", "POSIXt"))
checkXtsClass(.xts(1, structure(1, tzone="",tclass="yearmon"), tclass="timeDate"), "timeDate")
checkXtsClass(.xts(1, structure(1, tzone="",tclass="yearmon"), .indexCLASS="Date"), "Date")
checkXtsClass(.xts(1, structure(1, tzone="",tclass="yearmon"), tclass="timeDate", .indexCLASS="Date"), "Date")
}

checkXtsTz <- function(xts, tzone) {
checkEquals(tzone(xts), tzone)
checkEquals(indexTZ(xts), tzone)
checkEquals(attr(attr(xts, "index"), "tzone"), tzone)
}

.setUp <- function() {
Sys.setenv(TZ="UTC")
}

### Check that tzone is honoured and .indexTZ ignored
test..xts_tzone <- function() {
checkXtsTz(.xts(1, 1), "UTC")
checkXtsTz(.xts(1, 1, tzone="Europe/London"), "Europe/London")
## this case passes in 0.10-2 but looks wrong
checkXtsTz(.xts(1, 1, .indexTZ="America/New_York"), "UTC")
checkXtsTz(.xts(1, 1, tzone="Europe/London", .indexTZ="America/New_York"), "Europe/London")

## Cases where tzone is specified in the index
checkXtsTz(.xts(1, structure(1, tzone="Asia/Tokyo",tclass="yearmon")), "Asia/Tokyo")
checkXtsTz(.xts(1, structure(1, tzone="Asia/Tokyo",tclass="yearmon"), tzone="Europe/London"), "Europe/London")
checkXtsTz(.xts(1, structure(1, tzone="Asia/Tokyo",tclass="yearmon"), .indexTZ="America/New_York"), "Asia/Tokyo")
checkXtsTz(.xts(1, structure(1, tzone="Asia/Tokyo",tclass="yearmon"), tzone="Europe/London", .indexTZ="America/New_York"), "Europe/London")
}

0 comments on commit beb4267

Please sign in to comment.