# setup tinytest for checkmate functionality
library("tinytest")
library("checkmate")
using("checkmate")

# ------ default call ------

# returns invisible numeric vector of bar midpoints
mp <- plot_pubs()
expect_true(is.numeric(mp))
expect_true(length(mp) > 0L)

# ------ author_id filtering ------

# single author
mp_author <- plot_pubs(author_id = "jfisher")
expect_true(is.numeric(mp_author))
expect_true(length(mp_author) > 0L)

# multiple authors
mp_multi <- plot_pubs(author_id = c("jfisher", "ktreinen"))
expect_true(is.numeric(mp_multi))

# ------ full_range ------

# full_range = FALSE limits x-axis to author's year range
mp_full  <- plot_pubs(author_id = "jfisher", full_range = TRUE)
mp_short <- plot_pubs(author_id = "jfisher", full_range = FALSE)
expect_true(length(mp_full) >= length(mp_short))

# ------ custom colors ------

mp_cols <- plot_pubs(colors = c("USGS" = "#FF0000", "Non-USGS" = "#0000FF"))
expect_true(is.numeric(mp_cols))

# ------ par() is restored after call ------

graphics::par(mar = c(5, 4, 4, 2))
plot_pubs()
expect_equal(graphics::par("mar"), c(5, 4, 4, 2))

# ------ invalid arguments ------

expect_error(plot_pubs(author_id = "zzznobody"))
expect_error(plot_pubs(colors = c("#FF0000", "#0000FF")))           # unnamed
expect_error(plot_pubs(colors = c("A" = "#FF0000", "B" = "#000"))) # wrong names
expect_error(plot_pubs(pubs = data.frame()))                        # wrong class
