Skip to content

Commit

Permalink
tests for core.loader.clj and small change v0.2 (#159)
Browse files Browse the repository at this point in the history
* save for now

* codeclimate says dorun is non-idio -- so switching to doseq since we just want the side effects

* be explicit about what "YB-all-namespaces" means

* small optimization when finding and loading namespaces

* found an issue where namespaces were being "double loaded" -- using distinct to make unique

* put it all back to normal but include distinct fix

* small movements

* use original src loader.clj, drop test related to checking for dups

* load all commands in cmd fact

* try it all again
  • Loading branch information
gkspranger committed Mar 12, 2021
1 parent ec12947 commit 465de69
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/yetibot/core/loader.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
(ns/find-namespaces (cp/classpath))))

(defn find-namespaces [pattern]
(filter #(re-matches pattern (str %)) (all-namespaces)))
(->> (all-namespaces)
(filter #(re-matches pattern (str %)))
(distinct)))

(def yetibot-command-namespaces
[;; support for e.g.:
Expand Down
13 changes: 6 additions & 7 deletions test/yetibot/core/test/commands/cmd.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
(ns yetibot.core.test.commands.cmd
(:require
[midje.sweet :refer [fact => contains has-prefix just]]
[matcher-combinators.midje :refer [match]]
[yetibot.core.midje :refer [value data error]]
[yetibot.core.commands.cmd :refer [cmd]]))
[midje.sweet :refer [fact =>]]
[yetibot.core.commands.cmd :refer [cmd]]
[yetibot.core.loader :as loader]))

(fact
"cmd should work as expected"
(cmd {:match "echo hi"}) => "hi")
(fact "cmd should work as expected"
(loader/load-commands)
(cmd {:match "echo hi"}) => "hi")
41 changes: 31 additions & 10 deletions test/yetibot/core/test/loader.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
(ns yetibot.core.test.loader
(:require
[yetibot.core.loader :as loader]
[clojure.test :refer :all]))

(deftest loader
(testing
"Loading all namespaces. This can help find invalid requires or errors in
code at test time (instead of waiting till runtime)"
(loader/load-observers)
(loader/load-commands)))
(:require [yetibot.core.loader :as loader]
[midje.sweet :refer [=> =not=> contains fact facts every-checker]]))

(fact "all-namespaces returns non-empty collection that contains expected namespace"
(loader/all-namespaces)
=> (every-checker coll? not-empty (contains 'yetibot.core.loader)))

(facts "about load-ns"
(fact "loads a legit namesapce and returns said namespace"
(loader/load-ns 'yetibot.core.commands.help)
=> 'yetibot.core.commands.help)
(fact "returns nil when loading illegitimate namespace"
(loader/load-ns 'i.am.fake) => nil))

(facts "about find-and-load-namespaces"
(let [patterns '(#"yetibot\.core\.commands\.help"
#"yetibot\.core\.commands\.error")
nss (loader/find-and-load-namespaces patterns)
nss-count (count nss)]
(fact "results is non-empty collection that contains expected namespace"
nss => (every-checker coll? not-empty) (contains 'yetibot.core.commands.help))
(fact "results is exactly 2 commands"
nss-count => 2)
(fact "results don't contain extraneous namespaces"
nss =not=> (contains 'yetibot.core.commands.echo))))

(fact "load-observers returns a non-empty collection"
(loader/load-observers) => (every-checker coll? not-empty))

(fact "load-commands returns a non-empty collection"
(loader/load-commands) => (every-checker coll? not-empty))

0 comments on commit 465de69

Please sign in to comment.