Skip to content

Commit

Permalink
[R-package] silence logs in print(), show(), summary() tests (#5237)
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb authored May 25, 2022
1 parent f2e1ad6 commit 1a3afd2
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions R-package/tests/testthat/test_lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -1255,24 +1255,64 @@ test_that("Booster's print, show, and summary work correctly", {
)
}

.has_expected_content_for_fitted_model <- function(printed_txt) {
expect_true(any(grepl("^LightGBM Model", printed_txt)))
expect_true(any(grepl("^Fitted to dataset", printed_txt)))
}

.has_expected_content_for_finalized_model <- function(printed_txt) {
expect_true(any(grepl("^LightGBM Model$", printed_txt)))
expect_true(any(grepl("Booster handle is invalid", printed_txt)))
}

.check_methods_work <- function(model) {

# should work for fitted models
ret <- print(model)
#--- should work for fitted models --- #

# print()
log_txt <- capture.output({
ret <- print(model)
})
.have_same_handle(ret, model)
ret <- show(model)
.has_expected_content_for_fitted_model(log_txt)

# show()
log_txt <- capture.output({
ret <- show(model)
})
expect_null(ret)
ret <- summary(model)
.has_expected_content_for_fitted_model(log_txt)

# summary()
log_text <- capture.output({
ret <- summary(model)
})
.have_same_handle(ret, model)
.has_expected_content_for_fitted_model(log_txt)

# should not fail for finalized models
#--- should not fail for finalized models ---#
model$finalize()
ret <- print(model)

# print()
log_txt <- capture.output({
ret <- print(model)
})
.has_expected_content_for_finalized_model(log_txt)

# show()
.have_same_handle(ret, model)
ret <- show(model)
log_txt <- capture.output({
ret <- show(model)
})
expect_null(ret)
ret <- summary(model)
.has_expected_content_for_finalized_model(log_txt)

# summary()
log_txt <- capture.output({
ret <- summary(model)
})
.have_same_handle(ret, model)
.has_expected_content_for_finalized_model(log_txt)
}

data("mtcars")
Expand Down

0 comments on commit 1a3afd2

Please sign in to comment.