diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/clojure/contrib/condt.clj | 62 | ||||
-rw-r--r-- | src/clojure/contrib/template.clj | 198 | ||||
-rw-r--r-- | src/clojure/contrib/test_clojure/clojure_set.clj | 4 | ||||
-rw-r--r-- | src/clojure/contrib/test_clojure/compilation.clj | 2 | ||||
-rw-r--r-- | src/clojure/contrib/test_clojure/control.clj | 12 | ||||
-rw-r--r-- | src/clojure/contrib/test_clojure/data_structures.clj | 116 | ||||
-rw-r--r-- | src/clojure/contrib/test_clojure/java_interop.clj | 104 | ||||
-rw-r--r-- | src/clojure/contrib/test_clojure/logic.clj | 16 | ||||
-rw-r--r-- | src/clojure/contrib/test_clojure/numbers.clj | 42 | ||||
-rw-r--r-- | src/clojure/contrib/test_clojure/other_functions.clj | 4 | ||||
-rw-r--r-- | src/clojure/contrib/test_clojure/sequences.clj | 106 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/fnmap.clj | 6 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/monads.clj | 6 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/pprint/helper.clj | 2 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/shell_out.clj | 10 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/walk.clj | 34 | ||||
-rw-r--r-- | src/clojure/contrib/test_is.clj | 6 | ||||
-rw-r--r-- | src/clojure/contrib/walk.clj | 4 |
18 files changed, 284 insertions, 450 deletions
diff --git a/src/clojure/contrib/condt.clj b/src/clojure/contrib/condt.clj deleted file mode 100644 index b5ac4da4..00000000 --- a/src/clojure/contrib/condt.clj +++ /dev/null @@ -1,62 +0,0 @@ -;;; condt.clj - generic case-like macro using template expressions - -;; By Stuart Sierra, http://stuartsierra.com/ -;; February 21, 2009 - -;; Copyright (c) Stuart Sierra, 2008. 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. - - -;; CHANGE LOG -;; -;; February 21, 2009: fixed to work with new lazy Clojure -;; -;; December 23, 2008: renamed to condt, since clojure.core now -;; contains a (different) condp as of Clojure SVN rev. 1180 -;; -;; December 15, 2008: original version, named "condp" - - -(ns - #^{:author "Stuart Sierra" - :doc "Generic case-like macro using template expressions"} - clojure.contrib.condt - (:require clojure.contrib.template)) - -(defmacro condt - "expr is a template expression (see template), clauses are test/expr - pairs like cond. Evalautes the template on each test value, one at - a time. If a test returns logical true, condt evaluates the - corresponding expr and returns its value. If none of the tests are - true, and there are an odd number of clauses, the last clause is - evaluated, otherwise returns nil." - [expr & clauses] - (let [test-fn-sym (gensym "test_") - f (fn this [c] - (cond - (empty? c) nil - (= 1 (count c)) (first c) - :else (list 'if (list test-fn-sym (first c)) - (second c) - (this (nthnext c 2)))))] - `(let [~test-fn-sym (clojure.contrib.template/template ~expr)] - ~(f clauses)))) - -(defmacro econdt - "Like condt but throws Exception if no tests match." - [expr & clauses] - (let [test-fn-sym (gensym "test_") - f (fn this [c] - (cond - (empty? c) '(throw (Exception. "Nothing matched in econdt.")) - (= 1 (count c)) (throw (IllegalStateException. "Odd number of clauses in econdt.")) - :else (list 'if (list test-fn-sym (first c)) - (second c) - (this (nthnext c 2)))))] - `(let [~test-fn-sym (clojure.contrib.template/template ~expr)] - ~(f clauses)))) diff --git a/src/clojure/contrib/template.clj b/src/clojure/contrib/template.clj index f5049289..748ad4a4 100644 --- a/src/clojure/contrib/template.clj +++ b/src/clojure/contrib/template.clj @@ -1,7 +1,7 @@ ;;; template.clj - anonymous functions that pre-evaluate sub-expressions ;; By Stuart Sierra, http://stuartsierra.com/ -;; January 20, 2009 +;; June 23, 2009 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use ;; and distribution terms for this software are covered by the Eclipse @@ -11,185 +11,45 @@ ;; agreeing to be bound by the terms of this license. You must not ;; remove this notice, or any other, from this software. - - ;; CHANGE LOG ;; +;; June 23, 2009: complete rewrite, eliminated _1,_2,... argument +;; syntax +;; ;; January 20, 2009: added "template?" and checks for valid template ;; expressions. ;; ;; December 15, 2008: first version -;; DOCUMENTATION -;; -;; This file defines macros for using template expressions. These are -;; useful for writing macros. -;; -;; A template is an expression containing "holes" represented by the -;; symbols _1, _2, _3, and so on. ("_" is a synonym for "_1".) -;; -;; The "template" macro is similar to #(). It returns an anonymous -;; function containing the body of the template. Unlike #() or "fn", -;; however, any expressions that do not have any holes will be -;; evaluated only once, at the time the function is created, not every -;; time the function is called. -;; -;; Examples: -;; -(comment - ;; Assume we have some big, slow calculation. - (defn think-hard [] - (Thread/sleep 1000) - 1000) - - ;; With #(), think-hard gets called every time. - (time (doall (map #(+ % (think-hard)) - (range 5)))) - ;;=> "Elapsed time: 5001.33455 msecs" - ;;=> (1000 1001 1002 1003 1004) - - ;; With a template, think-hard only gets called once. - (time (doall (map (template (+ _ (think-hard))) - (range 5)))) - ;;=> "Elapsed time: 1000.907326 msecs" - ;;=> (1000 1001 1002 1003 1004) -) -;; -;; -;; There is also the do-template macro, which works differently. It -;; calls the same template multiple times, filling in values, and puts -;; it all inside a "do" block. It will split up the values based on -;; the number of holes in the template. -(comment - (do-template (foo _1 _2) :a :b :c :d) - ;; expands to: (do (foo :a :b) (foo :c :d)) - - (do-template (foo _1 _2 _3) 10 11 12 13 14 15) - ;; expands to: (foo 10 11 12) (foo 13 14 15) - ) - - - -(ns - #^{:author "Stuart Sierra", - :doc "Anonymous functions that pre-evaluate sub-expressions - - This file defines macros for using template expressions. These are - useful for writing macros. - - A template is an expression containing \"holes\" represented by the - symbols _1, _2, _3, and so on. (\"_\" is a synonym for \"_1\".) - - The \"template\" macro is similar to #(). It returns an anonymous - function containing the body of the template. Unlike #() or \"fn\", - however, any expressions that do not have any holes will be - evaluated only once, at the time the function is created, not every - time the function is called. - - Examples: - - Assume we have some big, slow calculation. - (defn think-hard [] - (Thread/sleep 1000) - 1000) - - With #(), think-hard gets called every time. - (time (doall (map #(+ % (think-hard)) - (range 5)))) - => \"Elapsed time: 5001.33455 msecs\" - => (1000 1001 1002 1003 1004) - - With a template, think-hard only gets called once. - (time (doall (map (template (+ _ (think-hard))) - (range 5)))) - => \"Elapsed time: 1000.907326 msecs\" - => (1000 1001 1002 1003 1004) - - There is also the do-template macro, which works differently. It - calls the same template multiple times, filling in values, and puts - it all inside a \"do\" block. It will split up the values based on - the number of holes in the template. - - (do-template (foo _1 _2) :a :b :c :d) - expands to: (do (foo :a :b) (foo :c :d)) - - (do-template (foo _1 _2 _3) 10 11 12 13 14 15) - expands to: (foo 10 11 12) (foo 13 14 15)"} +(ns #^{:author "Stuart Sierra" + :doc "Macros that expand to repeated copies of a template expression."} clojure.contrib.template - (:use clojure.contrib.walk)) - -(defn find-symbols - "Recursively finds all symbols in form." - [form] - (distinct (filter symbol? (tree-seq coll? seq form)))) - -(defn find-holes - "Recursively finds all symbols starting with _ in form." - [form] - (sort (distinct (filter #(.startsWith (name %) "_") - (find-symbols form))))) + (:require [clojure.contrib.walk :as walk])) -(defn find-pure-exprs - "Recursively finds all sub-expressions in form that do not contain - any symbols starting with _" - [form] - (filter #(and (list? %) - (empty? (find-holes %))) - (tree-seq seq? seq form))) - -(defn flatten-map - "Transforms a map into a vector like [key value key value]." - [m] - (reduce (fn [coll [k v]] (conj coll k v)) - [] m)) - -(defn template? - "Returns true if form is a valid template expression." - [form] - (if (seq (find-holes form)) true false)) +(defn apply-template + "For use in macros. argv is an argument list, as in defn. expr is + a quoted expression using the symbols in argv. values is a sequence + of values to be used for the arguments. -(defmacro template - "Expands to a fn using _1, _2, _3, etc. as arguments (_ is the same - as _1). Any sub-expressions without any _* variables are evaluated - when the fn is created, not when it is called." - [& form] - (when-not (template? form) - (throw (IllegalArgumentException. (str (pr-str form) " is not a valid template.")))) - (let [form (postwalk-replace {'_ '_1} form) - holes (find-holes form) - pures (find-pure-exprs form) - smap (zipmap pures (repeatedly #(gensym "HOLE_"))) - newform (prewalk-replace smap form) - ;; Now, make sure we omit nested sub-expressions: - used (set (filter #(.startsWith (name %) "HOLE_") - (find-symbols newform))) - newmap (reduce (fn [m [k v]] (if (used v) (assoc m k v) m)) - {} smap)] - `(let ~(flatten-map (clojure.set/map-invert newmap)) - (fn ~(vec holes) - ~@newform)))) + apply-template will recursively replace argument symbols in expr + with their corresponding values, returning a modified expr. -(defn apply-template - "Replaces _1, _2, _3, etc. in expr with corresponding elements of - values. Returns the modified expression. For use in macros." - [expr values] - (when-not (template? expr) - (throw (IllegalArgumentException. (str (pr-str expr) " is not a valid template.")))) - (let [expr (postwalk-replace {'_ '_1} expr) - holes (find-holes expr) - smap (zipmap holes values)] - (prewalk-replace smap expr))) + Example: (apply-template '[x] '(+ x x) '[2]) + ;=> (+ 2 2)" + [argv expr values] + (assert (vector? argv)) + (assert (every? symbol? argv)) + (walk/prewalk-replace (zipmap argv values) expr)) (defmacro do-template - "Repeatedly evaluates template expr (in a do block) using values in - args. args are grouped by the number of holes in the template. - Example: (do-template (check _1 _2) :a :b :c :d) - expands to (do (check :a :b) (check :c :d))" - [expr & args] - (when-not (template? expr) - (throw (IllegalArgumentException. (str (pr-str expr) " is not a valid template.")))) - (let [expr (postwalk-replace {'_ '_1} expr) - argcount (count (find-holes expr))] - `(do ~@(map (fn [a] (apply-template expr a)) - (partition argcount args))))) + "Repeatedly copies expr (in a do block) for each group of arguments + in values. values are automatically partitioned by the number of + arguments in argv, an argument vector as in defn. + + Example: (macroexpand '(do-template [x y] (+ y x) 2 4 3 5)) + ;=> (do (+ 4 2) (+ 5 3))" + [argv expr & values] + (let [c (count argv)] + `(do ~@(map (fn [a] (apply-template argv expr a)) + (partition c values))))) diff --git a/src/clojure/contrib/test_clojure/clojure_set.clj b/src/clojure/contrib/test_clojure/clojure_set.clj index 33e9f2c3..3e698406 100644 --- a/src/clojure/contrib/test_clojure/clojure_set.clj +++ b/src/clojure/contrib/test_clojure/clojure_set.clj @@ -12,7 +12,7 @@ (deftest test-union - (are (= _1 _2) + (are [x y] (= x y) (set/union) #{} ; identity @@ -65,7 +65,7 @@ ; at least one argument is needed (is (thrown? IllegalArgumentException (set/intersection))) - (are (= _1 _2) + (are [x y] (= x y) ; identity (set/intersection #{}) #{} (set/intersection #{1}) #{1} diff --git a/src/clojure/contrib/test_clojure/compilation.clj b/src/clojure/contrib/test_clojure/compilation.clj index 8f7bad90..d3e59a7a 100644 --- a/src/clojure/contrib/test_clojure/compilation.clj +++ b/src/clojure/contrib/test_clojure/compilation.clj @@ -17,7 +17,7 @@ (deftest test-compiler-metadata (let [m ^#'when] - (are (= _1 _2) + (are [x y] (= x y) (list? (:arglists m)) true (> (count (:arglists m)) 0) true diff --git a/src/clojure/contrib/test_clojure/control.clj b/src/clojure/contrib/test_clojure/control.clj index 15542bf6..79bf93fb 100644 --- a/src/clojure/contrib/test_clojure/control.clj +++ b/src/clojure/contrib/test_clojure/control.clj @@ -16,7 +16,7 @@ ;; *** Helper functions *** (defn maintains-identity [f] - (are (= (f _) _) + (are [x] (= (f x) x) nil false true 0 42 @@ -37,7 +37,7 @@ ; http://clojure.org/macros (deftest test-do - (are (= _1 _2) + (are [x y] (= x y) ; no params => nil (do) nil @@ -65,7 +65,7 @@ (deftest test-cond - (are (= _1 _2) + (are [x y] (= x y) (cond) nil (cond nil true) nil @@ -76,11 +76,11 @@ (cond nil 1 false 2 true 3 true (exception)) 3 ) ; false - (are (= (cond _ :a true :b) :b) + (are [x] (= (cond x :a true :b) :b) nil false ) ; true - (are (= (cond _ :a true :b) :a) + (are [x] (= (cond x :a true :b) :a) true 0 42 0.0 3.14 @@ -96,7 +96,7 @@ #{} #{1 2} ) ; evaluation - (are (= _1 _2) + (are [x y] (= x y) (cond (> 3 2) (+ 1 2) true :result true (exception)) 3 (cond (< 3 2) (+ 1 2) true :result true (exception)) :result ) diff --git a/src/clojure/contrib/test_clojure/data_structures.clj b/src/clojure/contrib/test_clojure/data_structures.clj index 8dda7182..7ca8f4e6 100644 --- a/src/clojure/contrib/test_clojure/data_structures.clj +++ b/src/clojure/contrib/test_clojure/data_structures.clj @@ -24,7 +24,7 @@ (deftest test-equality ; nil is not equal to any other value - (are (not (= nil _)) + (are [x] (not (= nil x)) true false 0 0.0 \space @@ -47,7 +47,7 @@ (is (not= 2/3 0.6666666666666666)) ; vectors equal other seqs by items equality - (are (= _1 _2) + (are [x y] (= x y) '() [] ; regression fixed in r1208; was not equal '(1) [1] '(1 2) [1 2] @@ -58,7 +58,7 @@ (is (not= [1 2] '(2 1))) ; order of items matters ; list and vector vs. set and map - (are (not= _1 _2) + (are [x y] (not= x y) ; only () equals [] () #{} () {} @@ -70,33 +70,35 @@ [1] #{1} ) ; sorted-map, hash-map and array-map - classes differ, but content is equal - (all-are (not= (class _1) (class _2)) - (sorted-map :a 1) - (hash-map :a 1) - (array-map :a 1)) - (all-are (= _1 _2) - (sorted-map) - (hash-map) - (array-map)) - (all-are (= _1 _2) - (sorted-map :a 1) - (hash-map :a 1) - (array-map :a 1)) - (all-are (= _1 _2) - (sorted-map :a 1 :z 3 :c 2) - (hash-map :a 1 :z 3 :c 2) - (array-map :a 1 :z 3 :c 2)) + +;; TODO: reimplement all-are with new do-template? +;; (all-are (not= (class _1) (class _2)) +;; (sorted-map :a 1) +;; (hash-map :a 1) +;; (array-map :a 1)) +;; (all-are (= _1 _2) +;; (sorted-map) +;; (hash-map) +;; (array-map)) +;; (all-are (= _1 _2) +;; (sorted-map :a 1) +;; (hash-map :a 1) +;; (array-map :a 1)) +;; (all-are (= _1 _2) +;; (sorted-map :a 1 :z 3 :c 2) +;; (hash-map :a 1 :z 3 :c 2) +;; (array-map :a 1 :z 3 :c 2)) ; struct-map vs. sorted-map, hash-map and array-map - (are (and (not= (class (struct equality-struct 1 2)) (class _)) - (= (struct equality-struct 1 2) _)) + (are [x] (and (not= (class (struct equality-struct 1 2)) (class x)) + (= (struct equality-struct 1 2) x)) (sorted-map :a 1 :b 2) (hash-map :a 1 :b 2) (array-map :a 1 :b 2)) ; sorted-set vs. hash-set (is (not= (class (sorted-set 1)) (class (hash-set 1)))) - (are (= _1 _2) + (are [x y] (= x y) (sorted-set) (hash-set) (sorted-set 1) (hash-set 1) (sorted-set 3 2 1) (hash-set 3 2 1) )) @@ -105,7 +107,7 @@ ;; *** Collections *** (deftest test-count - (are (= _1 _2) + (are [x y] (= x y) (count nil) 0 (count ()) 0 @@ -141,7 +143,7 @@ (count (java.util.HashMap. {:a 1 :b 2 :c 3})) 3 ) ; different types - (are (= (count [_]) 1) + (are [x] (= (count [x]) 1) nil true false 0 0.0 "" \space () [] #{} {} )) @@ -152,7 +154,7 @@ (is (thrown? ClassCastException (conj "" \a))) (is (thrown? ClassCastException (conj (into-array []) 1))) - (are (= _1 _2) + (are [x y] (= x y) (conj nil 1) '(1) (conj nil 3 2 1) '(1 2 3) @@ -232,7 +234,7 @@ (is (thrown? ClassCastException (peek #{1}))) (is (thrown? ClassCastException (peek {:a 1}))) - (are (= _1 _2) + (are [x y] (= x y) (peek nil) nil ; list = first @@ -269,7 +271,7 @@ (is (thrown? IllegalStateException (pop ()))) (is (thrown? IllegalStateException (pop []))) - (are (= _1 _2) + (are [x y] (= x y) (pop nil) nil ; list - pop first @@ -298,18 +300,18 @@ ;; *** Lists (IPersistentList) *** (deftest test-list - (are (list? _) + (are [x] (list? x) () '() (list) (list 1 2 3) ) ; order is important - (are (not (= _1 _2)) + (are [x y] (not (= x y)) (list 1 2) (list 2 1) (list 3 1 2) (list 1 2 3) ) - (are (= _1 _2) + (are [x y] (= x y) '() () (list) '() (list 1) '(1) @@ -343,7 +345,7 @@ ;; *** Maps (IPersistentMap) *** (deftest test-find - (are (= _1 _2) + (are [x y] (= x y) (find {} :a) nil (find {:a 1} :a) [:a 1] @@ -360,7 +362,7 @@ (deftest test-contains? ; contains? is designed to work preferably on maps and sets - (are (= _1 _2) + (are [x y] (= x y) (contains? {} :a) false (contains? {} nil) false @@ -388,7 +390,7 @@ ; numerically indexed collections (e.g. vectors and Java arrays) ; => test if the numeric key is WITHIN THE RANGE OF INDEXES - (are (= _1 _2) + (are [x y] (= x y) (contains? [] 0) false (contains? [] -1) false (contains? [] 1) false @@ -418,7 +420,7 @@ ; 'contains?' operates constant or logarithmic time, ; it WILL NOT perform a linear search for a value. - (are (= _ false) + (are [x] (= x false) (contains? '(1 2 3) 0) (contains? '(1 2 3) 1) (contains? '(1 2 3) 3) @@ -428,13 +430,13 @@ (deftest test-keys - (are (= _1 _2) ; other than map data structures + (are [x y] (= x y) ; other than map data structures (keys ()) nil (keys []) nil (keys #{}) nil (keys "") nil ) - (are (= _1 _2) + (are [x y] (= x y) ; (class {:a 1}) => clojure.lang.PersistentArrayMap (keys {}) nil (keys {:a 1}) '(:a) @@ -452,13 +454,13 @@ (deftest test-vals - (are (= _1 _2) ; other than map data structures + (are [x y] (= x y) ; other than map data structures (vals ()) nil (vals []) nil (vals #{}) nil (vals "") nil ) - (are (= _1 _2) + (are [x y] (= x y) ; (class {:a 1}) => clojure.lang.PersistentArrayMap (vals {}) nil (vals {:a 1}) '(1) @@ -476,7 +478,7 @@ (deftest test-key - (are (= (key (first (hash-map _ :value))) _) + (are [x] (= (key (first (hash-map x :value))) x) nil false true 0 42 @@ -494,7 +496,7 @@ (deftest test-val - (are (= (val (first (hash-map :key _))) _) + (are [x] (= (val (first (hash-map :key x))) x) nil false true 0 42 @@ -514,22 +516,22 @@ ;; *** Sets *** (deftest test-hash-set - (are (set? _) + (are [x] (set? x) #{} #{1 2} (hash-set) (hash-set 1 2) ) ; order isn't important - (are (= _1 _2) + (are [x y] (= x y) #{1 2} #{2 1} #{3 1 2} #{1 2 3} (hash-set 1 2) (hash-set 2 1) (hash-set 3 1 2) (hash-set 1 2 3) ) ; equal and unique - (are (and (= (hash-set _) #{_}) - (= (hash-set _ _) #{_})) + (are [x] (and (= (hash-set x) #{x}) + (= (hash-set x x) #{x})) nil false true 0 42 @@ -545,7 +547,7 @@ {} {:a 1 :b 2} #{} #{1 2} ) - (are (= _1 _2) + (are [x y] (= x y) ; equal classes (class #{}) (class (hash-set)) (class #{1 2}) (class (hash-set 1 2)) @@ -586,13 +588,13 @@ (is (thrown? ClassCastException (sorted-set '(1 2) [3 4]))) ; creates set? - (are (set? _) - (sorted-set) - (sorted-set 1 2) ) + (are [x] (set? x) + (sorted-set) + (sorted-set 1 2) ) ; equal and unique - (are (and (= (sorted-set _) #{_}) - (= (sorted-set _ _) (sorted-set _))) + (are [x] (and (= (sorted-set x) #{x}) + (= (sorted-set x x) (sorted-set x))) nil false true 0 42 @@ -613,7 +615,7 @@ (is (thrown? ClassCastException (sorted-set {:a 1 :b 2} {:a 1 :b 2}))) (is (thrown? ClassCastException (sorted-set #{1 2} #{1 2}))) - (are (= _1 _2) + (are [x y] (= x y) ; generating (sorted-set) #{} (sorted-set 1) #{1} @@ -631,7 +633,7 @@ (deftest test-set ; set? - (are (set? (set _)) + (are [x] (set? (set x)) () '(1 2) [] [1 2] #{} #{1 2} @@ -640,7 +642,7 @@ "" "abc" ) ; unique - (are (= (set [_ _]) #{_}) + (are [x] (= (set [x x]) #{x}) nil false true 0 42 @@ -657,7 +659,7 @@ #{} #{1 2} ) ; conversion - (are (= (set _1) _2) + (are [x y] (= (set x) y) () #{} '(1 2) #{1 2} @@ -684,7 +686,7 @@ (is (thrown? ClassCastException (disj {:a 1} :a))) ; identity - (are (= (disj _) _) + (are [x] (= (disj x) x) #{} #{1 2 3} ; different data types @@ -703,13 +705,13 @@ #{} #{1 2}} ) ; type identity - (are (= (class (disj _)) (class _)) + (are [x] (= (class (disj x)) (class x)) (hash-set) (hash-set 1 2) (sorted-set) (sorted-set 1 2) ) - (are (= _1 _2) + (are [x y] (= x y) (disj #{} :a) #{} (disj #{} :a :b) #{} diff --git a/src/clojure/contrib/test_clojure/java_interop.clj b/src/clojure/contrib/test_clojure/java_interop.clj index 62eec24e..8c040255 100644 --- a/src/clojure/contrib/test_clojure/java_interop.clj +++ b/src/clojure/contrib/test_clojure/java_interop.clj @@ -15,30 +15,30 @@ (deftest test-dot ; (.instanceMember instance args*) - (are (= _ "FRED") + (are [x] (= x "FRED") (.toUpperCase "fred") (. "fred" toUpperCase) (. "fred" (toUpperCase)) ) - (are (= _ true) + (are [x] (= x true) (.startsWith "abcde" "ab") (. "abcde" startsWith "ab") (. "abcde" (startsWith "ab")) ) ; (.instanceMember Classname args*) - (are (= _ "java.lang.String") + (are [x] (= x "java.lang.String") (.getName String) (. (identity String) getName) (. (identity String) (getName)) ) ; (Classname/staticMethod args*) - (are (= _ 7) + (are [x] (= x 7) (Math/abs -7) (. Math abs -7) (. Math (abs -7)) ) ; Classname/staticField - (are (= _ 2147483647) + (are [x] (= x 2147483647) Integer/MAX_VALUE (. Integer MAX_VALUE) )) @@ -52,32 +52,32 @@ (let [m (doto (new java.util.HashMap) (.put "a" 1) (.put "b" 2))] - (are (= _1 _2) + (are [x y] (= x y) (class m) java.util.HashMap m {"a" 1 "b" 2} ))) (deftest test-new ; Integer - (are (and (= (class _1) _2) - (= _1 _3)) + (are [expr cls value] (and (= (class expr) cls) + (= expr value)) (new java.lang.Integer 42) java.lang.Integer 42 (java.lang.Integer. 123) java.lang.Integer 123 ) ; Date - (are (= (class _) java.util.Date) + (are [x] (= (class x) java.util.Date) (new java.util.Date) (java.util.Date.) )) (deftest test-instance? ; evaluation - (are (= _1 _2) + (are [x y] (= x y) (instance? java.lang.Integer (+ 1 2)) true (instance? java.lang.Long (+ 1 2)) false ) ; different types - (are (instance? _2 _1) + (are [type literal] (instance? literal type) 1 java.lang.Integer 1.0 java.lang.Double 1M java.math.BigDecimal @@ -85,7 +85,7 @@ "a" java.lang.String ) ; it is an int, nothing else - (are (= (instance? _1 42) _2) + (are [x y] (= (instance? x 42) y) java.lang.Integer true java.lang.Long false java.lang.Character false @@ -99,7 +99,7 @@ (deftest test-bean (let [b (bean java.awt.Color/black)] - (are (= _1 _2) + (are [x y] (= x y) (map? b) true (:red b) 0 @@ -117,14 +117,14 @@ (deftest test-bases - (are (= _1 _2) + (are [x y] (= x y) (bases java.lang.Math) (list java.lang.Object) (bases java.lang.Integer) (list java.lang.Number java.lang.Comparable) )) (deftest test-supers - (are (= _1 _2) + (are [x y] (= x y) (supers java.lang.Math) #{java.lang.Object} (supers java.lang.Integer) @@ -142,25 +142,25 @@ (is (= (class (first (~type-array [1 2]))) (class (~type 1)))) ; given size (and empty) - (are (and (= (alength (~type-array _)) _) - (= (vec (~type-array _)) (repeat _ 0))) + (are [x] (and (= (alength (~type-array x)) x) + (= (vec (~type-array x)) (repeat x 0))) 0 1 5 ) ; copy of a sequence - (are (and (= (alength (~type-array _)) (count _)) - (= (vec (~type-array _)) _)) + (are [x] (and (= (alength (~type-array x)) (count x)) + (= (vec (~type-array x)) x)) ;; [] ;; ERROR [1] [1 -2 3 0 5] ) ; given size and init-value - (are (and (= (alength (~type-array _ 42)) _) - (= (vec (~type-array _ 42)) (repeat _ 42))) + (are [x] (and (= (alength (~type-array x 42)) x) + (= (vec (~type-array x 42)) (repeat x 42))) 0 1 5 ) ; given size and init-seq - (are (and (= (alength (~type-array _1 _2)) _1) - (= (vec (~type-array _1 _2)) _3)) + (are [x y z] (and (= (alength (~type-array x y)) x) + (= (vec (~type-array x y)) z)) 0 [] [] 0 [1] [] 0 [1 2 3] [] @@ -180,11 +180,11 @@ ; separate test for exceptions (doesn't work with above macro...) (deftest test-type-array-exceptions - (are (thrown? NegativeArraySizeException _) - (int-array -1) - (long-array -1) - (float-array -1) - (double-array -1) )) + (are [x] (thrown? NegativeArraySizeException x) + (int-array -1) + (long-array -1) + (float-array -1) + (double-array -1) )) (deftest test-make-array @@ -192,19 +192,19 @@ (is (thrown? NegativeArraySizeException (make-array Integer -1))) ; one-dimensional - (are (= (alength (make-array Integer _)) _) + (are [x] (= (alength (make-array Integer x)) x) 0 1 5 ) (let [a (make-array Integer 5)] (aset a 3 42) - (are (= _1 _2) + (are [x y] (= x y) (aget a 3) 42 (class (aget a 3)) Integer )) ; multi-dimensional (let [a (make-array Integer 3 2 4)] (aset a 0 1 2 987) - (are (= _1 _2) + (are [x y] (= x y) (alength a) 3 (alength (first a)) 2 (alength (first (first a))) 4 @@ -216,7 +216,7 @@ (deftest test-to-array (let [v [1 "abc" :kw \c []] a (to-array v)] - (are (= _1 _2) + (are [x y] (= x y) ; length (alength a) (count v) @@ -229,8 +229,8 @@ (class (aget a 4)) (class (nth v 4)) )) ; different kinds of collections - (are (and (= (alength (to-array _)) (count _)) - (= (vec (to-array _)) (vec _))) + (are [x] (and (= (alength (to-array x)) (count x)) + (= (vec (to-array x)) (vec x))) () '(1 2) [] @@ -254,23 +254,23 @@ ; simple case (let [v [1 2 3 4 5] a (into-array v)] - (are (= _1 _2) + (are [x y] (= x y) (alength a) (count v) (vec a) v (class (first a)) (class (first v)) )) ; given type (let [a (into-array Integer/TYPE [(byte 2) (short 3) (int 4)])] - (are (= _ Integer) + (are [x] (= x Integer) (class (aget a 0)) (class (aget a 1)) (class (aget a 2)) )) ; different kinds of collections - (are (and (= (alength (into-array _)) (count _)) - (= (vec (into-array _)) (vec _)) - (= (alength (into-array Integer/TYPE _)) (count _)) - (= (vec (into-array Integer/TYPE _)) (vec _))) + (are [x] (and (= (alength (into-array x)) (count x)) + (= (vec (into-array x)) (vec x)) + (= (alength (into-array Integer/TYPE x)) (count x)) + (= (vec (into-array Integer/TYPE x)) (vec x))) () '(1 2) [] @@ -292,7 +292,7 @@ ; ragged array (let [v [[1] [2 3] [4 5 6]] a (to-array-2d v)] - (are (= _1 _2) + (are [x y] (= x y) (alength a) (count v) (alength (aget a 0)) (count (nth v 0)) (alength (aget a 1)) (count (nth v 1)) @@ -304,13 +304,13 @@ ; empty array (let [a (to-array-2d [])] - (are (= _1 _2) + (are [x y] (= x y) (alength a) 0 (vec a) [] ))) (deftest test-alength - (are (= (alength _) 0) + (are [x] (= (alength x) 0) (int-array 0) (long-array 0) (float-array 0) @@ -320,7 +320,7 @@ (into-array []) (to-array-2d []) ) - (are (= (alength _) 1) + (are [x] (= (alength x) 1) (int-array 1) (long-array 1) (float-array 1) @@ -330,7 +330,7 @@ (into-array [1]) (to-array-2d [[1]]) ) - (are (= (alength _) 3) + (are [x] (= (alength x) 3) (int-array 3) (long-array 3) (float-array 3) @@ -343,8 +343,8 @@ (deftest test-aclone ; clone all arrays except 2D - (are (and (= (alength (aclone _)) (alength _)) - (= (vec (aclone _)) (vec _))) + (are [x] (and (= (alength (aclone x)) (alength x)) + (= (vec (aclone x)) (vec x))) (int-array 0) (long-array 0) (float-array 0) @@ -362,9 +362,9 @@ (into-array [1 2 3]) ) ; clone 2D - (are (and (= (alength (aclone _)) (alength _)) - (= (map alength (aclone _)) (map alength _)) - (= (map vec (aclone _)) (map vec _))) + (are [x] (and (= (alength (aclone x)) (alength x)) + (= (map alength (aclone x)) (map alength x)) + (= (map vec (aclone x)) (map vec x))) (to-array-2d []) (to-array-2d [[1] [2 3] [4 5 6]]) )) @@ -377,8 +377,8 @@ ; ints/longs/floats/doubles (deftest test-boolean - (are (and (instance? java.lang.Boolean (boolean _1)) - (= (boolean _1) _2)) + (are [x y] (and (instance? java.lang.Boolean (boolean x)) + (= (boolean x) y)) nil false false false true true diff --git a/src/clojure/contrib/test_clojure/logic.clj b/src/clojure/contrib/test_clojure/logic.clj index 40a94c37..6c46b711 100644 --- a/src/clojure/contrib/test_clojure/logic.clj +++ b/src/clojure/contrib/test_clojure/logic.clj @@ -17,7 +17,7 @@ (deftest test-if ; true/false/nil - (are (= _1 _2) + (are [x y] (= x y) (if true :t) :t (if true :t :f) :t (if true :t (exception)) :t @@ -31,7 +31,7 @@ (if nil (exception) :f) :f ) ; zero/empty is true - (are (= (if _ :t :f) :t) + (are [x] (= (if x :t :f) :t) (byte 0) (short 0) (int 0) @@ -53,7 +53,7 @@ (into-array []) ) ; anything except nil/false is true - (are (= (if _ :t :f) :t) + (are [x] (= (if x :t :f) :t) (byte 2) (short 2) (int 2) @@ -80,7 +80,7 @@ (deftest test-nil-punning - (are (= (if _1 :no :yes) _2) + (are [x y] (= (if x :no :yes) y) (first []) :yes (next [1]) :yes (rest [1]) :no @@ -111,7 +111,7 @@ (deftest test-and - (are (= _1 _2) + (are [x y] (= x y) (and) true (and true) true (and nil) nil @@ -130,7 +130,7 @@ (deftest test-or - (are (= _1 _2) + (are [x y] (= x y) (or) nil (or true) true (or nil) nil @@ -151,10 +151,10 @@ (deftest test-not (is (thrown? IllegalArgumentException (not))) - (are (= (not _) true) + (are [x] (= (not x) true) nil false ) - (are (= (not _) false) + (are [x] (= (not x) false) true ; numbers diff --git a/src/clojure/contrib/test_clojure/numbers.clj b/src/clojure/contrib/test_clojure/numbers.clj index 82450641..aa480d7c 100644 --- a/src/clojure/contrib/test_clojure/numbers.clj +++ b/src/clojure/contrib/test_clojure/numbers.clj @@ -23,7 +23,7 @@ (deftest Coerced-Byte (let [v (byte 3)] - (are _ + (are [x] (instance? Byte v) (number? v) (integer? v) @@ -31,7 +31,7 @@ (deftest Coerced-Short (let [v (short 3)] - (are _ + (are [x] (instance? Short v) (number? v) (integer? v) @@ -39,7 +39,7 @@ (deftest Coerced-Integer (let [v (int 3)] - (are _ + (are [x] (instance? Integer v) (number? v) (integer? v) @@ -47,7 +47,7 @@ (deftest Coerced-Long (let [v (long 3)] - (are _ + (are [x] (instance? Long v) (number? v) (integer? v) @@ -55,7 +55,7 @@ (deftest Coerced-BigInteger (let [v (bigint 3)] - (are _ + (are [x] (instance? BigInteger v) (number? v) (integer? v) @@ -63,21 +63,21 @@ (deftest Coerced-Float (let [v (float 3)] - (are _ + (are [x] (instance? Float v) (number? v) (float? v)))) (deftest Coerced-Double (let [v (double 3)] - (are _ + (are [x] (instance? Double v) (number? v) (float? v)))) (deftest Coerced-BigDecimal (let [v (bigdec 3)] - (are _ + (are [x] (instance? BigDecimal v) (number? v) (decimal? v) @@ -89,7 +89,7 @@ (defonce DELTA 1e-12) (deftest test-add - (are (= _1 _2) + (are [x y] (= x y) (+) 0 (+ 1) 1 (+ 1 2) 3 @@ -106,7 +106,7 @@ (+ 2/3 1) 5/3 (+ 2/3 1/3) 1 ) - (are (< (- _1 _2) DELTA) + (are [x y] (< (- x y) DELTA) (+ 1.2) 1.2 (+ 1.1 2.4) 3.5 (+ 1.1 2.2 3.3) 6.6 ) @@ -117,7 +117,7 @@ (deftest test-subtract (is (thrown? IllegalArgumentException (-))) - (are (= _1 _2) + (are [x y] (= x y) (- 1) -1 (- 1 2) -1 (- 1 2 3) -4 @@ -133,7 +133,7 @@ (- 2/3 1) -1/3 (- 2/3 1/3) 1/3 ) - (are (< (- _1 _2) DELTA) + (are [x y] (< (- x y) DELTA) (- 1.2) -1.2 (- 2.2 1.1) 1.1 (- 6.6 2.2 1.1) 3.3 ) @@ -142,7 +142,7 @@ (deftest test-multiply - (are (= _1 _2) + (are [x y] (= x y) (*) 1 (* 2) 2 (* 2 3) 6 @@ -156,7 +156,7 @@ (* 1/2 1/3) 1/6 (* 1/2 1/3 -1/4) -1/24 ) - (are (< (- _1 _2) DELTA) + (are [x y] (< (- x y) DELTA) (* 1.2) 1.2 (* 2.0 1.2) 2.4 (* 3.5 2.0 1.2) 8.4 ) @@ -165,7 +165,7 @@ (deftest test-divide - (are (= _1 _2) + (are [x y] (= x y) (/ 1) 1 (/ 2) 1/2 (/ 3 2) 3/2 @@ -179,7 +179,7 @@ (/ -4 -2) 2 (/ -4 2) -2 ) - (are (< (- _1 _2) DELTA) + (are [x y] (< (- x y) DELTA) (/ 4.5 3) 1.5 (/ 4.5 3.0 3.0) 0.5 ) @@ -208,7 +208,7 @@ (is (thrown? ArithmeticException (mod 9 0))) (is (thrown? ArithmeticException (mod 0 0))) - (are (= _1 _2) + (are [x y] (= x y) (mod 4 2) 0 (mod 3 2) 1 (mod 6 4) 2 @@ -258,7 +258,7 @@ (is (thrown? ArithmeticException (rem 9 0))) (is (thrown? ArithmeticException (rem 0 0))) - (are (= _1 _2) + (are [x y] (= x y) (rem 4 2) 0 (rem 3 2) 1 (rem 6 4) 2 @@ -305,7 +305,7 @@ (is (thrown? ArithmeticException (quot 9 0))) (is (thrown? ArithmeticException (quot 0 0))) - (are (= _1 _2) + (are [x y] (= x y) (quot 4 2) 2 (quot 3 2) 1 (quot 6 4) 1 @@ -369,7 +369,7 @@ ;; even? odd? (deftest test-even? - (are _ + (are [x] (even? -4) (not (even? -3)) (even? 0) @@ -379,7 +379,7 @@ (is (thrown? ArithmeticException (even? (double 10))))) (deftest test-odd? - (are _ + (are [x] (not (odd? -4)) (odd? -3) (not (odd? 0)) diff --git a/src/clojure/contrib/test_clojure/other_functions.clj b/src/clojure/contrib/test_clojure/other_functions.clj index 3138fbd9..17b1f3b8 100644 --- a/src/clojure/contrib/test_clojure/other_functions.clj +++ b/src/clojure/contrib/test_clojure/other_functions.clj @@ -19,7 +19,7 @@ (is (thrown? IllegalArgumentException (identity))) (is (thrown? IllegalArgumentException (identity 1 2))) - (are (= (identity _) _) + (are [x] (= (identity x) x) nil false true 0 42 @@ -36,7 +36,7 @@ #{} #{1 2} ) ; evaluation - (are (= (identity _1) _2) + (are [x y] (= (identity x) y) (+ 1 2) 3 (> 5 0) true )) diff --git a/src/clojure/contrib/test_clojure/sequences.clj b/src/clojure/contrib/test_clojure/sequences.clj index 7c2721b4..762e157a 100644 --- a/src/clojure/contrib/test_clojure/sequences.clj +++ b/src/clojure/contrib/test_clojure/sequences.clj @@ -20,7 +20,7 @@ (deftest test-equality ; lazy sequences - (are (= _1 _2) + (are [x y] (= x y) ; fixed SVN 1288 - LazySeq and EmptyList equals/equiv ; http://groups.google.com/group/clojure/browse_frm/thread/286d807be9cae2a5# (map inc nil) () @@ -31,12 +31,12 @@ (deftest test-lazy-seq - (are (seq? _) + (are [x] (seq? x) (lazy-seq nil) (lazy-seq []) (lazy-seq [1 2])) - (are (= _1 _2) + (are [x y] (= x y) (lazy-seq nil) () (lazy-seq [nil]) '(nil) @@ -59,7 +59,7 @@ (is (not (seq? (seq [])))) (is (seq? (seq [1 2]))) - (are (= _1 _2) + (are [x y] (= x y) (seq nil) nil (seq [nil]) '(nil) @@ -80,7 +80,7 @@ (deftest test-cons (is (thrown? IllegalArgumentException (cons 1 2))) - (are (= _1 _2) + (are [x y] (= x y) (cons 1 nil) '(1) (cons nil nil) '(nil) @@ -102,8 +102,8 @@ (deftest test-empty - (are (and (= (empty _1) _2) - (= (class (empty _1)) (class _2))) + (are [x y] (and (= (empty x) y) + (= (class (empty x)) (class y))) nil nil () () @@ -141,7 +141,7 @@ (deftest test-not-empty ; empty coll/seq => nil - (are (= (not-empty _) nil) + (are [x] (= (not-empty x) nil) () [] {} @@ -152,8 +152,8 @@ (lazy-seq []) ) ; non-empty coll/seq => identity - (are (and (= (not-empty _) _) - (= (class (not-empty _)) (class _))) + (are [x] (and (= (not-empty x) x) + (= (class (not-empty x)) (class x))) '(1 2) [1 2] {:a 1} @@ -173,7 +173,7 @@ (is (thrown? IllegalArgumentException (first \a))) (is (thrown? IllegalArgumentException (first 's))) (is (thrown? IllegalArgumentException (first :k))) - (are (= _1 _2) + (are [x y] (= x y) (first nil) nil ; string @@ -240,7 +240,7 @@ (is (thrown? IllegalArgumentException (next \a))) (is (thrown? IllegalArgumentException (next 's))) (is (thrown? IllegalArgumentException (next :k))) - (are (= _1 _2) + (are [x y] (= x y) (next nil) nil ; string @@ -306,7 +306,7 @@ (deftest test-last - (are (= _1 _2) + (are [x y] (= x y) (last nil) nil ; list @@ -368,7 +368,7 @@ ;; (deftest test-ffirst (is (thrown? IllegalArgumentException (ffirst))) - (are (= _1 _2) + (are [x y] (= x y) (ffirst nil) nil (ffirst ()) nil @@ -388,7 +388,7 @@ ;; (deftest test-fnext (is (thrown? IllegalArgumentException (fnext))) - (are (= _1 _2) + (are [x y] (= x y) (fnext nil) nil (fnext ()) nil @@ -412,7 +412,7 @@ ;; (deftest test-nfirst (is (thrown? IllegalArgumentException (nfirst))) - (are (= _1 _2) + (are [x y] (= x y) (nfirst nil) nil (nfirst ()) nil @@ -432,7 +432,7 @@ ;; (deftest test-nnext (is (thrown? IllegalArgumentException (nnext))) - (are (= _1 _2) + (are [x y] (= x y) (nnext nil) nil (nnext ()) nil @@ -489,7 +489,7 @@ (is (thrown? ArrayIndexOutOfBoundsException (nth (java.util.ArrayList. []) -1))) ; ??? (is (thrown? ArrayIndexOutOfBoundsException (nth (java.util.ArrayList. [1 2 3]) -1))) ; ??? - (are (= _1 _2) + (are [x y] (= x y) (nth '(1) 0) 1 (nth '(1 2 3) 0) 1 (nth '(1 2 3 4 5) 1) 2 @@ -523,7 +523,7 @@ ; regex Matchers (let [m (re-matcher #"(a)(b)" "ababaa")] (re-find m) ; => ["ab" "a" "b"] - (are (= _1 _2) + (are [x y] (= x y) (nth m 0) "ab" (nth m 1) "a" (nth m 2) "b" @@ -534,7 +534,7 @@ (let [m (re-matcher #"c" "ababaa")] (re-find m) ; => nil - (are (= _1 _2) + (are [x y] (= x y) (nth m 0 :not-found) :not-found (nth m 2 :not-found) :not-found (nth m -1 :not-found) :not-found ) @@ -548,7 +548,7 @@ ; http://code.google.com/p/clojure/source/detail?r=1278 ; (deftest test-distinct - (are (= _1 _2) + (are [x y] (= x y) (distinct ()) () (distinct '(1)) '(1) (distinct '(1 2 3)) '(1 2 3) @@ -570,7 +570,7 @@ (distinct "aaab") '(\a \b) (distinct "abab") '(\a \b) ) - (are (= (distinct [_ _]) [_]) ; (distinct [x x]) = [x] + (are [x] (= (distinct [x x]) [x]) nil false true 0 42 @@ -588,7 +588,7 @@ (deftest test-interpose - (are (= _1 _2) + (are [x y] (= x y) (interpose 0 []) () (interpose 0 [1]) '(1) (interpose 0 [1 2]) '(1 0 2) @@ -596,7 +596,7 @@ (deftest test-interleave - (are (= _1 _2) + (are [x y] (= x y) (interleave [1 2] [3 4]) '(1 3 2 4) (interleave [1] [3 4]) '(1 3) @@ -608,7 +608,7 @@ (deftest test-zipmap - (are (= _1 _2) + (are [x y] (= x y) (zipmap [:a :b] [1 2]) {:a 1 :b 2} (zipmap [:a] [1 2]) {:a 1} @@ -620,7 +620,7 @@ (deftest test-concat - (are (= _1 _2) + (are [x y] (= x y) (concat) () (concat []) () @@ -635,7 +635,7 @@ (deftest test-cycle - (are (= _1 _2) + (are [x y] (= x y) (cycle []) () (take 3 (cycle [1])) '(1 1 1) @@ -645,7 +645,7 @@ (deftest test-partition - (are (= _1 _2) + (are [x y] (= x y) (partition 2 [1 2 3]) '((1 2)) (partition 2 [1 2 3 4]) '((1 2) (3 4)) (partition 2 []) () @@ -665,7 +665,7 @@ (deftest test-reverse - (are (= _1 _2) + (are [x y] (= x y) (reverse nil) () ; since SVN 1294 (reverse []) () (reverse [1]) '(1) @@ -673,7 +673,7 @@ (deftest test-take - (are (= _1 _2) + (are [x y] (= x y) (take 1 [1 2 3 4 5]) '(1) (take 3 [1 2 3 4 5]) '(1 2 3) (take 5 [1 2 3 4 5]) '(1 2 3 4 5) @@ -685,7 +685,7 @@ (deftest test-drop - (are (= _1 _2) + (are [x y] (= x y) (drop 1 [1 2 3 4 5]) '(2 3 4 5) (drop 3 [1 2 3 4 5]) '(4 5) (drop 5 [1 2 3 4 5]) () @@ -697,7 +697,7 @@ (deftest test-take-nth - (are (= _1 _2) + (are [x y] (= x y) (take-nth 1 [1 2 3 4 5]) '(1 2 3 4 5) (take-nth 2 [1 2 3 4 5]) '(1 3 5) (take-nth 3 [1 2 3 4 5]) '(1 4) @@ -713,7 +713,7 @@ (deftest test-take-while - (are (= _1 _2) + (are [x y] (= x y) (take-while pos? []) () (take-while pos? [1 2 3 4]) '(1 2 3 4) (take-while pos? [1 2 3 -1]) '(1 2 3) @@ -723,7 +723,7 @@ (deftest test-drop-while - (are (= _1 _2) + (are [x y] (= x y) (drop-while pos? []) () (drop-while pos? [1 2 3 4]) () (drop-while pos? [1 2 3 -1]) '(-1) @@ -733,14 +733,14 @@ (deftest test-butlast - (are (= _1 _2) + (are [x y] (= x y) (butlast []) nil (butlast [1]) nil (butlast [1 2 3]) '(1 2) )) (deftest test-drop-last - (are (= _1 _2) + (are [x y] (= x y) ; as butlast (drop-last []) () (drop-last [1]) () @@ -776,7 +776,7 @@ (is (vector? (split-at 2 []))) (is (vector? (split-at 2 [1 2 3]))) - (are (= _1 _2) + (are [x y] (= x y) (split-at 2 []) [() ()] (split-at 2 [1 2 3 4 5]) [(list 1 2) (list 3 4 5)] @@ -790,7 +790,7 @@ (is (vector? (split-with pos? []))) (is (vector? (split-with pos? [1 2 -1 0 3 4]))) - (are (= _1 _2) + (are [x y] (= x y) (split-with pos? []) [() ()] (split-with pos? [1 2 -1 0 3 4]) [(list 1 2) (list -1 0 3 4)] @@ -802,14 +802,14 @@ (is (thrown? IllegalArgumentException (repeat))) ; infinite sequence => use take - (are (= _1 _2) + (are [x y] (= x y) (take 0 (repeat 7)) () (take 1 (repeat 7)) '(7) (take 2 (repeat 7)) '(7 7) (take 5 (repeat 7)) '(7 7 7 7 7) ) ; limited sequence - (are (= _1 _2) + (are [x y] (= x y) (repeat 0 7) () (repeat 1 7) '(7) (repeat 2 7) '(7 7) @@ -819,7 +819,7 @@ (repeat -3 7) () ) ; test different data types - (are (= (repeat 3 _) (list _ _ _)) + (are [x] (= (repeat 3 x) (list x x x)) nil false true 0 42 @@ -837,7 +837,7 @@ (deftest test-range - (are (= _1 _2) + (are [x y] (= x y) (range 0) () ; exclusive end! (range 1) '(0) (range 5) '(0 1 2 3 4) @@ -875,7 +875,7 @@ (deftest test-empty? - (are (empty? _) + (are [x] (empty? x) nil () (lazy-seq nil) ; => () @@ -885,7 +885,7 @@ "" (into-array []) ) - (are (not (empty? _)) + (are [x] (not (empty? x)) '(1 2) (lazy-seq [1 2]) [1 2] @@ -897,13 +897,13 @@ (deftest test-every? ; always true for nil or empty coll/seq - (are (= (every? pos? _) true) + (are [x] (= (every? pos? x) true) nil () [] {} #{} (lazy-seq []) (into-array []) ) - (are (= _1 _2) + (are [x y] (= x y) true (every? pos? [1]) true (every? pos? [1 2]) true (every? pos? [1 2 3 4 5]) @@ -916,7 +916,7 @@ false (every? pos? [1 2 -3]) false (every? pos? [1 2 -3 4]) ) - (are (= _1 _2) + (are [x y] (= x y) true (every? #{:a} [:a :a]) ;! false (every? #{:a} [:a :b]) ; Issue 68: every? returns nil instead of false ;! false (every? #{:a} [:b :b]) ; http://code.google.com/p/clojure/issues/detail?id=68 @@ -925,13 +925,13 @@ (deftest test-not-every? ; always false for nil or empty coll/seq - (are (= (not-every? pos? _) false) + (are [x] (= (not-every? pos? x) false) nil () [] {} #{} (lazy-seq []) (into-array []) ) - (are (= _1 _2) + (are [x y] (= x y) false (not-every? pos? [1]) false (not-every? pos? [1 2]) false (not-every? pos? [1 2 3 4 5]) @@ -944,7 +944,7 @@ true (not-every? pos? [1 2 -3]) true (not-every? pos? [1 2 -3 4]) ) - (are (= _1 _2) + (are [x y] (= x y) false (not-every? #{:a} [:a :a]) true (not-every? #{:a} [:a :b]) true (not-every? #{:a} [:b :b]) )) @@ -952,13 +952,13 @@ (deftest test-not-any? ; always true for nil or empty coll/seq - (are (= (not-any? pos? _) true) + (are [x] (= (not-any? pos? x) true) nil () [] {} #{} (lazy-seq []) (into-array []) ) - (are (= _1 _2) + (are [x y] (= x y) false (not-any? pos? [1]) false (not-any? pos? [1 2]) false (not-any? pos? [1 2 3 4 5]) @@ -972,7 +972,7 @@ false (not-any? pos? [1 2 -3]) false (not-any? pos? [1 2 -3 4]) ) - (are (= _1 _2) + (are [x y] (= x y) false (not-any? #{:a} [:a :a]) false (not-any? #{:a} [:a :b]) true (not-any? #{:a} [:b :b]) )) diff --git a/src/clojure/contrib/test_contrib/fnmap.clj b/src/clojure/contrib/test_contrib/fnmap.clj index ccd7a54a..8d833360 100644 --- a/src/clojure/contrib/test_contrib/fnmap.clj +++ b/src/clojure/contrib/test_contrib/fnmap.clj @@ -4,16 +4,16 @@ (deftest acts-like-map (let [m1 (fnmap get assoc :key1 1 :key2 2)] - (are (= _2 (get m1 _1)) + (are [k v] (= v (get m1 k)) :key1 1 :key2 2 :nonexistent-key nil) - (are (= _2 (_1 m1)) + (are [k v] (= v (k m1)) :key1 1 :key2 2 :nonexistent-key nil) (let [m2 (assoc m1 :key3 3 :key4 4)] - (are (= _2 (get m2 _1)) + (are [k v] (= v (get m2 k)) :key1 1 :key2 2 :key3 3 diff --git a/src/clojure/contrib/test_contrib/monads.clj b/src/clojure/contrib/test_contrib/monads.clj index 5d38b544..a09d646d 100644 --- a/src/clojure/contrib/test_contrib/monads.clj +++ b/src/clojure/contrib/test_contrib/monads.clj @@ -19,7 +19,7 @@ (deftest sequence-monad (with-monad sequence-m - (are (= _1 _2) + (are [a b] (= a b) (domonad [x (range 3) y (range 2)] (+ x y)) '(0 1 1 2 2 3) (domonad [x (range 5) y (range (+ 1 x)) :when (= (+ x y) 2)] (list x y)) @@ -37,7 +37,7 @@ (with-monad maybe-m (let [m+ (m-lift 2 +) mdiv (fn [x y] (domonad [a x b y :when (not (zero? b))] (/ a b)))] - (are (= _1 _2) + (are [a b] (= a b) (m+ (m-result 1) (m-result 3)) (m-result 4) (mdiv (m-result 1) (m-result 3)) @@ -50,7 +50,7 @@ (deftest seq-maybe-monad (with-monad (maybe-t sequence-m) (letfn [(pairs [xs] ((m-lift 2 #(list %1 %2)) xs xs))] - (are (= _1 _2) + (are [a b] (= a b) ((m-lift 1 inc) (for [n (range 10)] (when (odd? n) n))) '(nil 2 nil 4 nil 6 nil 8 nil 10) (pairs (for [n (range 5)] (when (odd? n) n))) diff --git a/src/clojure/contrib/test_contrib/pprint/helper.clj b/src/clojure/contrib/test_contrib/pprint/helper.clj index c7112e68..9a4005d6 100644 --- a/src/clojure/contrib/test_contrib/pprint/helper.clj +++ b/src/clojure/contrib/test_contrib/pprint/helper.clj @@ -17,5 +17,5 @@ (:use [clojure.contrib.test-is :only (deftest are run-tests)])) (defmacro simple-tests [name & test-pairs] - `(deftest ~name (are (= _1 _2) ~@test-pairs))) + `(deftest ~name (are [x y] (= x y) ~@test-pairs))) diff --git a/src/clojure/contrib/test_contrib/shell_out.clj b/src/clojure/contrib/test_contrib/shell_out.clj index 0bd1afbe..b6cedabb 100644 --- a/src/clojure/contrib/test_contrib/shell_out.clj +++ b/src/clojure/contrib/test_contrib/shell_out.clj @@ -9,7 +9,7 @@ (def as-env-string ((ns-interns 'clojure.contrib.shell-out) 'as-env-string)) (deftest test-parse-args - (are (= _1 _2) + (are [x y] (= x y) {:cmd [nil] :out "UTF-8" :dir nil :env nil} (parse-args []) {:cmd ["ls"] :out "UTF-8" :dir nil :env nil} (parse-args ["ls"]) {:cmd ["ls" "-l"] :out "UTF-8" :dir nil :env nil} (parse-args ["ls" "-l"]) @@ -17,17 +17,17 @@ )) (deftest test-with-sh-dir - (are (= _1 _2) + (are [x y] (= x y) nil *sh-dir* "foo" (with-sh-dir "foo" *sh-dir*))) (deftest test-with-sh-env - (are (= _1 _2) + (are [x y] (= x y) nil *sh-env* {:KEY "VAL"} (with-sh-env {:KEY "VAL"} *sh-env*))) (deftest test-as-env-string - (are (= _1 _2) + (are [x y] (= x y) nil (as-env-string nil) ["FOO=BAR"] (seq (as-env-string {"FOO" "BAR"})) ["FOO_SYMBOL=BAR"] (seq (as-env-string {'FOO_SYMBOL "BAR"})) @@ -35,7 +35,7 @@ (deftest test-as-file - (are (= _1 _2) + (are [x y] (= x y) (File. "foo") (as-file "foo") nil (as-file nil) (File. "bar") (as-file (File. "bar"))))
\ No newline at end of file diff --git a/src/clojure/contrib/test_contrib/walk.clj b/src/clojure/contrib/test_contrib/walk.clj new file mode 100644 index 00000000..2fb36d4e --- /dev/null +++ b/src/clojure/contrib/test_contrib/walk.clj @@ -0,0 +1,34 @@ +(ns clojure.contrib.test-contrib.walk + (:require [clojure.contrib.walk :as w]) + (:use clojure.contrib.test-is)) + +(deftest t-prewalk-replace + (is (= (w/prewalk-replace {:a :b} [:a {:a :a} (list 3 :c :a)]) + [:b {:b :b} (list 3 :c :b)]))) + +(deftest t-postwalk-replace + (is (= (w/postwalk-replace {:a :b} [:a {:a :a} (list 3 :c :a)]) + [:b {:b :b} (list 3 :c :b)]))) + +(deftest t-stringify-keys + (is (= (w/stringify-keys {:a 1, nil {:b 2 :c 3}, :d 4}) + {"a" 1, nil {"b" 2 "c" 3}, "d" 4}))) + +(deftest t-prewalk-order + (is (= (let [a (atom [])] + (w/prewalk (fn [form] (swap! a conj form) form) + [1 2 {:a 3} (list 4 [5])]) + @a) + [[1 2 {:a 3} (list 4 [5])] + 1 2 {:a 3} [:a 3] :a 3 (list 4 [5]) + 4 [5] 5]))) + +(deftest t-postwalk-order + (is (= (let [a (atom [])] + (w/postwalk (fn [form] (swap! a conj form) form) + [1 2 {:a 3} (list 4 [5])]) + @a) + [1 2 + :a 3 [:a 3] {:a 3} + 4 5 [5] (list 4 [5]) + [1 2 {:a 3} (list 4 [5])]])))
\ No newline at end of file diff --git a/src/clojure/contrib/test_is.clj b/src/clojure/contrib/test_is.clj index 7175fa2a..8138ce3e 100644 --- a/src/clojure/contrib/test_is.clj +++ b/src/clojure/contrib/test_is.clj @@ -749,7 +749,7 @@ Chas Emerick, Allen Rohner, and Stuart Halloway", See clojure.contrib.template/do-template for an explanation of templates. - Example: (are (= _1 _2) + Example: (are [x y] (= x y) 2 (+ 1 1) 4 (* 2 2)) Expands to: @@ -757,8 +757,8 @@ Chas Emerick, Allen Rohner, and Stuart Halloway", (is (= 4 (* 2 2)))) Note: This breaks some reporting features, such as line numbers." - [expr & args] - `(temp/do-template (is ~expr) ~@args)) + [argv expr & args] + `(temp/do-template ~argv (is ~expr) ~@args)) (defmacro testing "Adds a new string to the list of testing contexts. May be nested, diff --git a/src/clojure/contrib/walk.clj b/src/clojure/contrib/walk.clj index 0352aeb2..525fe5a7 100644 --- a/src/clojure/contrib/walk.clj +++ b/src/clojure/contrib/walk.clj @@ -55,9 +55,9 @@ the sorting function."} (list? form) (outer (apply list (map inner form))) (seq? form) (outer (doall (map inner form))) (vector? form) (outer (vec (map inner form))) - (map? form) (outer (into (outer (if (sorted? form) (sorted-map) {})) + (map? form) (outer (into (if (sorted? form) (sorted-map) {}) (map inner form))) - (set? form) (outer (into (outer (if (sorted? form) (sorted-set) #{})) + (set? form) (outer (into (if (sorted? form) (sorted-set) #{}) (map inner form))) :else (outer form))) |