Skip to content

Commit

Permalink
Pushed more things into midje.clojure.core and midje.parsing.util.core
Browse files Browse the repository at this point in the history
  • Loading branch information
marick committed Jan 27, 2013
1 parent 0be9fa0 commit 2fb38ee
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 155 deletions.
23 changes: 19 additions & 4 deletions src/midje/checking/checkers/chatty.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,14 +1,18 @@
(ns ^{:doc "Checkers that explain more about a failure."} (ns ^{:doc "Checkers that explain more about a failure."}
midje.checking.checkers.chatty midje.checking.checkers.chatty
(:use [midje.checking.checkers.util :only [named-as-call]] (:use midje.clojure.core
midje.util.form-utils
[midje.checking.checkers.util :only [named-as-call]]
[midje.checking.extended-falsehood :only [data-laden-falsehood? [midje.checking.extended-falsehood :only [data-laden-falsehood?
as-data-laden-falsehood as-data-laden-falsehood
extended-false?]] extended-false?]]
[midje.checking.checkers.defining :only [as-checker]] [midje.checking.checkers.defining :only [as-checker]]
[midje.util.form-utils :only [pairs quoted? single-destructuring-arg->form+name]])) [midje.parsing.util.core :only [quoted?]]))


;; Note: checkers need to be exported in ../checkers.clj ;; Note: checkers need to be exported in ../checkers.clj




(defn as-chatty-checker [function] (defn as-chatty-checker [function]
(as-checker (vary-meta function assoc :midje/chatty-checker true))) (as-checker (vary-meta function assoc :midje/chatty-checker true)))


Expand All @@ -34,6 +38,17 @@
[[] []] [[] []]
arglist)) arglist))


(defn- ^{:testable true} single-destructuring-arg->form+name [arg-form]
(let [as-symbol (gensym 'symbol-for-destructured-arg)
snd-to-last-is-as? #(= :as (second (reverse %)))
has-key-as? #(contains? % :as)]
(pred-cond arg-form
(every-pred-m vector? snd-to-last-is-as?) [arg-form (last arg-form)]
vector? [(-> arg-form (conj :as) (conj as-symbol)) as-symbol]
(every-pred-m map? has-key-as?) [arg-form (:as arg-form)]
map? [(assoc arg-form :as as-symbol) as-symbol]
:else [arg-form arg-form] )))

(defmacro chatty-checker (defmacro chatty-checker
"Create a function that returns either true or a description of a failure "Create a function that returns either true or a description of a failure
that shows the value of subexpressions. For example, consider this: that shows the value of subexpressions. For example, consider this:
Expand All @@ -60,7 +75,7 @@
(fn [~arg-form] (fn [~arg-form]
(let [~result-symbol (vec ~complex-forms)] (let [~result-symbol (vec ~complex-forms)]
(if (extended-false? (~f ~@substituted-args)) (if (extended-false? (~f ~@substituted-args))
(let [pairs# (pairs '~complex-forms ~result-symbol)] (let [pairs# (vertical-slices '~complex-forms ~result-symbol)]
(as-data-laden-falsehood {:actual ~arg-name (as-data-laden-falsehood {:actual ~arg-name
:intermediate-results pairs#})) :intermediate-results pairs#}))
true)))))) true))))))
5 changes: 2 additions & 3 deletions src/midje/checking/extended_equality.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,7 @@
(ns ^{:doc "`=` extended for regular expressions, functions, etc."} (ns ^{:doc "`=` extended for regular expressions, functions, etc."}
midje.checking.extended-equality midje.checking.extended-equality
(:use midje.clojure.core (:use midje.clojure.core
[midje.checking.extended-falsehood :only [as-data-laden-falsehood data-laden-falsehood?]] [midje.checking.extended-falsehood :only [as-data-laden-falsehood data-laden-falsehood?]]))
[midje.util.form-utils :only [pairs]]))




(defn evaluate-checking-function (defn evaluate-checking-function
Expand Down Expand Up @@ -35,4 +34,4 @@
(defn extended-list-= (defn extended-list-=
"Element-by-element comparison, using extended-= for the right-hand-side values." "Element-by-element comparison, using extended-= for the right-hand-side values."
[actual-args checkers] [actual-args checkers]
(every? (partial apply extended-=) (pairs actual-args checkers))) (every? (partial apply extended-=) (vertical-slices actual-args checkers)))
7 changes: 6 additions & 1 deletion src/midje/clojure/core.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ metadata (as provided by def) merged into the metadata of the original."


;;; Sequences ;;; Sequences



(defn rotations (defn rotations
"Returns a lazy seq of all rotations of a seq" "Returns a lazy seq of all rotations of a seq"
[coll] [coll]
Expand All @@ -141,6 +140,12 @@ metadata (as provided by def) merged into the metadata of the original."
[f x] [f x]
(cons (f (first x)) (rest x))) (cons (f (first x)) (rest x)))


(defn vertical-slices
"Given N sequences, return one sequence whose first element
is a sequence of all the first elements, etc."
[& sequences]
(apply (partial map (fn [& args] args)) sequences))

(immigrate-from 'utilize.seq '[separate find-first only]) (immigrate-from 'utilize.seq '[separate find-first only])


;;; Sets ;;; Sets
Expand Down
8 changes: 6 additions & 2 deletions src/midje/parsing/0_to_fact_form/formulas.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,19 +1,23 @@
(ns ^{:doc "Midje's special blend of generative-style testing."} (ns ^{:doc "Midje's special blend of generative-style testing."}
midje.parsing.0-to-fact-form.formulas midje.parsing.0-to-fact-form.formulas
(:use midje.clojure.core (:use midje.clojure.core
[midje.util.form-utils :only [first-named? pop-docstring pop-opts-map]] midje.parsing.util.core
[midje.error-handling.validation-errors :only [simple-validation-error-report-form validate-m validate]] [midje.error-handling.validation-errors :only [simple-validation-error-report-form validate-m validate]]
[midje.parsing.util.arrows :only [leaf-expect-arrows leaves-contain-arrow?]] [midje.parsing.util.arrows :only [leaf-expect-arrows leaves-contain-arrow?]]
[midje.parsing.1-to-explicit-form.future-facts :only [future-prefixes]] [midje.parsing.1-to-explicit-form.future-facts :only [future-prefixes]]
[clojure.algo.monads :only [domonad]] [clojure.algo.monads :only [domonad]]
[clojure.string :only [join]] [clojure.string :only [join]]
[midje.util.form-utils :only [macro-for]] [midje.util.form-utils]
[clojure.walk :only [prewalk]]) [clojure.walk :only [prewalk]])
(:require [midje.emission.boundaries :as emission-boundary] (:require [midje.emission.boundaries :as emission-boundary]
[midje.emission.api :as emit] [midje.emission.api :as emit]
[midje.emission.state :as state] [midje.emission.state :as state]
[midje.emission.plugins.silence :as emission-silence])) [midje.emission.plugins.silence :as emission-silence]))






;; Formulas work by running up to *num-trials* trials per formula. ;; Formulas work by running up to *num-trials* trials per formula.
(def ^{:doc "The number of trials generated per formula." (def ^{:doc "The number of trials generated per formula."
:dynamic true} :dynamic true}
Expand Down
4 changes: 2 additions & 2 deletions src/midje/parsing/1_to_explicit_form/background.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
prerequisites that pertain to a group of facts."} prerequisites that pertain to a group of facts."}
midje.parsing.1-to-explicit-form.background midje.parsing.1-to-explicit-form.background
(:use midje.clojure.core (:use midje.clojure.core
midje.parsing.util.core
midje.parsing.util.zip midje.parsing.util.zip
[midje.parsing.util.arrows :only [start-of-checking-arrow-sequence? take-arrow-sequence]] [midje.parsing.util.arrows :only [start-of-checking-arrow-sequence? take-arrow-sequence]]
[midje.parsing.1-to-explicit-form.metaconstants :only [predefine-metaconstants-from-form]] [midje.parsing.1-to-explicit-form.metaconstants :only [predefine-metaconstants-from-form]]
[midje.parsing.1-to-explicit-form.prerequisites :only [metaconstant-prerequisite? prerequisite-to-fake]] [midje.parsing.1-to-explicit-form.prerequisites :only [metaconstant-prerequisite? prerequisite-to-fake]]
[midje.data.prerequisite-state :only [with-installed-fakes]] [midje.data.prerequisite-state :only [with-installed-fakes]]
[midje.parsing.util.wrapping :only [for-wrapping-target? with-wrapping-target]] [midje.parsing.util.wrapping :only [for-wrapping-target? with-wrapping-target]]
[midje.util.form-utils :only [first-named? pred-cond [midje.util.form-utils :only [pred-cond]]
symbol-named?]]
[midje.util.laziness :only [eagerly]] [midje.util.laziness :only [eagerly]]
[midje.util.thread-safe-var-nesting :only [namespace-values-inside-out [midje.util.thread-safe-var-nesting :only [namespace-values-inside-out
with-pushed-namespace-values]]) with-pushed-namespace-values]])
Expand Down
1 change: 0 additions & 1 deletion src/midje/parsing/1_to_explicit_form/expects.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
midje.sweet arrow forms into semi-sweet expcet forms."} midje.sweet arrow forms into semi-sweet expcet forms."}
midje.parsing.1-to-explicit-form.expects midje.parsing.1-to-explicit-form.expects
(:use midje.parsing.util.core (:use midje.parsing.util.core
[midje.util.form-utils :only [first-named?]]
[midje.parsing.util.zip :only [skip-to-rightmost-leaf n-times remove-moving-right]] [midje.parsing.util.zip :only [skip-to-rightmost-leaf n-times remove-moving-right]]
[midje.parsing.util.arrows :only [start-of-checking-arrow-sequence? arrow-sequence-overrides]] [midje.parsing.util.arrows :only [start-of-checking-arrow-sequence? arrow-sequence-overrides]]
[midje.parsing.util.file-position :only [arrow-line-number]]) [midje.parsing.util.file-position :only [arrow-line-number]])
Expand Down
4 changes: 2 additions & 2 deletions src/midje/parsing/1_to_explicit_form/facts.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
against-background-facts-and-checks-wrappers against-background-facts-and-checks-wrappers
against-background?]] against-background?]]
[midje.parsing.1-to-explicit-form.metaconstants :only [predefine-metaconstants-from-form]] [midje.parsing.1-to-explicit-form.metaconstants :only [predefine-metaconstants-from-form]]
[midje.util.form-utils :only [def-many-methods first-named? [midje.util.form-utils :only [def-many-methods
preserve-type quoted? pred-cond reader-line-number]] pred-cond]]
[midje.util.laziness :only [eagerly]] [midje.util.laziness :only [eagerly]]
[midje.parsing.util.zip :only [skip-to-rightmost-leaf]] [midje.parsing.util.zip :only [skip-to-rightmost-leaf]]
[swiss-arrows.core :only [-<>]]) [swiss-arrows.core :only [-<>]])
Expand Down
3 changes: 2 additions & 1 deletion src/midje/parsing/1_to_explicit_form/future_facts.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns ^{:doc "Parsing future-facts."} (ns ^{:doc "Parsing future-facts."}
midje.parsing.1-to-explicit-form.future-facts midje.parsing.1-to-explicit-form.future-facts
(:use [midje.util.form-utils :only [macro-for first-named? reader-line-number]]) (:use midje.parsing.util.core)
(:use [midje.util.form-utils :only [macro-for]])
(:require [midje.parsing.util.file-position :as position] (:require [midje.parsing.util.file-position :as position]
[midje.data.nested-facts :as nested-facts] [midje.data.nested-facts :as nested-facts]
[midje.parsing.1-to-explicit-form.metadata :as parse-metadata] [midje.parsing.1-to-explicit-form.metadata :as parse-metadata]
Expand Down
1 change: 0 additions & 1 deletion src/midje/parsing/1_to_explicit_form/prerequisites.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns ^{:doc "Functions for turning provideds into semi-sweet fakes"} (ns ^{:doc "Functions for turning provideds into semi-sweet fakes"}
midje.parsing.1-to-explicit-form.prerequisites midje.parsing.1-to-explicit-form.prerequisites
(:use midje.parsing.util.core (:use midje.parsing.util.core
[midje.util.form-utils :only [symbol-named?]]
[midje.parsing.util.file-position :only [arrow-line-number-from-form]] [midje.parsing.util.file-position :only [arrow-line-number-from-form]]
midje.parsing.arrow-symbols midje.parsing.arrow-symbols
[midje.parsing.util.arrows :only [pull-all-arrow-seqs-from]] [midje.parsing.util.arrows :only [pull-all-arrow-seqs-from]]
Expand Down
3 changes: 1 addition & 2 deletions src/midje/parsing/2_to_lexical_maps/examples.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
[midje.checkers :only [exactly]] [midje.checkers :only [exactly]]
[midje.checking.checkers.defining :only [checker? checker-makers]] [midje.checking.checkers.defining :only [checker? checker-makers]]
[midje.parsing.1-to-explicit-form.expects :only [expect? up-to-full-expect-form]] [midje.parsing.1-to-explicit-form.expects :only [expect? up-to-full-expect-form]]
[midje.util.form-utils :only [first-named? map-difference [midje.util.form-utils :only [map-difference
pred-cond pred-cond
quoted-list-form?
def-many-methods ]] def-many-methods ]]
[midje.checking.extended-equality :only [extended-= extended-list-=]] [midje.checking.extended-equality :only [extended-= extended-list-=]]
[midje.parsing.util.file-position :only [user-file-position]] [midje.parsing.util.file-position :only [user-file-position]]
Expand Down
6 changes: 3 additions & 3 deletions src/midje/parsing/2_to_lexical_maps/fakes.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,14 +1,14 @@
(ns ^{:doc "An intermediate stage in the compilation of prerequisites."} (ns ^{:doc "An intermediate stage in the compilation of prerequisites."}
midje.parsing.2-to-lexical-maps.fakes midje.parsing.2-to-lexical-maps.fakes
(:use midje.clojure.core (:use midje.clojure.core
midje.parsing.util.core
midje.parsing.util.zip midje.parsing.util.zip
[midje.util.object-utils :only [object-name]] [midje.util.object-utils :only [object-name]]
[midje.checkers :only [exactly]] [midje.checkers :only [exactly]]
[midje.checking.checkers.defining :only [checker? checker-makers]] [midje.checking.checkers.defining :only [checker? checker-makers]]
[midje.parsing.1-to-explicit-form.expects :only [expect? up-to-full-expect-form]] [midje.parsing.1-to-explicit-form.expects :only [expect? up-to-full-expect-form]]
[midje.util.form-utils :only [first-named? map-difference [midje.util.form-utils :only [map-difference
pred-cond pred-cond]]
quoted-list-form?]]
[midje.checking.extended-equality :only [extended-= extended-list-=]] [midje.checking.extended-equality :only [extended-= extended-list-=]]
[midje.parsing.util.file-position :only [user-file-position]] [midje.parsing.util.file-position :only [user-file-position]]
[midje.util.thread-safe-var-nesting :only [namespace-values-inside-out [midje.util.thread-safe-var-nesting :only [namespace-values-inside-out
Expand Down
3 changes: 2 additions & 1 deletion src/midje/parsing/other/arglists.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns ^{:doc "Parsing function argument lists"} (ns ^{:doc "Parsing function argument lists"}
midje.parsing.other.arglists midje.parsing.other.arglists
(:use midje.clojure.core (:use midje.clojure.core
midje.parsing.util.core
[midje.error-handling.exceptions :only [user-error]]) [midje.error-handling.exceptions :only [user-error]])
(:require [midje.emission.levels :as levels] (:require [midje.emission.levels :as levels]
[midje.config :as config] [midje.config :as config]
Expand Down Expand Up @@ -29,7 +30,7 @@
(or (fn? arg) (keyword? arg))) (or (fn? arg) (keyword? arg)))


(defn name-matcher-for [desired] (defn name-matcher-for [desired]
#(form/stringlike-matches? desired (fact/name %))) #(stringlike-matches? desired (fact/name %)))
(defn callable-matcher-for [desired] (defn callable-matcher-for [desired]
(comp desired meta)) (comp desired meta))


Expand Down
58 changes: 58 additions & 0 deletions src/midje/parsing/util/core.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,3 +25,61 @@
(matches-symbols-in-semi-sweet-or-sweet-ns? '(expect fake not-called data-fake) loc)) (matches-symbols-in-semi-sweet-or-sweet-ns? '(expect fake not-called data-fake) loc))




(defn stringlike-matches? [stringlike given]
(cond (not (string? given))
false

(string? stringlike)
(.contains given stringlike)

:else
(boolean (re-find stringlike given))))

(defn symbol-named?
"Is the thing a symbol with the name given by the string?"
[x string]
(and (symbol? x)
(= (name x) string)))

(defn first-named?
"Is the form's first element a symbol whose name is the desired string?"
[form desired]
(and (sequential? form)
(symbol-named? (first form) desired)))

(defmulti quoted? tree-variant)
(defmethod quoted? :zipper [loc]
(quoted? (zip/node loc)))
(defmethod quoted? :form [form]
(first-named? form "quote"))

(def dequote #(if (quoted? %) (second %) %))


(defn reader-list-form?
"True if the form is a parenthesized list of the sort the reader can return."
[form]
(or (list? form) (= (type form) clojure.lang.Cons)))

(defn quoted-list-form?
"True if the form is a quoted list such as the reader might return"
[form]
(and (reader-list-form? form)
(quoted? form)))


(defn preserve-type
"If the original form was a vector, make the transformed form a vector too."
[original-form transformed-form]
(if (vector? original-form)
(vec transformed-form)
transformed-form))

(defn reader-line-number
"Find what line number the reader put on the given form or on
one of its elements. If no line numbers, a warning string."
[form]
(or (:line (meta form))
(some (comp :line meta) form)
"0 (no line info)"))

1 change: 0 additions & 1 deletion src/midje/parsing/util/file_position.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
midje.parsing.util.zip midje.parsing.util.zip
[clojure.string :only [split]] [clojure.string :only [split]]
[midje.parsing.util.zip :only [skip-to-rightmost-leaf]] [midje.parsing.util.zip :only [skip-to-rightmost-leaf]]
[midje.util.form-utils :only [quoted?]]
[midje.parsing.util.arrows :only [all-arrows at-arrow__add-key-value-to-end__no-movement]]) [midje.parsing.util.arrows :only [all-arrows at-arrow__add-key-value-to-end__no-movement]])
(:require [clojure.zip :as zip])) (:require [clojure.zip :as zip]))


Expand Down
2 changes: 1 addition & 1 deletion src/midje/parsing/util/wrapping.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
:contents, :facts, or :expects"} :contents, :facts, or :expects"}
midje.parsing.util.wrapping midje.parsing.util.wrapping
(:use midje.clojure.core (:use midje.clojure.core
[midje.util.form-utils :only [first-named?]] midje.parsing.util.core
[midje.util.thread-safe-var-nesting :only [namespace-values-inside-out [midje.util.thread-safe-var-nesting :only [namespace-values-inside-out
set-namespace-value set-namespace-value
with-pushed-namespace-values]]) with-pushed-namespace-values]])
Expand Down
Loading

0 comments on commit 2fb38ee

Please sign in to comment.