Permalink
Browse files

argument parsing is working well with some new options

  • Loading branch information...
1 parent ce0d551 commit 662d58db0eae989e82f27b8c51a8c9113fe1b42a @slagyr committed Nov 23, 2010
View
@@ -1,4 +1,5 @@
pom.xml
-*jar
lib
classes
+out
+
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2010 Micah Martin
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
Binary file not shown.
View
@@ -1,4 +1,7 @@
(defproject speclj "1.0.0"
:description "speclj: Pronounced 'speckle', is a Behavior Driven Development framework for Clojure."
- :dependencies [[org.clojure/clojure "1.2.0"]]
- :test-path "spec/")
+ :dependencies [[org.clojure/clojure "1.2.0"]
+ [mmargs "1.2.0"]]
+ :repositories { "localShared" "file://m2"}
+ :test-path "spec/"
+ :main speclj.main)
View
@@ -1,27 +1,39 @@
(ns speclj.main-spec
(:use
[speclj.core]
- [speclj.main])
+ [speclj.main]
+ [speclj.util :only (endl)])
(:require
[speclj.run.standard]
[speclj.run.vigilant]
- [speclj.report.console]
- [speclj.report.silent]))
+ [speclj.report.progress]
+ [speclj.report.silent]
+ [speclj.version])
+ (:import
+ [java.io ByteArrayOutputStream OutputStreamWriter]))
+
+
+(defn to-s [output]
+ (String. (.toByteArray output)))
(describe "speclj main"
+ (with output (ByteArrayOutputStream.))
+ (with writer (OutputStreamWriter. @output))
+ (around [spec] (binding [*out* @writer] (spec)))
+ (around [spec] (binding [exit identity] (spec)))
(it "has default configuration"
- (should= ["spec"] (:spec-dirs default-config))
- (should= "console" (:reporter default-config))
+ (should= ["spec"] (:specs default-config))
+ (should= "progress" (:reporter default-config))
(should= "standard" (:runner default-config)))
(it "parses no arguments"
(should= default-config (parse-args)))
(it "parses non-option arguments as spec dirs"
- (should= ["one"] (:spec-dirs (parse-args "one")))
- (should= ["one" "two"] (:spec-dirs (parse-args "one" "two")))
- (should= ["one" "two" "three"] (:spec-dirs (parse-args "one" "two" "three"))))
+ (should= ["one"] (:specs (parse-args "one")))
+ (should= ["one" "two"] (:specs (parse-args "one" "two")))
+ (should= ["one" "two" "three"] (:specs (parse-args "one" "two" "three"))))
(it "parses the runner argument"
(should= "fridge" (:runner (parse-args "--runner=fridge")))
@@ -44,10 +56,10 @@
(it "throws exception with unrecognized runner"
(should-throw Exception "Failed to load runner: blah" (load-runner "blah")))
- (it "dynaimcally loads ConsoleReporter"
- (let [reporter (load-reporter "console")]
+ (it "dynaimcally loads ProgressReporter"
+ (let [reporter (load-reporter "progress")]
(should-not= nil reporter)
- (should= speclj.report.console.ConsoleReporter (class reporter))))
+ (should= speclj.report.progress.ProgressReporter (class reporter))))
(it "dynaimcally loads SilentReporter"
(let [reporter (load-reporter "silent")]
@@ -56,7 +68,37 @@
(it "throws exception with unrecognized reporter"
(should-throw Exception "Failed to load reporter: blah" (load-reporter "blah")))
- )
+ (it "uses formatter as an alias to reporter"
+ (let [options (parse-args "--format" "silent")]
+ (should= "silent" (:reporter options))))
+
+ (it "parses the --version switch"
+ (should= nil (:version (parse-args "")))
+ (should= "on" (:version (parse-args "--version")))
+ (should= "on" (:version (parse-args "-v"))))
+
+ (it "handles the --version switch"
+ (should= 0 (run "--version"))
+ (should= (str "speclj " speclj.version/string endl) (to-s @output)))
+
+ (it "parses the --help switch"
+ (should= nil (:help (parse-args "")))
+ (should= "on" (:help (parse-args "--help")))
+ (should= "on" (:help (parse-args "-h"))))
+
+ (it "handles the --version switch"
+ (should= 0 (run "--help"))
+ (should-not= -1 (.indexOf (to-s @output) "Usage")))
+
+ (it "parses and translates the --autotest option"
+ (let [options (parse-args "--autotest")]
+ (should= "vigilant" (:runner options))
+ (should= "specdoc" (:reporter options)))
+ (let [options (parse-args "-a")]
+ (should= "vigilant" (:runner options))
+ (should= "specdoc" (:reporter options))))
+
+ )
(conclude-single-file-run)
@@ -1,7 +1,7 @@
-(ns speclj.report.console-spec
+(ns speclj.report.progress-spec
(:use
[speclj.core]
- [speclj.report.console :only (new-console-reporter)]
+ [speclj.report.progress :only (new-progress-reporter)]
[speclj.reporting]
[speclj.exec :only (pass-result fail-result)]
[speclj.components :only (new-description new-characteristic)]
@@ -13,10 +13,10 @@
(defn to-s [output]
(String. (.toByteArray output)))
-(describe "Console Reporter"
+(describe "Progress Reporter"
(with output (ByteArrayOutputStream.))
(with writer (OutputStreamWriter. @output))
- (with reporter (new-console-reporter))
+ (with reporter (new-progress-reporter))
(around [spec] (binding [*out* @writer] (spec)))
(it "reports pass"
@@ -63,17 +63,17 @@
(should= "1)" (lines 2))
(should= "'Crazy flips' FAILED" (lines 3))
(should= "Expected flips" (lines 4))
-; (should= "/Users/micahmartin/Projects/clojure/speclj/spec/speclj/report/console_spec.clj:54" (lines 5))
+; (should= "/Users/micahmartin/Projects/clojure/speclj/spec/speclj/report/progress_spec.clj:54" (lines 5))
(should= "" (lines 6))
(should= "2)" (lines 7))
(should= "'Crazy spins' FAILED" (lines 8))
(should= "Expected spins" (lines 9))
-; (should= "/Users/micahmartin/Projects/clojure/speclj/spec/speclj/report/console_spec.clj:55" (lines 10))
+; (should= "/Users/micahmartin/Projects/clojure/speclj/spec/speclj/report/progress_spec.clj:55" (lines 10))
(should= "" (lines 11))
(should= "3)" (lines 12))
(should= "'Crazy dives' FAILED" (lines 13))
(should= "Expected dives" (lines 14))
-; (should= "/Users/micahmartin/Projects/clojure/speclj/spec/speclj/report/console_spec.clj:56" (lines 15))
+; (should= "/Users/micahmartin/Projects/clojure/speclj/spec/speclj/report/progress_spec.clj:56" (lines 15))
(should= "" (lines 16))
(should= "Finished in 0.32100 seconds" (lines 17))
(should= "" (lines 18))
@@ -1,7 +1,7 @@
-(ns speclj.report.verbose-spec
+(ns speclj.report.specdoc-spec
(:use
[speclj.core]
- [speclj.report.verbose :only (new-verbose-reporter)]
+ [speclj.report.specdoc :only (new-specdoc-reporter)]
[speclj.reporting]
[speclj.exec :only (pass-result fail-result)]
[speclj.components :only (new-description new-characteristic)]
@@ -14,10 +14,10 @@
(defn to-s [output]
(String. (.toByteArray output)))
-(describe "Verbose Reporter"
+(describe "Speccdoc Reporter"
(with output (ByteArrayOutputStream.))
(with writer (OutputStreamWriter. @output))
- (with reporter (new-verbose-reporter))
+ (with reporter (new-specdoc-reporter))
(with description (new-description "Verbosity"))
(around [spec] (binding [*out* @writer] (spec)))
View
@@ -26,7 +26,7 @@
<orderEntry type="module-library">
<library>
<CLASSES>
- <root url="jar://$MODULE_DIR$/lib/clojure-contrib-1.2.0.jar!/" />
+ <root url="file://$MODULE_DIR$/spec" />
</CLASSES>
<JAVADOC />
<SOURCES />
@@ -35,7 +35,7 @@
<orderEntry type="module-library">
<library>
<CLASSES>
- <root url="file://$MODULE_DIR$/spec" />
+ <root url="jar://$MODULE_DIR$/lib/mmargs-1.2.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
View
@@ -8,6 +8,5 @@
[java.io File]))
(defn spec [project & args]
-; (let [exec-form `(~'do (~'use 'speclj.main)(~'run '~@args))]
-; (eval-in-project project exec-form nil nil nil)))
- (eval-in-project project `(~'run ~@args) nil nil `(use 'speclj.main)))
+ (let [exec-form `(binding [~'invoke-method "lein spec"] (~'run ~@args))]
+ (eval-in-project project exec-form nil nil `(use 'speclj.main))))
View
@@ -6,7 +6,7 @@
[speclj.util :only (endl)])
(:require
[speclj.run.standard]
- [speclj.report.console])
+ [speclj.report.progress])
(:import [speclj SpecFailure]))
(defmacro it [name & body]
View
@@ -1,24 +1,75 @@
(ns speclj.main
(:use
- [speclj.running :only (run-directories report)]))
+ [speclj.running :only (run-directories report)]
+ [speclj.util :only (endl)])
+ (:require
+ [speclj.version])
+ (:import
+ [mmargs Arguments]))
(def default-config {
- :spec-dirs ["spec"]
+ :specs ["spec"]
:runner "standard"
- :reporter "console"
+ :reporter "progress"
})
-(defn- parse-arg [config arg]
+(def invoke-method "clj speclj.main")
+
+(def arg-spec (Arguments.))
+(doto arg-spec
+ (.addMultiParameter "specs" "directories specifying which specs to run.")
+ (.addValueOption "r" "runner" "RUNNER" (str "Use a custom Runner." endl
+ endl
+ "Builtin runners:" endl
+ "standard : (default) Runs all the specs once" endl
+ "vigilant : Watches for file changes and re-runs affected specs (used by autotest)" endl))
+ (.addValueOption "f" "reporter" "REPORTER" (str "Specifies how to report spec results. Ouput will be written to *out*." endl
+ endl
+ "Builtin reporters:" endl
+ "silent : No output" endl
+ "progress : (default) Text-based progress bar" endl
+ "specdoc : Code example doc strings" endl))
+ (.addValueOption "f" "format" "FORMAT" "An alias for reporter.")
+ (.addSwitchOption "a" "autotest" "Alias to use the 'vigilant' runner and 'specdoc' reporter.")
+ (.addSwitchOption "v" "version" "Shows the current speclj version.")
+ (.addSwitchOption "h" "help" "You're looking at it.")
+ )
+
+(defn- resolve-aliases [options]
(cond
- (.startsWith arg "--runner=") (assoc config :runner (.substring arg (count "--runner=")))
- (.startsWith arg "--reporter=") (assoc config :reporter (.substring arg (count "--reporter=")))
- :else (assoc config :spec-dirs (conj (vec (:spec-dirs config)) arg))))
+ (:format options) (recur (dissoc (assoc options :reporter (:format options)) :format))
+ (:autotest options) (recur (dissoc (assoc options :runner "vigilant" :reporter "specdoc") :autotest))
+ :else options))
+
+(defn exit [code]
+ (System/exit code))
+
+(defn usage [errors]
+ (if (seq errors)
+ (do
+ (println "ERROR!!!")
+ (doseq [error (seq errors)]
+ (println error))))
+ (println)
+ (println "Usage: " invoke-method (.argString arg-spec))
+ (println)
+ (println (.parametersString arg-spec))
+ (println (.optionsString arg-spec))
+ (if (seq errors)
+ (exit -1)
+ (exit 0)))
+
+(defn print-version []
+ (println speclj.version/summary)
+ (exit 0))
(defn parse-args [& args]
- (loop [config {} args (filter identity args)]
- (if (not (seq args))
- (merge default-config config)
- (recur (parse-arg config (first args)) (rest args)))))
+ (let [parse-result (.parse arg-spec (into-array String args))
+ options (reduce (fn [result entry] (assoc result (keyword (.getKey entry)) (.getValue entry))) {} parse-result)
+ options (resolve-aliases options)]
+ (if-let [errors (options :*errors)]
+ (usage errors)
+ (merge default-config options))))
(defn load-runner [name]
(let [ns-name (symbol (str "speclj.run." name))
@@ -36,13 +87,19 @@
(eval expr)
(catch Exception e (throw (Exception. (str "Failed to load reporter: " name) e))))))
-(defn run [& args]
- (let [config (apply parse-args args)
- runner (load-runner (:runner config))
+(defn run-specs [config]
+ (let [runner (load-runner (:runner config))
reporter (load-reporter (:reporter config))
- spec-dirs (:spec-dirs config)
+ spec-dirs (:specs config)
fail-count (run-directories runner spec-dirs reporter)]
- (System/exit fail-count)))
+ (exit fail-count)))
+
+(defn run [& args]
+ (let [config (apply parse-args args)]
+ (cond
+ (:version config) (print-version)
+ (:help config) (usage nil)
+ :else (run-specs config))))
(if *command-line-args*
(let [args *command-line-args*]
@@ -1,4 +1,4 @@
-(ns speclj.report.console
+(ns speclj.report.progress
(:use
[speclj.reporting :only (failure-source tally-time default-reporter)]
[speclj.exec :only (pass? fail?)])
@@ -40,8 +40,10 @@
(print-duration results)
(print-tally results))
-(deftype ConsoleReporter []
+(deftype ProgressReporter []
Reporter
+ (report-message [this message]
+ (println message))
(report-description [this description])
(report-pass [this result]
(print ".") (flush))
@@ -50,7 +52,7 @@
(report-runs [this results]
(print-summary results)))
-(defn new-console-reporter []
- (ConsoleReporter.))
+(defn new-progress-reporter []
+ (ProgressReporter.))
-(swap! default-reporter (fn [_] (new-console-reporter)))
+(swap! default-reporter (fn [_] (new-progress-reporter)))
@@ -6,9 +6,10 @@
(deftype SilentReporter [passes fails results]
Reporter
+ (report-message [this message])
(report-description [this description])
- (report-pass [this characteristic])
- (report-fail [this characteristic])
+ (report-pass [this result])
+ (report-fail [this result])
(report-runs [this results]))
(defn new-silent-reporter []
Oops, something went wrong.

0 comments on commit 662d58d

Please sign in to comment.