test-core-lib.R 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. test_that("threading macro", {
  2. res <- llr_test("
  3. (->
  4. (r/seq 1 10)
  5. (r/lapply (fn [x] (* x x)))
  6. (r/purrr::keep (fn [x] (= x 25))))
  7. ")
  8. expect_equal(res, list(25), ignore_attr = TRUE)
  9. })
  10. test_that("addition uses sum for variadic functions", {
  11. res <- llr_test("(+ 1 2 3 4 5 6 7 8 9 10)")
  12. expect_equal(res, sum(1:10), ignore_attr = TRUE)
  13. })
  14. test_that("comp works", {
  15. res <- llr_test("((comp inc inc inc) 0)")
  16. expect_equal(res, 3, ignore_attr = TRUE)
  17. res <- llr_test("((comp inc) 0)")
  18. expect_equal(res, 1, ignore_attr = TRUE)
  19. })
  20. test_that("contains", {
  21. expect_true(llr_test("(contains? {:a 1 :b 2} :a)"))
  22. expect_false(llr_test("(contains? {:a 1 :b 2} :x)"))
  23. })
  24. test_that("assoc and conj on maps", {
  25. res <- llr_test("(assoc (conj {:a 1} {:b 2}) :a 42)")
  26. expect_equal(res$get(":a"), 42, ignore_attr = TRUE)
  27. expect_equal(res$get(":b"), 2, ignore_attr = TRUE)
  28. })
  29. test_that("assoc on vectors", {
  30. res <- llr_test("(assoc [1 2 3] 2 42)")
  31. expect_equal(res, list(1, 42, 3), ignore_attr = TRUE)
  32. })
  33. test_that("count on maps counts keys", {
  34. res <- llr_test("(count {:a 1})")
  35. expect_equal(res, 1, ignore_attr = TRUE)
  36. })
  37. test_that("= compares values", {
  38. expect_true(llr_test("(= 1 1)"))
  39. expect_true(llr_test("(= [1] [1])"))
  40. expect_true(llr_test("(= [1 2 3] '(1 2 3))"))
  41. expect_false(llr_test("(= 1 \"1\")"))
  42. expect_false(llr_test("(= r/mtcars \"1\")"))
  43. })
  44. test_that("map is type stable", {
  45. expect_true(llr_test("(vector? (map dec [1 2 3]))"))
  46. })
  47. test_that("correct and stable conj", {
  48. expect_true(
  49. llr_test("(= (conj [1 2] [5 6]) [1 2 [5 6]])")
  50. )
  51. expect_true(
  52. llr_test("(= (conj '(1 2) [3 4]) '([3 4] 1 2))")
  53. )
  54. expect_true(
  55. llr_test("(= (conj [1 2] 3 4 5) [1 2 3 4 5])")
  56. )
  57. expect_true(
  58. llr_test("(= (conj '(1 2) 3 4 5) '(5 4 3 1 2))")
  59. )
  60. expect_true(
  61. llr_test("(list? (conj '(1 2) 3 4 5))")
  62. )
  63. })
  64. test_that("get indexes start at 0", {
  65. expect_true(
  66. llr_test("(= (get [1 2 3] 2) 3)")
  67. )
  68. expect_true(
  69. llr_test("(nil? (get [1 2 3] -1))")
  70. )
  71. expect_true(
  72. llr_test("(nil? (get [1 2 3] 3))")
  73. )
  74. })
  75. test_that("map2", {
  76. res <- llr_test("(map r/sum [1 2 3] [2 3 4])")
  77. expect_equal(res, do.call(ral_list, as.list(1:3 + 2:4)))
  78. })
  79. test_that("vector", {
  80. res <- llr_test("(vector (r/seq_len 10))")
  81. expect_equal(res, do.call(ral_vector, as.list(seq_len(10))))
  82. })