PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/inst/tests/test-has.R

https://bitbucket.org/fmark/assertive
R | 197 lines | 159 code | 38 blank | 0 comment | 0 complexity | 4f1e38f6f3dfb73d1ae6fe72cb6a22b9 MD5 | raw file
  1. test.has_attributes.struct_with_attrs.returns_true_if_attr_exists <- function()
  2. {
  3. x <- structure(list(a = 1), foo = 1, bar = 2)
  4. attrs <- c("names", "foo", "bar", "baz", NA)
  5. expected <- c(TRUE, TRUE, TRUE, FALSE, FALSE)
  6. names(expected) <- attrs
  7. checkEquals(
  8. expected,
  9. has_attributes(x, c("names", "foo", "bar", "baz", NA))
  10. )
  11. }
  12. test.has_colnames.with_colnames.returns_true <- function()
  13. {
  14. x <- matrix(1:12, nrow = 3, dimnames = list(letters[1:3], LETTERS[1:4]))
  15. checkTrue(has_colnames(x))
  16. }
  17. test.has_colnames.without_colnames.returns_false <- function()
  18. {
  19. x <- matrix(1:12, nrow = 3)
  20. checkTrue(!has_colnames(x))
  21. }
  22. test.has_colnames.with_empty_colnames.returns_false <- function()
  23. {
  24. x <- matrix(
  25. 1:12,
  26. nrow = 3,
  27. dimnames = list(character(3), character(4))
  28. )
  29. checkTrue(!has_colnames(x))
  30. }
  31. test.has_cols.with_columns.returns_true <- function()
  32. {
  33. x <- matrix(1:12, nrow = 3)
  34. checkTrue(has_cols(x))
  35. }
  36. test.has_cols.without_columns.returns_true <- function()
  37. {
  38. x <- 1:10
  39. checkTrue(!has_cols(x))
  40. }
  41. test.has_dimnames.with_dimnames.returns_true <- function()
  42. {
  43. x <- matrix(1:12, nrow = 3, dimnames = list(letters[1:3], LETTERS[1:4]))
  44. checkTrue(has_dimnames(x))
  45. }
  46. test.has_dimnames.without_dimnames.returns_false <- function()
  47. {
  48. x <- matrix(1:12, nrow = 3)
  49. checkTrue(!has_dimnames(x))
  50. }
  51. test.has_dimnames.with_empty_dimnames.returns_false <- function()
  52. {
  53. x <- matrix(
  54. 1:12,
  55. nrow = 3,
  56. dimnames = list(character(3), character(4))
  57. )
  58. checkTrue(!has_dimnames(x))
  59. }
  60. test.has_dims.a_matrix.returns_true <- function()
  61. {
  62. mat <- matrix(1:12, nrow = 3)
  63. checkTrue(has_dims(mat))
  64. }
  65. test.has_dims.a_data_frame.returns_true <- function()
  66. {
  67. dfr <- data.frame(x = 1:5, y = runif(5))
  68. checkTrue(has_dims(dfr))
  69. }
  70. test.has_dims.a_vector.returns_false <- function()
  71. {
  72. x <- 1:3
  73. checkTrue(!has_dims(x))
  74. }
  75. test.has_no_duplicates.without_duplicates.returns_false <- function()
  76. {
  77. x <- 1:10
  78. checkTrue(has_no_duplicates(x))
  79. }
  80. test.has_no_duplicates.with_duplicates.returns_false <- function()
  81. {
  82. x <- rep.int(1, 2)
  83. checkTrue(!has_no_duplicates(x))
  84. }
  85. test.has_duplicates.without_duplicates.returns_false <- function()
  86. {
  87. x <- 1:10
  88. checkTrue(!has_duplicates(x))
  89. }
  90. test.has_duplicates.with_duplicates.returns_false <- function()
  91. {
  92. x <- rep.int(1, 2)
  93. checkTrue(has_duplicates(x))
  94. }
  95. test.has_names.named_vector.returns_true <- function()
  96. {
  97. x <- c(foo = 1, 2, 3)
  98. checkTrue(has_names(x))
  99. }
  100. test.has_names.data_frame.returns_true <- function()
  101. {
  102. dfr <- data.frame(x = 1:5, y = runif(5))
  103. checkTrue(has_names(dfr))
  104. }
  105. test.has_names.unnamed_vector.returns_false <- function()
  106. {
  107. x <- 1:3
  108. checkTrue(!has_names(x))
  109. }
  110. test.has_rows.with_rows.returns_true <- function()
  111. {
  112. x <- matrix(1:12, nrow = 3)
  113. checkTrue(has_rows(x))
  114. }
  115. test.has_rows.without_rows.returns_true <- function()
  116. {
  117. x <- 1:10
  118. checkTrue(!has_rows(x))
  119. }
  120. test.has_rownames.with_rownames.returns_true <- function()
  121. {
  122. x <- matrix(1:12, nrow = 3, dimnames = list(letters[1:3], LETTERS[1:4]))
  123. checkTrue(has_rownames(x))
  124. }
  125. test.has_rownames.without_rownames.returns_false <- function()
  126. {
  127. x <- matrix(1:12, nrow = 3)
  128. checkTrue(!has_rownames(x))
  129. }
  130. test.has_rownames.with_empty_rownames.returns_false <- function()
  131. {
  132. x <- matrix(
  133. 1:12,
  134. nrow = 3,
  135. dimnames = list(character(3), character(4))
  136. )
  137. checkTrue(!has_rownames(x))
  138. }
  139. test.has_terms.without_terms.returns_false <- function()
  140. {
  141. x <- 1:10
  142. checkTrue(!has_terms(x))
  143. }
  144. test.has_terms.with_terms_component.returns_false <- function()
  145. {
  146. x <- list(terms = 1:10)
  147. checkTrue(has_terms(x))
  148. }
  149. test.has_terms.with_terms_attribute.returns_false <- function()
  150. {
  151. x <- 1:10
  152. attr(x, "terms") <- 1:10
  153. checkTrue(has_terms(x))
  154. }
  155. test.has_terms.lm_model.returns_false <- function()
  156. {
  157. x <- lm(uptake ~ conc, CO2)
  158. checkTrue(has_terms(x))
  159. }