hy/tests/native_tests/quote.hy

101 lines
2.7 KiB
Hy
Raw Normal View History

2020-01-03 19:47:51 +01:00
;; Copyright 2020 the authors.
;; This file is part of Hy, which is free software licensed under the Expat
;; license. See the LICENSE.
2018-04-06 19:11:59 +02:00
(import [hy [HyExpression HySymbol HyString HyBytes HyDict]])
2013-05-14 12:10:29 +02:00
2013-05-10 23:42:13 +02:00
(defn test-quote []
"NATIVE: test for quoting functionality"
2013-07-10 02:16:49 +02:00
(setv q (quote (a b c)))
2013-05-10 23:42:13 +02:00
(assert (= (len q) 3))
2019-08-02 23:17:24 +02:00
(assert (= q (HyExpression [(quote a) (quote b) (quote c)]))))
2013-05-10 23:42:13 +02:00
(defn test-basic-quoting []
(assert (= (type (quote (foo bar))) HyExpression))
(assert (= (type (quote foo)) HySymbol))
(assert (= (type (quote "string")) HyString))
(assert (= (type (quote b"string")) HyBytes)))
2013-05-10 23:42:13 +02:00
(defn test-quoted-hoistable []
"NATIVE: check whether quote works on hoisted things"
(setv f (quote (if True True True)))
(assert (= (get f 0) (quote if)))
(assert (= (cut f 1) (quote (True True True)))))
2013-05-10 23:42:13 +02:00
2013-05-11 00:29:42 +02:00
(defn test-quoted-macroexpand []
"NATIVE: check that we don't expand macros in quoted expressions"
2013-07-10 02:16:49 +02:00
(setv q1 (quote (-> a b c)))
(setv q2 (quasiquote (-> a b c)))
2013-05-11 00:29:42 +02:00
(assert (= q1 q2))
(assert (= (get q1 0) (quote ->)))
(assert (= (cut q1 1) (quote (a b c)))))
2013-05-11 00:29:42 +02:00
2013-05-14 12:10:29 +02:00
(defn test-quote-dicts []
"NATIVE: test quoting dicts"
2013-07-10 02:16:49 +02:00
(setv q (quote {foo bar baz quux}))
2013-05-14 12:10:29 +02:00
(assert (= (len q) 4))
(assert (= (get q 0) (quote foo)))
(assert (= (get q 1) (quote bar)))
(assert (= (get q 2) (quote baz)))
(assert (= (get q 3) (quote quux)))
2018-04-06 19:11:59 +02:00
(assert (= (type q) HyDict)))
2013-05-14 12:10:29 +02:00
(defn test-quote-expr-in-dict []
"NATIVE: test quoting nested exprs in dict"
2013-07-10 02:16:49 +02:00
(setv q (quote {(foo bar) 0}))
2013-05-14 12:10:29 +02:00
(assert (= (len q) 2))
2013-07-10 02:16:49 +02:00
(setv qq (get q 0))
2013-05-14 12:10:29 +02:00
(assert (= qq (quote (foo bar)))))
2013-05-10 23:42:13 +02:00
(defn test-quasiquote []
"NATIVE: test that quasiquote and quote are equivalent for simple cases"
2013-07-10 02:16:49 +02:00
(setv q (quote (a b c)))
(setv qq (quasiquote (a b c)))
2013-05-10 23:42:13 +02:00
(assert (= q qq)))
(defn test-unquote []
"NATIVE: test that unquote works as expected"
2013-07-10 02:16:49 +02:00
(setv q (quote (unquote foo)))
2013-05-10 23:42:13 +02:00
(assert (= (len q) 2))
(assert (= (get q 1) (quote foo)))
2013-07-10 02:16:49 +02:00
(setv qq (quasiquote (a b c (unquote (+ 1 2)))))
2013-05-10 23:42:13 +02:00
(assert (= (len qq) 4))
(assert (= qq (quote (a b c 3)))))
(defn test-unquote-splice []
"NATIVE: test splicing unquotes"
2013-07-10 02:16:49 +02:00
(setv q (quote (c d e)))
(setv qq `(a b ~@q f ~@q ~@0 ~@False ~@None g ~@(when False 1) h))
(assert (= (len qq) 11))
(assert (= qq (quote (a b c d e f c d e g h)))))
2013-05-11 00:26:37 +02:00
2013-05-10 23:42:13 +02:00
(defn test-nested-quasiquote []
"NATIVE: test nested quasiquotes"
(setv qq `(1 `~(+ 1 ~(+ 2 3) ~@None) 4))
(setv q (quote (1 `~(+ 1 5) 4)))
2013-05-10 23:42:13 +02:00
(assert (= (len q) 3))
(assert (= (get qq 1) (quote `~(+ 1 5))))
2013-05-10 23:42:13 +02:00
(assert (= q qq)))
2013-09-22 15:12:59 +02:00
(defmacro doodle [&rest body]
`(do ~@body))
(defn test-unquote-splice []
"NATIVE: test unquote-splice does what's intended"
(assert (=
(doodle
[1 2 3]
[4 5 6])
[4 5 6])))