|
| 1 | +(ns glojure.test-glojure.cli |
| 2 | + (:use glojure.test) |
| 3 | + (:require [glojure.string :as str])) |
| 4 | + |
| 5 | +(defmacro #^{:private true} test-that |
| 6 | + "Provides a useful way for specifying the purpose of tests. If the first-level |
| 7 | + forms are lists that make a call to a glojure.test function, it supplies the |
| 8 | + purpose as the msg argument to those functions. Otherwise, the purpose just |
| 9 | + acts like a comment and the forms are run unchanged." |
| 10 | + [purpose & test-forms] |
| 11 | + (let [tests (map |
| 12 | + #(if (= (:ns (meta (resolve (first %)))) |
| 13 | + (the-ns 'glojure.test)) |
| 14 | + (concat % (list purpose)) |
| 15 | + %) |
| 16 | + test-forms)] |
| 17 | + `(do ~@tests))) |
| 18 | + |
| 19 | +(defn run-cli-cmd [& args] |
| 20 | + (let [bytes-to-string (fn [bytes] |
| 21 | + (if (nil? bytes) |
| 22 | + "" |
| 23 | + (apply str (map char (seq bytes))))) |
| 24 | + cmd (apply os$exec.Command args) |
| 25 | + [output err] (.CombinedOutput cmd)] |
| 26 | + [(bytes-to-string output) (bytes-to-string err)])) |
| 27 | + |
| 28 | +(def glj |
| 29 | + (let [[out err] (run-cli-cmd "find" "bin" "-name" "glj" "-executable")] |
| 30 | + (if (and (seq out) (empty? err)) |
| 31 | + (first (str/split-lines out)) |
| 32 | + (throw (Exception. (str "Failed to find glj bin: " err)))))) |
| 33 | + |
| 34 | +(deftest e-flag-test |
| 35 | + (test-that |
| 36 | + "glj -e flag works correctly" |
| 37 | + (let [[out err] (run-cli-cmd glj "-e" "(* 6 7)")] |
| 38 | + (is (= out "42\n") "Command should output 42") |
| 39 | + (is (empty? err) "Command should not return an error")))) |
| 40 | + |
| 41 | +(deftest version-flag-test |
| 42 | + (test-that |
| 43 | + "glj --version flag works correctly" |
| 44 | + (let [[out err] (run-cli-cmd glj "--version")] |
| 45 | + (is (re-matches #"glojure v\d+\.\d+\.\d+\n" out) |
| 46 | + "Command should output version") |
| 47 | + (is (empty? err) "Command should not return an error")))) |
| 48 | + |
| 49 | +(deftest glojure-version-test |
| 50 | + (test-that |
| 51 | + "*glojure-version* should be set correctly" |
| 52 | + (let [[out err] (run-cli-cmd glj "-e" "*glojure-version*")] |
| 53 | + (is (= out "{:major 0, :minor 3, :incremental 0, :qualifier nil}\n") |
| 54 | + "Version should match expected format") |
| 55 | + (is (empty? err) "Command should not return an error")))) |
| 56 | + |
| 57 | +(deftest help-flag-test |
| 58 | + (test-that |
| 59 | + "glj --help flag works correctly" |
| 60 | + (let [[out err] (run-cli-cmd glj "--help")] |
| 61 | + (is (re-matches |
| 62 | + #"(?s).*Glojure v0\.3\.0.*Usage: glj.*Options:.*-e.*-h.*--help.*--version.*Examples:.*" |
| 63 | + out) |
| 64 | + "Command should output help information") |
| 65 | + (is (empty? err) "Command should not return an error")))) |
| 66 | + |
| 67 | +(deftest short-help-flag-test |
| 68 | + (test-that |
| 69 | + "glj -h flag works correctly" |
| 70 | + (let [[out err] (run-cli-cmd glj "-h")] |
| 71 | + (is (re-matches |
| 72 | + #"(?s).*Glojure v0\.3\.0.*Usage: glj.*Options:.*-e.*-h.*--help.*--version.*Examples:.*" |
| 73 | + out) |
| 74 | + "Command should output help information") |
| 75 | + (is (empty? err) "Command should not return an error")))) |
| 76 | + |
| 77 | +(run-tests) |
0 commit comments