From 180e14f8de5debaef3ff86c724bf6b1497c25533 Mon Sep 17 00:00:00 2001
From: mclark <matt.clark.1@gmail.com>
Date: Mon, 28 Sep 2009 22:35:28 -0300
Subject: [PATCH 1/2] cleaned up formatting and improved documentation of c.c.mock

---
 src/clojure/contrib/mock.clj              |   46 +++++++++--------
 src/clojure/contrib/mock/test_adapter.clj |   79 +++++++++++++++-------------
 2 files changed, 67 insertions(+), 58 deletions(-)

diff --git a/src/clojure/contrib/mock.clj b/src/clojure/contrib/mock.clj
index e079719..a0f2658 100644
--- a/src/clojure/contrib/mock.clj
+++ b/src/clojure/contrib/mock.clj
@@ -1,22 +1,22 @@
 ;;; clojure.contrib.mock.clj: 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.
+;;  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) 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.
 ;;------------------------------------------------------------------------------
 
 (comment
-  ;; This is a simple function mocking library I accidentally wrote as a side
-  ;; effect of trying to write an opengl library in clojure. This is loosely
-  ;; based on various ruby and java mocking frameworks I have used in the past
-  ;; such as mockito, easymock, and whatever rspec uses.
+  ;; Mock is a function mocking utility loosely based on various ruby and java
+  ;; mocking frameworks such as mockito, easymock, and rspec yet adapted to
+  ;; fit the functional style of clojure.
   ;;
-  ;; expect uses bindings to wrap the functions that are being tested and
+  ;; Mock uses bindings to wrap the functions that are being tested and
   ;; then validates the invocation count at the end. The expect macro is the
   ;; main entry point and it is given a vector of binding pairs.
   ;; The first of each pair names the dependent function you want to override,
@@ -117,7 +117,8 @@ of the mock info object created by make-mock."
     (fn [fn-name & args]
       (every? true?
         (map (fn [pred arg pred-form i] (if (pred arg) true
-                                          (unexpected-args fn-name pred-form arg i)))
+                                          (unexpected-args fn-name
+                                            pred-form arg i)))
           sanitized-preds args arg-pred-forms (iterate inc 0))))))
 
 
@@ -181,6 +182,7 @@ count checker for each dependency."
 ;; library. Useful in expressions such as:
 ;; (expect [dep-fn1 (times (more-than 1) (returns 15)) etc)
 
+;; best used in the times function
 (defn once [x] (= 1 x))
 
 (defn never [x] (zero? x))
@@ -191,6 +193,9 @@ count checker for each dependency."
 
 (defn between [x y] #(and (< x %) (> y %)))
 
+;;best used in the has-args function
+(defn anything [x] true)
+
 
 ;;------------------------------------------------------------------------------
 ;; The following functions can be used to build up the expectation hash.
@@ -209,7 +214,7 @@ Usage:
 (defn calls
   "Creates or associates to an existing expectation hash the :calls key with a
 function that will be called with the given arguments. The return value from
-this function will be returned returned by the expected function. If both this
+this function will be returned by the expected function. If both this
 and returns are specified, the return value of \"calls\" will have precedence.
 Usage:
 (calls some-fn expectation-hash?)"
@@ -239,11 +244,10 @@ Usage:
 (defmacro times
   "Creates or associates to an existing expectation hash the :times key with a
 value corresponding to a predicate function which expects an integer value.
-This function can either be specified as the first argument to times or can be
-the result of calling times with an integer argument, in which case the
-predicate will default to being an exact match.  This predicate is called at
-the end of an expect expression to validate that an expected dependency
-function was called the expected number of times.
+Also, an integer can be specified, in which case the times will only be an
+exact match. The times check is called at the end of an expect expression to
+validate that an expected dependency function was called the expected
+number of times.
 Usage:
 (times n)
 (times #(> n %))
@@ -259,7 +263,7 @@ Usage:
  "Use expect to redirect calls to dependent functions that are made within the
 code under test. Instead of calling the functions that would normally be used,
 temporary stubs are used, which can verify function parameters and call counts.
-Return values can also be specified as needed.
+Return values of overridden functions can also be specified as needed.
 Usage:
 (expect [dep-fn (has-args [arg-pred1] (times n (returns x)))]
   (function-under-test a b c))"
diff --git a/src/clojure/contrib/mock/test_adapter.clj b/src/clojure/contrib/mock/test_adapter.clj
index b5aa434..522973e 100644
--- a/src/clojure/contrib/mock/test_adapter.clj
+++ b/src/clojure/contrib/mock/test_adapter.clj
@@ -1,37 +1,42 @@
-;;; test_adapter.clj: clojure.test 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.mock.test-adapter
- (:require [clojure.contrib.mock :as mock])
- (:use clojure.test
-       clojure.contrib.ns-utils))
-
-(immigrate 'clojure.contrib.mock)
-
-(defn report-problem
- "This function is designed to be used in a binding macro to override
-the report-problem function in clojure.contrib.mock. Instead of printing
-the error to the console, the error is logged via clojure.test."
- [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 c.c.mock expect macro to have
-failures reported through clojure.test."
-  `(binding [mock/report-problem report-problem]
-     (mock/expect ~@body)))
-
-
-
+;;; test_adapter.clj: clojure.test 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) 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.mock.test-adapter
+  #^{:author "Matt Clark",
+     :doc "an adapter namespace to enable clojure.contrib.mock to
+           report problems via clojure.test." }
+  (:require [clojure.contrib.mock :as mock])
+  (:use clojure.test
+    clojure.contrib.ns-utils))
+
+(immigrate 'clojure.contrib.mock)
+
+(defn report-problem
+ "This function is designed to be used in a binding macro to override
+the report-problem function in clojure.contrib.mock. Instead of printing
+the error to the console, the error is logged via clojure.test."
+ [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 c.c.mock expect macro to have
+failures reported through clojure.test."
+  `(binding [mock/report-problem report-problem]
+     (mock/expect ~@body)))
+
+
+
-- 
1.6.3.3

