Mercurial > lasercutter
comparison src/clojure/contrib/test_is.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:35cf337adfcf | 10:ef7dbbd6452c |
---|---|
1 ;;; test_is.clj: Compatibility layer for old clojure.contrib.test-is | |
2 | |
3 ;; by Stuart Sierra, http://stuartsierra.com/ | |
4 ;; August 28, 2009 | |
5 | |
6 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use | |
7 ;; and distribution terms for this software are covered by the Eclipse | |
8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | |
9 ;; which can be found in the file epl-v10.html at the root of this | |
10 ;; distribution. By using this software in any fashion, you are | |
11 ;; agreeing to be bound by the terms of this license. You must not | |
12 ;; remove this notice, or any other, from this software. | |
13 | |
14 ;; DEPRECATED in 1.2: Moved to clojure.test | |
15 | |
16 (ns ^{:deprecated "1.2" | |
17 :doc "Backwards-compatibility for clojure.contrib.test-is | |
18 | |
19 The clojure.contrib.test-is library moved from Contrib into the | |
20 Clojure distribution as clojure.test. | |
21 | |
22 This happened on or around clojure-contrib Git commit | |
23 82cf0409d0fcb71be477ebfc4da18ee2128a2ad1 on June 25, 2009. | |
24 | |
25 This file makes the clojure.test interface available under the old | |
26 namespace clojure.contrib.test-is. | |
27 | |
28 This includes support for the old syntax of the 'are' macro. | |
29 | |
30 This was suggested by Howard Lewis Ship in ticket #26, | |
31 http://www.assembla.com/spaces/clojure-contrib/tickets/26" | |
32 :author "Stuart Sierra"} | |
33 clojure.contrib.test-is | |
34 (:require clojure.test | |
35 [clojure.walk :as walk])) | |
36 | |
37 | |
38 ;;; COPY INTERNED VARS (EXCEPT are) FROM clojure.test | |
39 | |
40 (doseq [v (disj (set (vals (ns-interns 'clojure.test))) | |
41 #'clojure.test/are)] | |
42 (intern *ns* (with-meta (:name (meta v)) (meta v)) (var-get v))) | |
43 | |
44 | |
45 ;;; REDEFINE OLD clojure.contrib.template | |
46 | |
47 (defn find-symbols | |
48 "Recursively finds all symbols in form." | |
49 [form] | |
50 (distinct (filter symbol? (tree-seq coll? seq form)))) | |
51 | |
52 (defn find-holes | |
53 "Recursively finds all symbols starting with _ in form." | |
54 [form] | |
55 (sort (distinct (filter #(.startsWith (name %) "_") | |
56 (find-symbols form))))) | |
57 | |
58 (defn find-pure-exprs | |
59 "Recursively finds all sub-expressions in form that do not contain | |
60 any symbols starting with _" | |
61 [form] | |
62 (filter #(and (list? %) | |
63 (empty? (find-holes %))) | |
64 (tree-seq seq? seq form))) | |
65 | |
66 (defn flatten-map | |
67 "Transforms a map into a vector like [key value key value]." | |
68 [m] | |
69 (reduce (fn [coll [k v]] (conj coll k v)) | |
70 [] m)) | |
71 | |
72 (defn template? | |
73 "Returns true if form is a valid template expression." | |
74 [form] | |
75 (if (seq (find-holes form)) true false)) | |
76 | |
77 (defn apply-template | |
78 "Replaces _1, _2, _3, etc. in expr with corresponding elements of | |
79 values. Returns the modified expression. For use in macros." | |
80 [expr values] | |
81 (when-not (template? expr) | |
82 (throw (IllegalArgumentException. (str (pr-str expr) " is not a valid template.")))) | |
83 (let [expr (walk/postwalk-replace {'_ '_1} expr) | |
84 holes (find-holes expr) | |
85 smap (zipmap holes values)] | |
86 (walk/prewalk-replace smap expr))) | |
87 | |
88 (defmacro do-template | |
89 "Repeatedly evaluates template expr (in a do block) using values in | |
90 args. args are grouped by the number of holes in the template. | |
91 Example: (do-template (check _1 _2) :a :b :c :d) | |
92 expands to (do (check :a :b) (check :c :d))" | |
93 [expr & args] | |
94 (when-not (template? expr) | |
95 (throw (IllegalArgumentException. (str (pr-str expr) " is not a valid template.")))) | |
96 (let [expr (walk/postwalk-replace {'_ '_1} expr) | |
97 argcount (count (find-holes expr))] | |
98 `(do ~@(map (fn [a] (apply-template expr a)) | |
99 (partition argcount args))))) | |
100 | |
101 | |
102 | |
103 ;;; REDEFINE are MACRO TO MATCH OLD TEMPLATE BEHAVIOR | |
104 | |
105 (defmacro are | |
106 "Checks multiple assertions with a template expression. | |
107 See clojure.contrib.template/do-template for an explanation of | |
108 templates. | |
109 | |
110 Example: (are (= _1 _2) | |
111 2 (+ 1 1) | |
112 4 (* 2 2)) | |
113 Expands to: | |
114 (do (is (= 2 (+ 1 1))) | |
115 (is (= 4 (* 2 2)))) | |
116 | |
117 Note: This breaks some reporting features, such as line numbers." | |
118 [expr & args] | |
119 `(do-template (is ~expr) ~@args)) |