Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbfnk committed Nov 13, 2024
1 parent d58f6dd commit ce5664c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(stanedit)

test_check("stanedit")
56 changes: 56 additions & 0 deletions tests/testthat/test-model-edits.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
model_file_name <- system.file(package = "stanedit", "regression.stan")
reg <- stanedit(filename = model_file_name)

test_that("the example file can be loaded and the right class is assigned", {
expect_s3_class(reg, "stanedit")
})

test_that("operators work", {
reg2 <- remove_lines(reg, "parameters")
expect_true(reg == reg)
expect_false(reg == reg2)
expect_true(reg != reg2)
expect_false(reg == reg2)
expect_equal(reg[3], "vector[N] x;")
reg[3] <- "test;"
expect_equal(reg[3], "test;")
})

test_that("blocks can be added and retrieved", {
block <- c("real gamma;", "gamma = alpha * beta;")
reg2 <- add_block(reg, "transformed parameters", block)
line_nos <- find_block(reg2, "transformed parameters", inner = TRUE)
line_nos_with_shell <- find_block(reg2, "transformed parameters")
lines <- get_block(reg2, "transformed parameters")
lines_with_shell <- get_block(reg2, "transformed parameters", shell = TRUE)

expect_equal(lines, block)
expect_equal(reg2[line_nos], block)
expect_equal(reg2[line_nos_with_shell], lines_with_shell)
expect_equal(lines_with_shell[-c(1, length(lines_with_shell))], block)
})

test_that("declarations are found", {
expect_equal(find_declaration(reg, "alpha"), 7)
})

test_that("variables are found", {
expect_equal(get_vars(reg, "parameters"), c("alpha", "beta", "sigma"))
})

test_that("lines can be inserted", {
newline <- "real test;"
reg2 <- insert_lines(reg, newline, before = 3)
expect_equal(reg2[3], newline)
})

test_that("lines can be removed", {
reg2 <- remove_lines(reg, "model", only = "y", type = "sample")
reg3 <- remove_lines(
reg, "model", only = "y", type = "sample", preserve_shell = TRUE
)
reg4 <- remove_lines(reg, "model", only = "y", type = "assignment")
expect_length(get_block(reg2, "model"), 0)
expect_length(get_block(reg3, "model", shell = TRUE), 2)
expect_equal(reg4, reg)
})

0 comments on commit ce5664c

Please sign in to comment.