annotate src/clojure/contrib/test_contrib/test_macro_utils.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 ;; Test routines for macro_utils.clj
rlm@10 2
rlm@10 3 ;; by Konrad Hinsen
rlm@10 4 ;; last updated May 6, 2009
rlm@10 5
rlm@10 6 ;; Copyright (c) Konrad Hinsen, 2008. All rights reserved. The use
rlm@10 7 ;; and distribution terms for this software are covered by the Eclipse
rlm@10 8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 9 ;; which can be found in the file epl-v10.html at the root of this
rlm@10 10 ;; distribution. By using this software in any fashion, you are
rlm@10 11 ;; agreeing to be bound by the terms of this license. You must not
rlm@10 12 ;; remove this notice, or any other, from this software.
rlm@10 13
rlm@10 14 (ns clojure.contrib.test-macro-utils
rlm@10 15 (:use [clojure.test :only (deftest is are run-tests use-fixtures)]
rlm@10 16 [clojure.contrib.macro-utils
rlm@10 17 :only (macrolet symbol-macrolet defsymbolmacro with-symbol-macros
rlm@10 18 mexpand-1 mexpand mexpand-all)]
rlm@10 19 [clojure.contrib.monads
rlm@10 20 :only (with-monad domonad)]))
rlm@10 21
rlm@10 22 (use-fixtures :each
rlm@10 23 (fn [f] (binding [*ns* (the-ns 'clojure.contrib.test-macro-utils)]
rlm@10 24 (f))))
rlm@10 25
rlm@10 26 (deftest macrolet-test
rlm@10 27 (is (= (macroexpand-1
rlm@10 28 '(macrolet [(foo [form] `(~form ~form))] (foo x)))
rlm@10 29 '(do (x x)))))
rlm@10 30
rlm@10 31 (deftest symbol-macrolet-test
rlm@10 32 (is (= (macroexpand-1
rlm@10 33 '(symbol-macrolet [x xx y yy]
rlm@10 34 (exp [a y] (x y))))
rlm@10 35 '(do (exp [a yy] (xx yy)))))
rlm@10 36 (is (= (macroexpand-1
rlm@10 37 '(symbol-macrolet [def foo]
rlm@10 38 (def def def)))
rlm@10 39 '(do (def def foo))))
rlm@10 40 (is (= (macroexpand-1
rlm@10 41 '(symbol-macrolet [x foo z bar]
rlm@10 42 (let [a x b y x b] [a b x z])))
rlm@10 43 '(do (let* [a foo b y x b] [a b x bar]))))
rlm@10 44 (is (= (macroexpand-1
rlm@10 45 '(symbol-macrolet [x foo z bar]
rlm@10 46 (fn ([x y] [x y z]) ([x y z] [x y z]))))
rlm@10 47 '(do (fn* ([x y] [x y bar]) ([x y z] [x y z])))))
rlm@10 48 (is (= (macroexpand-1
rlm@10 49 '(symbol-macrolet [x foo z bar]
rlm@10 50 (fn f ([x y] [x y z]) ([x y z] [x y z]))))
rlm@10 51 '(do (fn* f ([x y] [x y bar]) ([x y z] [x y z])))))
rlm@10 52 (is (= (nth (second (macroexpand-1
rlm@10 53 '(symbol-macrolet [x xx y yy z zz]
rlm@10 54 (domonad m [a x b y x z] [a b x z])))) 2)
rlm@10 55 '(do (m-bind xx (fn* ([a]
rlm@10 56 (m-bind yy (fn* ([b]
rlm@10 57 (m-bind zz (fn* ([x]
rlm@10 58 (m-result [a b x zz]))))))))))))))
rlm@10 59
rlm@10 60 (deftest symbol-test
rlm@10 61 (defsymbolmacro sum-2-3 (plus 2 3))
rlm@10 62 (is (= (macroexpand '(with-symbol-macros (+ 1 sum-2-3)))
rlm@10 63 '(do (+ 1 (plus 2 3)))))
rlm@10 64 (is (= (macroexpand '(macrolet [(plus [a b] `(+ ~a ~b))] (+ 1 sum-2-3)))
rlm@10 65 '(do (+ 1 (clojure.core/+ 2 3)))))
rlm@10 66 (ns-unmap *ns* 'sum-2-3))
rlm@10 67