aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/test_contrib/macro_utils.clj
blob: ac1ced06a1d7a80b406a5e84a4e57c8842a52edb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
;; Test routines for macro_utils.clj

;; by Konrad Hinsen
;; last updated May 6, 2009

;; Copyright (c) Konrad Hinsen, 2008. All rights reserved.  The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
;; distribution.  By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license.  You must not
;; remove this notice, or any other, from this software.

(ns clojure.contrib.test-contrib.macro-utils
  (:use [clojure.test :only (deftest is are run-tests use-fixtures)]
	[clojure.contrib.macro-utils
	 :only (macrolet symbol-macrolet defsymbolmacro with-symbol-macros
		mexpand-1 mexpand mexpand-all)]
	[clojure.contrib.monads
	 :only (with-monad domonad)]))

(use-fixtures :each
  (fn [f] (binding [*ns* (the-ns 'clojure.contrib.test-contrib.macro-utils)]
	    (f))))

(deftest macrolet-test
  (is (= (macroexpand-1
	   '(macrolet [(foo [form] `(~form ~form))]  (foo x)))
	 '(do (x x)))))

(deftest symbol-macrolet-test
  (is (= (macroexpand-1
	   '(symbol-macrolet [x xx y yy]
              (exp [a y] (x y))))
	 '(do (exp [a yy] (xx yy)))))
  (is (= (macroexpand-1
	   '(symbol-macrolet [def foo]
              (def def def)))
	 '(do (def def foo))))
  (is (= (macroexpand-1
	   '(symbol-macrolet [x foo z bar]
	      (let [a x b y x b] [a b x z])))
	 '(do (let* [a foo b y x b] [a b x bar]))))
  (is (= (macroexpand-1
	   '(symbol-macrolet [x foo z bar]
	      (fn ([x y] [x y z]) ([x y z] [x y z]))))
	 '(do (fn* ([x y] [x y bar]) ([x y z] [x y z])))))
  (is (= (macroexpand-1
	   '(symbol-macrolet [x foo z bar]
	      (fn f ([x y] [x y z]) ([x y z] [x y z]))))
	 '(do (fn* f ([x y] [x y bar]) ([x y z] [x y z])))))
  (is (= (nth (second (macroexpand-1
		       '(symbol-macrolet [x xx y yy z zz]
			  (domonad m [a x b y x z] [a b x z])))) 2)
	 '(do (m-bind xx (fn* ([a]
	      (m-bind yy (fn* ([b]
	      (m-bind zz (fn* ([x]
	      (m-result [a b x zz]))))))))))))))

(deftest symbol-test
  (defsymbolmacro sum-2-3 (plus 2 3))
  (is (= (macroexpand '(with-symbol-macros (+ 1 sum-2-3)))
	 '(do (+ 1 (plus 2 3)))))
  (is (= (macroexpand '(macrolet [(plus [a b] `(+ ~a ~b))] (+ 1 sum-2-3)))
	 '(do (+ 1 (clojure.core/+ 2 3)))))
  (ns-unmap *ns* 'sum-2-3))