Permalink
Browse files

Pushed more things into midje.clojure.core and midje.parsing.util.core

  • Loading branch information...
1 parent 0be9fa0 commit 2fb38eece0afa4c706043e803f8aaddca1711151 @marick committed Jan 27, 2013
@@ -1,14 +1,18 @@
(ns ^{:doc "Checkers that explain more about a failure."}
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?
as-data-laden-falsehood
extended-false?]]
[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
+
+
(defn as-chatty-checker [function]
(as-checker (vary-meta function assoc :midje/chatty-checker true)))
@@ -34,6 +38,17 @@
[[] []]
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
"Create a function that returns either true or a description of a failure
that shows the value of subexpressions. For example, consider this:
@@ -60,7 +75,7 @@
(fn [~arg-form]
(let [~result-symbol (vec ~complex-forms)]
(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
- :intermediate-results pairs#}))
+ :intermediate-results pairs#}))
true))))))
@@ -1,8 +1,7 @@
(ns ^{:doc "`=` extended for regular expressions, functions, etc."}
midje.checking.extended-equality
(:use midje.clojure.core
- [midje.checking.extended-falsehood :only [as-data-laden-falsehood data-laden-falsehood?]]
- [midje.util.form-utils :only [pairs]]))
+ [midje.checking.extended-falsehood :only [as-data-laden-falsehood data-laden-falsehood?]]))
(defn evaluate-checking-function
@@ -35,4 +34,4 @@
(defn extended-list-=
"Element-by-element comparison, using extended-= for the right-hand-side values."
[actual-args checkers]
- (every? (partial apply extended-=) (pairs actual-args checkers)))
+ (every? (partial apply extended-=) (vertical-slices actual-args checkers)))
@@ -129,7 +129,6 @@ metadata (as provided by def) merged into the metadata of the original."
;;; Sequences
-
(defn rotations
"Returns a lazy seq of all rotations of a seq"
[coll]
@@ -141,6 +140,12 @@ metadata (as provided by def) merged into the metadata of the original."
[f 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])
;;; Sets
@@ -1,19 +1,23 @@
(ns ^{:doc "Midje's special blend of generative-style testing."}
midje.parsing.0-to-fact-form.formulas
(: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.parsing.util.arrows :only [leaf-expect-arrows leaves-contain-arrow?]]
[midje.parsing.1-to-explicit-form.future-facts :only [future-prefixes]]
[clojure.algo.monads :only [domonad]]
[clojure.string :only [join]]
- [midje.util.form-utils :only [macro-for]]
+ [midje.util.form-utils]
[clojure.walk :only [prewalk]])
(:require [midje.emission.boundaries :as emission-boundary]
[midje.emission.api :as emit]
[midje.emission.state :as state]
[midje.emission.plugins.silence :as emission-silence]))
+
+
+
+
;; Formulas work by running up to *num-trials* trials per formula.
(def ^{:doc "The number of trials generated per formula."
:dynamic true}
@@ -2,14 +2,14 @@
prerequisites that pertain to a group of facts."}
midje.parsing.1-to-explicit-form.background
(:use midje.clojure.core
+ midje.parsing.util.core
midje.parsing.util.zip
[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.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 [first-named? pred-cond
- symbol-named?]]
+ [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]])
@@ -2,7 +2,6 @@
midje.sweet arrow forms into semi-sweet expcet forms."}
midje.parsing.1-to-explicit-form.expects
(: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.arrows :only [start-of-checking-arrow-sequence? arrow-sequence-overrides]]
[midje.parsing.util.file-position :only [arrow-line-number]])
@@ -22,8 +22,8 @@
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 first-named?
- preserve-type quoted? pred-cond reader-line-number]]
+ [midje.util.form-utils :only [def-many-methods
+ pred-cond]]
[midje.util.laziness :only [eagerly]]
[midje.parsing.util.zip :only [skip-to-rightmost-leaf]]
[swiss-arrows.core :only [-<>]])
@@ -1,6 +1,7 @@
(ns ^{:doc "Parsing 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]
[midje.data.nested-facts :as nested-facts]
[midje.parsing.1-to-explicit-form.metadata :as parse-metadata]
@@ -1,7 +1,6 @@
(ns ^{:doc "Functions for turning provideds into semi-sweet fakes"}
midje.parsing.1-to-explicit-form.prerequisites
(: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.arrow-symbols
[midje.parsing.util.arrows :only [pull-all-arrow-seqs-from]]
@@ -7,9 +7,8 @@
[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 [first-named? map-difference
+ [midje.util.form-utils :only [map-difference
pred-cond
- quoted-list-form?
def-many-methods ]]
[midje.checking.extended-equality :only [extended-= extended-list-=]]
[midje.parsing.util.file-position :only [user-file-position]]
@@ -1,14 +1,14 @@
(ns ^{:doc "An intermediate stage in the compilation of prerequisites."}
midje.parsing.2-to-lexical-maps.fakes
(:use midje.clojure.core
+ midje.parsing.util.core
midje.parsing.util.zip
[midje.util.object-utils :only [object-name]]
[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 [first-named? map-difference
- pred-cond
- quoted-list-form?]]
+ [midje.util.form-utils :only [map-difference
+ pred-cond]]
[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,6 +1,7 @@
(ns ^{:doc "Parsing function argument lists"}
midje.parsing.other.arglists
(:use midje.clojure.core
+ midje.parsing.util.core
[midje.error-handling.exceptions :only [user-error]])
(:require [midje.emission.levels :as levels]
[midje.config :as config]
@@ -29,7 +30,7 @@
(or (fn? arg) (keyword? arg)))
(defn name-matcher-for [desired]
- #(form/stringlike-matches? desired (fact/name %)))
+ #(stringlike-matches? desired (fact/name %)))
(defn callable-matcher-for [desired]
(comp desired meta))
@@ -25,3 +25,61 @@
(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)"))
+
@@ -4,7 +4,6 @@
midje.parsing.util.zip
[clojure.string :only [split]]
[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]])
(:require [clojure.zip :as zip]))
@@ -2,7 +2,7 @@
:contents, :facts, or :expects"}
midje.parsing.util.wrapping
(: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
set-namespace-value
with-pushed-namespace-values]])
Oops, something went wrong.

0 comments on commit 2fb38ee

Please sign in to comment.