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 |