<commit_before>(ns midje.semi-sweet-examples-test
  (:use [midje.semi-sweet] :reload-all)
  (:use [midje.checkers])
  (:use [clojure.test])
  (:use [midje.test-util]))


(def mocked-function)
(defn function-under-test [& rest]
  (apply mocked-function rest))
(defn no-caller [])

(def reported (atom []))

(def reportX) ;; so you can easily see real reports.
(defmacro one-case [description during-form & check-forms]
  (let [form-is-during? (fn [form] (and (seq? form)
				    (= (first form) 'during)))]
    (assert (form-is-during? during-form))
    (assert (every? #(not (form-is-during? %)) check-forms))
    `(do 
       (binding [report (fn [report-map#] (swap! reported conj report-map#))]
	 (reset! reported [])
	 ~during-form)
       ~@check-forms)))

(defn last-subtype? [expected]
  (= (:subtype (last (deref reported))) expected))
(defn no-failures? []
  (every? #(= (:type %) :pass) (deref reported)))
  

(deftest example-tests
  (one-case "successful mocking"
    (during (function-under-test) => 33
	    [ (fake (mocked-function) => 33) ])
;    (println (deref reported))
    (is (no-failures?)))

  (one-case "mock call omitted"
    (during (no-caller) => nil
	    [ (fake (mocked-function) => 33) ])
;    (println (deref reported))
    (is (last-subtype? :incorrect-call-count)))
)
<commit_msg>Added more tests - there are still some pending ones.<commit_after>(ns midje.semi-sweet-examples-test
  (:use [midje.semi-sweet] :reload-all)
  (:use [midje.checkers])
  (:use [clojure.test])
  (:use [midje.test-util]))


(def mocked-function)
(defn function-under-test [& rest]
  (apply mocked-function rest))
(defn no-caller [])

(def reported (atom []))

(def reportX) ;; so you can easily see real reports.
(defmacro one-case 
  ([description]
   `(println "Pending:"  ~description))
  ([description during-form & check-forms]
   (let [form-is-during? (fn [form] (and (seq? form)
					 (= (first form) 'during)))]
     (assert (form-is-during? during-form))
     (assert (every? #(not (form-is-during? %)) check-forms))
     `(do 
	(binding [report (fn [report-map#] (swap! reported conj report-map#))]
	  (reset! reported [])
	  ~during-form)
	~@check-forms))))

(defn last-subtype? [expected]
  (= (:subtype (last (deref reported))) expected))
(defn no-failures? []
  (every? #(= (:type %) :pass) (deref reported)))
  

(deftest simple-examples
  (one-case "mocked functions must be declared before use")

  (one-case "successful mocking"
    (during (function-under-test) => 33
	    [ (fake (mocked-function) => 33) ])
    (is (no-failures?)))

  (one-case "mocking is made, but function under test produces the wrong result"
     (during (function-under-test 33) => 12
	     [ (fake (mocked-function 33) => (not 12) ) ])
     (is (= (:actual (last @reported)) '(not (clojure.core/= false 12)))))

  (one-case "mock call supposed to be made, but wasn't (zero call count)"
    (during (no-caller) => nil
	    [ (fake (mocked-function) => 33) ])
    (is (last-subtype? :incorrect-call-count)))

  (def other-function)
  (one-case "call not from inside function"
     (during (+ (mocked-function 12) (other-function 12)) => 12
	     [ (fake (mocked-function 12) => 11)
	       (fake (other-function 12) => 1) ])
     (is (no-failures?)))

  (one-case "call that matches none of the expected arguments"
     (during (+ (mocked-function 12) (mocked-function 33)) => "result irrelevant because of earlier failure"
	     [ (fake (mocked-function 12) => "hi") ])
     (is (last-subtype? :unexpected-call)))

  (one-case "failure because one variant of doubly-mocked function is not called")

  (one-case "multiple calls to a mocked function are perfectly fine")
)

{'fraction_non_alphanumeric': 0.1792709466811752, 'fraction_numerical': 0.011153427638737758, 'mean_word_length': 3.5507425742574257, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5900763', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1279, 'n_words': 355}