tests/testthat/test-create_relation_matrix.R

# Tests for create_relation_matrix()

test_that("create_relation_matrix creates empty matrix from integer", {
  result <- create_relation_matrix(3)

  expect_true(is.matrix(result))
  expect_equal(nrow(result), 3)
  expect_equal(ncol(result), 3)
  expect_true(all(result == 0))
})

test_that("create_relation_matrix uses custom node names", {
  result <- create_relation_matrix(3, nodes = c("X", "Y", "Z"))

  expect_equal(rownames(result), c("X", "Y", "Z"))
  expect_equal(colnames(result), c("X", "Y", "Z"))
})

test_that("create_relation_matrix converts edge list", {
  edge_df <- data.frame(
    source = c("A", "B"),
    target = c("B", "C"),
    stringsAsFactors = FALSE
  )

  result <- create_relation_matrix(edge_df, from = "source", to = "target")

  expect_equal(result["A", "B"], 1)
  expect_equal(result["B", "C"], 1)
})

test_that("create_relation_matrix validates input", {
  # Non-positive dimension
  expect_error(create_relation_matrix(0), "positive integer")
  expect_error(create_relation_matrix(-1), "positive integer")

  # Mismatched nodes length
  expect_error(create_relation_matrix(3, nodes = c("A", "B")),
               "must equal matrix dimension")
})

test_that("create_relation_matrix handles existing matrix", {
  existing <- matrix(c(0, 1, 0, 0), nrow = 2)

  result <- create_relation_matrix(existing)

  # Result should be a valid matrix with same values
  expect_true(is.matrix(result))
  expect_equal(nrow(result), 2)
  expect_equal(ncol(result), 2)
  expect_equal(as.vector(result), as.vector(existing))
})

Try the ISMtools package in your browser

Any scripts or data that you put into this service are public.

ISMtools documentation built on March 13, 2026, 1:06 a.m.