$30 off During Our Annual Pro Sale. View Details »

Clojure のテストについて

ayato
April 21, 2016

Clojure のテストについて

ayato

April 21, 2016
Tweet

More Decks by ayato

Other Decks in Programming

Transcript

  1. $MPKVSFͷςετ ʹ͍ͭͯ $MPKVSF੒෼ೱ͍Ί ୈճौ୩KBWB !BZBUP@Q$ZCP[V4UBSUVQT *OD

  2. BZBUP@Q ͋΍ͽʔͰ͢ &NBDTͱ$MPKVSF͕޷͖Ͱ͢ $MPKVSFྺ೥ͷ$MPKVSFΤϯδχΞͰ͢ ݄͔Βฐࣾ$MPKVSJBOਓʹͳΓ·͢

  3. ຖճ$MPKVSFͷ࿩Λͯ͠ΔํͰ͢

  4. ໨࣍ ͦ΋ͦ΋$MPKVSFͬͯͳΜͩ $MPKVSFͰͷςετʹ͍ͭͯ DMPKVSFUFTUΛ࢖ͬͨجຊతͳςετͷॻ͖ํ ΑΓ࣮ફతͳॻ͖ํ ଞͷςετϑϨʔϜϫʔΫʹ͍ͭͯ

  5. ͦ΋ͦ΋$MPKVSF ͬͯͳΜͩ

  6. ͦ΋ͦ΋$MPKVSFͬͯͳΜͩ -JTQ ಈత։ൃ ؔ਺ܕϓϩάϥϛϯά ࣮ߦ࣌ϙϦϞʔϑΟζϜ ฒߦϓϩάϥϛϯά +7.ϗετ PS+BWB4DSJQU /&5

  7. $MPKVSFͰͷ ςετʹ͍ͭͯ

  8. 3&1-ฤ 3&1-%SJWFO%FWFMPQNFOU ୯७ͳಈ࡞֬ೝͰ͸ςετέʔεॻ͘ΑΓ
 3&1-Ͱ֬ೝͨ͠ํ͕ૣ͍

  9. ςετίʔυฤ ࠓ೔ͷຊฤ 3%%Λओு͢Δ͜ͱ͸ଟ͍͚ͲςετΛશ͘
 ॻ͔ͳ͍Θ͚Ͱ͸ͳ͍ ଞͷݴޠʹൺ΂Δͱςετ͕ॻ͖΍͍͢ͱ
 ײ͡Δͱ͜Ζ͸ଟ͍

  10. DMPKVSFUFTUΛ࢖ͬͨ جຊతͳ ςετͷॻ͖ํ

  11. (require '[clojure.test :refer :all]) (is (= 4 (+ 2 2)))

    (is (instance? Long 256)) (is (.startsWith "abcde" "ab")) Ξαʔγϣϯ DMPKVSFUFTUωʔϜεϖʔεΛ࢖͏ JTϚΫϩ͚֮ͩ͑Ε͹؆୯ʹςετ͕ग़དྷΔ
  12. user> (is (= 5 (+ 2 2))) FAIL in ()

    (form-init305064439250635979.clj:3) expected: (= 5 (+ 2 2)) actual: (not (= 5 4)) false Ξαʔγϣϯ 3&1-Ͱ΋࣮ߦՄೳ
  13. (is (thrown? ArithmeticException (/ 1 0))) (is (thrown-with-msg? ArithmeticException #"Divide

    by zero" (/ 1 0))) Ξαʔγϣϯ ྫ֎ͷ৔߹͸ಛघͳUISPXO ͳͲΛ࢖͏
  14. (is (= 5 (+ 2 2)) "Arithmetic") ςετͷυΩϡϝϯτԽ JTϚΫϩ͸ୈೋҾ਺ʹΞαʔγϣϯͷ
 આ໌Λ௥Ճग़དྷΔ

  15. (testing "Arithmetic" (testing "with positive integers" (is (= 4 (+

    2 2))) (is (= 7 (+ 3 4)))) (testing "with negative integers" (is (= -4 (+ -2 -2))) (is (= -1 (+ 3 -4))))) ςετͷυΩϡϝϯτԽ UFTUJOHϚΫϩͰςετͷάϧʔϓԽ͕ग़དྷΔ จࣈྻ͸GBJMVSFSFQPSUTʹؚ·ΕΔ Ϩϙʔτ͸34QFD෩
  16. (with-test (defn my-function [x y] (+ x y)) (is (=

    4 (my-function 2 2))) (is (= 7 (my-function 3 4)))) ςετͷఆٛ XJUIUFTUΛ࢖͏ͱؔ਺ͷఆٛͱಉ࣌ʹ
 ςετΛهड़ग़དྷΔ ௨ৗ࢖͏͜ͱ͸ͳ͍ ίʔυશମͷݟ௨͕͠ѱ͘ͳΔ
  17. (deftest addition (is (= 4 (+ 2 2))) (is (=

    7 (+ 3 4)))) (deftest subtraction (is (= 1 (- 4 3))) (is (= 3 (- 7 4)))) ςετͷఆٛ EFGUFTUϚΫϩΛ࢖͏ ςετର৅ͱςετΛ෼͚ͯهड़ग़དྷΔ ௨ৗͪ͜ΒΛ࢖͏ͷ͕Ұൠత
  18. (defn my-fixture [f] ;; 前処理 (f) ;; テストの実行 ;; 後処理

    ) (use-fixtures :each my-fixture) ςετϑΟΫενϟ ϑΟΫενϟࣗମ͸ؔ਺Λͻͱͭड͚औΔ
 ؔ਺ͱͯ͠ఆٛͰ͖Δ ωʔϜεϖʔεʹରͯ͠Ξλον͢Δ
  19. ΑΓ࣮ફతͳॻ͖ํ

  20. !U@XBEB͞Μͷ໰୊Λղ͘ ໰Լهͷ࢓༷ΛςεςΟϯάϑϨʔϜϫʔΫΛ࢖ͬͯςετίʔυΛ ॻ͖ͳ͕Β࣮૷͍ͯͩ͘͠͞ɻ ʲ࢓༷ʳʮݱࡏ࣌ࠁʯʹԠͯ͡ɺѫࡰͷ಺༰ΛԼهͷΑ͏ʹͦΕͧΕฦ ͢ػೳΛ࡞੒͍ͨ͠ɻ λΠϜκʔϯ͸"TJB5PLZPͱ͢Δ  ே Ҏ্ະຬ ͷ৔߹ɺʮ͓͸Α͏͍͟͝·͢ʯͱฦ͢

    ன Ҏ্ະຬ ͷ৔߹ɺʮ͜Μʹͪ͸ʯͱฦ͢ ໷ Ҏ্ະຬ ͷ৔߹ɺʮ͜Μ͹Μ͸ʯͱฦ͢ ྫ࣌ʹHSFFUFSHSFFU ΛݺͿͱ͜Μʹͪ͸ͱฦ͢ IUUQTDPEFJRKQNBHB[JOF
  21. (ns demoapp.greeter) (defn current-date-time [] (java.time.LocalDateTime/now)) (defn greet [] (let

    [current-hour (.getHour (current-date-time))] (cond (<= 5 current-hour 11) "おはようございます" (<= 12 current-hour 17) "こんにちは" :else "こんばんは"))) ςετର৅ͷίʔυ
  22. (ns demoapp.greeter-test (:require [clojure.test :refer :all] [demoapp.greeter :as g :refer

    :all]) (:import [java.time LocalDateTime])) (deftest greet-test (are [dt-str expect] (with-redefs [g/current-date-time (constantly (LocalDateTime/parse dt-str))] (= (greet) expect)) "2016-04-22T00:00" "こんばんは" "2016-04-22T04:59" "こんばんは" "2016-04-22T05:00" "おはようございます" "2016-04-22T11:59" "おはようございます" "2016-04-22T12:00" "こんにちは" "2016-04-22T17:59" "こんにちは" "2016-04-22T18:00" "こんばんは" "2016-04-22T23:59" "こんばんは")) ςετίʔυ
  23. ࢖͍ͬͯΔςΫχοΫ BSFϚΫϩʹΑΔύϥϝλϥΠζυςετ XJUISFEFGTʹΑΔςετμϒϧͷ࣮ݱ

  24. BSFϚΫϩ ࢓૊Έࣗମ͸୯७ JTϚΫϩͷܗʹల։͞ΕΔ ;; これが (are [a b x] (=

    (+ a b) x) 1 2 3 5 10 15) ;; こう展開される (do (is (= (+ 1 2) 3)) (is (= (+ 5 10) 15)))
  25. XJUISFEFGT 7BSΛ࠶ఆٛ͢Δ CPEZ಺෦ͷΈͰ༗ޮ (defn add3 [x] (+ x 3)) (with-redefs

    [add3 (fn [x] (+ x 10))] (= (add3 10) 20)) ;; => true
  26. (do (is (with-redefs [g/current-date-time (constantly (LocalDateTime/parse "2016-04-22T00:00"))] (= (greet) "こんばんは")))

    ;; ... 中略 (is (with-redefs [g/current-date-time (constantly (LocalDateTime/parse "2016-04-22T23:59"))] (= (greet) "こんばんは")))) ͭ·Γ͜͏ͳΔ
  27. ଞͷ ςετϑϨʔϜϫʔΫ ʹ͍ͭͯ

  28. લఏͱͯ͠ ࠷ۙͰ͸৽͍͠%4-Λ࢖Θͳ͍Ͱ
 DMPKVSFUFTUΛ࢖͏ͷ͕ྲྀߦΓ  IUUQTSBTUFSJ[FJPCMPHDMPKVSFUIFHPPE QBSUTIUNM ࢖͍ͬͯΔଆͷײ֮ͱͯ͠΋
 DMPKVSFUFTUͰ଍ΓΔͱײ͡Δ͜ͱ͸ଟ͍

  29. TQFDMK 34QFDΛ$MPKVSFͱ$MPKVSF4DSJQUʹ
 ͖࣋ͬͯͨͱ͍͏ײ͡ͷςετϑϨʔϜϫʔΫ ͋·Γಛ༗ͷػೳΈ͍ͨͳͷ͕ͳ͍ͷͰ
 ࢖͏ϝϦοτ͕ྑ͘෼͔Βͳ͍

  30. .JEKF ϙϐϡϥʔͳςετϑϨʔϜϫʔΫ GBDUBDUVBMFYQFDU ͱॻ͚Δ ߏ଄ͷൺֱ͕༰қͰ͋Δͷͱ.FUBDPOTUBOUT ͕ศརͰ͋Δ Ϧϩʔυ͢Δͱςετ͕࣮ߦ͞Εͯ͠·͏ υΩϡϝϯτ͕͋·Γ៉ྷʹ·ͱΊΒΕͯͳ͍

  31. (def complex-data {"平成" [ {:id 1 :name "ayato_p"} {:id 2

    :name "alea12"} ] "昭和" [ {:id 99 :name "foo"} {:id 100 :name "bar"} ]}) (fact complex-data => (contains {"平成" (contains [{:id 1 :name "ayato_p"}])})) .JEKF
  32. (def members {1 "ayato_p" 2 "alea12" 3 "zer0_u"}) (defn get-id

    [m] (get m :id)) (defn find-member [m] (get members (get-id m))) (fact (find-member ...m...) => "ayato_p" (provided (get-id ...m...) => 1)) .JEKF
  33. GVEKF ܰྔ൛.JEKF _LCKBS  ґଘؔ܎͕গͳ͍ͷ΋ಛ௃ ·ͩࢼͨ͜͠ͱ͸ͳ͍͚Ͳྑͦ͞͏

  34. DMPKVSFUFTUDIFDL 2VJDL$IFDLGPS$MPKVSF $MPKVSFຊମͷςετʹ΋࢖ΘΕ͍ͯΔ

  35. ·ͱΊ

  36. ·ͱΊ $MPKVSFͰςετΛॻ͘ͷ͸؆୯ ͱΓ͋͑ͣ໎ͬͨΒDMPKVSFUFTUΛ࢖͏ UFTUDIFDL͸Ұॹʹ࢖͑Δ