From ce5664c36b0a64250864d2c6e55688b82ceb9744 Mon Sep 17 00:00:00 2001 From: Sebastian Funk Date: Wed, 13 Nov 2024 16:15:51 +0000 Subject: [PATCH] add tests --- tests/testthat.R | 12 +++++++ tests/testthat/test-model-edits.R | 56 +++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-model-edits.R diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..966999d --- /dev/null +++ b/tests/testthat.R @@ -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") diff --git a/tests/testthat/test-model-edits.R b/tests/testthat/test-model-edits.R new file mode 100644 index 0000000..f802c32 --- /dev/null +++ b/tests/testthat/test-model-edits.R @@ -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) +})