rlm@10: ;; Test routines for macro_utils.clj rlm@10: rlm@10: ;; by Konrad Hinsen rlm@10: ;; last updated May 6, 2009 rlm@10: rlm@10: ;; Copyright (c) Konrad Hinsen, 2008. All rights reserved. The use rlm@10: ;; and distribution terms for this software are covered by the Eclipse rlm@10: ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) rlm@10: ;; which can be found in the file epl-v10.html at the root of this rlm@10: ;; distribution. By using this software in any fashion, you are rlm@10: ;; agreeing to be bound by the terms of this license. You must not rlm@10: ;; remove this notice, or any other, from this software. rlm@10: rlm@10: (ns clojure.contrib.test-macro-utils rlm@10: (:use [clojure.test :only (deftest is are run-tests use-fixtures)] rlm@10: [clojure.contrib.macro-utils rlm@10: :only (macrolet symbol-macrolet defsymbolmacro with-symbol-macros rlm@10: mexpand-1 mexpand mexpand-all)] rlm@10: [clojure.contrib.monads rlm@10: :only (with-monad domonad)])) rlm@10: rlm@10: (use-fixtures :each rlm@10: (fn [f] (binding [*ns* (the-ns 'clojure.contrib.test-macro-utils)] rlm@10: (f)))) rlm@10: rlm@10: (deftest macrolet-test rlm@10: (is (= (macroexpand-1 rlm@10: '(macrolet [(foo [form] `(~form ~form))] (foo x))) rlm@10: '(do (x x))))) rlm@10: rlm@10: (deftest symbol-macrolet-test rlm@10: (is (= (macroexpand-1 rlm@10: '(symbol-macrolet [x xx y yy] rlm@10: (exp [a y] (x y)))) rlm@10: '(do (exp [a yy] (xx yy))))) rlm@10: (is (= (macroexpand-1 rlm@10: '(symbol-macrolet [def foo] rlm@10: (def def def))) rlm@10: '(do (def def foo)))) rlm@10: (is (= (macroexpand-1 rlm@10: '(symbol-macrolet [x foo z bar] rlm@10: (let [a x b y x b] [a b x z]))) rlm@10: '(do (let* [a foo b y x b] [a b x bar])))) rlm@10: (is (= (macroexpand-1 rlm@10: '(symbol-macrolet [x foo z bar] rlm@10: (fn ([x y] [x y z]) ([x y z] [x y z])))) rlm@10: '(do (fn* ([x y] [x y bar]) ([x y z] [x y z]))))) rlm@10: (is (= (macroexpand-1 rlm@10: '(symbol-macrolet [x foo z bar] rlm@10: (fn f ([x y] [x y z]) ([x y z] [x y z])))) rlm@10: '(do (fn* f ([x y] [x y bar]) ([x y z] [x y z]))))) rlm@10: (is (= (nth (second (macroexpand-1 rlm@10: '(symbol-macrolet [x xx y yy z zz] rlm@10: (domonad m [a x b y x z] [a b x z])))) 2) rlm@10: '(do (m-bind xx (fn* ([a] rlm@10: (m-bind yy (fn* ([b] rlm@10: (m-bind zz (fn* ([x] rlm@10: (m-result [a b x zz])))))))))))))) rlm@10: rlm@10: (deftest symbol-test rlm@10: (defsymbolmacro sum-2-3 (plus 2 3)) rlm@10: (is (= (macroexpand '(with-symbol-macros (+ 1 sum-2-3))) rlm@10: '(do (+ 1 (plus 2 3))))) rlm@10: (is (= (macroexpand '(macrolet [(plus [a b] `(+ ~a ~b))] (+ 1 sum-2-3))) rlm@10: '(do (+ 1 (clojure.core/+ 2 3))))) rlm@10: (ns-unmap *ns* 'sum-2-3)) rlm@10: