Skip to content

Commit

Permalink
Fully-functioning 'focus' feature.
Browse files Browse the repository at this point in the history
Tree of descriptions and characteristics is recursively scanned just
before run-time to determine if any components have been 'focused'.
  • Loading branch information
mdwhatcott committed Mar 19, 2022
1 parent 2dadd3c commit 5e5aad5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/focus/focus.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
(focus-context "a"
(it "yes-1" (should false))
(focus-it "yes-2" (should false))
(context "b"
(context "aa"
(it "yes-3" (should false))))
(it "no" (should false)))

Expand Down
4 changes: 2 additions & 2 deletions spec/speclj/core_spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
(focus-context "a"
(it "yes-1" (should true))
(focus-it "yes-2" (should true))
(context "b"
(context "aa"
(it "yes-3" (should true))))
(it "no" (should true)))

Expand All @@ -30,7 +30,7 @@
(it "yes-5" (should true))
(it "yes-6" (should true)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(describe "The basic spec structure"
(tags :one)
Expand Down
2 changes: 1 addition & 1 deletion spec/speclj/report/documentation_spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(#?(:clj :require :cljs :require-macros)
[speclj.core :refer [before context describe it should= with -new-exception -new-failure -new-pending]])
(:require #?(:cljs [goog.string]) ;cljs bug?
[speclj.components :refer [new-description new-characteristic install focus!]]
[speclj.components :refer [new-description new-characteristic install]]
[speclj.platform :refer [endl]]
[speclj.report.documentation :refer [new-documentation-reporter]]
[speclj.reporting :refer [report-description report-pass report-pending
Expand Down
2 changes: 1 addition & 1 deletion spec/speclj/run/standard_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
(should= 8 (run-directories @runner [failures-dir] @reporters)))

(it "limits execution to focused components"
(should= 6 (run-directories @runner [focus-dir] @reporters))
(run-directories @runner [focus-dir] @reporters)
(should= ["yes-1" "yes-2" "yes-3" "yes-4" "yes-5" "yes-6"]
(->> @(.-results @runner)
(map #(.-characteristic %))
Expand Down
38 changes: 7 additions & 31 deletions src/speclj/components.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -29,58 +29,34 @@
object
(install [this description] (comment "Whatever... Let them pass."))))

(defn enable-focus-mode [component]
(reset! (.-has-focus? component) true)
(when-let [parent @(.-parent component)]
(recur parent)))

(defn focused? [component]
(when component @(.-is-focused? component)))

(defn focus! [component]
(reset! (.-is-focused? component) true))

(deftype Description [name is-focused? has-focus? ns parent children characteristics tags befores before-alls afters after-alls withs with-alls arounds around-alls]
SpecComponent
(install [this description]
(reset! (.-parent this) description)
(swap! (.-children description) conj this)
(when (focused? this) (enable-focus-mode description))
(when (focused? description) (focus! this)))
(swap! (.-children description) conj this))
Object
(toString [this] (str "Description: " \" name \")))

(defn new-description [name is-focused? ns]
(Description. name (atom is-focused?) (atom false) ns (atom nil) (atom []) (atom []) (atom #{}) (atom []) (atom []) (atom []) (atom []) (atom []) (atom []) (atom []) (atom [])))

(defn is-description? [component]
(instance? Description component))

(deftype Characteristic [name parent body is-focused?]
SpecComponent
(install [this description]
(reset! (.-parent this) description)
(swap! (.-characteristics description) conj this)
(when (focused? this) (enable-focus-mode description))
(when (focused? description) (focus! this)))
(swap! (.-characteristics description) conj this))
Object
(toString [this] (str \" name \")))

(defn new-characteristic
([name body is-focused?] (Characteristic. name (atom nil) body (atom is-focused?)))
([name description body is-focused?] (Characteristic. name (atom description) body (atom is-focused?))))

(defn has-focus? [component]
(when (and component (instance? Description component))
@(.-has-focus? component)))

(defn focus-mode? [component]
(or (focused? component)
(has-focus? component)
(when-let [parent @(.-parent component)]
(recur parent))))

(defn can-run? [component]
(or (focused? component)
(has-focus? component)
(not (focus-mode? component))))
(defn is-characteristic? [component]
(instance? Characteristic component))

(deftype Before [body]
SpecComponent
Expand Down
5 changes: 2 additions & 3 deletions src/speclj/report/documentation.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns speclj.report.documentation
(:require [speclj.components :refer [focused?]]
[speclj.platform :refer [error-message]]
(:require [speclj.platform :refer [error-message]]
[speclj.report.progress :refer [print-summary]]
[speclj.reporting :refer [green indent red yellow]]))

Expand All @@ -11,7 +10,7 @@
level)))

(defn maybe-focused [component text]
(if-not (focused? component) text (str text " " (yellow "[FOCUS]"))))
(if-not @(.-is-focused? component) text (str text " " (yellow "[FOCUS]"))))

(deftype DocumentationReporter []
speclj.reporting/Reporter
Expand Down
67 changes: 65 additions & 2 deletions src/speclj/running.cljc
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
(ns speclj.running
(:require [clojure.string :as str]
[speclj.components :refer [focus-mode? can-run? reset-with]]
[speclj.components :as components]
[speclj.config :refer [active-reporters]]
[speclj.platform :refer [current-time pending? secs-since]]
[speclj.reporting :refer [report-description* report-run]]
[speclj.results :refer [error-result fail-result pass-result pending-result]]
[speclj.tags :refer [pass-tag-filter? tag-sets-for tags-for]]))

(defn focusable? [component]
(and (some? component)
(or (components/is-description? component)
(components/is-characteristic? component))))

(defn focused? [component]
@(.-is-focused? component))

(defn has-focus? [component]
(and (components/is-description? component)
@(.-has-focus? component)))

(defn focus-mode? [component]
(or (focused? component)
(has-focus? component)
(when-let [parent @(.-parent component)]
(recur parent))))

(defn can-run? [component]
(or (focused? component)
(has-focus? component)
(not (focus-mode? component))))

(defn all-children [component]
(if (components/is-description? component)
(concat @(.-characteristics component) @(.-children component))
[]))

(defn focus! [component]
(reset! (.-is-focused? component) true))

(defn focus-characteristics! [component]
(focus! component)
(doall (map focus! @(.-characteristics component))))

(defn focus-children! [component]
(focus! component)
(doall (map focus-children! @(.-children component))))

(defn enable-focus-mode! [component]
(when-let [parent @(.-parent component)]
(reset! (.-has-focus? parent) true)
(recur parent)))

(defn track-focused-descriptions! [descriptions]
(doseq [component descriptions]
(when (focused? component)
(enable-focus-mode! component)
(focus-children! component)
(focus-characteristics! component))))

(defn track-focused-characteristics! [characteristics]
(doseq [characteristic characteristics
:when (focused? characteristic)]
(enable-focus-mode! characteristic)))

(defn scan-for-focus! [description]
(let [all (->> (tree-seq some? all-children description))]
(track-focused-descriptions! (filter components/is-description? all))
(track-focused-characteristics! (filter components/is-characteristic? all))
description))

(defn filter-focused [descriptions]
(doseq [description descriptions] (scan-for-focus! description))
(or (seq (filter focus-mode? descriptions)) descriptions))

(defn- eval-components [components]
Expand All @@ -26,7 +89,7 @@
(eval-components afters))))

(defn- reset-withs [withs]
(doseq [with withs] (reset-with with)))
(doseq [with withs] (components/reset-with with)))

(defn- collect-components [getter description]
(loop [description description components []]
Expand Down

0 comments on commit 5e5aad5

Please sign in to comment.