|
| 1 | +(ns net.lewisship.cli-tools.completions-test |
| 2 | + "Tests related to command completions." |
| 3 | + (:require [clojure.java.io :as io] |
| 4 | + [clojure.string :as string] |
| 5 | + [clojure.test :refer [deftest is]] |
| 6 | + [net.lewisship.cli-tools.aux :refer [dispatch-with-result]])) |
| 7 | + |
| 8 | +(defn- expected [file] |
| 9 | + (-> (str "expected/" file) |
| 10 | + io/resource |
| 11 | + slurp |
| 12 | + string/split-lines)) |
| 13 | + |
| 14 | +(defn- dispatch |
| 15 | + ([options] |
| 16 | + (-> options |
| 17 | + (assoc :arguments ["completions"]) |
| 18 | + dispatch-with-result |
| 19 | + :out-lines)) |
| 20 | + ([to options] |
| 21 | + (let [result (-> options |
| 22 | + (assoc :arguments ["completions"]) |
| 23 | + dispatch-with-result |
| 24 | + :out)] |
| 25 | + (-> (str "test-resources/expected/" to) |
| 26 | + io/file |
| 27 | + (spit result)) |
| 28 | + (println result)))) |
| 29 | + |
| 30 | +(deftest simple-completions |
| 31 | + (is (match? (expected "simple-completions.txt") |
| 32 | + (dispatch '{:tool-name "simple" |
| 33 | + :namespaces [net.lewisship.cli-tools.colors |
| 34 | + net.lewisship.cli-tools.completions]})))) |
| 35 | + |
| 36 | +(deftest subgroup-completion |
| 37 | + (is (match? (expected "subgroup-completions.txt") |
| 38 | + (dispatch |
| 39 | + '{:tool-name "subgroup" |
| 40 | + :namespaces [net.lewisship.cli-tools.completions] |
| 41 | + :groups |
| 42 | + {"subgroup" {:namespaces [net.lewisship.cli-tools.completion-group]}}})))) |
| 43 | + |
| 44 | +(deftest messy-completions |
| 45 | + ;; where command name and group name collide |
| 46 | + ;; Not sure the current behavior is correct |
| 47 | + (is (match? (expected "messy-completions.txt") |
| 48 | + (dispatch |
| 49 | + {:tool-name "messy" |
| 50 | + :namespaces '[net.lewisship.cli-tools.completions |
| 51 | + net.lewisship.messy-commands] |
| 52 | + :groups {"messy" {:namespaces '[net.lewisship.messy] |
| 53 | + :doc "Messy command and group at same time"}}})))) |
0 commit comments