Mercurial > lasercutter
view 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 source
1 ;;; test_is.clj: Compatibility layer for old clojure.contrib.test-is3 ;; by Stuart Sierra, http://stuartsierra.com/4 ;; August 28, 20096 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use7 ;; and distribution terms for this software are covered by the Eclipse8 ;; 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 this10 ;; distribution. By using this software in any fashion, you are11 ;; agreeing to be bound by the terms of this license. You must not12 ;; remove this notice, or any other, from this software.14 ;; DEPRECATED in 1.2: Moved to clojure.test16 (ns ^{:deprecated "1.2"17 :doc "Backwards-compatibility for clojure.contrib.test-is19 The clojure.contrib.test-is library moved from Contrib into the20 Clojure distribution as clojure.test.22 This happened on or around clojure-contrib Git commit23 82cf0409d0fcb71be477ebfc4da18ee2128a2ad1 on June 25, 2009.25 This file makes the clojure.test interface available under the old26 namespace clojure.contrib.test-is.28 This includes support for the old syntax of the 'are' macro.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-is34 (:require clojure.test35 [clojure.walk :as walk]))38 ;;; COPY INTERNED VARS (EXCEPT are) FROM clojure.test40 (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)))45 ;;; REDEFINE OLD clojure.contrib.template47 (defn find-symbols48 "Recursively finds all symbols in form."49 [form]50 (distinct (filter symbol? (tree-seq coll? seq form))))52 (defn find-holes53 "Recursively finds all symbols starting with _ in form."54 [form]55 (sort (distinct (filter #(.startsWith (name %) "_")56 (find-symbols form)))))58 (defn find-pure-exprs59 "Recursively finds all sub-expressions in form that do not contain60 any symbols starting with _"61 [form]62 (filter #(and (list? %)63 (empty? (find-holes %)))64 (tree-seq seq? seq form)))66 (defn flatten-map67 "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))72 (defn template?73 "Returns true if form is a valid template expression."74 [form]75 (if (seq (find-holes form)) true false))77 (defn apply-template78 "Replaces _1, _2, _3, etc. in expr with corresponding elements of79 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)))88 (defmacro do-template89 "Repeatedly evaluates template expr (in a do block) using values in90 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)))))103 ;;; REDEFINE are MACRO TO MATCH OLD TEMPLATE BEHAVIOR105 (defmacro are106 "Checks multiple assertions with a template expression.107 See clojure.contrib.template/do-template for an explanation of108 templates.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))))117 Note: This breaks some reporting features, such as line numbers."118 [expr & args]119 `(do-template (is ~expr) ~@args))