Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 91 lines (80 sloc) 3.472 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
(ns clojure.tools.cli-test
  (:use [clojure.test]
        [clojure.tools.cli]))

(testing "syntax"
  (deftest should-handle-simple-strings
    (is (= {:host "localhost"}
           (first (cli ["--host" "localhost"]
                       ["--host"])))))

  (testing "booleans"
    (deftest should-handle-trues
      (is (= {:verbose true}
             (first (cli ["--verbose"]
                         ["--[no-]verbose"])))))
    (deftest should-handle-falses
      (is (= {:verbose false}
             (first (cli ["--no-verbose"]
                         ["--[no-]verbose"])))))
    
    (testing "explicit syntax"
      (is (= {:verbose true}
             (first (cli ["--verbose"]
                         ["--verbose" :flag true]))))
      (is (= {:verbose false}
             (first (cli ["--no-verbose"]
                         ["--verbose" :flag true]))))))

  (testing "default values"
    (deftest should-default-when-no-value
      (is (= {:server "10.0.1.10"}
             (first (cli []
                         ["--server" :default "10.0.1.10"])))))
    (deftest should-override-when-supplied
      (is (= {:server "127.0.0.1"}
             (first (cli ["--server" "127.0.0.1"]
                         ["--server" :default "10.0.1.10"]))))))

  (deftest should-apply-parse-fn
    (is (= {:names ["john" "jeff" "steve"]}
           (first (cli ["--names" "john,jeff,steve"]
                       ["--names" :parse-fn #(vec (.split % ","))])))))

  (testing "aliases"
    (deftest should-support-multiple-aliases
      (is (= {:server "localhost"}
             (first (cli ["-s" "localhost"]
                         ["-s" "--server"])))))

    (deftest should-use-last-alias-provided-as-name-in-map
      (is (= {:server "localhost"}
             (first (cli ["-s" "localhost"]
                         ["-s" "--server"]))))))

  (testing "extra arguments"
    (deftest should-provide-access-to-trailing-args
      (let [[options args _] (cli ["--foo" "bar" "a" "b" "c"]
                                  ["-f" "--foo"])]
        (is (= {:foo "bar"} options))
        (is (= ["a" "b" "c"] args))))

    (deftest should-work-with-trailing-boolean-args
      (let [[options args _] (cli ["--no-verbose" "some-file"]
                                  ["--[no-]verbose"])]
        (is (= {:verbose false}))
        (is (= ["some-file"] args))))

    (deftest should-accept-double-hyphen-as-end-of-args
      (let [[options args _] (cli ["--foo" "bar" "--verbose" "--" "file" "-x" "other"]
                                  ["--foo"]
                                  ["--[no-]verbose"])]
        (is (= {:foo "bar" :verbose true} options))
        (is (= ["file" "-x" "other"] args))))))

(deftest all-together-now
  (let [[options args _] (cli ["-p" "8080"
                               "--no-verbose"
                               "--log-directory" "/tmp"
                               "--server" "localhost"
                               "filename"]
                              ["-p" "--port" :parse-fn #(Integer. %)]
                              ["--host" :default "localhost"]
                              ["--[no-]verbose" :default true]
                              ["--log-directory" :default "/some/path"]
                              ["--server"])]
    (is (= {:port 8080
            :host "localhost"
            :verbose false
            :log-directory "/tmp"
            :server "localhost"} options))
    (is (= ["filename"] args))))
Something went wrong with that request. Please try again.