@@ -155,8 +155,6 @@ test_that("print() throws the expected messages", {
155155 test <- data.table :: copy(example_point )
156156 class(test ) <- c(" point" , " forecast" , " data.table" , " data.frame" )
157157
158- # note that since introducing a length maximum for validation to be triggered,
159- # we don't throw a warning automatically anymore
160158 suppressMessages(
161159 expect_message(
162160 capture.output(print(test )),
@@ -174,6 +172,131 @@ test_that("print() throws the expected messages", {
174172})
175173
176174
175+ # ==============================================================================
176+ # Autoprint suppression during := operations (issue #935)
177+ # ==============================================================================
178+
179+ test_that(" := on forecast objects does not trigger spurious printing" , {
180+ ex <- data.table :: copy(example_quantile )
181+ output <- capture.output(ex [, model : = paste(model , " a" )])
182+ expect_identical(output , character (0 ))
183+ })
184+
185+ test_that(" := adding a new column to forecast objects does not print" , {
186+ ex <- data.table :: copy(example_quantile )
187+ output <- capture.output(ex [, new_col : = " test" ])
188+ expect_identical(output , character (0 ))
189+ })
190+
191+ test_that(" := on different forecast types does not trigger printing" , {
192+ # Test across all forecast types to ensure the fix is comprehensive
193+ forecast_objects <- list (
194+ binary = data.table :: copy(example_binary ),
195+ quantile = data.table :: copy(example_quantile ),
196+ point = data.table :: copy(example_point ),
197+ sample_continuous = data.table :: copy(example_sample_continuous ),
198+ sample_discrete = data.table :: copy(example_sample_discrete )
199+ )
200+
201+ for (name in names(forecast_objects )) {
202+ ex <- forecast_objects [[name ]]
203+ output <- capture.output(ex [, model : = paste(model , " a" )])
204+ expect_identical(
205+ output , character (0 ),
206+ label = paste(" Spurious printing for forecast type:" , name )
207+ )
208+ }
209+ })
210+
211+ test_that(" multiple sequential := operations do not trigger printing" , {
212+ ex <- data.table :: copy(example_quantile )
213+ output <- capture.output({
214+ ex [, model : = paste(model , " a" )]
215+ ex [, new_col1 : = 1 ]
216+ ex [, new_col2 : = " test" ]
217+ })
218+ expect_identical(output , character (0 ))
219+ })
220+
221+ test_that(" explicit print() still works after := suppression" , {
222+ # After :=, data.table sets a flag that suppresses the next print.
223+ # In interactive R, autoprint consumes this flag. In non-interactive
224+ # contexts (testthat, scripts), we consume it manually with x[].
225+ ex <- data.table :: copy(example_quantile )
226+ ex [, model : = paste(model , " a" )]
227+ invisible (capture.output(suppressMessages(ex []))) # consume shouldPrint flag
228+
229+ output <- capture.output(suppressMessages(print(ex )))
230+ expect_gt(length(output ), 0 )
231+ })
232+
233+ test_that(" print() on forecast objects still shows header and data" , {
234+ ex <- as_forecast_quantile(na.omit(example_quantile ))
235+
236+ messages <- capture.output(print(ex ), type = " message" )
237+ output <- capture.output(suppressMessages(print(ex )))
238+
239+ # Header should contain forecast type and unit info
240+ header_text <- paste(messages , collapse = " " )
241+ expect_true(grepl(" Forecast type" , header_text , fixed = TRUE ))
242+ expect_true(grepl(" Forecast unit" , header_text , fixed = TRUE ))
243+
244+ # Data should be printed
245+ expect_gt(length(output ), 0 )
246+ })
247+
248+ test_that(" x[] force-print still works on forecast objects" , {
249+ # x[] is data.table's force-print syntax, should still produce output
250+ ex <- as_forecast_quantile(na.omit(example_quantile ))
251+ output <- capture.output(suppressMessages(ex []))
252+ expect_gt(length(output ), 0 )
253+ })
254+
255+ test_that(" := on scores objects does not trigger spurious printing" , {
256+ scores <- score(example_quantile )
257+ output <- capture.output(scores [, test : = 3 ])
258+ expect_identical(output , character (0 ))
259+ })
260+
261+ test_that(" := that breaks the forecast contract still validates" , {
262+ # Validation must keep running during in-place `:=` modification: removing a
263+ # required column should warn, even though the autoprint stays suppressed.
264+ ex <- as_forecast_quantile(na.omit(example_quantile ))
265+ expect_warning(
266+ ex [, observed : = NULL ],
267+ " Error in validating"
268+ )
269+ })
270+
271+ test_that(" := that breaks the contract does not trigger spurious printing" , {
272+ # Restoring validation must not reintroduce the spurious autoprint from #935.
273+ ex <- as_forecast_quantile(na.omit(example_quantile ))
274+ output <- capture.output(
275+ suppressWarnings(ex [, observed : = NULL ])
276+ )
277+ expect_identical(output , character (0 ))
278+ })
279+
280+ test_that(" [.forecast() validates subsets regardless of size" , {
281+ # After removing the 30-row hack, validation should trigger for
282+ # any size subset that breaks the forecast contract
283+ test <- na.omit(data.table :: copy(example_quantile ))
284+
285+ # Small subset (previously skipped validation due to nrow <= 30 hack)
286+ small_test <- test [1 : 20 ]
287+ expect_warning(
288+ local(small_test [, colnames(small_test ) != " observed" , with = FALSE ]),
289+ " Error in validating"
290+ )
291+
292+ # Large subset (was already validated before)
293+ expect_warning(
294+ local(test [, colnames(test ) != " observed" , with = FALSE ]),
295+ " Error in validating"
296+ )
297+ })
298+
299+
177300# ==============================================================================
178301# check_number_per_forecast() # nolint: commented_code_linter
179302# ==============================================================================
0 commit comments