source("helpers.R")
using("tinysnapshot")

f = function() {
  tinyplot(mpg ~ hp | factor(cyl),
    data = mtcars,
    facet = ~am,
    type = type_text(
      labels = row.names(mtcars),
      font = 2,
      adj = 0))
}
expect_snapshot_plot(f, label = "text_facets")


f = function() {
  x = c(1, 2, 3)
  y = c(1, 2, 3)
  z = c("a", NA, "c")
  tinyplot(x, y, labels = z, type = "text")
}
expect_snapshot_plot(f, label = "text_missing_label")


f = function() {
  x = c(1, 2, 3)
  y = c(1, 2, 3)
  z = "a"
  tinyplot(x, y, labels = z, type = "text")
}
expect_snapshot_plot(f, label = "text_single_character")


# labeller arg formats text labels, e.g. to match a formatted axis (#617)
f = function() {
  d = data.frame(x = c("A", "B"), y = c(0.5, 0.8))
  tinyplot(y ~ x, data = d, type = "bar", ylim = c(0, 1), yaxl = "%")
  tinyplot_add(type = type_text(labeller = "%", adj = c(0.5, -0.5)))
}
expect_snapshot_plot(f, label = "text_labeller_percent")


# repel arg nudges overlapping labels apart (#318)
f = function() {
  tinyplot(mpg ~ wt, data = mtcars,
    type = type_text(labels =  row.names(mtcars), repel = TRUE),
    main = "repel = TRUE")
}
expect_snapshot_plot(f, label = "text_repel")


# top-level `labels` with NSE in the formula method (#332). airquality has NAs
# that the formula drops, so the bare `labels` column must align with the rows
# retained after na.action.
mtcars2 = within(
  mtcars, {
    make = sub(" .*", "", row.names(mtcars))
    model = sub("^\\S+\\s+", "", row.names(mtcars))
  }
)
f = function() {
  tinyplot(mpg ~ wt, data = mtcars2, labels = make,
    type = type_text(repel = TRUE),
    main = "labels NSE + repel = TRUE")
  
}
expect_snapshot_plot(f, label = "text_labels_nse")

# logical checks for behaviours a snapshot can't express
# length mismatch is caught (model.frame in the formula path)
expect_error(tinyplot(mpg ~ wt, data = mtcars, type = "text", labels = c("a", "b")))
# top-level `labels` overrides the constructor-level arg
expect_silent(tinyplot(1:3, type = type_text(labels = "x"), labels = c("a", "b", "c")))
