-
Notifications
You must be signed in to change notification settings - Fork 38
/
test-sink.R
130 lines (101 loc) · 2.72 KB
/
test-sink.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
context("With sink")
test_that("with_output_sink works as expected", {
tmp <- tempfile()
on.exit(unlink(tmp), add = TRUE)
tmp2 <- tempfile()
on.exit(unlink(tmp2), add = TRUE)
tmp3 <- tempfile()
on.exit(unlink(tmp3), add = TRUE)
expect_identical(sink.number(), 0L)
with_output_sink(tmp, {
expect_identical(sink.number(), 1L)
cat("output\n")
})
expect_identical(readLines(tmp), "output")
expect_identical(sink.number(), 0L)
with_output_sink(tmp, append = TRUE, {
expect_identical(sink.number(), 1L)
cat("output 2\n")
})
expect_identical(readLines(tmp), c("output", "output 2"))
expect_identical(sink.number(), 0L)
expect_warning(
with_output_sink(tmp, {
sink()
}),
"already removed"
)
expect_identical(sink.number(), 0L)
expect_error(
with_output_sink(NULL, {
NULL
}),
"cannot be NULL"
)
expect_identical(sink.number(), 0L)
})
# don't use test_that() here to avoid any message redirection
local({
tmp <- tempfile()
on.exit(unlink(tmp), add = TRUE)
tmp2 <- tempfile()
on.exit(unlink(tmp2), add = TRUE)
tmp3 <- tempfile()
on.exit(unlink(tmp3), add = TRUE)
expect_identical(sink.number(type = "message"), 2L)
with_message_sink(tmp, {
expect_gt(sink.number(type = "message"), 2L)
message("message")
})
expect_identical(sink.number(type = "message"), 2L)
expect_identical(readLines(tmp), "message")
with_message_sink(tmp, append = TRUE, {
expect_gt(sink.number(type = "message"), 2L)
message("message 2")
})
expect_identical(sink.number(type = "message"), 2L)
expect_identical(readLines(tmp), c("message", "message 2"))
# Message and output sinks don't interfere
with_message_sink(tmp, {
sink(tmp2)
})
expect_identical(sink.number(type = "message"), 2L)
expect_identical(sink.number(), 1L)
sink()
con <- file(tmp2, "w")
on.exit(close(con), add = TRUE)
expect_warning(
with_message_sink(tmp, {
sink(con, type = "message")
}),
"Not removing a different"
)
expect_gt(sink.number(type = "message"), 2L)
sink(type = "message")
expect_warning(
with_message_sink(tmp, {
sink(type = "message")
}),
"No message sink to remove"
)
expect_identical(sink.number(type = "message"), 2L)
expect_warning(
with_message_sink(tmp, {
expect_error(
with_message_sink(tmp2, NULL),
"Cannot establish message sink when another sink is active."
)
}),
NA
)
expect_identical(sink.number(type = "message"), 2L)
expect_error(
with_message_sink(NULL, {
NULL
}),
"cannot be NULL"
)
expect_identical(sink.number(type = "message"), 2L)
# Enable to check that test actually reaches this point
#expect_true(FALSE)
})