2020-01-03 13:47:51 -05:00
|
|
|
;; Copyright 2020 the authors.
|
2017-04-27 14:16:57 -07:00
|
|
|
;; This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
;; license. See the LICENSE.
|
2013-11-28 23:53:02 -05:00
|
|
|
|
2015-06-17 00:11:55 +02:00
|
|
|
(import [hy.errors [HyMacroExpansionError]])
|
2016-12-26 14:44:59 -08:00
|
|
|
(require [hy.extra.anaphoric [*]])
|
2013-11-28 23:53:02 -05:00
|
|
|
|
2013-12-25 12:11:25 -05:00
|
|
|
(defn test-ap-if []
|
2019-12-05 08:41:10 -05:00
|
|
|
(ap-if True (assert (is it True)))
|
2019-12-21 13:26:37 -05:00
|
|
|
(ap-if False True (assert (is it False)))
|
|
|
|
|
|
|
|
; https://github.com/hylang/hy/issues/1847
|
|
|
|
(setv it "orig")
|
|
|
|
(setv out (ap-if (+ 1 1) (+ it 1) (+ it 10)))
|
|
|
|
(assert (= out 3))
|
2020-03-28 22:48:16 +00:00
|
|
|
(assert (= it "orig"))
|
|
|
|
|
|
|
|
(ap-if
|
|
|
|
(->> [1 2 3 4 5]
|
|
|
|
(ap-filter (= (% it 2) 0))
|
|
|
|
(list))
|
|
|
|
(assert (= it [2 4]))))
|
|
|
|
|
2013-12-25 12:11:25 -05:00
|
|
|
|
2013-11-28 23:53:02 -05:00
|
|
|
(defn test-ap-each []
|
|
|
|
(setv res [])
|
2019-12-21 14:29:52 -05:00
|
|
|
(assert (is (ap-each [1 2 3 4] (.append res it)) None))
|
2020-03-28 22:48:16 +00:00
|
|
|
(assert (= res [1 2 3 4]))
|
|
|
|
|
|
|
|
(setv res [])
|
|
|
|
(ap-each
|
|
|
|
(->> [1 2 3 4]
|
|
|
|
(ap-map (+ 1 it))
|
|
|
|
(list))
|
|
|
|
(.append res it))
|
|
|
|
(assert (= res [2 3 4 5])))
|
2013-11-28 23:53:02 -05:00
|
|
|
|
|
|
|
(defn test-ap-each-while []
|
|
|
|
(setv res [])
|
|
|
|
(ap-each-while [2 2 4 3 4 5 6] (even? it) (.append res it))
|
2020-03-28 22:48:16 +00:00
|
|
|
(assert (= res [2 2 4]))
|
|
|
|
|
|
|
|
(setv res [])
|
|
|
|
(ap-each-while
|
|
|
|
(->> [2 2 4 3 4 5 6]
|
|
|
|
(ap-map (+ 1 it))
|
|
|
|
(list))
|
|
|
|
(odd? it) (.append res it))
|
|
|
|
(assert (= res [3 3 5])))
|
2013-11-28 23:53:02 -05:00
|
|
|
|
|
|
|
(defn test-ap-map []
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (list (ap-map (* it 3) [1 2 3]))
|
|
|
|
[3 6 9]))
|
|
|
|
(assert (= (list (ap-map (* it 3) []))
|
|
|
|
[]))
|
|
|
|
(assert (= (do (setv v 1 f 1) (list (ap-map (it v f) [(fn [a b] (+ a b))])))
|
2020-03-28 22:48:16 +00:00
|
|
|
[2]))
|
|
|
|
|
|
|
|
(assert (=
|
|
|
|
(->> [1 2 3]
|
|
|
|
(ap-filter (even? it))
|
|
|
|
(ap-map (* 3 it))
|
|
|
|
(list))
|
|
|
|
[6])))
|
2013-11-28 23:53:02 -05:00
|
|
|
|
|
|
|
(defn test-ap-map-when []
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (list (ap-map-when even? (* it 2) [1 2 3 4]))
|
2020-03-28 22:48:16 +00:00
|
|
|
[1 4 3 8]))
|
|
|
|
|
|
|
|
(assert (=
|
|
|
|
(->> [1 2 3 4]
|
|
|
|
(ap-map (+ 1 it))
|
|
|
|
(ap-map-when even? (* 2 it))
|
|
|
|
(list))
|
|
|
|
[4 3 8 5])))
|
2013-11-28 23:53:02 -05:00
|
|
|
|
|
|
|
(defn test-ap-filter []
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (list (ap-filter (> it 2) [1 2 3 4]))
|
|
|
|
[3 4]))
|
|
|
|
(assert (= (list (ap-filter (even? it) [1 2 3 4]))
|
2020-03-28 22:48:16 +00:00
|
|
|
[2 4]))
|
|
|
|
|
|
|
|
(assert (=
|
|
|
|
(->> [1 2 3 4]
|
|
|
|
(ap-map (+ 3 it))
|
|
|
|
(ap-filter (even? it))
|
|
|
|
(list))
|
|
|
|
[4 6])))
|
2013-12-26 00:48:09 +05:30
|
|
|
|
|
|
|
(defn test-ap-reject []
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (list (ap-reject (> it 2) [1 2 3 4]))
|
|
|
|
[1 2]))
|
|
|
|
(assert (= (list (ap-reject (even? it) [1 2 3 4]))
|
2020-03-28 22:48:16 +00:00
|
|
|
[1 3]))
|
|
|
|
|
|
|
|
(assert (=
|
|
|
|
(->> [1 2 3 4]
|
|
|
|
(ap-map (+ 3 it))
|
|
|
|
(ap-reject (even? it))
|
|
|
|
(list))
|
|
|
|
[5 7])))
|
2013-12-26 00:48:09 +05:30
|
|
|
|
|
|
|
(defn test-ap-dotimes []
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (do (setv n []) (ap-dotimes 3 (.append n 3)) n)
|
|
|
|
[3 3 3]))
|
|
|
|
(assert (= (do (setv n []) (ap-dotimes 3 (.append n it)) n)
|
2019-12-21 11:10:30 -05:00
|
|
|
[0 1 2]))
|
|
|
|
|
|
|
|
; https://github.com/hylang/hy/issues/1853
|
|
|
|
(setv n 5)
|
|
|
|
(setv x "")
|
|
|
|
(ap-dotimes n (+= x "."))
|
2020-03-28 22:48:16 +00:00
|
|
|
(assert (= x "....."))
|
|
|
|
|
|
|
|
(assert (=
|
|
|
|
(do
|
|
|
|
(setv n [])
|
|
|
|
(ap-dotimes
|
|
|
|
(ap-first (odd? it) [2 4 5 6 3 8])
|
|
|
|
(.append n it))
|
|
|
|
n)
|
|
|
|
[0 1 2 3 4])))
|
2013-12-26 00:48:09 +05:30
|
|
|
|
|
|
|
(defn test-ap-first []
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (ap-first (> it 5) (range 10)) 6))
|
|
|
|
(assert (= (ap-first (even? it) [1 2 3 4]) 2))
|
2020-03-28 22:48:16 +00:00
|
|
|
(assert (= (ap-first (> it 10) (range 10)) None))
|
|
|
|
|
|
|
|
(assert (=
|
|
|
|
(->> [1 2 3 4]
|
|
|
|
(ap-map (+ 4 it))
|
|
|
|
(ap-first (even? it)))
|
|
|
|
6)))
|
2013-12-26 00:48:09 +05:30
|
|
|
|
|
|
|
(defn test-ap-last []
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (ap-last (> it 5) (range 10)) 9))
|
|
|
|
(assert (= (ap-last (even? it) [1 2 3 4]) 4))
|
2020-03-28 22:48:16 +00:00
|
|
|
(assert (= (ap-last (> it 10) (range 10)) None))
|
|
|
|
|
|
|
|
(assert (=
|
|
|
|
(->> [1 2 3 4]
|
|
|
|
(ap-map (+ 4 it))
|
|
|
|
(ap-last (odd? it)))
|
|
|
|
7)))
|
2013-12-26 00:48:09 +05:30
|
|
|
|
|
|
|
(defn test-ap-reduce []
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (ap-reduce (* acc it) [1 2 3]) 6))
|
|
|
|
(assert (= (ap-reduce (* acc it) [1 2 3] 6) 36))
|
|
|
|
(assert (= (ap-reduce (+ acc " on " it) ["Hy" "meth"])
|
|
|
|
"Hy on meth"))
|
2019-12-15 14:54:23 -05:00
|
|
|
(assert (= (ap-reduce (+ acc it) [] 1) 1))
|
|
|
|
|
|
|
|
; https://github.com/hylang/hy/issues/1848
|
|
|
|
(assert (= (ap-reduce (* acc it) (map inc [1 2 3])) 24))
|
|
|
|
(assert (= (ap-reduce (* acc it) (map inc [1 2 3]) 4) 96))
|
|
|
|
|
|
|
|
(setv expr-evaluated 0)
|
|
|
|
(assert (=
|
|
|
|
(ap-reduce (* acc it) (do (+= expr-evaluated 1) [4 5 6])))
|
|
|
|
120)
|
2020-03-28 22:48:16 +00:00
|
|
|
(assert (= expr-evaluated 1))
|
|
|
|
|
|
|
|
(assert (=
|
|
|
|
(->> [1 2 3]
|
|
|
|
(ap-map (+ 2 it))
|
|
|
|
(ap-reduce (* acc it)))
|
|
|
|
60)))
|
2017-10-26 12:53:08 -06:00
|
|
|
|
2017-10-22 19:53:05 -06:00
|
|
|
(defn test-tag-fn []
|
2015-08-11 18:11:33 -06:00
|
|
|
;; test ordering
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (#%(/ %1 %2) 2 4) 0.5))
|
|
|
|
(assert (= (#%(/ %2 %1) 2 4) 2))
|
|
|
|
(assert (= (#%(identity (, %5 %4 %3 %2 %1)) 1 2 3 4 5) (, 5 4 3 2 1)))
|
|
|
|
(assert (= (#%(identity (, %1 %2 %3 %4 %5)) 1 2 3 4 5) (, 1 2 3 4 5)))
|
|
|
|
(assert (= (#%(identity (, %1 %5 %2 %3 %4)) 1 2 3 4 5) (, 1 5 2 3 4)))
|
2015-08-11 18:11:33 -06:00
|
|
|
;; test &rest
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (#%(sum %*) 1 2 3) 6))
|
|
|
|
(assert (= (#%(identity (, %1 %*)) 10 1 2 3) (, 10 (, 1 2 3))))
|
2015-08-11 18:11:33 -06:00
|
|
|
;; no parameters
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (#%(list)) []))
|
|
|
|
(assert (= (#%(identity "Hy!")) "Hy!"))
|
|
|
|
(assert (= (#%(identity "%*")) "%*"))
|
|
|
|
(assert (= (#%(+ "Hy " "world!")) "Hy world!"))
|
2015-08-11 18:11:33 -06:00
|
|
|
;; test skipped parameters
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (#%(identity [%3 %1]) 1 2 3) [3 1]))
|
2015-08-11 18:11:33 -06:00
|
|
|
;; test nesting
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (#%(identity [%1 (, %2 [%3] "Hy" [%*])]) 1 2 3 4 5)
|
|
|
|
[1 (, 2 [3] "Hy" [(, 4 5)])]))
|
2015-08-12 08:45:43 -06:00
|
|
|
;; test arg as function
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (#%(%1 2 4) +) 6))
|
|
|
|
(assert (= (#%(%1 2 4) -) -2))
|
|
|
|
(assert (= (#%(%1 2 4) /) 0.5))
|
2017-10-22 21:36:30 -06:00
|
|
|
;; test &rest &kwargs
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (#%(, %* %**) 1 2 :a 'b)
|
|
|
|
(, (, 1 2)
|
|
|
|
(dict :a 'b))))
|
2017-10-25 20:46:38 -06:00
|
|
|
;; test other expression types
|
2019-12-05 08:41:10 -05:00
|
|
|
(assert (= (#% %* 1 2 3)
|
|
|
|
(, 1 2 3)))
|
|
|
|
(assert (= (#% %** :foo 2)
|
|
|
|
(dict :foo 2)))
|
|
|
|
(assert (= (#%[%3 %2 %1] 1 2 3)
|
|
|
|
[3 2 1]))
|
|
|
|
(assert (= (#%{%1 %2} 10 100)
|
|
|
|
{10 100}))
|
|
|
|
(assert (= (#% #{%3 %2 %1} 1 3 2)
|
|
|
|
#{3 1 2})) ; sets are not ordered.
|
|
|
|
(assert (= (#% "%1")
|
|
|
|
"%1")))
|