googleAPIから天気を取得2
http://d.hatena.ne.jp/trotr/20100328
昨日のは無駄な再実装をしていた。
zip-filterあたりを使えば良かったようです。t
(require '[clojure.zip :as z]) (require '[clojure.contrib.zip-filter :as zf]) (require '[clojure.contrib.zip-filter.xml :as zfx]) (require '[clojure.xml :as x]) (defn describe-weather [area-name] (let [url (format "http://www.google.com/ig/api?weather=%s" area-name) zip-xml (z/xml-zip (x/parse url)) conditions (zfx/xml-> zip-xml :weather zf/children :current_conditions zf/children)] (doseq [item conditions] (->> item first :attrs :data println)))) (describe-weather "Tokyo")
clojureでテスト
clojure.testを使ってテストを書いてみる。テストの書き方には2つ方法がある。
- defnでの関数定義と一緒にテストを書く
- deftestで個別にテストを書く
テスト書く->(run-tests)という感じで大丈夫
簡単な例
(use 'clojure.test) ;;metaに:testを定義することも可能。 (defn add2 ([x] (+ x 2)) {:test #(do (is (= (add2 -1) 1)) (is (= (add2 3) 5)) (is (= (add2 -4) -2)) (is (> (add2 50) 50)))}) ;;deftestを利用するときには元の関数と名前が被るとまずい (deftest add2d (is (= (add2 1) 3))) (run-tests)
結果
Testing user Ran 2 tests containing 5 assertions. 0 failures, 0 errors. {:type :summary, :test 2, :pass 5, :fail 0, :error 0}
追記
makingさんから教えてもらった。
leiningen使う(もしくは今後移行する予定あり)ならtest pathに"run-testsすればテストが実行されるファイル"を置いとくのが無難(デファクト)かと思います