-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redact fully-matched data-structures from output (#214)
* attach meta to seq/maps that don't match * use mismatch metadata to elide matched results when printing * add enabling/disabling of output redaction of matched data-structures users can call `(matcher-combinators.config/enable-redaction!)` in their test startup to enable redaction * create matcher-combinators.config and move ansi & redact controls there * include ... ellision element when printing redacted results e.g. for maps: ``` {:a [{:b [(mismatch (expected 5) (actual 6))]}] ... } ``` and for sequences: ``` [{:b [(mismatch (expected 5) (actual 6))]}] ...] ```
- Loading branch information
1 parent
5799704
commit 3df9b17
Showing
9 changed files
with
219 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ pom.xml.asc | |
.hg/ | ||
.clj-kondo/* | ||
.shadow-cljs/* | ||
.cpcache/* | ||
.lsp/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
(ns matcher-combinators.config | ||
"Global output behavior configurations" | ||
(:require [matcher-combinators.ansi-color :as ansi-color])) | ||
|
||
;; Abbreviating match results to only include mismatched data in the output | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
(def ^{:dynamic true | ||
:doc "thread-local way to control, via `binding`, the abbreviation of fully-matched data-structures in the matcher-combinator output"} | ||
*use-abbreviation* | ||
false) | ||
|
||
(defn- set-use-abbreviation! | ||
"internal function, use matcher-combinators.config/{enable|disable}-abbreviation!" | ||
[v] | ||
#?(:clj (alter-var-root #'*use-abbreviation* (constantly v)) | ||
:cljs (set! *use-abbreviation* v))) | ||
|
||
(defn enable-abbreviation! | ||
"**Experimental, subject to change** | ||
Thread-global way to enable the abbreviation of fully-matched data-structures in matcher-combinator output." | ||
[] | ||
(set-use-abbreviation! true)) | ||
|
||
(defn disable-abbreviation! | ||
"**Experimental, subject to change** | ||
Thread-global way to disable the abbreviation of fully-matched data-structures in matcher-combinator output." | ||
[] | ||
(set-use-abbreviation! false)) | ||
|
||
|
||
;; Disable special ANSI color characters in output | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
(defn- set-use-color! [v] | ||
#?(:clj (alter-var-root #'ansi-color/*use-color* (constantly v)) | ||
:cljs (set! ansi-color/*use-color* v))) | ||
|
||
(defn enable-ansi-color! | ||
"Thread-global way to enable the usage of ANSI color codes in matcher-combinator output." | ||
[] | ||
(set-use-color! true)) | ||
|
||
(defn disable-ansi-color! | ||
"Thread-global way to disable the usage of ANSI color codes in matcher-combinator output." | ||
[] | ||
(set-use-color! false)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
(ns matcher-combinators.config-test | ||
(:require [clojure.test :refer [deftest is testing use-fixtures]] | ||
[colorize.core :as colorize] | ||
[matcher-combinators.config :as config] | ||
[matcher-combinators.core :as c] | ||
[matcher-combinators.printer :as printer] | ||
matcher-combinators.test)) | ||
|
||
(defn set-config-defaults! [] | ||
(config/enable-ansi-color!) | ||
(config/disable-abbreviation!)) | ||
|
||
(use-fixtures :each | ||
(fn [t] | ||
(set-config-defaults!) | ||
(t) | ||
(set-config-defaults!))) | ||
|
||
(deftest ansi-color-test | ||
(testing "with color" | ||
(is (= (str "(unexpected " (colorize/red 1) ")\n") | ||
(printer/as-string (list 'unexpected (printer/->ColorTag :red 1)))))) | ||
(testing "disable coloring" | ||
(config/disable-ansi-color!) | ||
(is (= (str "(unexpected 1)\n") | ||
(printer/as-string (list 'unexpected (printer/->ColorTag :red 1))))))) | ||
|
||
(deftest abbreviated-matched-output-test | ||
(is (= (str "[1\n 2\n {:a 2,\n :b [4 (mismatch (expected " (colorize/yellow 5) ") (actual " (colorize/red 6) "))],\n :c [2 [3 4]]}]\n") | ||
(printer/as-string | ||
(:matcher-combinators.result/value | ||
(c/match [1 2 {:a 2 :b [4 5] :c [2 [3 4]]}] | ||
[1 2 {:a 2 :b [4 6] :c [2 [3 4]]}]))))) | ||
|
||
(config/enable-abbreviation!) | ||
(is (= (str "{:stuff [{:b [(mismatch (expected " (colorize/yellow 5) ") (actual " (colorize/red 6) "))]}],\n ... }\n") | ||
(printer/as-string | ||
(:matcher-combinators.result/value | ||
(c/match {:stuff [1 2 {:a 2 :b [4 5] :c [2 [3 4]]}]} | ||
{:stuff [1 2 {:a 2 :b [4 6] :c [2 [3 4]]}]}))))) | ||
|
||
(is (= (str "[{:b [(mismatch (expected " (colorize/yellow 5) ") (actual " (colorize/red 6) "))]} ...]\n") | ||
(printer/as-string | ||
(:matcher-combinators.result/value | ||
(c/match [1 2 {:a 2 :b [4 5] :c [2 [3 4]]}] | ||
[1 2 {:a 2 :b [4 6] :c [2 [3 4]]}])))))) |