hy/tests/native_tests/extra/anaphoric.hy

126 lines
3.9 KiB
Hy
Raw Normal View History

2019-02-07 14:57:35 +01:00
;; Copyright 2019 the authors.
;; This file is part of Hy, which is free software licensed under the Expat
;; license. See the LICENSE.
2015-06-17 00:11:55 +02:00
(import [hy.errors [HyMacroExpansionError]])
(require [hy.extra.anaphoric [*]])
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)))
(ap-if False True (assert (is it False))))
2013-12-25 18:11:25 +01:00
(defn test-ap-each []
(setv res [])
(ap-each [1 2 3 4] (.append res it))
2019-12-05 14:41:10 +01:00
(assert (= res [1 2 3 4])))
(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])))
(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])))
(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])))
(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])))
(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])))
(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 ".....")))
(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)))
(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)))
(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 []
;; 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)))
;; 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))))
;; no parameters
2019-12-05 14:41:10 +01:00
(assert (= (#%(list)) []))
(assert (= (#%(identity "Hy!")) "Hy!"))
(assert (= (#%(identity "%*")) "%*"))
(assert (= (#%(+ "Hy " "world!")) "Hy world!"))
;; test skipped parameters
2019-12-05 14:41:10 +01:00
(assert (= (#%(identity [%3 %1]) 1 2 3) [3 1]))
;; 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)])]))
;; 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")))