test_that("xml_question generates correct XML structure with all inputs", {
# Inputs
type <- "multichoice"
name <- "Sample Question"
questiontext <- ''
question_body <- '4'
question_tags <- 'math'
# Expected output
expected_output <- structure(
"\n\n Sample Question\n \n 4\n math\n",
class = c("glue", "character")
)
# Run the function
result <- xml_question(type, name, questiontext, question_body, question_tags)
# Check if the result matches the expected output
expect_equal(result, expected_output)
})
test_that("xml_question_tags returns empty string for NULL input", {
# Input
tag_values <- NULL
# Expected output
expected_output <- ""
# Run the function
result <- xml_question_tags(tag_values)
# Check if the result matches the expected output
expect_equal(result, expected_output)
})
test_that("xml_question_tags returns empty string for empty vector input", {
# Input
tag_values <- character(0)
# Expected output
expected_output <- ""
# Run the function
result <- xml_question_tags(tag_values)
# Check if the result matches the expected output
expect_equal(result, expected_output)
})
test_that("xml_question_tags generates correct XML for a single tag", {
# Input
tag_values <- "math"
# Expected output
expected_output <- paste0(
"\n",
" math\n",
" "
)
# Run the function
result <- xml_question_tags(tag_values)
# Check if the result matches the expected output
expect_equal(result, expected_output)
})
test_that("xml_question_tags generates correct XML for multiple tags", {
# Input
tag_values <- c("math", "science", "history")
# Expected output
expected_output <- paste0(
"\n",
" math\n",
" science\n",
" history\n",
" "
)
# Run the function
result <- xml_question_tags(tag_values)
# Check if the result matches the expected output
expect_equal(result, expected_output)
})
test_that("xml_question_category generates correct XML for a valid category", {
# Input
category <- "Mathematics"
# Expected output
expected_output <- glue::glue(
'
$course$/top/{category}
'
)
# Run the function
result <- xml_question_category(category)
# Check if the result matches the expected output
expect_equal(result, expected_output)
})
test_that("xml_question_category handles empty category name", {
# Input
category <- ""
# Expected output
expected_output <- glue::glue(
'
$course$/top/{category}
'
)
# Run the function
result <- xml_question_category(category)
# Check if the result matches the expected output
expect_equal(result, expected_output)
})
test_that("xml_image generates correct output for valid image input", {
# Load the test image
image_path <- system.file("extdata", "divide.png", package = "moodef")
image_alt <- "Test Image"
# Call the function
result <- xml_image(image = image_path, image_alt = image_alt, adapt_images = FALSE)
# Check the generated img and fimg
expect_match(result$img, "
", fixed = TRUE)
})
test_that("xml_image handles empty image input gracefully", {
# Call the function with empty image
result <- xml_image(image = "", image_alt = "No Image", adapt_images = FALSE)
# Check the output
expect_equal(result$img, "")
expect_equal(result$fimg, "")
})
test_that("xml_image correctly adapts image dimensions when adapt_images is TRUE", {
# Load the test image
image_path <- system.file("extdata", "divide.png", package = "moodef")
image_alt <- "Test Image"
# Mock the adapt_image function
mock_adapt_image <- function(image_file, width, height) {
# Return a modified image path for testing
return(image_file)
}
with_mocked_bindings(
adapt_image = mock_adapt_image,
{
# Call the function
result <- xml_image(image = image_path, image_alt = image_alt, adapt_images = TRUE, width = 100, height = 100)
}
)
# Check the generated img and fimg
expect_match(result$img, "width=\"100\" height=\"100\"", fixed = TRUE)
})
test_that("xml_image handles images with no alternative text", {
# Load the test image
image_path <- system.file("extdata", "divide.png", package = "moodef")
# Call the function without image_alt
result <- xml_image(image = image_path, image_alt = "", adapt_images = FALSE)
# Check the generated img
expect_match(result$img, "
Characters"
# Call the function
result <- xml_image(image = image_path, image_alt = image_alt, adapt_images = FALSE)
# Check that special characters are properly handled
expect_match(result$img, "alt=\"Special < & > Characters\"", fixed = TRUE)
})
test_that("xml_questiontext generates correct question text with all inputs", {
# Inputs
copyright <- "Copyright Info"
license <- "License Info"
adapt_images <- FALSE
width <- 200
height <- 100
question <- "What is the capital of France?"
image <- system.file("extdata", "divide.png", package = "moodef")
image_alt <- "Sample Image"
type <- "multichoice"
author <- "John Doe"
fb_general <- "Paris is the capital of France."
idnumber <- "Q001"
# Call the function
result <- xml_questiontext(copyright, license, adapt_images, width, height,
question, image, image_alt, type, author,
fb_general, idnumber)
# Expectations
expect_match(result, "", fixed = TRUE)
expect_match(result, "", fixed = TRUE)
expect_match(result, "", fixed = TRUE)
expect_match(result, "
What is the capital of France?
", fixed = TRUE)
expect_match(result, "Paris is the capital of France.]]>", fixed = TRUE)
expect_match(result, "1.0", fixed = TRUE)
expect_match(result, "0.5", fixed = TRUE)
expect_match(result, "Q001", fixed = TRUE)
})
test_that("xml_questiontext handles missing optional inputs gracefully", {
# Inputs
copyright <- ""
license <- ""
adapt_images <- FALSE
width <- 200
height <- 100
question <- "What is 2 + 2?"
image <- system.file("extdata", "divide.png", package = "moodef")
image_alt <- "Math Image"
type <- "truefalse"
author <- ""
fb_general <- ""
idnumber <- ""
# Call the function
result <- xml_questiontext(copyright, license, adapt_images, width, height,
question, image, image_alt, type, author,
fb_general, idnumber)
# Expectations
expect_false(grepl("", result))
expect_false(grepl("", result))
expect_false(grepl("