Skip to content

Commit

Permalink
Merge pull request daroczig#169 from hadley/test-style-tweaks
Browse files Browse the repository at this point in the history
Test style tweaks
  • Loading branch information
daroczig authored Aug 4, 2024
2 parents df9d3a0 + b5067ac commit fa5ac9e
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 48 deletions.
3 changes: 0 additions & 3 deletions tests/testthat/test-CRANSKIP-appenders.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library(logger)
library(testthat)

## save current settings so that we can reset later
threshold <- log_threshold()
layout <- log_layout()
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-CRANSKIP-logger-namespaces.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library(logger)
library(testthat)

## save current settings so that we can reset later
layout <- log_layout()
appender <- log_appender()
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-appender.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library(logger)
library(testthat)

## save current settings so that we can reset later
threshold <- log_threshold()
layout <- log_layout()
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-eval.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library(logger)
library(testthat)

## save current settings so that we can reset later
layout <- log_layout()
threshold <- log_threshold()
Expand Down
16 changes: 6 additions & 10 deletions tests/testthat/test-formatters.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
library(logger)
library(testthat)
library(jsonlite)

## save current settings so that we can reset later
formatter <- log_formatter()
appender <- log_appender()
Expand All @@ -28,8 +24,8 @@ test_that('glue works', {
expect_equal(formatter_glue("Hi {everything}"), "Hi 42")
expect_equal(formatter_glue("Hi {1:2}"), paste("Hi", 1:2))

expect_output(do.call(logger, logger:::namespaces$global[[1]])(INFO, 42), '42')
expect_output(do.call(logger, logger:::namespaces$global[[1]])(INFO, "Hi {everything}"), '42')
expect_output(do.call(logger, namespaces$global[[1]])(INFO, 42), '42')
expect_output(do.call(logger, namespaces$global[[1]])(INFO, "Hi {everything}"), '42')

expect_output(log_info("Hi {everything}"), '42')
expect_output(log_warn("Hi {everything}"), '42')
Expand Down Expand Up @@ -130,10 +126,10 @@ test_that('formatter_logging works', {
})

test_that('special chars in the text work', {
expect_equal(formatter_glue('JSON: {toJSON(1:4)}'), 'JSON: [1,2,3,4]')
expect_equal(formatter_glue('JSON: {toJSON(iris[1:2, ], auto_unbox = TRUE)}'), 'JSON: [{"Sepal.Length":5.1,"Sepal.Width":3.5,"Petal.Length":1.4,"Petal.Width":0.2,"Species":"setosa"},{"Sepal.Length":4.9,"Sepal.Width":3,"Petal.Length":1.4,"Petal.Width":0.2,"Species":"setosa"}]') # nolint
expect_output(log_info('JSON: {toJSON(1:4)}'), '[1,2,3,4]')
expect_output(log_info('JSON: {toJSON(iris[1:2, ], auto_unbox = TRUE)}'), '[{"Sepal.Length":5.1,"Sepal.Width":3.5,"Petal.Length":1.4,"Petal.Width":0.2,"Species":"setosa"},{"Sepal.Length":4.9,"Sepal.Width":3,"Petal.Length":1.4,"Petal.Width":0.2,"Species":"setosa"}]') # nolint
expect_equal(formatter_glue('JSON: {jsonlite::toJSON(1:4)}'), 'JSON: [1,2,3,4]')
expect_equal(formatter_glue('JSON: {jsonlite::toJSON(iris[1:2, ], auto_unbox = TRUE)}'), 'JSON: [{"Sepal.Length":5.1,"Sepal.Width":3.5,"Petal.Length":1.4,"Petal.Width":0.2,"Species":"setosa"},{"Sepal.Length":4.9,"Sepal.Width":3,"Petal.Length":1.4,"Petal.Width":0.2,"Species":"setosa"}]') # nolint
expect_output(log_info('JSON: {jsonlite::toJSON(1:4)}'), '[1,2,3,4]')
expect_output(log_info('JSON: {jsonlite::toJSON(iris[1:2, ], auto_unbox = TRUE)}'), '[{"Sepal.Length":5.1,"Sepal.Width":3.5,"Petal.Length":1.4,"Petal.Width":0.2,"Species":"setosa"},{"Sepal.Length":4.9,"Sepal.Width":3,"Petal.Length":1.4,"Petal.Width":0.2,"Species":"setosa"}]') # nolint
})

test_that('pander formatter', {
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library(logger)
library(testthat)

## save current settings so that we can reset later
appender <- log_appender()

Expand Down
8 changes: 2 additions & 6 deletions tests/testthat/test-layout.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
library(logger)
library(testthat)
library(jsonlite)

## save current settings so that we can reset later
layout <- log_layout()
appender <- log_appender()
Expand Down Expand Up @@ -33,8 +29,8 @@ test_that('metavars', {

log_layout(layout_json())
test_that('JSON layout', {
expect_equal(fromJSON(capture.output(log_info('foobar')))$level, 'INFO')
expect_equal(fromJSON(capture.output(log_info('foobar')))$msg, 'foobar')
expect_equal(jsonlite::fromJSON(capture.output(log_info('foobar')))$level, 'INFO')
expect_equal(jsonlite::fromJSON(capture.output(log_info('foobar')))$msg, 'foobar')
})

log_layout(layout_json_parser(fields = c()))
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-logger.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library(logger)
library(testthat)

## save current settings so that we can reset later
threshold <- log_threshold()
appender <- log_appender()
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-return.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library(logger)
library(testthat)

## save current settings so that we can reset later
formatter <- log_formatter()
appender <- log_appender()
Expand Down
19 changes: 8 additions & 11 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library(logger)
library(testthat)

## save current settings so that we can reset later
appender <- log_appender()
log_appender(appender_stdout)
Expand All @@ -17,23 +14,23 @@ test_that('except helper', {
})

test_that('validate_log_level', {
expect_equal(logger:::validate_log_level(ERROR), ERROR)
expect_equal(logger:::validate_log_level('ERROR'), ERROR)
expect_error(logger:::validate_log_level('FOOBAR'), 'log level')
expect_equal(validate_log_level(ERROR), ERROR)
expect_equal(validate_log_level('ERROR'), ERROR)
expect_error(validate_log_level('FOOBAR'), 'log level')
})

test_that('catch_base_log', {
expect_true(nchar(logger:::catch_base_log(ERROR, NA_character_)) == 28)
expect_true(nchar(logger:::catch_base_log(INFO, NA_character_)) == 27)
expect_true(nchar(catch_base_log(ERROR, NA_character_)) == 28)
expect_true(nchar(catch_base_log(INFO, NA_character_)) == 27)
layout_original <- log_layout()
log_layout(layout_blank)
expect_true(nchar(logger:::catch_base_log(INFO, NA_character_)) == 0)
expect_true(nchar(catch_base_log(INFO, NA_character_)) == 0)
log_layout(layout_original)
layout_original <- log_layout(namespace = 'TEMP')
logger <- layout_glue_generator(format = '{namespace}/{fn} {level}: {msg}')
log_layout(logger, namespace = 'TEMP')
expect_true(nchar(logger:::catch_base_log(INFO, 'TEMP', .topcall = NA)) == 14)
expect_true(nchar(logger:::catch_base_log(INFO, 'TEMP', .topcall = call('5char'))) == 17)
expect_true(nchar(catch_base_log(INFO, 'TEMP', .topcall = NA)) == 14)
expect_true(nchar(catch_base_log(INFO, 'TEMP', .topcall = call('5char'))) == 17)
log_layout(layout_original)
})

Expand Down

0 comments on commit fa5ac9e

Please sign in to comment.