Skip to content

Commit

Permalink
Use humane-are (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
camsaul authored Sep 8, 2022
1 parent 829e455 commit fd7a1a0
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 178 deletions.
11 changes: 6 additions & 5 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@
:profiles
{:dev
{:dependencies
[[org.clojure/clojure "1.11.1"]
[[criterium "0.4.6"]
[io.github.camsaul/humane-are "1.0.2"]
[org.clojure/clojure "1.11.1"]
[org.clojure/math.combinatorics "0.1.6"]
[criterium "0.4.6"]
[pjstadig/humane-test-output "0.11.0"]]

:injections
[(require 'pjstadig.humane-test-output)
(pjstadig.humane-test-output/activate!)]

:jvm-opts ["-Xverify:none"]
(pjstadig.humane-test-output/activate!)
(require 'humane-are.core)
(humane-are.core/install!)]

:source-paths ["dev"]}

Expand Down
220 changes: 106 additions & 114 deletions test/methodical/impl/combo/clos_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -53,59 +53,55 @@

(t/deftest before-test
(t/testing "before methods for CLOS method combinations"
(t/are [args]
(t/testing (format "%d args" (count args))
(let [[calls make-method] (make-method-fn)
f (combine-methods
[(make-primary-method make-method)]
{:before [(make-method :before-1)
(make-method :before-2)]})]
(t/testing "result"
(t/is (= [:primary]
(apply f args))
"Return values of before methods should be ignored"))

(t/testing "calls"
(t/is (= [(cons 'before-1 args)
(cons 'before-2 args)
(cons 'primary args)]
(calls))
"Before methods should be called in order from most-specific to least-specific"))))

[]
[[]]
[[] :v2]
[[] :v2 :v3]
[[] :v2 :v3 :v4]
[[] :v2 :v3 :v4 :v5])))
(doseq [args [[]
[[]]
[[] :v2]
[[] :v2 :v3]
[[] :v2 :v3 :v4]
[[] :v2 :v3 :v4 :v5]]]
(t/testing (format "%d args" (count args))
(let [[calls make-method] (make-method-fn)
f (combine-methods
[(make-primary-method make-method)]
{:before [(make-method :before-1)
(make-method :before-2)]})]
(t/testing "result"
(t/is (= [:primary]
(apply f args))
"Return values of before methods should be ignored"))

(t/testing "calls"
(t/is (= [(cons 'before-1 args)
(cons 'before-2 args)
(cons 'primary args)]
(calls))
"Before methods should be called in order from most-specific to least-specific")))))))

(t/deftest after-test
(t/testing "after methods for CLOS method combinations"
(t/are [args]
(t/testing (format "%d args" (count args))
(let [[calls make-method] (make-method-fn)
f (combine-methods
[(make-primary-method make-method)]
{:after [(make-method :after-1)
(make-method :after-2)]})]
(t/testing "result"
(t/is (= [:primary]
(apply f args))
"Return values of after methods should be ignored"))

(t/testing "calls"
(t/is (= [(cons 'primary args)
'(after-2 [:primary])
'(after-1 [:primary])]
(calls))
"after methods should be called in order from least- to most-specific with result of primary fn"))))

[]
[[]]
[[] :v2]
[[] :v2 :v3]
[[] :v2 :v3 :v4]
[[] :v2 :v3 :v4 :v5])))
(doseq [args [[]
[[]]
[[] :v2]
[[] :v2 :v3]
[[] :v2 :v3 :v4]
[[] :v2 :v3 :v4 :v5]]]
(t/testing (format "%d args" (count args))
(let [[calls make-method] (make-method-fn)
f (combine-methods
[(make-primary-method make-method)]
{:after [(make-method :after-1)
(make-method :after-2)]})]
(t/testing "result"
(t/is (= [:primary]
(apply f args))
"Return values of after methods should be ignored"))

(t/testing "calls"
(t/is (= [(cons 'primary args)
'(after-2 [:primary])
'(after-1 [:primary])]
(calls))
"after methods should be called in order from least- to most-specific with result of primary fn")))))))

(defn- make-around-method
"Makes an around method that appends `<method-key>-before` to the first arg (if any), wraps all other args
Expand All @@ -125,47 +121,45 @@

(t/deftest around-test
(t/testing "around methods"
(t/are [args]
(t/testing (format "%d args" (count args))
(let [[calls make-method record-call!] (make-method-fn)
f (combine-methods
[(make-primary-method make-method)]
{:around [(make-around-method record-call! :around-1)
(make-around-method record-call! :around-2)]})]
(t/testing "result"
(let [expected-args (if (empty? args)
[:primary :around-1-after :around-2-after]
[:around-2-before :around-1-before :primary :around-1-after :around-2-after])]
(t/is (= expected-args
(apply f args))
"Around methods should be able to modify args, and modify the results")))

(t/testing "calls"
(let [expected-calls (if (empty? args)
'[(around-2-before)
(around-1-before)
(primary)
(around-1-after [:primary])
(around-2-after [:primary :around-1-after])]
[(cons 'around-2-before args)
(concat '(around-1-before [:around-2-before])
(for [arg (rest args)]
(list 'around-2-before arg)))
(concat '(primary [:around-2-before :around-1-before])
(for [arg (rest args)]
(list 'around-1-before (list 'around-2-before arg))))
'(around-1-after [:around-2-before :around-1-before :primary])
'(around-2-after [:around-2-before :around-1-before :primary :around-1-after])])]
(t/is (= expected-calls
(calls))
"Around methods should be applied, in or in order from least- to most- specific")))))

[]
[[]]
[[] :v2]
[[] :v2 :v3]
[[] :v2 :v3 :v4]
[[] :v2 :v3 :v4 :v5])))
(doseq [args [[]
[[]]
[[] :v2]
[[] :v2 :v3]
[[] :v2 :v3 :v4]
[[] :v2 :v3 :v4 :v5]]]
(t/testing (format "%d args" (count args))
(let [[calls make-method record-call!] (make-method-fn)
f (combine-methods
[(make-primary-method make-method)]
{:around [(make-around-method record-call! :around-1)
(make-around-method record-call! :around-2)]})]
(t/testing "result"
(let [expected-args (if (empty? args)
[:primary :around-1-after :around-2-after]
[:around-2-before :around-1-before :primary :around-1-after :around-2-after])]
(t/is (= expected-args
(apply f args))
"Around methods should be able to modify args, and modify the results")))

(t/testing "calls"
(let [expected-calls (if (empty? args)
'[(around-2-before)
(around-1-before)
(primary)
(around-1-after [:primary])
(around-2-after [:primary :around-1-after])]
[(cons 'around-2-before args)
(concat '(around-1-before [:around-2-before])
(for [arg (rest args)]
(list 'around-2-before arg)))
(concat '(primary [:around-2-before :around-1-before])
(for [arg (rest args)]
(list 'around-1-before (list 'around-2-before arg))))
'(around-1-after [:around-2-before :around-1-before :primary])
'(around-2-after [:around-2-before :around-1-before :primary :around-1-after])])]
(t/is (= expected-calls
(calls))
"Around methods should be applied, in or in order from least- to most- specific"))))))))

(t/deftest primary-method-test
(t/testing "Empty primary-methods"
Expand All @@ -174,29 +168,27 @@
"combine-methods should return nil if there are no matching primary methods."))

(t/testing "next-method"
(t/are [args]
(t/testing (format "%d args" (count args))
(let [[calls make-method] (make-method-fn)

f
(combine-methods [(make-primary-method make-method :primary-1)
(make-primary-method make-method :primary-2)]
nil)]
(t/is (= [:primary-1 :primary-2 :primary-1-after]
(f []))
"Calling `next-method` should invoke the next method")

(t/testing "calls"
(t/is (= '[(primary-1 [])
(primary-2 [:primary-1])]
(calls))))))

[]
[[]]
[[] :v2]
[[] :v2 :v3]
[[] :v2 :v3 :v4]
[[] :v2 :v3 :v4 :v5])))
(doseq [args [[]
[[]]
[[] :v2]
[[] :v2 :v3]
[[] :v2 :v3 :v4]
[[] :v2 :v3 :v4 :v5]]]
(t/testing (format "%d args" (count args))
(let [[calls make-method] (make-method-fn)

f
(combine-methods [(make-primary-method make-method :primary-1)
(make-primary-method make-method :primary-2)]
nil)]
(t/is (= [:primary-1 :primary-2 :primary-1-after]
(f []))
"Calling `next-method` should invoke the next method")

(t/testing "calls"
(t/is (= '[(primary-1 [])
(primary-2 [:primary-1])]
(calls)))))))))

(t/deftest everything-test
(let [[calls make-method record-call!] (make-method-fn)
Expand Down
62 changes: 30 additions & 32 deletions test/methodical/impl/combo/operator_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,39 @@
[methodical.interface :as i]))

(t/deftest primary-test
(t/are [operator] (t/testing operator
(t/testing "Empty primary methods"
(t/is (= nil
((combo.operator/operator operator) []))
"Combine-methods should return nil if `primary-methods` is empty.")))
:+ :and :concat :do :max :min :or :seq))
(t/testing "Empty primary methods"
(t/testing "Combine-methods should return nil if `primary-methods` is empty."
(t/are [operator] (= nil
((combo.operator/operator operator) []))
:+ :and :concat :do :max :min :or :seq))))

(t/deftest around-test
(t/are [operator primary-result combined-result]
(t/testing "around methods"
(doseq [[operator primary-result combined-result] [[:+ 100 100]
[:and true true]
[:concat [:v] [:v]]
[:do :x :x]
[:max 100 100]
[:min 100 100]
[:or true true]
[:seq :v [:v]]]]
(t/testing operator
(t/testing "around methods"
(let [calls (atom [])
primary (constantly primary-result)
around (fn [next-method]
(swap! calls conj '(around-before))
(let [result (next-method)]
(swap! calls conj (list 'around-after result))
result))
f ((combo.operator/operator operator)
[primary]
{:around [around]})]
(t/is (= ['(around-before)
(list 'around-after combined-result)]
(do
(f)
@calls))
"Operator method combinations should support around methods"))))
:+ 100 100
:and true true
:concat [:v] [:v]
:do :x :x
:max 100 100
:min 100 100
:or true true
:seq :v [:v]))
(let [calls (atom [])
primary (constantly primary-result)
around (fn [next-method]
(swap! calls conj '(around-before))
(let [result (next-method)]
(swap! calls conj (list 'around-after result))
result))
f ((combo.operator/operator operator)
[primary]
{:around [around]})]
(t/is (= ['(around-before)
(list 'around-after combined-result)]
(do
(f)
@calls))
"Operator method combinations should support around methods"))))))

(defn- record-calls [method-vars]
(let [calls (atom [])
Expand Down
15 changes: 6 additions & 9 deletions test/methodical/impl/combo/threaded_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,12 @@
(after-xform acc)))))

(t/deftest primary-test
(t/are [threading]
(t/testing threading
(t/testing "Empty primary methods"
(t/is (= nil
(combine-methods threading [] {:before [(constantly :before)]}))
"Combine-methods should return nil if `primary-methods` is empty.")))

:thread-first
:thread-last))
(t/testing "Empty primary methods"
(t/testing "Combine-methods should return nil if `primary-methods` is empty."
(t/are [threading] (= nil
(combine-methods threading [] {:before [(constantly :before)]}))
:thread-first
:thread-last))))

(t/deftest before-test
(t/are [threading expected-1 expected-4]
Expand Down
20 changes: 8 additions & 12 deletions test/methodical/impl/dispatcher/common_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
(let [h (-> (make-hierarchy)
(derive :x :x-parent)
(derive :y :y-parent))]
(t/are [msg prefs] (t/testing (format "x should be preferred over y with prefers = %s" prefs)
(t/is (= true
(dispatcher.common/prefers? h prefs :x :y))
msg))
"x is directly preferred over y"
{:x #{:y}}
"x is preferred over an ancestor of y"
{:x #{:y-parent}}
"an ancestor of x is preferred over y"
{:x-parent #{:y}}
"an ancestor of x is preferred over an ancestor of y"
{:x-parent #{:y-parent}}))))
(doseq [[msg prefs] {"x is directly preferred over y" {:x #{:y}}
"x is preferred over an ancestor of y" {:x #{:y-parent}}
"an ancestor of x is preferred over y" {:x-parent #{:y}}
"an ancestor of x is preferred over an ancestor of y" {:x-parent #{:y-parent}}}]
(t/testing (format "x should be preferred over y with prefers = %s" prefs)
(t/is (= true
(dispatcher.common/prefers? h prefs :x :y))
msg))))))

(t/deftest distinct-by-test
(t/is (= [:a :b :c :d]
Expand Down
Loading

0 comments on commit fd7a1a0

Please sign in to comment.