diff options
author | Frantisek Sodomka <fsodomka@gmail.com> | 2009-02-23 20:22:43 +0000 |
---|---|---|
committer | Frantisek Sodomka <fsodomka@gmail.com> | 2009-02-23 20:22:43 +0000 |
commit | ccffa61541d29faabc6cc4bdac6894e294b743db (patch) | |
tree | e1b4ebab7d81e84c37caadd335a433aaf9d10ac7 | |
parent | 188682d2022573b832bf5c72d115f56938eee8e8 (diff) |
test logic: nil-punning
test reader: 'nil 'false 'true
-rw-r--r-- | src/clojure/contrib/test_clojure/logic.clj | 291 | ||||
-rw-r--r-- | src/clojure/contrib/test_clojure/reader.clj | 7 |
2 files changed, 158 insertions, 140 deletions
diff --git a/src/clojure/contrib/test_clojure/logic.clj b/src/clojure/contrib/test_clojure/logic.clj index b082d3f3..1dba6d35 100644 --- a/src/clojure/contrib/test_clojure/logic.clj +++ b/src/clojure/contrib/test_clojure/logic.clj @@ -20,174 +20,187 @@ ;; *** Tests *** (deftest test-if - ;(is (thrown? Exception (if))) ; !!! ERROR IN CATCHING THIS EXCEPTION - ;(is (thrown? Exception (if true))) ; !!! ERROR IN CATCHING THIS EXCEPTION - ; true/false/nil (are (= _1 _2) - (if true :t) :t - (if true :t :f) :t - (if true :t (exception)) :t + (if true :t) :t + (if true :t :f) :t + (if true :t (exception)) :t - (if false :t) nil - (if false :t :f) :f - (if false (exception) :f) :f + (if false :t) nil + (if false :t :f) :f + (if false (exception) :f) :f - (if nil :t) nil - (if nil :t :f) :f - (if nil (exception) :f) :f - ) + (if nil :t) nil + (if nil :t :f) :f + (if nil (exception) :f) :f ) ; zero/empty is true (are (= (if _ :t :f) :t) - (byte 0) - (short 0) - (int 0) - (long 0) - (bigint 0) - (float 0) - (double 0) - (bigdec 0) - - 0/2 - "" - #"" - (symbol "") - - () - [] - {} - #{} - (into-array []) - ) + (byte 0) + (short 0) + (int 0) + (long 0) + (bigint 0) + (float 0) + (double 0) + (bigdec 0) + + 0/2 + "" + #"" + (symbol "") + + () + [] + {} + #{} + (into-array []) ) ; anything except nil/false is true (are (= (if _ :t :f) :t) - (byte 2) - (short 2) - (int 2) - (long 2) - (bigint 2) - (float 2) - (double 2) - (bigdec 2) - - 2/3 - \a - "abc" - #"a*b" - 'abc - :kw - - '(1 2) - [1 2] - {:a 1 :b 2} - #{1 2} - (into-array [1 2]) - - (new java.util.Date) - ) -) - -; nil punning: -; (if (rest [9]) 1 2) -> is currently 2, but without nil punning would be 1 -; (if (seq (rest [1])) 1 2) -> 2 + (byte 2) + (short 2) + (int 2) + (long 2) + (bigint 2) + (float 2) + (double 2) + (bigdec 2) + + 2/3 + \a + "abc" + #"a*b" + 'abc + :kw + + '(1 2) + [1 2] + {:a 1 :b 2} + #{1 2} + (into-array [1 2]) + + (new java.util.Date) )) + + +(deftest test-nil-punning + (are (= (if _1 :no :yes) _2) + (first []) :yes + (next [1]) :yes + (rest [1]) :no + + (butlast [1]) :yes + + (seq nil) :yes + (seq []) :yes + + (sequence nil) :no + (sequence []) :no + + (lazy-seq nil) :no + (lazy-seq []) :no + + (filter #(> % 10) [1 2 3]) :no + (map identity []) :no + (apply concat []) :no + + (concat) :no + (concat []) :no + + (reverse nil) :no + (reverse []) :no + + (sort nil) :no + (sort []) :no )) (deftest test-and (are (= _1 _2) - (and) true - (and true) true - (and nil) nil - (and false) false + (and) true + (and true) true + (and nil) nil + (and false) false - (and true nil) nil - (and true false) false + (and true nil) nil + (and true false) false - (and 1 true :kw 'abc "abc") "abc" + (and 1 true :kw 'abc "abc") "abc" - (and 1 true :kw nil 'abc "abc") nil - (and 1 true :kw nil (exception) 'abc "abc") nil + (and 1 true :kw nil 'abc "abc") nil + (and 1 true :kw nil (exception) 'abc "abc") nil - (and 1 true :kw 'abc "abc" false) false - (and 1 true :kw 'abc "abc" false (exception)) false - ) -) + (and 1 true :kw 'abc "abc" false) false + (and 1 true :kw 'abc "abc" false (exception)) false )) (deftest test-or (are (= _1 _2) - (or) nil - (or true) true - (or nil) nil - (or false) false + (or) nil + (or true) true + (or nil) nil + (or false) false - (or nil false true) true - (or nil false 1 2) 1 - (or nil false "abc" :kw) "abc" + (or nil false true) true + (or nil false 1 2) 1 + (or nil false "abc" :kw) "abc" - (or false nil) nil - (or nil false) false - (or nil nil nil false) false + (or false nil) nil + (or nil false) false + (or nil nil nil false) false - (or nil true false) true - (or nil true (exception) false) true - (or nil false "abc" (exception)) "abc" - ) -) + (or nil true false) true + (or nil true (exception) false) true + (or nil false "abc" (exception)) "abc" )) (deftest test-not (is (thrown? IllegalArgumentException (not))) (are (= (not _) true) - nil - false - ) + nil + false ) (are (= (not _) false) - true - - ; numbers - 0 - 0.0 - 42 - 1.2 - 0/2 - 2/3 - - ; characters - \space - \tab - \a - - ; strings - "" - "abc" - - ; regexes - #"" - #"a*b" - - ; symbols - (symbol "") - 'abc - - ; keywords - :kw - - ; collections/arrays - () - '(1 2) - [] - [1 2] - {} - {:a 1 :b 2} - #{} - #{1 2} - (into-array []) - (into-array [1 2]) - - ; Java objects - (new java.util.Date) - ) -) + true + + ; numbers + 0 + 0.0 + 42 + 1.2 + 0/2 + 2/3 + + ; characters + \space + \tab + \a + + ; strings + "" + "abc" + + ; regexes + #"" + #"a*b" + + ; symbols + (symbol "") + 'abc + + ; keywords + :kw + + ; collections/arrays + () + '(1 2) + [] + [1 2] + {} + {:a 1 :b 2} + #{} + #{1 2} + (into-array []) + (into-array [1 2]) + + ; Java objects + (new java.util.Date) )) diff --git a/src/clojure/contrib/test_clojure/reader.clj b/src/clojure/contrib/test_clojure/reader.clj index cab364d9..d8839863 100644 --- a/src/clojure/contrib/test_clojure/reader.clj +++ b/src/clojure/contrib/test_clojure/reader.clj @@ -31,6 +31,12 @@ ;; Literals +(deftest Literals + ; 'nil 'false 'true are reserved by Clojure and are not symbols + (is (= 'nil nil)) + (is (= 'false false)) + (is (= 'true true)) ) + ;; Strings (deftest Strings @@ -106,7 +112,6 @@ (is (instance? BigDecimal -1.0M)) ) - ;; Characters (deftest t-Characters) |