|
| 1 | + |
| 2 | +## Copyright (C) 2010 - 2019 Dirk Eddelbuettel and Romain Francois |
| 3 | +## |
| 4 | +## This file is part of Rcpp. |
| 5 | +## |
| 6 | +## Rcpp is free software: you can redistribute it and/or modify it |
| 7 | +## under the terms of the GNU General Public License as published by |
| 8 | +## the Free Software Foundation, either version 2 of the License, or |
| 9 | +## (at your option) any later version. |
| 10 | +## |
| 11 | +## Rcpp is distributed in the hope that it will be useful, but |
| 12 | +## WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +## GNU General Public License for more details. |
| 15 | +## |
| 16 | +## You should have received a copy of the GNU General Public License |
| 17 | +## along with Rcpp. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes" |
| 20 | + |
| 21 | +if (.runThisTest) exit_file("Skipping 'test_exceptions.R'") |
| 22 | + |
| 23 | +library(Rcpp) |
| 24 | +sourceCpp("cpp/exceptions.cpp") |
| 25 | + |
| 26 | +#test.stdException <- function() { |
| 27 | +## Code works normally without an exception |
| 28 | +expect_identical(takeLog(1L), log(1L)) |
| 29 | + |
| 30 | +## C++ exceptions are converted to R conditions |
| 31 | +condition <- tryCatch(takeLog(-1L), error = identity) |
| 32 | + |
| 33 | +expect_identical(condition$message, "Inadmissible value") |
| 34 | +expect_identical(class(condition), c("std::range_error", "C++Error", "error", "condition")) |
| 35 | + |
| 36 | +## C++ stack only available for Rcpp::exceptions |
| 37 | +expect_true(is.null(condition$cppstack)) |
| 38 | + |
| 39 | +expect_identical(condition$call, quote(takeLog(-1L))) |
| 40 | + |
| 41 | + |
| 42 | +#test.rcppException <- function() { |
| 43 | + |
| 44 | +## Code works normally without an exception |
| 45 | +expect_identical(takeLog(1L), log(1L)) |
| 46 | + |
| 47 | +## C++ exceptions are converted to R conditions |
| 48 | +condition <- tryCatch(takeLogRcpp(-1L), error = identity) |
| 49 | + |
| 50 | +expect_identical(condition$message, "Inadmissible value") |
| 51 | +expect_identical(class(condition), c("Rcpp::exception", "C++Error", "error", "condition")) |
| 52 | + |
| 53 | +expect_true(!is.null(condition$cppstack)) |
| 54 | + |
| 55 | +expect_identical(class(condition$cppstack), "Rcpp_stack_trace") |
| 56 | + |
| 57 | +expect_equal(condition$call, quote(takeLogRcpp(-1L))) |
| 58 | + |
| 59 | + |
| 60 | +#test.rcppStop <- function() { |
| 61 | +## Code works normally without an exception |
| 62 | +expect_identical(takeLog(1L), log(1L)) |
| 63 | + |
| 64 | +## C++ exceptions are converted to R conditions |
| 65 | +condition <- tryCatch(takeLogStop(-1L), error = identity) |
| 66 | + |
| 67 | +expect_identical(condition$message, "Inadmissible value") |
| 68 | +expect_identical(class(condition), c("Rcpp::exception", "C++Error", "error", "condition")) |
| 69 | + |
| 70 | +expect_true(!is.null(condition$cppstack)) |
| 71 | + |
| 72 | +expect_identical(class(condition$cppstack), "Rcpp_stack_trace") |
| 73 | + |
| 74 | +expect_equal(condition$call, quote(takeLogStop(-1L))) |
| 75 | + |
| 76 | + |
| 77 | +#test.rcppExceptionLocation <- function() { |
| 78 | + |
| 79 | +## Code works normally without an exception |
| 80 | +expect_identical(takeLog(1L), log(1L)) |
| 81 | + |
| 82 | +## C++ exceptions are converted to R conditions |
| 83 | +condition <- tryCatch(takeLogRcppLocation(-1L), error = identity) |
| 84 | + |
| 85 | +expect_identical(condition$message, "Inadmissible value") |
| 86 | +expect_identical(class(condition), c("Rcpp::exception", "C++Error", "error", "condition")) |
| 87 | + |
| 88 | +expect_true(!is.null(condition$cppstack)) |
| 89 | +expect_identical(class(condition$cppstack), "Rcpp_stack_trace") |
| 90 | + |
| 91 | +#expect_identical(condition$cppstack$file, "exceptions.cpp") |
| 92 | +#expect_identical(condition$cppstack$line, 44L) |
| 93 | + |
| 94 | +expect_equal(condition$call, quote(takeLogRcppLocation(-1L))) |
| 95 | + |
| 96 | + |
| 97 | +#test.rcppExceptionLocation <- function() { |
| 98 | + |
| 99 | +## Nested exceptions work the same way |
| 100 | +normal <- tryCatch(takeLogRcppLocation(-1L), error = identity) |
| 101 | +f1 <- function(x) takeLogNested(x) |
| 102 | + |
| 103 | +nested <- tryCatch(f1(-1), error = identity) |
| 104 | + |
| 105 | +## Message the same |
| 106 | +expect_identical(normal$message, nested$message) |
| 107 | + |
| 108 | +expect_equal(nested$call, quote(takeLogNested(x))) |
| 109 | + |
| 110 | + |
| 111 | +#test.rcppExceptionNoCall <- function() { |
| 112 | + |
| 113 | +## Can throw exceptions that don't include a call stack |
| 114 | +e <- tryCatch(noCall(), error = identity) |
| 115 | + |
| 116 | +expect_identical(e$message, "Testing") |
| 117 | +expect_identical(e$call, NULL) |
| 118 | +expect_identical(e$cppstack, NULL) |
| 119 | +expect_identical(class(e), c("Rcpp::exception", "C++Error", "error", "condition")) |
0 commit comments