Add tests for dynamic functions

I accidentally broke `curry`, what better incentive for writing some
tests than preventing my own future silly mistakes :)

I also added and-internal because we cannot perform direct comparisons
on lists--so, instead we build member-wise comparisons by zipping, then
reduce the result using and.
This commit is contained in:
scottolsen 2020-04-24 17:00:30 -04:00
parent 3cbe0e8c79
commit ac87e8fbdc
2 changed files with 73 additions and 1 deletions

View File

@ -188,6 +188,11 @@
(defndynamic or-internal [x y]
(if x true y))
;; Dynamic.and already exists, but since it's a special form, it can't be passed
;; to higher order functions like reduce. So, we define an alternative here.
(defndynamic and-internal [x y]
(if x y false))
(doc curry
"Returns a curried function accepting a single argument, that applies f to x
and then to the following argument.

View File

@ -68,6 +68,46 @@
(Dynamic.and (= 'ace (Symbol.join (eval (Dynamic.car zipped))))
(= 'dog (Symbol.join (eval (Dynamic.cadr zipped)))))))
(defmacro test-curry []
(= 3 ((Dynamic.curry + 1) 2)))
(defmacro test-flip []
(= 'Foo.Bar ((Dynamic.flip Symbol.prefix) 'Bar 'Foo)))
(defmacro test-compose []
(= '() ((Dynamic.compose Dynamic.empty Dynamic.take) 2 '(1 2 3 4))))
(defmacro test-reduce []
(= 10 (Dynamic.reduce + 0 '(1 2 3 4))))
(defmacro test-unreduce []
(Dynamic.reduce Dynamic.and-internal true
(Dynamic.map 'eval
(Dynamic.zip = '(1 2 3 4) (Dynamic.unreduce (curry + 1) 0 4 (list))))))
(defmacro test-filter []
(Dynamic.reduce Dynamic.and-internal true
(Dynamic.map 'eval
(Dynamic.zip = '('a 'a 'a 'a)
(Dynamic.map Dynamic.quoted
(Dynamic.filter (fn [x] (= 'a x)) '(a b a b a b a b)))))))
(defmacro test-empty []
;; We can't compare '[] and '[] for some reason.
;; But '() and '() are comparable
(Dynamic.and (= '() (Dynamic.empty '(1 2 3 4)))
(empty? (Dynamic.empty '[1 2 3 4]))))
(defmacro test-reverse []
(Dynamic.reduce Dynamic.and-internal true
(Dynamic.map 'eval
(Dynamic.zip = '(4 3 2 1) (Dynamic.reverse '(1 2 3 4))))))
(defmacro test-take []
(let [result (Dynamic.take 2 '(1 2 3 4))]
(Dynamic.and (= 1 (car result ))
(= '() (cddr result)))))
(deftest test
(assert-true test
(test-let-do)
@ -236,5 +276,32 @@
"map works as expected")
(assert-true test
(test-zip)
"zip works as expected")
"zip works as expected")
(assert-true test
(test-curry)
"curry works as expected")
(assert-true test
(test-flip)
"filp works as expected")
(assert-true test
(test-compose)
"compose works as expected")
(assert-true test
(test-reduce)
"reduce works as expected")
(assert-true test
(test-unreduce)
"unreduce works as expected")
(assert-true test
(test-filter)
"filter works as expected")
(assert-true test
(test-reverse)
"reverse works as expected")
(assert-true test
(test-empty)
"empty works as expected")
(assert-true test
(test-take)
"take works as expected")
)