Skip to content

Commit 88169f8

Browse files
committed
tinytest step six: convert exceptions and exceptions_nocall
1 parent ce12601 commit 88169f8

File tree

7 files changed

+157
-163
lines changed

7 files changed

+157
-163
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* inst/tinytest/test_dispatch.R: Idem
1111
* inst/tinytest/test_embedded.R: Idem
1212
* inst/tinytest/test_environments.R: Idem
13+
* inst/tinytest/test_exceptions.R: Idem
14+
* inst/tinytest/test_exceptions_nocall.R: Idem
1315

1416
2019-11-23 Dirk Eddelbuettel <edd@debian.org>
1517

inst/tinytest/test_exceptions.R

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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"))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
## Copyright (C) 2018 - 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_nocall.R'")
22+
23+
library(Rcpp)
24+
sourceCpp("cpp/Exceptions_nocall.cpp")
25+
26+
# test.Rcpp_exception <- function() {
27+
tryCatch(Rcpp_exception(), error = function(e){
28+
expect_true(is.null(e$call))
29+
expect_true(is.null(e$cppstack))
30+
})
31+
32+
# test.eval_error <- function() {
33+
tryCatch(eval_error_no_call(), error = function(e){
34+
expect_true(is.null(e$call))
35+
expect_true(is.null(e$cppstack))
36+
})

inst/unitTests/runit.Exceptions_nocall.R

Lines changed: 0 additions & 40 deletions
This file was deleted.

inst/unitTests/runit.exceptions.R

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)