Skip to content

Commit

Permalink
fix: add backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jorainer committed Sep 27, 2024
1 parent 37ef79b commit 02d8394
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion R/MsBackend.R
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,9 @@ setMethod("dropNaSpectraVariables", "MsBackend", function(object) {
#'
#' @export
setMethod("extractByIndex", c("MsBackend", "ANY"), function(object, i) {
stop("'extractByIndex' not implemented for ", class(object), ".")
if (existsMethod("[", class(object)[1L]))
object[i = i]
else stop("'extractByIndex' not implemented for ", class(object)[1L], ".")
})

#' @rdname MsBackend
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test_MsBackend.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ test_that("MsBackend methods throw errors", {
expect_error(extractByIndex(dm, 1), "implemented for")
})

test_that("extractByIndex not implemented fallback", {
## Backends that don't implement a dedicated `extractByIndex` method should
## fall back to the [ method.
setClass("DummyBackend",
contains = "MsBackend",
slots = c(d = "integer"))
dm <- new("DummyBackend")
expect_error(extractByIndex(dm, 1L), "'extractByIndex' not implemented")

dm@d <- 1:4

## Have an implementation for [ but not extractByIndex:
setMethod("[", "DummyBackend", function(x, i, j, ..., drop = FALSE) {
x@d <- x@d[i]
x
})

res <- dm[c(3, 1)]
expect_equal(res@d, c(3L, 1L))

res <- extractByIndex(dm, c(3, 1))
expect_equal(res@d, c(3L, 1L))
})

test_that("reset,MsBackend works", {
res <- reset(sciex_mzr)
expect_equal(res, sciex_mzr)
Expand Down

0 comments on commit 02d8394

Please sign in to comment.