diff --git a/tests/Makefile.in b/tests/Makefile.in index 9164edad2cb..6ec22223063 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -29,7 +29,7 @@ TESTsrc-strict-1 = \ is-things.R \ lm-tests.R \ primitive-funs.R \ - mode-methods.R \ + method-dispatch.R \ simple-true.R TESTsrc-strict-auto = \ isas-tests.R diff --git a/tests/method-dispatch.R b/tests/method-dispatch.R new file mode 100644 index 00000000000..12bbc16fff7 --- /dev/null +++ b/tests/method-dispatch.R @@ -0,0 +1,55 @@ +#### Testing UseMethod() and even more NextMethod() +#### + +###-- Group methods + +### Arithmetic "Ops" : +">.bar" <- function(...) print(">.bar") +">.foo" <- function(...) print(">.foo") +Ops.foo <- function(...) { + print("Ops.foo") + NextMethod() +} +Ops.bar <- function(...) + print("Ops.bar") + +x <- 2:4 ; class(x) <- c("foo", "bar") +y <- 4:2 ; class(y) <- c("bar", "foo") + +## The next 4 give a warning each about incompatible methods: +x > y +y < x # should be the same (warning msg not, however) +x == y +x <= y + +x > 3 ##[1] ">.foo" + +rm(list=">.foo") +x > 3 #-> "Ops.foo" and ">.bar" + + + +### ------------ was ./mode-methods.R till R ver. 1.0.x ---------------- + +###-- Using Method Dispatch on "mode" etc : + +abc <- function(x, ...) { + if (is.null(class(x))) class(x) <- data.class(x) + cat("abc: Before dispatching; x="); str(x) + UseMethod("abc", x,...) ## UseMethod("abc") (as in S) fails +} + +abc.default <- function(x, ...) sys.call() + +"abc.(" <- function(x) + cat("'(' method of abc:", deparse(sys.call(sys.parent())),"\n") +abc.expression <- function(x) + cat("'expression' method of abc:", deparse(sys.call(sys.parent())),"\n") + +abc(1) +e0 <- expression((x)) +e1 <- expression(sin(x)) +abc(e0) +abc(e1) +abc(e0[[1]]) +abc(e1[[1]]) diff --git a/tests/mode-methods.Rout.save b/tests/method-dispatch.Rout.save similarity index 52% rename from tests/mode-methods.Rout.save rename to tests/method-dispatch.Rout.save index 34f2a21bf75..9ec188abec1 100644 --- a/tests/mode-methods.Rout.save +++ b/tests/method-dispatch.Rout.save @@ -1,6 +1,6 @@ -R : Copyright 1999, The R Development Core Team -Version 0.90.0 Under development (unstable) (October 22, 1999) +R : Copyright 2000, The R Development Core Team +Version 1.1.0 Under development (unstable) (March 13, 2000) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. @@ -13,7 +13,55 @@ Type "demo()" for some demos, "help()" for on-line help, or "help.start()" for a HTML browser interface to help. Type "q()" to quit R. -> ### Using Method Dispatch on "mode" etc : +> #### Testing UseMethod() and even more NextMethod() +> #### +> +> ###-- Group methods +> +> ### Arithmetic "Ops" : +> ">.bar" <- function(...) print(">.bar") +> ">.foo" <- function(...) print(">.foo") +> Ops.foo <- function(...) { ++ print("Ops.foo") ++ NextMethod() ++ } +> Ops.bar <- function(...) ++ print("Ops.bar") +> +> x <- 2:4 ; class(x) <- c("foo", "bar") +> y <- 4:2 ; class(y) <- c("bar", "foo") +> +> ## The next 4 give a warning each about incompatible methods: +> x > y +[1] FALSE FALSE TRUE +Warning message: +Incompatible methods (">.foo", ">.bar") for ">" +> y < x # should be the same (warning msg not, however) +[1] FALSE FALSE TRUE +Warning message: +Incompatible methods ("Ops.bar", "Ops.foo") for "<" +> x == y +[1] FALSE TRUE FALSE +Warning message: +Incompatible methods ("Ops.foo", "Ops.bar") for "==" +> x <= y +[1] TRUE TRUE FALSE +Warning message: +Incompatible methods ("Ops.foo", "Ops.bar") for "<=" +> +> x > 3 ##[1] ">.foo" +[1] ">.foo" +> +> rm(list=">.foo") +> x > 3 #-> "Ops.foo" and ">.bar" +[1] "Ops.foo" +[1] ">.bar" +> +> +> +> ### ------------ was ./mode-methods.R till R ver. 1.0.x ---------------- +> +> ###-- Using Method Dispatch on "mode" etc : > > abc <- function(x, ...) { + if (is.null(class(x))) class(x) <- data.class(x) diff --git a/tests/mode-methods.R b/tests/mode-methods.R deleted file mode 100644 index aece9a716e3..00000000000 --- a/tests/mode-methods.R +++ /dev/null @@ -1,22 +0,0 @@ -### Using Method Dispatch on "mode" etc : - -abc <- function(x, ...) { - if (is.null(class(x))) class(x) <- data.class(x) - cat("abc: Before dispatching; x="); str(x) - UseMethod("abc", x,...) ## UseMethod("abc") (as in S) fails -} - -abc.default <- function(x, ...) sys.call() - -"abc.(" <- function(x) - cat("'(' method of abc:", deparse(sys.call(sys.parent())),"\n") -abc.expression <- function(x) - cat("'expression' method of abc:", deparse(sys.call(sys.parent())),"\n") - -abc(1) -e0 <- expression((x)) -e1 <- expression(sin(x)) -abc(e0) -abc(e1) -abc(e0[[1]]) -abc(e1[[1]])