Permalink
Browse files

Moved pred-cond into midje.clojure.core

  • Loading branch information...
1 parent 99958e8 commit a90c5ab2f36a8de0cc8a611900f50ff749986c9c @marick committed Jan 27, 2013
@@ -3,7 +3,7 @@
(ns ^{:doc "Checkers for collections and strings."}
midje.checking.checkers.collection
(:use midje.clojure.core
- [midje.util.form-utils :only [pred-cond macro-for]]
+ [midje.util.form-utils :only [macro-for]]
[midje.checking.checkers collection-util util chatty defining collection-comparison]
midje.checking.extended-equality
[midje.checking.extended-falsehood :only [extended-true?]]
@@ -9,7 +9,7 @@
[midje.checking.checkers.util :only [named-as-call]]
[midje.error-handling.exceptions :only [captured-throwable?]]
[midje.util.ecosystem :only [clojure-1-3? +M -M *M]]
- [midje.util.form-utils :only [def-many-methods pred-cond]]
+ [midje.util.form-utils :only [def-many-methods]]
[clojure.algo.monads :only [domonad set-m]])
(:import [midje.error_handling.exceptions ICapturedThrowable]))
View
@@ -152,6 +152,18 @@ metadata (as provided by def) merged into the metadata of the original."
(immigrate-from 'clojure.set '[union difference subset? intersection])
+ ;;; Control flow
+
+(defmacro pred-cond
+ "Checks each predicate against the item, returning the corresponding
+ result if it finds a match, otherwise returning nil.
+ Assumes item to be a value, as it will get evaluated multiple times."
+ [item pred result & preds+results]
+ (cond (= pred :else ) result
+ (not (seq preds+results)) `(if (~pred ~item) ~result nil) ;; last condition, but no :else in the form
+ :else `(if (~pred ~item)
+ ~result
+ (pred-cond ~item ~@preds+results))))
;;; Printing
@@ -1,6 +1,6 @@
(ns ^{:doc "General purpose plugin utilities"}
midje.emission.plugins.util
- (:use [midje.util.form-utils :only [pred-cond]]
+ (:use midje.clojure.core
[clojure.repl :only [demunge]])
(:require [midje.emission.clojure-test-facade :as ctf]
[midje.emission.colorize :as color]
@@ -6,7 +6,7 @@
[midje.parsing.util.arrows :only [start-of-checking-arrow-sequence? take-arrow-sequence]]
[midje.parsing.1-to-explicit-form.background :only [seq-headed-by-setup-teardown-form?]]
[midje.parsing.1-to-explicit-form.prerequisites :only [metaconstant-prerequisite?]]
- [midje.util.form-utils :only [def-many-methods pred-cond]]))
+ [midje.util.form-utils :only [def-many-methods]]))
(def #^:private possible-wrapping-targets #{:facts, :contents, :checks })
(def #^:private possible-state-descriptions #{"before" "after" "around"})
@@ -9,7 +9,6 @@
[midje.parsing.1-to-explicit-form.prerequisites :only [metaconstant-prerequisite? prerequisite-to-fake]]
[midje.data.prerequisite-state :only [with-installed-fakes]]
[midje.parsing.util.wrapping :only [for-wrapping-target? with-wrapping-target]]
- [midje.util.form-utils :only [pred-cond]]
[midje.util.laziness :only [eagerly]]
[midje.util.thread-safe-var-nesting :only [namespace-values-inside-out
with-pushed-namespace-values]])
@@ -22,8 +22,7 @@
against-background-facts-and-checks-wrappers
against-background?]]
[midje.parsing.1-to-explicit-form.metaconstants :only [predefine-metaconstants-from-form]]
- [midje.util.form-utils :only [def-many-methods
- pred-cond]]
+ [midje.util.form-utils :only [def-many-methods]]
[midje.util.laziness :only [eagerly]]
[midje.parsing.util.zip :only [skip-to-rightmost-leaf]]
[swiss-arrows.core :only [-<>]])
@@ -8,7 +8,6 @@
[midje.checking.checkers.defining :only [checker? checker-makers]]
[midje.parsing.1-to-explicit-form.expects :only [expect? up-to-full-expect-form]]
[midje.util.form-utils :only [map-difference
- pred-cond
def-many-methods ]]
[midje.checking.extended-equality :only [extended-= extended-list-=]]
[midje.parsing.util.file-position :only [user-file-position]]
@@ -7,8 +7,7 @@
[midje.checkers :only [exactly]]
[midje.checking.checkers.defining :only [checker? checker-makers]]
[midje.parsing.1-to-explicit-form.expects :only [expect? up-to-full-expect-form]]
- [midje.util.form-utils :only [map-difference
- pred-cond]]
+ [midje.util.form-utils :only [map-difference]]
[midje.checking.extended-equality :only [extended-= extended-list-=]]
[midje.parsing.util.file-position :only [user-file-position]]
[midje.util.thread-safe-var-nesting :only [namespace-values-inside-out
@@ -1,8 +1,7 @@
(ns ^{:doc "Unfolding prerequisites like `(f (g 1)) => 3`"}
midje.parsing.2-to-lexical-maps.folded-fakes
(:use midje.parsing.util.zip
- [midje.util.form-utils :only [map-difference
- pred-cond]]
+ [midje.util.form-utils :only [map-difference]]
[midje.checking.checkers.defining :only [checker? checker-makers]])
(:require [clojure.zip :as zip]
[midje.parsing.1-to-explicit-form.expects :as expects]
@@ -1,7 +1,8 @@
-(ns ^{:doc ""}
+(ns ^{:doc "A fnref is the first symbol-or-var in a list. These utilities
+ allow you to interpret it in multiple ways."}
midje.parsing.util.fnref
- (:use [midje.util.form-utils])
- )
+ (:use midje.clojure.core
+ [midje.util.form-utils]))
(defn classify-function-reference [reference]
@@ -1,14 +1,56 @@
-(ns ^{:doc "Utility functions dealing with checking or tranforming forms."}
+(ns ^{:doc "Functions that are somewhat general purpose, but seem too obscure to be in midje.clojure.core.
+ A placeholder for wiser decisions someday."}
midje.util.form-utils
(:use midje.clojure.core
midje.parsing.util.core)
(:require [clojure.zip :as zip]))
+;;; Maps
+
+(defn tack-on-to
+ "Conj new values onto appropriate keys of a map"
+ [hashmap & kvs]
+ (merge-with conj hashmap (apply hash-map-duplicates-ok kvs)))
+
+(defn map-difference [bigger smaller]
+ (select-keys bigger (difference (set (keys bigger)) (set (keys smaller)))))
+
+(defn sort-map [m]
+ (into (sorted-map) m))
+
+;;; Sequences
+
+(defn apply-pairwise
+ "(apply-pairwise [inc dec] [1 1] [2 2]) => [ [2 0] [3 1] ]
+ Note that the functions must take only a single argument."
+ [functions & arglists]
+ (map (partial map
+ (fn [f arg] (f arg))
+ functions)
+ arglists))
+
+
+
+(defn pop-if
+ "Extracts optional arg (that we assume is present if the pred is true) from head of args"
+ [pred args]
+ (if (pred (first args))
+ [(first args) (rest args)]
+ [nil args]))
+
+(def pop-docstring
+ ;; "Extracts optional map from head of args"
+ (partial pop-if string?))
+
+(def pop-opts-map
+ ;; "Extracts optional docstring from head of args"
+ (partial pop-if map?))
+
+
-;;;; Import pprint and cl-format
-;;; Some higher-order predicate helpers
+;;; Higher-order predicate helpers
(defn any-pred-from
"Returns a function that returns strictly true iff any
@@ -23,37 +65,9 @@
(cond (empty? preds) false
(candidate arg) true
:else (recur remainder))))))
-
-;;; Etc.
-(defn tack-on-to
- "Conj new values onto appropriate keys of a map"
- [hashmap & kvs]
- (merge-with conj hashmap (apply hash-map-duplicates-ok kvs)))
-
-(defn apply-pairwise
- "(apply-pairwise [inc dec] [1 1] [2 2]) => [ [2 0] [3 1] ]
- Note that the functions must take only a single argument."
- [functions & arglists]
- (map (partial map
- (fn [f arg] (f arg))
- functions)
- arglists))
-
-(defn map-difference [bigger smaller]
- (select-keys bigger (difference (set (keys bigger)) (set (keys smaller)))))
-
-(defmacro pred-cond
- "Checks each predicate against the item, returning the corresponding
- result if it finds a match, otherwise returning nil.
- Assumes item to be a value, as it will get evaluated multiple times."
- [item pred result & preds+results]
- (cond (= pred :else ) result
- (not (seq preds+results)) `(if (~pred ~item) ~result nil) ;; last condition, but no :else in the form
- :else `(if (~pred ~item)
- ~result
- (pred-cond ~item ~@preds+results))))
+;;; Definition helpers
(defmacro macro-for
"Macroexpands the body once for each of the elements in the
@@ -63,9 +77,6 @@
~body)]
`(do ~@macros#)))
-(defn sort-map [m]
- (into (sorted-map) m))
-
(defmacro def-many-methods
"Create multiple multimethods with different dispatch values
but the same implementation"
@@ -76,25 +87,3 @@
-
-
-;;; These functions assume old-style Midje metadata (nothing but doc strings).
-;;; Formulas have not been updated to use the metadata style. If they
-;;; work out, they should be.
-(defn pop-if
- "Extracts optional arg (that we assume is present if the pred is true) from head of args"
- [pred args]
- (if (pred (first args))
- [(first args) (rest args)]
- [nil args]))
-
-(def pop-docstring
- ;; "Extracts optional map from head of args"
- (partial pop-if string?))
-
-(def pop-opts-map
- ;; "Extracts optional docstring from head of args"
- (partial pop-if map?))
-
-
-
@@ -1,7 +1,6 @@
(ns ^{:doc "To evaluate a fact it needs to be eagerly evaluated."}
midje.util.laziness
- (:use [midje.clojure.core])
- (:use [midje.util.form-utils :only [pred-cond]]))
+ (:use [midje.clojure.core]))
(defn eagerly
"Descend form, converting all lazy seqs into lists.
@@ -80,3 +80,22 @@
(vertical-slices [1 2 3]
[:a :b :c])
=> [ [1 :a] [2 :b] [3 :c]])
+
+;;; Control flow
+
+(fact "checks each pred against the result of the first expression, returning if it finds a match"
+
+ (pred-cond "abcde"
+ #(.contains % "xyz") "contains 'xyz'"
+ string? "string"
+ :else "neither") => "string"
+
+ (pred-cond 1
+ even? "even"
+ string? "string"
+ :else "neither") => "neither"
+
+ "Don't need an :else"
+ (pred-cond 1
+ even? "even") => nil)
+
@@ -13,22 +13,6 @@
(fact "apply each function to each corresponding arg"
(apply-pairwise [inc dec] [1 1] [2 2]) => [[2 0] [3 1]])
-(fact "checks each pred against the result of the first expression, returning if it finds a match"
-
- (pred-cond "abcde"
- #(.contains % "xyz") "contains 'xyz'"
- string? "string"
- :else "neither") => "string"
-
- (pred-cond 1
- even? "even"
- string? "string"
- :else "neither") => "neither"
-
- "Don't need an :else"
- (pred-cond 1
- even? "even") => nil)
-
(fact "sort a map"
(sort-map {:z 26 :b 2 :a 1}) => {:a 1 :b 2 :z 26})

0 comments on commit a90c5ab

Please sign in to comment.