diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/test_is.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,119 @@
     1.4 +;;; test_is.clj: Compatibility layer for old clojure.contrib.test-is
     1.5 +
     1.6 +;; by Stuart Sierra, http://stuartsierra.com/
     1.7 +;; August 28, 2009
     1.8 +
     1.9 +;; Copyright (c) Stuart Sierra, 2009. All rights reserved.  The use
    1.10 +;; and distribution terms for this software are covered by the Eclipse
    1.11 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
    1.12 +;; which can be found in the file epl-v10.html at the root of this
    1.13 +;; distribution.  By using this software in any fashion, you are
    1.14 +;; agreeing to be bound by the terms of this license.  You must not
    1.15 +;; remove this notice, or any other, from this software.
    1.16 +
    1.17 +;; DEPRECATED in 1.2: Moved to clojure.test
    1.18 +
    1.19 +(ns ^{:deprecated "1.2"
    1.20 +      :doc "Backwards-compatibility for clojure.contrib.test-is
    1.21 +
    1.22 +  The clojure.contrib.test-is library moved from Contrib into the
    1.23 +  Clojure distribution as clojure.test.
    1.24 +
    1.25 +  This happened on or around clojure-contrib Git commit
    1.26 +  82cf0409d0fcb71be477ebfc4da18ee2128a2ad1 on June 25, 2009.
    1.27 +
    1.28 +  This file makes the clojure.test interface available under the old
    1.29 +  namespace clojure.contrib.test-is.
    1.30 +
    1.31 +  This includes support for the old syntax of the 'are' macro.
    1.32 +
    1.33 +  This was suggested by Howard Lewis Ship in ticket #26, 
    1.34 +  http://www.assembla.com/spaces/clojure-contrib/tickets/26"
    1.35 +       :author "Stuart Sierra"}
    1.36 +    clojure.contrib.test-is
    1.37 +    (:require clojure.test
    1.38 +              [clojure.walk :as walk]))
    1.39 +
    1.40 +
    1.41 +;;; COPY INTERNED VARS (EXCEPT are) FROM clojure.test
    1.42 +
    1.43 +(doseq [v (disj (set (vals (ns-interns 'clojure.test)))
    1.44 +                #'clojure.test/are)]
    1.45 +  (intern *ns* (with-meta (:name (meta v)) (meta v)) (var-get v)))
    1.46 +
    1.47 +
    1.48 +;;; REDEFINE OLD clojure.contrib.template 
    1.49 +
    1.50 +(defn find-symbols
    1.51 +  "Recursively finds all symbols in form."
    1.52 +  [form]
    1.53 +  (distinct (filter symbol? (tree-seq coll? seq form))))
    1.54 +
    1.55 +(defn find-holes
    1.56 +  "Recursively finds all symbols starting with _ in form."
    1.57 +  [form]
    1.58 +  (sort (distinct (filter #(.startsWith (name %) "_")
    1.59 +                          (find-symbols form)))))
    1.60 +
    1.61 +(defn find-pure-exprs
    1.62 +  "Recursively finds all sub-expressions in form that do not contain
    1.63 +  any symbols starting with _"
    1.64 +  [form]
    1.65 +  (filter #(and (list? %)
    1.66 +                (empty? (find-holes %)))
    1.67 +          (tree-seq seq? seq form)))
    1.68 +
    1.69 +(defn flatten-map
    1.70 +  "Transforms a map into a vector like [key value key value]."
    1.71 +  [m]
    1.72 +  (reduce (fn [coll [k v]] (conj coll k v))
    1.73 +          [] m))
    1.74 +
    1.75 +(defn template?
    1.76 +  "Returns true if form is a valid template expression."
    1.77 +  [form]
    1.78 +  (if (seq (find-holes form)) true false))
    1.79 +
    1.80 +(defn apply-template
    1.81 +  "Replaces _1, _2, _3, etc. in expr with corresponding elements of
    1.82 +  values.  Returns the modified expression.  For use in macros."
    1.83 +  [expr values]
    1.84 +  (when-not (template? expr)
    1.85 +    (throw (IllegalArgumentException. (str (pr-str expr) " is not a valid template."))))
    1.86 +  (let [expr (walk/postwalk-replace {'_ '_1} expr)
    1.87 +        holes (find-holes expr)
    1.88 +        smap (zipmap holes values)]
    1.89 +    (walk/prewalk-replace smap expr)))
    1.90 +
    1.91 +(defmacro do-template
    1.92 +  "Repeatedly evaluates template expr (in a do block) using values in
    1.93 +  args.  args are grouped by the number of holes in the template.
    1.94 +  Example: (do-template (check _1 _2) :a :b :c :d)
    1.95 +  expands to (do (check :a :b) (check :c :d))"
    1.96 +  [expr & args]
    1.97 +  (when-not (template? expr)
    1.98 +    (throw (IllegalArgumentException. (str (pr-str expr) " is not a valid template."))))
    1.99 +  (let [expr (walk/postwalk-replace {'_ '_1} expr)
   1.100 +        argcount (count (find-holes expr))]
   1.101 +    `(do ~@(map (fn [a] (apply-template expr a))
   1.102 +                (partition argcount args)))))
   1.103 +
   1.104 +
   1.105 +
   1.106 +;;; REDEFINE are MACRO TO MATCH OLD TEMPLATE BEHAVIOR
   1.107 +
   1.108 +(defmacro are
   1.109 +  "Checks multiple assertions with a template expression.
   1.110 +  See clojure.contrib.template/do-template for an explanation of
   1.111 +  templates.
   1.112 +
   1.113 +  Example: (are (= _1 _2)  
   1.114 +                2 (+ 1 1)
   1.115 +                4 (* 2 2))
   1.116 +  Expands to: 
   1.117 +           (do (is (= 2 (+ 1 1)))
   1.118 +               (is (= 4 (* 2 2))))
   1.119 +
   1.120 +  Note: This breaks some reporting features, such as line numbers."
   1.121 +  [expr & args]
   1.122 +  `(do-template (is ~expr) ~@args))