Geek-Side

Resource > Clojure Test

:testメタデータを使う方法

実装とテストが同じ箇所にまとまる。

 (import java.util.Calendar)
 
 (defn
   #^{:test (fn []
 	     (assert (not (nil? (make-month 1 2))))
 	     (assert (= 2 (. (make-month 2 2010) get (. Calendar MONTH))))
 	     (assert (= 2010 (. (make-month 2 2010) get (. Calendar YEAR))))
 	     (assert (= 1 (. (make-month 2 2010) get (. Calendar DATE))))
 	     (assert (= 0 (. (make-month 2 2010) get (. Calendar HOUR))))
 	     (assert (= 0 (. (make-month 2 2010) get (. Calendar SECOND))))
 )}
   make-month [m y]
   (let [date (. Calendar getInstance)]
   (. date set y m 1 0 0 0)
   date))
 
 (test #'make-month)

test-isライブラリを使う方法

 (import java.util.Calendar)
 
 (defn
   make-month [m y]
   (let [date (. Calendar getInstance)]
   (. date set y m 1 0 0 0)
   date))
 
 (use 'clojure.contrib.test-is)
 (deftest test-make-month
   (is (not (nil? (make-month 1 2))))
   (is (= 2 (. (make-month 2 2010) get (. Calendar MONTH))))
   (is (= 2010 (. (make-month 2 2010) get (. Calendar YEAR))))
   (is (= 1 (. (make-month 2 2010) get (. Calendar DATE))))
   (is (= 0 (. (make-month 2 2010) get (. Calendar HOUR))))
   (is (= 0 (. (make-month 2 2010) get (. Calendar SECOND)))))
 
 (run-tests)