2018-01-01 16:38:33 +01:00
|
|
|
;; Copyright 2018 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.
|
|
|
|
|
2018-10-23 20:06:22 +02:00
|
|
|
(import pytest
|
|
|
|
[hy.errors [HyTypeError]])
|
2015-10-13 23:48:23 +02:00
|
|
|
|
2013-05-11 20:57:46 +02:00
|
|
|
(defmacro rev [&rest body]
|
|
|
|
"Execute the `body` statements in reverse"
|
|
|
|
(quasiquote (do (unquote-splice (list (reversed body))))))
|
|
|
|
|
|
|
|
|
2013-05-11 19:40:48 +02:00
|
|
|
(defn test-rev-macro []
|
|
|
|
"NATIVE: test stararged native macros"
|
|
|
|
(setv x [])
|
|
|
|
(rev (.append x 1) (.append x 2) (.append x 3))
|
|
|
|
(assert (= x [3 2 1])))
|
2013-06-05 12:19:06 +02:00
|
|
|
|
2018-04-23 19:59:03 +02:00
|
|
|
(defn test-macros-returning-constants []
|
|
|
|
(defmacro an-int [] 42)
|
|
|
|
(assert (= (an-int) 42))
|
2013-06-05 12:19:06 +02:00
|
|
|
|
2018-04-23 19:59:03 +02:00
|
|
|
(defmacro a-true [] True)
|
|
|
|
(assert (= (a-true) True))
|
|
|
|
(defmacro a-false [] False)
|
|
|
|
(assert (= (a-false) False))
|
2013-06-05 12:19:06 +02:00
|
|
|
|
2018-04-23 19:59:03 +02:00
|
|
|
(defmacro a-float [] 42.)
|
|
|
|
(assert (= (a-float) 42.))
|
2013-06-07 16:35:28 +02:00
|
|
|
|
2018-04-23 19:59:03 +02:00
|
|
|
(defmacro a-complex [] 42j)
|
|
|
|
(assert (= (a-complex) 42j))
|
2013-06-05 12:19:06 +02:00
|
|
|
|
2018-04-23 19:59:03 +02:00
|
|
|
(defmacro a-string [] "foo")
|
|
|
|
(assert (= (a-string) "foo"))
|
2013-06-05 12:19:06 +02:00
|
|
|
|
2018-04-23 19:59:03 +02:00
|
|
|
(defmacro a-bytes [] b"foo")
|
|
|
|
(assert (= (a-bytes) b"foo"))
|
2013-06-05 12:19:06 +02:00
|
|
|
|
2018-04-23 19:59:03 +02:00
|
|
|
(defmacro a-list [] [1 2])
|
|
|
|
(assert (= (a-list) [1 2]))
|
2017-02-19 01:15:58 +01:00
|
|
|
|
2018-04-23 19:59:03 +02:00
|
|
|
(defmacro a-tuple [&rest b] b)
|
|
|
|
(assert (= (a-tuple 1 2) [1 2]))
|
2013-06-05 12:19:06 +02:00
|
|
|
|
2018-04-23 19:59:03 +02:00
|
|
|
(defmacro a-dict [] {1 2})
|
|
|
|
(assert (= (a-dict) {1 2}))
|
2014-02-19 06:09:37 +01:00
|
|
|
|
2018-04-23 19:59:03 +02:00
|
|
|
(defmacro a-set [] #{1 2})
|
|
|
|
(assert (= (a-set) #{1 2}))
|
2016-09-26 18:47:04 +02:00
|
|
|
|
2018-04-23 19:59:03 +02:00
|
|
|
(defmacro a-none [])
|
|
|
|
(assert (= (a-none) None)))
|
2013-06-02 17:21:03 +02:00
|
|
|
|
2013-12-04 22:59:49 +01:00
|
|
|
|
2013-06-02 17:21:03 +02:00
|
|
|
; A macro calling a previously defined function
|
|
|
|
(eval-when-compile
|
|
|
|
(defn foo [x y]
|
|
|
|
(quasiquote (+ (unquote x) (unquote y)))))
|
|
|
|
|
|
|
|
(defmacro bar [x y]
|
|
|
|
(foo x y))
|
|
|
|
|
2015-10-13 23:48:23 +02:00
|
|
|
(defn test-macro-kw []
|
2018-04-19 18:26:02 +02:00
|
|
|
"NATIVE: test that an error is raised when &kwonly or &kwargs is used in a macro"
|
2015-10-13 23:48:23 +02:00
|
|
|
(try
|
|
|
|
(eval '(defmacro f [&kwonly a b]))
|
|
|
|
(except [e HyTypeError]
|
|
|
|
(assert (= e.message "macros cannot use &kwonly")))
|
2016-11-24 03:35:17 +01:00
|
|
|
(else (assert False)))
|
2015-10-13 23:48:23 +02:00
|
|
|
|
|
|
|
(try
|
|
|
|
(eval '(defmacro f [&kwargs kw]))
|
|
|
|
(except [e HyTypeError]
|
|
|
|
(assert (= e.message "macros cannot use &kwargs")))
|
2016-11-24 03:35:17 +01:00
|
|
|
(else (assert False))))
|
2015-10-13 23:48:23 +02:00
|
|
|
|
2013-06-02 17:21:03 +02:00
|
|
|
(defn test-fn-calling-macro []
|
|
|
|
"NATIVE: test macro calling a plain function"
|
|
|
|
(assert (= 3 (bar 1 2))))
|
2013-06-05 11:41:58 +02:00
|
|
|
|
2017-07-16 01:54:43 +02:00
|
|
|
(defn test-optional-and-unpacking-in-macro []
|
2017-06-27 23:09:31 +02:00
|
|
|
; https://github.com/hylang/hy/issues/1154
|
|
|
|
(defn f [&rest args]
|
|
|
|
(+ "f:" (repr args)))
|
|
|
|
(defmacro mac [&optional x]
|
2017-07-16 01:54:43 +02:00
|
|
|
`(f #* [~x]))
|
2017-06-27 23:09:31 +02:00
|
|
|
(assert (= (mac) "f:(None,)")))
|
|
|
|
|
2018-05-21 20:37:46 +02:00
|
|
|
(defn test-macro-autoboxing-docstring []
|
|
|
|
(defmacro m []
|
|
|
|
(setv mystring "hello world")
|
|
|
|
`(fn [] ~mystring (+ 1 2)))
|
|
|
|
(setv f (m))
|
|
|
|
(assert (= (f) 3))
|
|
|
|
(assert (= f.__doc__ "hello world")))
|
|
|
|
|
2013-07-14 19:03:08 +02:00
|
|
|
(defn test-midtree-yield []
|
|
|
|
"NATIVE: test yielding with a returnable"
|
|
|
|
(defn kruft [] (yield) (+ 1 1)))
|
|
|
|
|
|
|
|
(defn test-midtree-yield-in-for []
|
|
|
|
"NATIVE: test yielding in a for with a return"
|
|
|
|
(defn kruft-in-for []
|
2018-06-12 19:57:57 +02:00
|
|
|
(for [i (range 5)]
|
2013-07-14 19:03:08 +02:00
|
|
|
(yield i))
|
|
|
|
(+ 1 2)))
|
|
|
|
|
|
|
|
(defn test-midtree-yield-in-while []
|
|
|
|
"NATIVE: test yielding in a while with a return"
|
|
|
|
(defn kruft-in-while []
|
|
|
|
(setv i 0)
|
|
|
|
(while (< i 5)
|
|
|
|
(yield i)
|
|
|
|
(setv i (+ i 1)))
|
|
|
|
(+ 2 3)))
|
|
|
|
|
|
|
|
(defn test-multi-yield []
|
|
|
|
"NATIVE: testing multiple yields"
|
|
|
|
(defn multi-yield []
|
2018-06-12 19:57:57 +02:00
|
|
|
(for [i (range 3)]
|
2013-07-14 19:03:08 +02:00
|
|
|
(yield i))
|
|
|
|
(yield "a")
|
|
|
|
(yield "end"))
|
|
|
|
(assert (= (list (multi-yield)) [0 1 2 "a" "end"])))
|
|
|
|
|
|
|
|
|
2013-06-05 11:41:58 +02:00
|
|
|
; Macro that checks a variable defined at compile or load time
|
|
|
|
(setv phase "load")
|
|
|
|
(eval-when-compile
|
|
|
|
(setv phase "compile"))
|
|
|
|
(defmacro phase-when-compiling [] phase)
|
|
|
|
(assert (= phase "load"))
|
|
|
|
(assert (= (phase-when-compiling) "compile"))
|
|
|
|
|
2013-06-07 16:30:00 +02:00
|
|
|
(setv initialized False)
|
|
|
|
(eval-and-compile
|
|
|
|
(setv initialized True))
|
|
|
|
(defmacro test-initialized [] initialized)
|
|
|
|
(assert initialized)
|
|
|
|
(assert (test-initialized))
|
|
|
|
|
2013-09-02 09:58:35 +02:00
|
|
|
(defn test-if-python2 []
|
|
|
|
(import sys)
|
|
|
|
(assert (= (get sys.version_info 0)
|
|
|
|
(if-python2 2 3))))
|
2013-12-15 01:33:56 +01:00
|
|
|
|
|
|
|
(defn test-gensym-in-macros []
|
|
|
|
(import ast)
|
2017-10-31 21:13:41 +01:00
|
|
|
(import [astor.code-gen [to-source]])
|
2018-11-10 19:53:28 +01:00
|
|
|
(import [hy.compiler [hy-compile]])
|
|
|
|
(import [hy.lex [hy-parse]])
|
2013-12-15 01:33:56 +01:00
|
|
|
(setv macro1 "(defmacro nif [expr pos zero neg]
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv g (gensym))
|
|
|
|
`(do
|
|
|
|
(setv ~g ~expr)
|
|
|
|
(cond [(pos? ~g) ~pos]
|
|
|
|
[(zero? ~g) ~zero]
|
|
|
|
[(neg? ~g) ~neg])))
|
2013-12-15 01:33:56 +01:00
|
|
|
|
|
|
|
(print (nif (inc -1) 1 0 -1))
|
|
|
|
")
|
|
|
|
;; expand the macro twice, should use a different
|
|
|
|
;; gensym each time
|
2018-08-20 06:29:29 +02:00
|
|
|
(setv _ast1 (hy-compile (hy-parse macro1) "foo"))
|
|
|
|
(setv _ast2 (hy-compile (hy-parse macro1) "foo"))
|
2013-12-15 01:33:56 +01:00
|
|
|
(setv s1 (to_source _ast1))
|
|
|
|
(setv s2 (to_source _ast2))
|
2018-02-27 20:53:23 +01:00
|
|
|
;; and make sure there is something new that starts with _;G|
|
2018-03-04 23:20:46 +01:00
|
|
|
(assert (in (mangle "_;G|") s1))
|
|
|
|
(assert (in (mangle "_;G|") s2))
|
2013-12-15 01:33:56 +01:00
|
|
|
;; but make sure the two don't match each other
|
|
|
|
(assert (not (= s1 s2))))
|
2013-12-16 02:47:46 +01:00
|
|
|
|
|
|
|
(defn test-with-gensym []
|
|
|
|
(import ast)
|
2017-10-31 21:13:41 +01:00
|
|
|
(import [astor.code-gen [to-source]])
|
2018-11-10 19:53:28 +01:00
|
|
|
(import [hy.compiler [hy-compile]])
|
|
|
|
(import [hy.lex [hy-parse]])
|
2013-12-16 02:47:46 +01:00
|
|
|
(setv macro1 "(defmacro nif [expr pos zero neg]
|
|
|
|
(with-gensyms [a]
|
2017-02-04 18:07:27 +01:00
|
|
|
`(do
|
|
|
|
(setv ~a ~expr)
|
2013-12-16 02:47:46 +01:00
|
|
|
(cond [(pos? ~a) ~pos]
|
|
|
|
[(zero? ~a) ~zero]
|
|
|
|
[(neg? ~a) ~neg]))))
|
|
|
|
|
|
|
|
(print (nif (inc -1) 1 0 -1))
|
|
|
|
")
|
|
|
|
;; expand the macro twice, should use a different
|
|
|
|
;; gensym each time
|
2018-08-20 06:29:29 +02:00
|
|
|
(setv _ast1 (hy-compile (hy-parse macro1) "foo"))
|
|
|
|
(setv _ast2 (hy-compile (hy-parse macro1) "foo"))
|
2013-12-16 02:47:46 +01:00
|
|
|
(setv s1 (to_source _ast1))
|
|
|
|
(setv s2 (to_source _ast2))
|
2018-03-04 23:20:46 +01:00
|
|
|
(assert (in (mangle "_;a|") s1))
|
|
|
|
(assert (in (mangle "_;a|") s2))
|
2013-12-16 02:47:46 +01:00
|
|
|
(assert (not (= s1 s2))))
|
|
|
|
|
2018-06-06 02:08:13 +02:00
|
|
|
(defn test-defmacro/g! []
|
2013-12-16 02:47:46 +01:00
|
|
|
(import ast)
|
2017-10-31 21:13:41 +01:00
|
|
|
(import [astor.code-gen [to-source]])
|
2018-11-10 19:53:28 +01:00
|
|
|
(import [hy.compiler [hy-compile]])
|
|
|
|
(import [hy.lex [hy-parse]])
|
2013-12-16 02:47:46 +01:00
|
|
|
(setv macro1 "(defmacro/g! nif [expr pos zero neg]
|
2017-02-04 18:07:27 +01:00
|
|
|
`(do
|
|
|
|
(setv ~g!res ~expr)
|
2013-12-16 02:47:46 +01:00
|
|
|
(cond [(pos? ~g!res) ~pos]
|
|
|
|
[(zero? ~g!res) ~zero]
|
|
|
|
[(neg? ~g!res) ~neg])))
|
|
|
|
|
|
|
|
(print (nif (inc -1) 1 0 -1))
|
|
|
|
")
|
|
|
|
;; expand the macro twice, should use a different
|
|
|
|
;; gensym each time
|
2018-08-20 06:29:29 +02:00
|
|
|
(setv _ast1 (hy-compile (hy-parse macro1) "foo"))
|
|
|
|
(setv _ast2 (hy-compile (hy-parse macro1) "foo"))
|
2013-12-16 02:47:46 +01:00
|
|
|
(setv s1 (to_source _ast1))
|
|
|
|
(setv s2 (to_source _ast2))
|
2018-06-06 02:08:13 +02:00
|
|
|
(assert (in (mangle "_;res|") s1))
|
|
|
|
(assert (in (mangle "_;res|") s2))
|
2014-05-06 20:48:17 +02:00
|
|
|
(assert (not (= s1 s2)))
|
|
|
|
|
|
|
|
;; defmacro/g! didn't like numbers initially because they
|
|
|
|
;; don't have a startswith method and blew up during expansion
|
|
|
|
(setv macro2 "(defmacro/g! two-point-zero [] `(+ (float 1) 1.0))")
|
2018-08-20 06:29:29 +02:00
|
|
|
(assert (hy-compile (hy-parse macro2) "foo")))
|
2014-01-18 16:27:26 +01:00
|
|
|
|
2016-12-15 01:10:46 +01:00
|
|
|
(defn test-defmacro! []
|
|
|
|
;; defmacro! must do everything defmacro/g! can
|
|
|
|
(import ast)
|
2017-10-31 21:13:41 +01:00
|
|
|
(import [astor.code-gen [to-source]])
|
2018-11-10 19:53:28 +01:00
|
|
|
(import [hy.compiler [hy-compile]])
|
|
|
|
(import [hy.lex [hy-parse]])
|
2016-12-15 01:10:46 +01:00
|
|
|
(setv macro1 "(defmacro! nif [expr pos zero neg]
|
2017-02-04 18:07:27 +01:00
|
|
|
`(do
|
|
|
|
(setv ~g!res ~expr)
|
2016-12-15 01:10:46 +01:00
|
|
|
(cond [(pos? ~g!res) ~pos]
|
|
|
|
[(zero? ~g!res) ~zero]
|
|
|
|
[(neg? ~g!res) ~neg])))
|
|
|
|
|
|
|
|
(print (nif (inc -1) 1 0 -1))
|
|
|
|
")
|
|
|
|
;; expand the macro twice, should use a different
|
|
|
|
;; gensym each time
|
2018-08-20 06:29:29 +02:00
|
|
|
(setv _ast1 (hy-compile (hy-parse macro1) "foo"))
|
|
|
|
(setv _ast2 (hy-compile (hy-parse macro1) "foo"))
|
2016-12-15 01:10:46 +01:00
|
|
|
(setv s1 (to_source _ast1))
|
|
|
|
(setv s2 (to_source _ast2))
|
2018-06-06 02:08:13 +02:00
|
|
|
(assert (in (mangle "_;res|") s1))
|
|
|
|
(assert (in (mangle "_;res|") s2))
|
2016-12-15 01:10:46 +01:00
|
|
|
(assert (not (= s1 s2)))
|
|
|
|
|
|
|
|
;; defmacro/g! didn't like numbers initially because they
|
|
|
|
;; don't have a startswith method and blew up during expansion
|
|
|
|
(setv macro2 "(defmacro! two-point-zero [] `(+ (float 1) 1.0))")
|
2018-08-20 06:29:29 +02:00
|
|
|
(assert (hy-compile (hy-parse macro2) "foo"))
|
2016-12-15 01:10:46 +01:00
|
|
|
|
|
|
|
(defmacro! foo! [o!foo] `(do ~g!foo ~g!foo))
|
|
|
|
;; test that o! becomes g!
|
|
|
|
(assert (= "Hy" (foo! "Hy")))
|
|
|
|
;; test that o! is evaluated once only
|
|
|
|
(setv foo 40)
|
|
|
|
(foo! (+= foo 1))
|
2018-06-11 17:37:31 +02:00
|
|
|
(assert (= 41 foo))
|
|
|
|
;; test &optional args
|
|
|
|
(defmacro! bar! [o!a &optional [o!b 1]] `(do ~g!a ~g!a ~g!b ~g!b))
|
|
|
|
;; test that o!s are evaluated once only
|
|
|
|
(bar! (+= foo 1) (+= foo 1))
|
|
|
|
(assert (= 43 foo))
|
|
|
|
;; test that the optional arg works
|
|
|
|
(assert (= (bar! 2) 1)))
|
2016-12-15 01:10:46 +01:00
|
|
|
|
2014-01-23 21:57:17 +01:00
|
|
|
|
2014-01-18 16:27:26 +01:00
|
|
|
(defn test-if-not []
|
|
|
|
(assert (= (if-not True :yes :no)
|
|
|
|
:no))
|
|
|
|
(assert (= (if-not False :yes :no)
|
|
|
|
:yes))
|
2016-11-24 03:35:17 +01:00
|
|
|
(assert (none? (if-not True :yes)))
|
2014-01-18 16:27:26 +01:00
|
|
|
(assert (= (if-not False :yes)
|
|
|
|
:yes)))
|
2014-01-23 21:57:17 +01:00
|
|
|
|
|
|
|
|
2015-08-09 09:21:12 +02:00
|
|
|
(defn test-lif []
|
|
|
|
"test that lif works as expected"
|
2016-11-24 03:35:17 +01:00
|
|
|
;; None is false
|
2015-08-09 09:21:12 +02:00
|
|
|
(assert (= (lif None "true" "false") "false"))
|
2014-02-24 16:39:45 +01:00
|
|
|
|
2015-10-14 03:38:15 +02:00
|
|
|
;; But everything else is True! Even falsey things.
|
2015-08-09 09:21:12 +02:00
|
|
|
(assert (= (lif True "true" "false") "true"))
|
|
|
|
(assert (= (lif False "true" "false") "true"))
|
|
|
|
(assert (= (lif 0 "true" "false") "true"))
|
|
|
|
(assert (= (lif "some-string" "true" "false") "true"))
|
|
|
|
(assert (= (lif "" "true" "false") "true"))
|
|
|
|
(assert (= (lif (+ 1 2 3) "true" "false") "true"))
|
2016-11-24 03:35:17 +01:00
|
|
|
(assert (= (lif None "true" "false") "false"))
|
2015-10-14 03:38:15 +02:00
|
|
|
(assert (= (lif 0 "true" "false") "true"))
|
|
|
|
|
|
|
|
;; Test ellif [sic]
|
2016-11-24 03:35:17 +01:00
|
|
|
(assert (= (lif None 0
|
|
|
|
None 1
|
2015-10-14 03:38:15 +02:00
|
|
|
0 2
|
|
|
|
3)
|
|
|
|
2)))
|
2014-02-24 16:39:45 +01:00
|
|
|
|
2015-08-09 09:21:12 +02:00
|
|
|
(defn test-lif-not []
|
|
|
|
"test that lif-not works as expected"
|
2016-11-24 03:35:17 +01:00
|
|
|
; None is false
|
2015-08-09 09:21:12 +02:00
|
|
|
(assert (= (lif-not None "false" "true") "false"))
|
2014-09-10 18:55:11 +02:00
|
|
|
|
|
|
|
; But everything else is True! Even falsey things.
|
2015-08-09 09:21:12 +02:00
|
|
|
(assert (= (lif-not True "false" "true") "true"))
|
|
|
|
(assert (= (lif-not False "false" "true") "true"))
|
|
|
|
(assert (= (lif-not 0 "false" "true") "true"))
|
|
|
|
(assert (= (lif-not "some-string" "false" "true") "true"))
|
|
|
|
(assert (= (lif-not "" "false" "true") "true"))
|
|
|
|
(assert (= (lif-not (+ 1 2 3) "false" "true") "true"))
|
2016-11-24 03:35:17 +01:00
|
|
|
(assert (= (lif-not None "false" "true") "false"))
|
2014-09-10 18:55:11 +02:00
|
|
|
(assert (= (lif-not 0 "false" "true") "true")))
|
|
|
|
|
2014-02-24 16:39:45 +01:00
|
|
|
|
2015-06-15 21:11:48 +02:00
|
|
|
(defn test-defmain []
|
|
|
|
"NATIVE: make sure defmain is clean"
|
|
|
|
(global --name--)
|
|
|
|
(setv oldname --name--)
|
|
|
|
(setv --name-- "__main__")
|
2018-11-26 20:17:32 +01:00
|
|
|
|
|
|
|
(defn main [x]
|
|
|
|
(print (integer? x))
|
|
|
|
x)
|
|
|
|
|
2015-06-15 21:11:48 +02:00
|
|
|
(try
|
|
|
|
(defmain [&rest args]
|
2018-11-26 20:17:32 +01:00
|
|
|
(main 42))
|
|
|
|
(assert False)
|
|
|
|
(except [e SystemExit]
|
|
|
|
(assert (= (str e) "42"))))
|
|
|
|
|
|
|
|
;; Try a `defmain` without args
|
|
|
|
(try
|
|
|
|
(defmain []
|
|
|
|
(main 42))
|
|
|
|
(assert False)
|
2015-06-15 21:11:48 +02:00
|
|
|
(except [e SystemExit]
|
|
|
|
(assert (= (str e) "42"))))
|
2018-11-26 20:17:32 +01:00
|
|
|
|
|
|
|
;; Try a `defmain` with only one arg
|
|
|
|
(import sys)
|
|
|
|
(setv oldargv sys.argv)
|
|
|
|
(try
|
|
|
|
(setv sys.argv [1])
|
|
|
|
(defmain [x]
|
|
|
|
(main x))
|
|
|
|
(assert False)
|
|
|
|
(except [e SystemExit]
|
|
|
|
(assert (= (str e) "1"))))
|
|
|
|
|
|
|
|
(setv sys.argv oldargv)
|
2015-06-15 21:11:48 +02:00
|
|
|
(setv --name-- oldname))
|
2018-10-23 20:06:22 +02:00
|
|
|
|
|
|
|
(defn test-macro-namespace-resolution []
|
|
|
|
"Confirm that local versions of macro-macro dependencies do not shadow the
|
|
|
|
versions from the macro's own module, but do resolve unbound macro references
|
|
|
|
in expansions."
|
|
|
|
|
|
|
|
;; `nonlocal-test-macro` is a macro used within
|
|
|
|
;; `tests.resources.macro-with-require.test-module-macro`.
|
|
|
|
;; Here, we introduce an equivalently named version in local scope that, when
|
|
|
|
;; used, will expand to a different output string.
|
|
|
|
(defmacro nonlocal-test-macro [x]
|
|
|
|
(print "this is the local version of `nonlocal-test-macro`!"))
|
|
|
|
|
|
|
|
;; Was the above macro created properly?
|
|
|
|
(assert (in "nonlocal_test_macro" __macros__))
|
|
|
|
|
|
|
|
(setv nonlocal-test-macro (get __macros__ "nonlocal_test_macro"))
|
|
|
|
|
|
|
|
(require [tests.resources.macro-with-require [*]])
|
|
|
|
|
|
|
|
;; Make sure our local version wasn't overwritten by a faulty `require` of the
|
|
|
|
;; one in tests.resources.macro-with-require.
|
|
|
|
(assert (= nonlocal-test-macro (get __macros__ "nonlocal_test_macro")))
|
|
|
|
|
|
|
|
(setv module-name-var "tests.native_tests.native_macros.test-macro-namespace-resolution")
|
|
|
|
(assert (= (+ "This macro was created in tests.resources.macros, "
|
|
|
|
"expanded in tests.native_tests.native_macros.test-macro-namespace-resolution "
|
|
|
|
"and passed the value 2.")
|
|
|
|
(test-module-macro 2)))
|
|
|
|
(assert (= (+ "This macro was created in tests.resources.macros, "
|
|
|
|
"expanded in tests.native_tests.native_macros.test-macro-namespace-resolution "
|
|
|
|
"and passed the value 2.")
|
|
|
|
#test-module-tag 2))
|
|
|
|
|
|
|
|
;; Now, let's use a `require`d macro that depends on another macro defined only
|
|
|
|
;; in this scope.
|
|
|
|
(defmacro local-test-macro [x]
|
|
|
|
(.format "This is the local version of `nonlocal-test-macro` returning {}!" x))
|
|
|
|
|
|
|
|
(assert (= "This is the local version of `nonlocal-test-macro` returning 3!"
|
|
|
|
(test-module-macro-2 3)))
|
|
|
|
(assert (= "This is the local version of `nonlocal-test-macro` returning 3!"
|
|
|
|
#test-module-tag-2 3)))
|
|
|
|
|
|
|
|
(defn test-macro-from-module []
|
|
|
|
"Macros loaded from an external module, which itself `require`s macros, should
|
|
|
|
work without having to `require` the module's macro dependencies (due to
|
|
|
|
[minimal] macro namespace resolution).
|
|
|
|
|
|
|
|
In doing so we also confirm that a module's `__macros__` attribute is correctly
|
|
|
|
loaded and used.
|
|
|
|
|
|
|
|
Additionally, we confirm that `require` statements are executed via loaded bytecode."
|
|
|
|
|
|
|
|
(import os sys marshal types)
|
|
|
|
(import [hy.importer [cache-from-source]])
|
|
|
|
|
|
|
|
(setv pyc-file (cache-from-source
|
|
|
|
(os.path.realpath
|
|
|
|
(os.path.join
|
|
|
|
"tests" "resources" "macro_with_require.hy"))))
|
|
|
|
|
|
|
|
;; Remove any cached byte-code, so that this runs from source and
|
|
|
|
;; gets evaluated in this module.
|
|
|
|
(when (os.path.isfile pyc-file)
|
|
|
|
(os.unlink pyc-file)
|
|
|
|
(.clear sys.path_importer_cache)
|
|
|
|
(when (in "tests.resources.macro_with_require" sys.modules)
|
|
|
|
(del (get sys.modules "tests.resources.macro_with_require"))
|
|
|
|
(__macros__.clear)
|
|
|
|
(__tags__.clear)))
|
|
|
|
|
|
|
|
;; Ensure that bytecode isn't present when we require this module.
|
|
|
|
(assert (not (os.path.isfile pyc-file)))
|
|
|
|
|
|
|
|
(defn test-requires-and-macros []
|
|
|
|
(require [tests.resources.macro-with-require
|
|
|
|
[test-module-macro]])
|
|
|
|
|
|
|
|
;; Make sure that `require` didn't add any of its `require`s
|
|
|
|
(assert (not (in "nonlocal-test-macro" __macros__)))
|
|
|
|
;; and that it didn't add its tags.
|
|
|
|
(assert (not (in "test_module_tag" __tags__)))
|
|
|
|
|
|
|
|
;; Now, require everything.
|
|
|
|
(require [tests.resources.macro-with-require [*]])
|
|
|
|
|
|
|
|
;; Again, make sure it didn't add its required macros and/or tags.
|
|
|
|
(assert (not (in "nonlocal-test-macro" __macros__)))
|
|
|
|
|
|
|
|
;; Its tag(s) should be here now.
|
|
|
|
(assert (in "test_module_tag" __tags__))
|
|
|
|
|
|
|
|
;; The test macro expands to include this symbol.
|
|
|
|
(setv module-name-var "tests.native_tests.native_macros")
|
|
|
|
(assert (= (+ "This macro was created in tests.resources.macros, "
|
|
|
|
"expanded in tests.native_tests.native_macros "
|
|
|
|
"and passed the value 1.")
|
|
|
|
(test-module-macro 1)))
|
|
|
|
|
|
|
|
(assert (= (+ "This macro was created in tests.resources.macros, "
|
|
|
|
"expanded in tests.native_tests.native_macros "
|
|
|
|
"and passed the value 1.")
|
|
|
|
#test-module-tag 1)))
|
|
|
|
|
|
|
|
(test-requires-and-macros)
|
|
|
|
|
|
|
|
;; Now that bytecode is present, reload the module, clear the `require`d
|
|
|
|
;; macros and tags, and rerun the tests.
|
|
|
|
(assert (os.path.isfile pyc-file))
|
|
|
|
|
|
|
|
;; Reload the module and clear the local macro context.
|
|
|
|
(.clear sys.path_importer_cache)
|
|
|
|
(del (get sys.modules "tests.resources.macro_with_require"))
|
|
|
|
(.clear __macros__)
|
|
|
|
(.clear __tags__)
|
|
|
|
|
|
|
|
;; XXX: There doesn't seem to be a way--via standard import mechanisms--to
|
|
|
|
;; ensure that an imported module used the cached bytecode. We'll simply have
|
|
|
|
;; to trust that the .pyc loading convention was followed.
|
|
|
|
(test-requires-and-macros))
|