test-special-forms.R 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. test_that("def works", {
  2. res <- llr_test("(def x 1)
  3. x")
  4. expect_equal(res, 1, ignore_attr = TRUE)
  5. })
  6. test_that("fn works", {
  7. res <- llr_test("(fn [a b] (+ a b))")
  8. expect_equal(res(1, 2), 3)
  9. })
  10. test_that("fn works with a name", {
  11. res <- llr_test("(fn myname [a b] (+ a b))")
  12. expect_equal(res(1, 2), 3)
  13. })
  14. test_that("meta data works 1", {
  15. interp <- llr_env$new()
  16. res <- interp$eval("(def x ^{:wat 1} [1 2 3])
  17. (meta x)")
  18. expect_equal(res$get(":wat"), ral_integer(1L))
  19. res <- interp$eval("(def ^{:wat 1} y [1 2 3])
  20. (meta y)")
  21. expect_equal(res, NULL)
  22. res <- interp$eval("(meta 'y)")
  23. expect_equal(res$get(":wat"), 1, ignore_attr = TRUE)
  24. })
  25. test_that("fn multi methods", {
  26. res <- llr_test("
  27. (fn
  28. ([] 0)
  29. ([a] a)
  30. ([a b] (+ a b)))
  31. ")
  32. expect_equal(res(), 0, ignore_attr = TRUE)
  33. expect_equal(res(32), 32, ignore_attr = TRUE)
  34. expect_equal(res(32, 10), 42, ignore_attr = TRUE)
  35. })
  36. test_that("fn multi methods with dots", {
  37. interp <- llr_env$new()
  38. interp$eval("
  39. (def plus2 (fn plus
  40. ([] 0)
  41. ([a] a)
  42. ([a b] (r/base::`+` a b))
  43. ([a b & more] (reduce plus (concat [a b] more)))))
  44. ")
  45. expect_equal(interp$eval("(plus2)"), 0, ignore_attr = TRUE)
  46. expect_equal(interp$eval("(plus2 42)"), 42, ignore_attr = TRUE)
  47. expect_equal(interp$eval("(plus2 42 1)"), 43, ignore_attr = TRUE)
  48. expect_equal(interp$eval("(plus2 1 2 3 4 5 6 7 8 9 10)"), sum(1:10), ignore_attr = TRUE)
  49. })
  50. test_that(":: works", {
  51. res <- llr_test("(r/base::round 10.4)")
  52. expect_equal(res, round(10.4), ignore_attr = TRUE)
  53. })
  54. test_that("$ works", {
  55. res <- llr_test("(r/$ r/datasets::mtcars hp)")
  56. expect_equal(res, mtcars$hp)
  57. })
  58. test_that("defmacro works", {
  59. res <- llr_test("
  60. (defmacro test [] (Sys.getpid))
  61. (def a (test))
  62. a
  63. ")
  64. expect_equal(res, Sys.getpid(), ignore_attr = TRUE)
  65. })
  66. test_that("defmacro #2", {
  67. res <- llr_test("
  68. (defmacro infix [a b c]
  69. `(~b ~a ~c))
  70. (infix 1 + 1)
  71. ")
  72. expect_equal(res, 2, ignore_attr = TRUE)
  73. })
  74. test_that("variadic fn args", {
  75. res <- llr_test("
  76. (def sum_list
  77. (fn [vals] (reduce + 0 vals)))
  78. (def myplus
  79. (fn [a b & more] (+ (* a b) (sum_list more))))
  80. (myplus 1 2 3)
  81. ")
  82. expect_equal(res, 1 * 2 + 3, ignore_attr = TRUE)
  83. })
  84. test_that("let works", {
  85. code <- "
  86. (def a 42)
  87. (def x (let [a 1 b (+ a a)]
  88. (* a b)))
  89. [a x]
  90. "
  91. res <- llr_test(code)
  92. expect_equal(res[[1]], 42, ignore_attr = TRUE)
  93. expect_equal(res[[2]], 2, ignore_attr = TRUE)
  94. })
  95. test_that("loop and recur", {
  96. res <- llr_test("
  97. (loop [n 10 acc 0]
  98. (if (zero? n) acc (recur (dec n) (inc acc))))
  99. ")
  100. expect_equal(res, 10, ignore_attr = TRUE)
  101. })
  102. test_that("loop and recur", {
  103. res <- llr_test("
  104. (loop [n 10 acc 0]
  105. (let [a n]
  106. (if (zero? n) acc (recur (dec a) (inc acc)))))
  107. ")
  108. expect_equal(res, 10, ignore_attr = TRUE)
  109. })
  110. test_that("if works", {
  111. res <- llr_test("(if 1 42 1)")
  112. expect_equal(res, 42, ignore_attr = TRUE)
  113. res <- llr_test("(if true 42 1)")
  114. expect_equal(res, 42, ignore_attr = TRUE)
  115. res <- llr_test("(if r/mtcars 42 1)")
  116. expect_equal(res, 42, ignore_attr = TRUE)
  117. res <- llr_test("(if false 42 1)")
  118. expect_equal(res, 1, ignore_attr = TRUE)
  119. res <- llr_test("(if r/NULL 42 1)")
  120. expect_equal(res, 1, ignore_attr = TRUE)
  121. })
  122. test_that("keyword format", {
  123. expect_equal(
  124. format(llr_test("{:a 1}")),
  125. "{:a 1}"
  126. )
  127. })