~subsetpark/ec

e85c9853e2f2332e47aaa381a543d0f78de5cd4e — Zach Smith 7 months ago 6651ead
test-arity helper
1 files changed, 17 insertions(+), 7 deletions(-)

M test/arity-checks.janet
M test/arity-checks.janet => test/arity-checks.janet +17 -7
@@ 1,28 1,38 @@
(import /src/calc)

(import /src/operations :prefix "o/")
(import /src/adverbs :prefix "a/")

(use testament)
(use /test-support)

(defn test-arity
  [n elem doc]
  (is (= n (calc/get-arity elem)) doc)
  (let [s (:new calc/Stack)]
    (loop [_ :range [0 n]]
      (calc/push s (:new calc/Int 0)))
    (calc/push s elem)
    (calc/push s a/apply-quotation)))

(deftest get-arity
  (is (= 2 (calc/get-arity (quote-wrap o/add)))
  (test-arity 2 (quote-wrap o/add)
      "A quoted operation's arity is the operation's arity")
  (is (= 3 (calc/get-arity (quote-wrap o/add o/add)))
  (test-arity 3 (quote-wrap o/add o/add)
      ```
      A composition of operations' arity is the greatest of the
      individual operations' arities, each adjusted for the out-arity
      of the one prior
      ```)
  (is (= 2 (calc/get-arity (quote-wrap o/add 2 o/add)))
  (test-arity 2 (quote-wrap o/add 2 o/add)
      "Interposed numbers will reduce the arity of a quotation")
  (is (= 0 (calc/get-arity (:new calc/Int 2)))
  (test-arity 0 (:new calc/Int 2)
      "A number's arity is 0")
  (is (= 0 (calc/get-arity (quote-wrap 2)))
  (test-arity 0 (quote-wrap 2)
      "A quoted number's arity is 0")
  (is (= 0 (calc/get-arity (quote-wrap 2 2 2 2)))
  (test-arity 0 (quote-wrap 2 2 2 2)
      "A quoted sequence of numbers is 0")
  (is (= 0 (calc/get-arity (quote-wrap 2 2 o/add)))
  (test-arity 0 (quote-wrap 2 2 o/add)
      "A quoted operation is 0 if the quote enough values to fulfill it"))

(run-tests!)