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