tests/testthat/test-basicexample.R

context("Basic example")
# This file was copied from flipStandardCharts/tests/testthat/test-basicexample.R
# The only difference is TestWidget() is not commented out

# Set up various data types to test
set.seed(1234)
random <- rnorm(20) # includes negative numbers
positives <- abs(random)

# Create lists of all charting functions, and data types and options to use
# Functions excluded: Waterfall, Venn, Stream, HeatMap
charting.funcs <- c("Column", "Bar", "Area", "Line", "Radar",
                    "Scatter", "LabeledScatter", "Box", "Bean", "Distribution",
                    "Density", "Violin")  # removed 'Pie' because of random diffs
dat.list <- c("random", "positives")

# Iterate through all combinations of charting functions and data types
for (func in charting.funcs)
{
    for (dat in dat.list)
    {
        # Create name which will appear in the error message if test fails
        # Filestem should be prefixed by test file name to avoid name conflicts
        filestem <- paste0("basicexample-", tolower(func), "-", dat)

        test_that(filestem, {

            # Create command that will create widget
            cmd <- paste0("pp <- ", func, "(", dat, ")")

            # Run command and check outputs
            # We need to separate cases which behave differently
            # For this example pie chart charts handle
            # negative values differently from the other chart types
            if (filestem == "basicexample-pie-random")
                expect_warning(eval(parse(text=cmd)))
            else
                expect_error(eval(parse(text=cmd)), NA)
            
            # The following lines are useful for interactive viewing
            # But should be commented out after everything is set up
            #print(pp)
            #readline(prompt=paste0(filestem, ": press [enter] to continue: "))
            
            # Create snapshot and compare against reference (in flipChartTests)
            # If none exists, the snapshot will be accepted as the reference
            expect_true(TestWidget(pp, filestem))
        })
    }
}
Displayr/flipChartTests documentation built on Feb. 23, 2024, 9:36 p.m.