diff options
Diffstat (limited to 'src/clojure/contrib/expect')
-rw-r--r-- | src/clojure/contrib/expect/test_is_adapter.clj | 37 | ||||
-rw-r--r-- | src/clojure/contrib/expect/util.clj | 21 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/clojure/contrib/expect/test_is_adapter.clj b/src/clojure/contrib/expect/test_is_adapter.clj new file mode 100644 index 00000000..ebe09773 --- /dev/null +++ b/src/clojure/contrib/expect/test_is_adapter.clj @@ -0,0 +1,37 @@ +;;; test_is_adapter.clj: test-is adapter for mocking/expectation framework for Clojure + +;; by Matt Clark + +;; Copyright (c) Matt Clark, 2009. 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). +;; 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.expect.test-is-adapter + (:require [clojure.contrib.expect :as expect]) + (:use clojure.test + clojure.contrib.ns-utils)) + +(immigrate 'clojure.contrib.expect) + +(defn report-problem + "This function is designed to be used in a binding macro to override +the report-problem function in the test-expect namespace. Instead of printing +the error to the console, the error is logged via test-is." + [fn-name expected actual msg] + (report {:type :fail, + :message (str msg " Function name: " fn-name), + :expected expected, + :actual actual})) + + +(defmacro expect [& body] + "Use this macro instead of the standard test-expect expect macro to have +failures reported through test-is." + `(binding [expect/report-problem report-problem] + (expect/expect ~@body))) + + + diff --git a/src/clojure/contrib/expect/util.clj b/src/clojure/contrib/expect/util.clj new file mode 100644 index 00000000..a5b018c1 --- /dev/null +++ b/src/clojure/contrib/expect/util.clj @@ -0,0 +1,21 @@ +(ns clojure.contrib.expect.util + (:use clojure.contrib.seq-utils)) + +(defmacro assert-args [fnname & pairs] + `(do (when-not ~(first pairs) + (throw (IllegalArgumentException. + ~(str fnname " requires " (second pairs))))) + ~(let [more (nnext pairs)] + (when more + (list* `assert-args fnname more))))) + +(defn indexes + "Returns a lazy sequence of the indexes in coll for which the element +is equal to v." + [coll v] + (map #(first %) (filter (fn [[i el]] (= el v)) (indexed coll)))) + +(defn index-of + "Returns the first index of value v in the collection or nil." + [coll v] + (first (indexes coll v))) |