├── .circleci └── config.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── doc ├── images │ └── colorized-pinpoint-result.png └── intro.md ├── project.clj ├── scripts ├── repl └── repl.clj ├── src └── pinpointer │ ├── core.cljc │ ├── formatter.cljc │ └── trace.cljc └── test ├── cljc └── pinpointer │ ├── core_test.cljc │ ├── formatter_test.cljc │ └── trace_test.cljc └── cljs └── pinpointer └── runner.cljs /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | - image: athos/cljc-dev:openjdk-8-alpine-20190219-0 6 | 7 | working_directory: ~/repo 8 | 9 | environment: 10 | LEIN_ROOT: "true" 11 | JVM_OPTS: -Xmx3200m 12 | 13 | steps: 14 | - checkout 15 | 16 | # Download and cache dependencies 17 | - restore_cache: 18 | key: v1-dependencies-{{ checksum "project.clj" }} 19 | 20 | - run: lein deps 21 | 22 | - save_cache: 23 | paths: 24 | - ~/.m2 25 | key: v1-dependencies-{{ checksum "project.clj" }} 26 | 27 | - run: 28 | name: test CLJ 29 | command: lein test-clj 30 | 31 | - run: 32 | name: test CLJS 33 | command: lein test-cljs 34 | 35 | - run: 36 | name: code-coverage 37 | command: | 38 | CLOVERAGE_VERSION=1.0.9 lein cloverage --codecov 39 | bash <(curl -s https://codecov.io/bash) -f target/coverage/codecov.json 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). 3 | 4 | ## [Unreleased] 5 | 6 | ## [0.1.1] - 2019-08-10 7 | ### Fixed 8 | - [Update Fipp to 0.6.18](https://github.com/athos/Pinpointer/commit/ac4adf16aa232df23177b7608fec03f7d0fc9c54) to support JDK11+ 9 | 10 | ## 0.1.0 - 2017-12-10 11 | - First released. 12 | 13 | [Unreleased]: https://github.com/athos/pinpointer/compare/0.1.1...HEAD 14 | [0.1.1]: https://github.com/athos/pinpointer/compare/0.1.0...0.1.1 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pinpointer 2 | [![Clojars Project](https://img.shields.io/clojars/v/pinpointer.svg)](https://clojars.org/pinpointer) 3 | [![CircleCI](https://circleci.com/gh/athos/Pinpointer/tree/master.svg?style=shield)](https://circleci.com/gh/athos/Pinpointer/tree/master) 4 | [![codecov](https://codecov.io/gh/athos/Pinpointer/branch/master/graph/badge.svg)](https://codecov.io/gh/athos/Pinpointer) 5 | [![join the chat at https://gitter.im/athos/pinpointer](https://badges.gitter.im/athos/pinpointer.svg)](https://gitter.im/athos/pinpointer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 6 | 7 | Pinpointer is yet another clojure.spec error reporter based on a precise error analysis. 8 | 9 | It has the following features: 10 | 11 | - Visually pinpoints which portion of the input data is causing the spec error, based on the spec error analysis of [`spectrace`](https://github.com/athos/spectrace), a fine-grained spec error analyzer 12 | - Formats and colorizes the error reports in an easy-to-grasp manner 13 | - Tracks 'value changes', i.e. reports the spec errors correctly even when `s/conformer` in the spec transforms the input data 14 | - Extensible to user-defined spec macros (not documented yet) 15 | 16 | **Notice**: Pinpointer is built on top of clojure.spec, which is one of Clojure's new features that have been developed most actively. So, it's still in alpha and its APIs are also subject to change. 17 | 18 | ## Installation 19 | 20 | Add the following to your `:dependencies`: 21 | 22 | [![Clojars Project](https://clojars.org/pinpointer/latest-version.svg)](http://clojars.org/pinpointer) 23 | 24 | ## Why and how to use it 25 | 26 | clojure.spec provides an API named `explain`, which describes which portion of the input data caused a spec error: 27 | 28 | ```clj 29 | => (s/def ::x integer?) 30 | :user/x 31 | => (s/def ::y string?) 32 | :user/y 33 | => (s/explain (s/keys :req-un [::x ::y]) {:y 1}) 34 | In: [:y] val: 1 fails spec: :user/y at: [:y] predicate: string? 35 | val: {:y 1} fails predicate: (contains? % :x) 36 | nil 37 | => 38 | ``` 39 | 40 | As you can see above, the result of `explain` is simple and plain, but it is often not easy to understand intuitively what was wrong. And it will take longer time to find out where the actual problem is as the spec and input data are getting larger. 41 | 42 | ### pinpoint: replacement of s/explain 43 | 44 | _Pinpointer_ provides an API compatible with `explain`, which is named `pinpoint`, and it shows the spec errors in a visually easy-to-grasp manner: 45 | 46 | ```clj 47 | => (require '[pinpointer.core :as p]) 48 | nil 49 | => (p/pinpoint (s/keys :req-un [::x ::y]) {:y 1}) 50 | Detected 2 spec errors: 51 | ---------------------------------------------------------------------- 52 | (1/2) 53 | 54 | Cause: {:y 1} 55 | ^^^^^^ 56 | Expected: (fn [%] (contains? % :x)) 57 | 58 | ---------------------------------------------------------------------- 59 | (2/2) 60 | 61 | Cause: {:y 1} 62 | ^ 63 | Expected: string? 64 | 65 | ---------------------------------------------------------------------- 66 | nil 67 | => 68 | ``` 69 | 70 | 71 | You can also colorize the error reports by adding the option `{:colorize :ansi}` to increase the readability: 72 | 73 | 74 | 75 | 76 | `pinpoint` has several other options. See the docstring for more details. 77 | 78 | ### pinpoint-out: plugin implementation for s/\*explain-out\* 79 | 80 | If you'd rather completely replace the `explain` facility for any kinds of spec error reporting, it would be helpful to replace `s/*explain-out*` with `pinpointer.core/pinpoint-out` instead: 81 | 82 | ```clj 83 | => (set! s/*explain-out* p/pinpoint-out) 84 | #function[pinpointer.core/pinpoint-out] 85 | => 86 | ;; from now on, p/pinpoint-out will be used in place of s/explain-printer 87 | => 88 | => (defn f [x] (inc x)) 89 | #'user/f 90 | => (s/fdef f 91 | :args (s/cat :x (s/and integer? even?)) 92 | :ret (s/and integer? odd?)) 93 | user/f 94 | => (require '[clojure.spec.test.alpha :as t]) 95 | nil 96 | => (t/instrument) 97 | [user/f] 98 | => (f 3) 99 | ExceptionInfo Call to #'user/f did not conform to spec: 100 | Detected 1 spec error: 101 | ---------------------------------------------------------------------- 102 | (1/1) 103 | 104 | Cause: (3) 105 | ^ 106 | Expected: even? 107 | 108 | ---------------------------------------------------------------------- 109 | user/eval3842 (form-init1169392389971828339.clj:1) 110 | clojure.core/ex-info (core.clj:4744) 111 | => 112 | ``` 113 | 114 | ## ClojureScript support 115 | 116 | Pinpointer also supports ClojureScript. Note, however, that it may use `eval` for its spec error analysis in general (especially, when analyzing specs with a literal fn in them) while ClojureScript doesn't have a platform-independent `eval` fn. 117 | 118 | If you use Pinpointer from a self-hosted ClojureScript implementation equipped with `eval` such as Planck or Lumo, it should work fine even in general cases as follows: 119 | 120 | ```clj 121 | => (require '[planck.core :as planck]) 122 | nil 123 | => (p/pinpoint (s/and integer? #(> % 10)) 5 {:eval planck/eval}) 124 | Detected 1 spec error: 125 | ---------------------------------------------------------------------- 126 | (1/1) 127 | 128 | Cause: 5 129 | ^ 130 | Expected: (fn [%] (> % 10)) 131 | 132 | ---------------------------------------------------------------------- 133 | nil 134 | => 135 | ``` 136 | 137 | ## Known Issues 138 | 139 | There are a couple of known issues in Pinpointer, primarily due to `clojure.spec`'s bugs. They can be found on [Issues page](https://github.com/athos/Pinpointer/issues?q=is%3Aissue+is%3Aopen+label%3A%22spec+bug%22), being tagged with `spec bug`. 140 | 141 | If you found something wrong when using Pinpointer and you want to report an issue, check whether or not it's already been filed there first. 142 | 143 | ## License 144 | 145 | Copyright © 2016-2018 Shogo Ohta ([@athos0220](https://twitter.com/athos0220)) 146 | 147 | Distributed under the Eclipse Public License 1.0. 148 | -------------------------------------------------------------------------------- /doc/images/colorized-pinpoint-result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/athos/Pinpointer/30ddfb2afb467423370960973202e44fa4e956eb/doc/images/colorized-pinpoint-result.png -------------------------------------------------------------------------------- /doc/intro.md: -------------------------------------------------------------------------------- 1 | # Introduction to pinpointer 2 | 3 | TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) 4 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject pinpointer "0.1.2-SNAPSHOT" 2 | :description "Pinpointer is yet another spec error reporter based on a precise error analysis" 3 | :url "https://github.com/athos/Pinpointer" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | 7 | :test-paths ["test/cljc"] 8 | 9 | :dependencies [[org.clojure/clojure "1.9.0" :scope "provided"] 10 | [org.clojure/clojurescript "1.9.946" :scope "provided"] 11 | [clansi "1.0.0"] 12 | [fipp "0.6.18"] 13 | [spectrace "0.1.0"]] 14 | 15 | :plugins [[lein-cloverage "1.0.9"] 16 | [lein-cljsbuild "1.1.4"] 17 | [lein-doo "0.1.8"] 18 | [lein-eftest "0.3.1"]] 19 | 20 | :cljsbuild {:builds [{:id "test" 21 | :source-paths ["src" "test/cljc" "test/cljs"] 22 | :compiler {:output-to "target/out/test.js" 23 | :output-dir "target/out" 24 | :main pinpointer.runner 25 | :optimizations :none}} 26 | {:id "node-test" 27 | :source-paths ["src" "test/cljc" "test/cljs"] 28 | :compiler {:output-to "target/node_out/test.js" 29 | :output-dir "target/node_out" 30 | :main pinpointer.runner 31 | :optimizations :none 32 | :target :nodejs}}]} 33 | 34 | :eftest {:report eftest.report.pretty/report} 35 | 36 | :profiles 37 | {:dev {:dependencies [[org.clojure/test.check "0.10.0-alpha2"]]}} 38 | 39 | :aliases {"test-all" ["do" ["test-clj"] ["test-cljs"]] 40 | "test-clj" ["eftest"] 41 | "test-cljs" ["doo" "node" "node-test" "once"]}) 42 | -------------------------------------------------------------------------------- /scripts/repl: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | lein trampoline run -m clojure.main scripts/repl.clj 4 | -------------------------------------------------------------------------------- /scripts/repl.clj: -------------------------------------------------------------------------------- 1 | (require '[cljs.repl :as repl] 2 | '[cljs.repl.node :as node]) 3 | 4 | (repl/repl* (node/repl-env) 5 | {:output-dir "target/compiled/js/out" 6 | :optimizations :none 7 | :cache-analysis true 8 | :source-map true}) 9 | -------------------------------------------------------------------------------- /src/pinpointer/core.cljc: -------------------------------------------------------------------------------- 1 | (ns pinpointer.core 2 | (:require #?(:clj [clansi]) 3 | [clojure.spec.alpha :as s] 4 | [clojure.spec.test.alpha :as t] 5 | [clojure.string :as str] 6 | [clojure.walk :as walk] 7 | [fipp.clojure :as fipp] 8 | [pinpointer.formatter :as formatter] 9 | [pinpointer.trace :as trace] 10 | [spectrace.core :as strace])) 11 | 12 | (def ^:dynamic ^:private *colorize-fn*) 13 | 14 | (defn- colorize [color s] 15 | (*colorize-fn* color s)) 16 | 17 | (defn- ansi-colorize [color s] 18 | #?(:clj (clansi/style s color) 19 | :cljs s)) 20 | 21 | (defn- none-colorize [_ s] s) 22 | 23 | (def ^:private builtin-colorize-fns 24 | {:ansi ansi-colorize 25 | :none none-colorize}) 26 | 27 | (defn- choose-colorize-fn [colorize] 28 | (let [colorize (or colorize :none)] 29 | (if (keyword? colorize) 30 | (get builtin-colorize-fns colorize none-colorize) 31 | colorize))) 32 | 33 | (defn- times [n c] 34 | (str/join (repeat n c))) 35 | 36 | (defn- space [n] 37 | (times n \space)) 38 | 39 | (defn- pad-left [s n] 40 | (str (space (- n (count s))) s)) 41 | 42 | (defn- wavy-line [n] 43 | (colorize :red (times n \^))) 44 | 45 | (defn- format-line [line hiliting?] 46 | (let [[_ indent line'] (re-matches #"(\s*)(.*)" line) 47 | parts (str/split line' #"\u0000")] 48 | (if (= (count parts) 1) 49 | (if hiliting? 50 | [line (str (space (count indent)) (wavy-line (count line))) true] 51 | [line nil false]) 52 | (loop [parts parts, hiliting? hiliting?, ret [indent], wavy [indent]] 53 | (if (empty? parts) 54 | [(str/join ret) (str/join wavy) (not hiliting?)] 55 | (let [[part & parts] parts] 56 | (if hiliting? 57 | (recur parts (not hiliting?) 58 | (conj ret (colorize :red part)) 59 | (conj wavy (wavy-line (count part)))) 60 | (recur parts (not hiliting?) 61 | (conj ret part) 62 | (conj wavy (space (count part))))))))))) 63 | 64 | (defn- format-data [value trace opts] 65 | (let [lines (binding [formatter/*highlighting-mark* "\u0000"] 66 | (str/split (formatter/format value trace opts) #"\n"))] 67 | (loop [[line & more] lines, hiliting? false, ret []] 68 | (if-not line 69 | ret 70 | (let [[line wavy hiliting?] (format-line line hiliting?)] 71 | (recur more 72 | hiliting? 73 | (cond-> (conj ret line) 74 | wavy (conj wavy)))))))) 75 | 76 | (defn- print-headline [nproblems] 77 | (if (= nproblems 1) 78 | (println "Detected 1 spec error:") 79 | (println "Detected" nproblems "spec errors:"))) 80 | 81 | (defn- hline [width] 82 | (->> (str (times width \-)) 83 | (colorize :cyan) 84 | println)) 85 | 86 | (defn- correct-paths [ed] 87 | (if (::s/args ed) 88 | ;; Probably :path in each problem has extra :args key 89 | (update ed ::s/problems 90 | (fn [problems] (map #(update % :path subvec 1) problems))) 91 | ed)) 92 | 93 | (defn- simplify-spec [spec] 94 | (walk/postwalk (fn [x] 95 | (if (symbol? x) 96 | (condp #(= (namespace %2) %1) x 97 | #?(:clj "clojure.core" :cljs "cljs.core") 98 | (symbol (name x)) 99 | 100 | #?(:clj "clojure.spec.alpha" :cljs "cljs.spec.alpha") 101 | (symbol "s" (name x)) 102 | 103 | x) 104 | x)) 105 | spec)) 106 | 107 | (defn- print-error [total value [i problem trace] opts] 108 | (letfn [(print-with-caption [caption s] 109 | (println (str (pad-left caption 9) \:) s)) 110 | (print-data [caption chunk] 111 | (let [val (:val (first chunk)) 112 | [line & lines] (format-data val chunk opts)] 113 | (print-with-caption caption line) 114 | (doseq [line lines] 115 | (println " " line)))) 116 | (print-spec [caption spec] 117 | (let [[line & lines] (as-> spec it 118 | (simplify-spec it) 119 | (with-out-str (fipp/pprint it opts)) 120 | (str/split it #"\n"))] 121 | (print-with-caption caption line) 122 | (doseq [line lines] 123 | (println " " line))))] 124 | (let [[chunk & more] (rseq trace)] 125 | (println (str "(" (inc i) "/" total ")\n")) 126 | (print-data "Cause" chunk) 127 | (print-spec "Expected" (:pred problem)) 128 | (when-let [reason (:reason problem)] 129 | (println (pad-left "Failure " 9)) 130 | (print-with-caption "Reason" reason)) 131 | (when (seq more) 132 | (doseq [chunk more] 133 | (->> "\n --- This comes originally from ---\n" 134 | (colorize :cyan) 135 | println) 136 | (print-data "Original" chunk) 137 | (println (pad-left "Spec" 9)) 138 | (print-spec "Applied" (:spec (peek chunk)))))))) 139 | 140 | (defonce ^:private last-explain-data (atom nil)) 141 | 142 | (defn pinpoint-out 143 | "A plugin implementation of s/*explain-out*. 144 | 145 | Takes the same options as pinpoint." 146 | ([ed] (pinpoint-out ed {})) 147 | ([ed {:keys [width colorize fallback-on-error eval] :as opts 148 | :or {width 70, fallback-on-error true}}] 149 | (if ed 150 | (let [{:keys [::s/problems ::s/value] :as ed'} (correct-paths ed) 151 | nproblems (count problems) 152 | traces (try 153 | (binding [strace/*eval-fn* (or eval strace/*eval-fn*)] 154 | (trace/traces ed')) 155 | (catch #?(:clj Throwable :cljs :default) e e))] 156 | (cond (vector? traces) 157 | (let [opts (-> opts 158 | (assoc :width (max (- width 11) 0)) 159 | (dissoc :colorize :fallback-on-error))] 160 | (reset! last-explain-data ed) 161 | (binding [*colorize-fn* (choose-colorize-fn colorize)] 162 | (print-headline nproblems) 163 | (hline width) 164 | (doseq [t (map vector (range) problems traces)] 165 | (print-error nproblems value t opts) 166 | (newline) 167 | (hline width)) 168 | (when-let [caller (::t/caller ed')] 169 | (println (str " " (:var-scope caller) 170 | " (" (:file caller) 171 | ":" (:line caller) ")"))))) 172 | 173 | fallback-on-error 174 | (do (println "[PINPOINTER] Failed to analyze the spec errors, and will fall back to s/explain-printer\n") 175 | (s/explain-printer ed)) 176 | 177 | :else (throw traces))) 178 | (println "Success!!")))) 179 | 180 | (defn pinpoint 181 | "Given a spec and a value that fails to conform, reports the spec 182 | error(s) in a human-friendly manner. 183 | 184 | The opts map may have the following keys: 185 | :width - Number of columns to try to wrap the report at. 186 | Defaults to 70. 187 | :colorize - Can either be a keyword :ansi, :none or a fn that 188 | takes a color keyword and a string, and returns the colorized 189 | string. Defaults to :none. 190 | :fallback-on-error - If set to true, falls back to 191 | s/explain-printer in case of an error during the analysis. 192 | Otherwise, rethrows the error. Defaults to true. 193 | :eval - eval fn to be used to analyze spec errors. Defaults to 194 | clojure.core/eval in Clojure, nil in ClojureScript." 195 | ([spec x] (pinpoint spec x {})) 196 | ([spec x opts] 197 | (pinpoint-out (s/explain-data spec x) opts))) 198 | 199 | (defn replay 200 | ([] (replay {})) 201 | ([opts] 202 | (pinpoint-out @last-explain-data opts))) 203 | 204 | #?(:clj 205 | (defn- find-spec-error [^Throwable t] 206 | (when t 207 | (let [data (ex-data t)] 208 | (if (and data (::s/problems data)) 209 | t 210 | (recur (.getCause t)))))) 211 | 212 | :cljs 213 | (defn- find-spec-error [e] 214 | (when (some-> e ex-data ::s/problems) 215 | e))) 216 | 217 | (defn ppt 218 | ([] (ppt {})) 219 | ([opts] 220 | (when-let [e (find-spec-error *e)] 221 | (pinpoint-out (ex-data e) opts)))) 222 | -------------------------------------------------------------------------------- /src/pinpointer/formatter.cljc: -------------------------------------------------------------------------------- 1 | (ns pinpointer.formatter 2 | #?(:clj (:refer-clojure :exclude [format])) 3 | (:require [clojure.spec.alpha :as s] 4 | [fipp.edn :as edn] 5 | [fipp.engine :as fipp] 6 | [fipp.visit :as visit])) 7 | 8 | (def ^:dynamic *highlighting-mark* "!!!") 9 | 10 | (defmulti render 11 | (fn [{:keys [spec]} printer x] (when (seq? spec) (first spec)))) 12 | (defmethod render :default [_ printer x] 13 | (let [spec (-> (:trace printer) first :spec) 14 | msg (str "spec " spec 15 | " must have its own method implementation of " `render)] 16 | (throw (ex-info msg {:spec spec})))) 17 | 18 | (defn- fn-name [func] 19 | #?(:clj (-> (.getClass func) 20 | .getSimpleName 21 | clojure.lang.Compiler/demunge) 22 | :cljs (not-empty (demunge (.-name func))))) 23 | 24 | (defn call-with-base-printer [f printer x] 25 | (if (fn? x) 26 | (let [fn-name (or (fn-name x) "anonymous fn")] 27 | (str "#function[" fn-name "]")) 28 | (f (:base-printer printer) x))) 29 | 30 | (defn- highlight [x] 31 | [:span [:escaped *highlighting-mark*] x [:escaped *highlighting-mark*]]) 32 | 33 | (defn- wrap [f {:keys [trace] :as printer} x] 34 | (let [frame (first trace)] 35 | (if (= x (:val frame)) 36 | (if (and (= (count trace) 1) (not (:reason frame))) 37 | (highlight (call-with-base-printer f printer x)) 38 | (render frame printer x)) 39 | (call-with-base-printer f printer x)))) 40 | 41 | (defn pop-trace [printer] 42 | (update printer :trace rest)) 43 | 44 | (defrecord HighlightPrinter [base-printer trace] 45 | visit/IVisitor 46 | (visit-unknown [this x] 47 | (wrap visit/visit-unknown this x)) 48 | (visit-nil [this] 49 | (wrap (fn [printer _] (visit/visit-nil printer)) this nil)) 50 | (visit-boolean [this x] 51 | (wrap visit/visit-boolean this x)) 52 | (visit-string [this x] 53 | (wrap visit/visit-string this x)) 54 | (visit-character [this x] 55 | (wrap visit/visit-character this x)) 56 | (visit-symbol [this x] 57 | (wrap visit/visit-symbol this x)) 58 | (visit-keyword [this x] 59 | (wrap visit/visit-keyword this x)) 60 | (visit-number [this x] 61 | (wrap visit/visit-number this x)) 62 | (visit-seq [this x] 63 | (wrap visit/visit-seq this x)) 64 | (visit-vector [this x] 65 | (wrap visit/visit-vector this x)) 66 | (visit-map [this x] 67 | (wrap visit/visit-map this x)) 68 | (visit-set [this x] 69 | (wrap visit/visit-set this x)) 70 | (visit-tagged [this x] 71 | (wrap visit/visit-tagged this x)) 72 | (visit-meta [this meta x] 73 | (wrap #(visit/visit-meta %1 meta %2) this x)) 74 | (visit-var [this x] 75 | (wrap visit/visit-var this x)) 76 | (visit-pattern [this x] 77 | (wrap visit/visit-pattern this x)) 78 | (visit-record [this x] 79 | (wrap visit/visit-record this x)) 80 | ) 81 | 82 | (defn format 83 | ([x trace] (format x trace {})) 84 | ([x trace opts] 85 | (let [base-printer (or (:base-printer opts) 86 | (edn/map->EdnPrinter {:symbols {}})) 87 | printer (->HighlightPrinter base-printer trace)] 88 | (with-out-str 89 | (fipp/pprint-document (visit/visit printer x) 90 | (dissoc opts :base-printer)))))) 91 | 92 | ;; 93 | ;; Method implementations of `render` 94 | ;; 95 | 96 | (defn- render-next [printer x] 97 | (visit/visit (pop-trace printer) x)) 98 | 99 | (defmethod render `s/spec [frame printer x] 100 | (render-next printer x)) 101 | 102 | (defmethod render `s/and [frame printer x] 103 | (render-next printer x)) 104 | 105 | (defmethod render `s/or [frame printer x] 106 | (render-next printer x)) 107 | 108 | (defmethod render `s/nilable [frame printer x] 109 | (render-next printer x)) 110 | 111 | (defn- pretty-coll [printer open xs sep close f] 112 | (let [xform (comp (map-indexed #(f printer %1 %2)) 113 | (interpose sep)) 114 | ys (sequence xform xs)] 115 | [:group open ys close])) 116 | 117 | (defn render-coll 118 | ([frame printer x] 119 | (render-coll frame printer x nil)) 120 | ([{[n] :steps} printer x each-fn] 121 | (let [[open close] (cond (seq? x) ["(" ")"] 122 | (vector? x) ["[" "]"] 123 | (map? x) ["{" "}"] 124 | (set? x) ["#{" "}"]) 125 | sep (if (map? x) [:span "," :line] :line) 126 | each-fn (if each-fn 127 | #(each-fn %2 %3) 128 | (fn [_ i x] 129 | (visit/visit (cond-> printer (= i n) pop-trace) x)))] 130 | (pretty-coll printer open x sep close each-fn)))) 131 | 132 | (defmethod render `s/tuple [{:keys [steps] :as frame} printer x] 133 | (if (empty? steps) 134 | (render-next printer x) 135 | (render-coll frame printer x))) 136 | 137 | (defn- render-every [{:keys [steps] :as frame} printer x] 138 | (if (empty? steps) 139 | (render-next printer x) 140 | (render-coll frame printer x))) 141 | 142 | (defmethod render `s/every [frame printer x] 143 | (render-every frame printer x)) 144 | 145 | (defmethod render `s/coll-of [frame printer x] 146 | (render-every frame printer x)) 147 | 148 | (defn- render-every-kv [{:keys [steps] :as frame} printer x] 149 | (if (empty? steps) 150 | (render-next printer x) 151 | (let [[key k-or-v] steps] 152 | (render-coll frame printer x 153 | (fn [i [k v]] 154 | (let [kprinter (cond-> printer 155 | (and (= k key) (= k-or-v 0)) 156 | pop-trace) 157 | vprinter (cond-> printer 158 | (and (= k key) (= k-or-v 1)) 159 | pop-trace)] 160 | [:span (visit/visit kprinter k) " " (visit/visit vprinter v)])))))) 161 | 162 | (defmethod render `s/every-kv [frame printer x] 163 | (render-every-kv frame printer x)) 164 | 165 | (defmethod render `s/map-of [frame printer x] 166 | (render-every-kv frame printer x)) 167 | 168 | (defmethod render `s/keys [{[key] :steps :as frame} printer x] 169 | (if (nil? key) 170 | (visit/visit (pop-trace printer) x) 171 | (render-coll frame printer x 172 | (fn [i [k v]] 173 | (let [vprinter (cond-> printer (= k key) pop-trace)] 174 | [:span (visit/visit printer k) " " (visit/visit vprinter v)]))))) 175 | 176 | (defmethod render `s/merge [frame printer x] 177 | (render-next printer x)) 178 | 179 | (defn- render-regex [{:keys [reason steps] :as frame} printer x] 180 | (cond (= reason "Extra input") 181 | (render-coll frame (:base-printer printer) x 182 | (fn [i v] 183 | (cond-> (call-with-base-printer visit/visit printer v) 184 | (>= i (first steps)) highlight))) 185 | 186 | (or (= reason "Insufficient input") ;; special case for s/alt 187 | (when-let [next-frame (second (:trace printer))] 188 | (= (:reason next-frame) "Insufficient input"))) 189 | (let [x (if (seq? x) (concat x ['...]) (conj x '...))] 190 | (render-coll frame printer x 191 | (fn [i v] 192 | (cond-> (call-with-base-printer visit/visit printer v) 193 | (= i (dec (count x))) highlight)))) 194 | 195 | (empty? steps) 196 | (render-next printer x) 197 | 198 | :else (render-coll frame printer x))) 199 | 200 | (defmethod render `s/cat [frame printer x] 201 | (render-regex frame printer x)) 202 | 203 | (defmethod render `s/& [frame printer x] 204 | (render-regex frame printer x)) 205 | 206 | (defmethod render `s/alt [frame printer x] 207 | (render-regex frame printer x)) 208 | 209 | (defmethod render `s/? [frame printer x] 210 | (render-regex frame printer x)) 211 | 212 | (defmethod render `s/* [frame printer x] 213 | (render-regex frame printer x)) 214 | 215 | (defmethod render `s/+ [frame printer x] 216 | (render-regex frame printer x)) 217 | 218 | (defmethod render `s/int-in [frame printer x] 219 | (render-next printer x)) 220 | 221 | (defmethod render `s/double-in [frame printer x] 222 | (render-next printer x)) 223 | 224 | (defmethod render `s/inst-in [frame printer x] 225 | (render-next printer x)) 226 | 227 | (defmethod render `s/fspec [frame printer x] 228 | (if (some-> (second (:trace printer)) :reason) 229 | (highlight (call-with-base-printer visit/visit printer x)) 230 | (render-next printer x))) 231 | 232 | (defmethod render `s/multi-spec [frame printer x] 233 | (if (= (:reason frame) "no method") 234 | (highlight (call-with-base-printer visit/visit printer x)) 235 | (render-next printer x))) 236 | 237 | (defmethod render `s/nonconforming [frame printer x] 238 | (render-next printer x)) 239 | 240 | (defmethod render `s/with-gen [frame printer x] 241 | (render-next printer x)) 242 | -------------------------------------------------------------------------------- /src/pinpointer/trace.cljc: -------------------------------------------------------------------------------- 1 | (ns pinpointer.trace 2 | (:require [spectrace.core :as strace])) 3 | 4 | (defn- partition-trace [t] 5 | (loop [t t, chunk [], prev nil, ret []] 6 | (if (empty? t) 7 | (conj ret chunk) 8 | (let [{:keys [snapshots] :as frame} (first t)] 9 | (if (and (seq snapshots) 10 | prev 11 | (not= (:val prev) (peek snapshots))) 12 | (let [val (peek snapshots) 13 | new-chunk [(assoc prev :val val) frame]] 14 | (recur (rest t) new-chunk frame (conj ret chunk))) 15 | (recur (rest t) (conj chunk frame) frame ret)))))) 16 | 17 | (defn- trace [t] 18 | (letfn [(diff-steps [steps1 steps2] 19 | (let [index (max 0 (- (count steps1) (count steps2))) 20 | suffix (subvec steps1 index)] 21 | (if (= suffix steps2) 22 | (subvec steps1 0 index) 23 | steps1))) 24 | (add-to-chunk [chunk [curr next]] 25 | (conj chunk 26 | (cond-> {:spec (:spec curr) 27 | :val (:val curr) 28 | :steps (if next 29 | (diff-steps (:in curr) (:in next)) 30 | (:in curr))} 31 | (:reason curr) (assoc :reason (:reason curr)))))] 32 | (mapv (fn [chunk] 33 | (reduce add-to-chunk [] (partition 2 1 [nil] chunk))) 34 | (partition-trace t)))) 35 | 36 | (defn traces [ed] 37 | (mapv trace (strace/traces ed))) 38 | -------------------------------------------------------------------------------- /test/cljc/pinpointer/core_test.cljc: -------------------------------------------------------------------------------- 1 | (ns pinpointer.core-test 2 | (:require [clojure.spec.alpha :as s] 3 | [clojure.test :refer [deftest are]] 4 | [pinpointer.core :as p])) 5 | 6 | (s/def ::x integer?) 7 | (s/def ::y string?) 8 | 9 | (deftest pinpoint-out-test 10 | (are [spec input result] 11 | (= result 12 | (with-out-str 13 | (p/pinpoint-out (s/explain-data spec input)))) 14 | (s/tuple integer? string?) 15 | [1 :foo] 16 | "Detected 1 spec error: 17 | ---------------------------------------------------------------------- 18 | (1/1) 19 | 20 | Cause: [1 :foo] 21 | ^^^^ 22 | Expected: string? 23 | 24 | ---------------------------------------------------------------------- 25 | " 26 | 27 | (s/cat :first integer? :second integer?) 28 | [1 2 3 4] 29 | "Detected 1 spec error: 30 | ---------------------------------------------------------------------- 31 | (1/1) 32 | 33 | Cause: [1 2 3 4] 34 | ^ ^ 35 | Expected: (s/cat :first integer? :second integer?) 36 | Failure 37 | Reason: Extra input 38 | 39 | ---------------------------------------------------------------------- 40 | " 41 | 42 | (s/keys :req-un [::x ::y]) 43 | {:y 42} 44 | "Detected 2 spec errors: 45 | ---------------------------------------------------------------------- 46 | (1/2) 47 | 48 | Cause: {:y 42} 49 | ^^^^^^^ 50 | Expected: (fn [%] (contains? % :x)) 51 | 52 | ---------------------------------------------------------------------- 53 | (2/2) 54 | 55 | Cause: {:y 42} 56 | ^^ 57 | Expected: string? 58 | 59 | ---------------------------------------------------------------------- 60 | " 61 | 62 | (s/and string? (s/conformer seq) (s/* #{\a \b})) 63 | "abcab" 64 | #?(:clj 65 | "Detected 1 spec error: 66 | ---------------------------------------------------------------------- 67 | (1/1) 68 | 69 | Cause: (\\a \\b \\c \\a \\b) 70 | ^^ 71 | Expected: #{\\a \\b} 72 | 73 | --- This comes originally from --- 74 | 75 | Original: \"abcab\" 76 | ^^^^^^^ 77 | Spec 78 | Applied: (s/and string? (s/conformer seq) (s/* #{\\a \\b})) 79 | 80 | ---------------------------------------------------------------------- 81 | " 82 | :cljs 83 | "Detected 1 spec error: 84 | ---------------------------------------------------------------------- 85 | (1/1) 86 | 87 | Cause: (\"a\" \"b\" \"c\" \"a\" \"b\") 88 | ^^^ 89 | Expected: #{\"a\" \"b\"} 90 | 91 | --- This comes originally from --- 92 | 93 | Original: \"abcab\" 94 | ^^^^^^^ 95 | Spec 96 | Applied: (s/and string? (s/conformer seq) (s/* #{\"a\" \"b\"})) 97 | 98 | ---------------------------------------------------------------------- 99 | ") 100 | 101 | )) 102 | -------------------------------------------------------------------------------- /test/cljc/pinpointer/formatter_test.cljc: -------------------------------------------------------------------------------- 1 | (ns pinpointer.formatter-test 2 | (:require [clojure.spec.alpha :as s] 3 | [clojure.spec.gen.alpha :as gen] 4 | [clojure.test :refer [deftest is are]] 5 | [pinpointer.formatter :as formatter] 6 | [pinpointer.trace :as trace])) 7 | 8 | (s/def ::x integer?) 9 | (s/def ::y string?) 10 | 11 | (defmulti shape-spec :type) 12 | (s/def ::shape (s/multi-spec shape-spec :type)) 13 | 14 | (s/def ::radius number?) 15 | (defmethod shape-spec :circle [_] 16 | (s/keys :req-un [::radius])) 17 | 18 | (deftest format-test 19 | (are [spec input result] 20 | (= result 21 | (for [trace (trace/traces (s/explain-data spec input))] 22 | (map #(formatter/format (:val (first %)) %) trace))) 23 | (s/spec integer?) 24 | :foo 25 | [["!!!:foo!!!\n"]] 26 | 27 | (s/and integer? even?) 28 | 3 29 | [["!!!3!!!\n"]] 30 | 31 | (s/and string? (s/conformer seq) (s/* #{\a \b})) 32 | "abcab" 33 | [["!!!\"abcab\"!!!\n" 34 | #?(:clj "(\\a \\b !!!\\c!!! \\a \\b)\n" 35 | :cljs "(\"a\" \"b\" !!!\"c\"!!! \"a\" \"b\")\n")]] 36 | 37 | (s/or :i integer? :s string?) 38 | :foo 39 | [["!!!:foo!!!\n"] ["!!!:foo!!!\n"]] 40 | 41 | (s/nilable integer?) 42 | :foo 43 | [["!!!:foo!!!\n"] ["!!!:foo!!!\n"]] 44 | 45 | (s/tuple integer? string?) 46 | 42 47 | [["!!!42!!!\n"]] 48 | 49 | (s/tuple integer? string?) 50 | [1 :foo] 51 | [["[1 !!!:foo!!!]\n"]] 52 | 53 | (s/every (s/spec integer?)) 54 | 42 55 | [["!!!42!!!\n"]] 56 | 57 | (s/every (s/spec integer?)) 58 | [1 :foo 'bar] 59 | [["[1 !!!:foo!!! bar]\n"] 60 | ["[1 :foo !!!bar!!!]\n"]] 61 | 62 | (s/coll-of (s/spec integer?)) 63 | 42 64 | [["!!!42!!!\n"]] 65 | 66 | (s/coll-of (s/spec integer?)) 67 | [1 :foo 'bar] 68 | [["[1 !!!:foo!!! bar]\n"] 69 | ["[1 :foo !!!bar!!!]\n"]] 70 | 71 | (s/coll-of (s/spec integer?)) 72 | #{1 :foo 3} 73 | [["!!!#{1 3 :foo}!!!\n" 74 | "(1 3 !!!:foo!!!)\n"]] 75 | 76 | #?@(:clj 77 | ((s/coll-of (s/spec (fn [[id m]] (= id (:id m))))) 78 | {1 {:id 1} 2 {:id 3}} 79 | [["!!!{1 {:id 1}, 2 {:id 3}}!!!\n" 80 | "([1 {:id 1}] !!![2 {:id 3}]!!!)\n"]] 81 | )) 82 | 83 | (s/every-kv keyword? integer?) 84 | 42 85 | [["!!!42!!!\n"]] 86 | 87 | (s/every-kv keyword? integer?) 88 | {:a 1 2 :b} 89 | [["{:a 1, !!!2!!! :b}\n"] 90 | ["{:a 1, 2 !!!:b!!!}\n"]] 91 | 92 | (s/map-of keyword? integer?) 93 | 42 94 | [["!!!42!!!\n"]] 95 | 96 | (s/map-of keyword? integer?) 97 | {:a 1 2 :b} 98 | [["{:a 1, !!!2!!! :b}\n"] 99 | ["{:a 1, 2 !!!:b!!!}\n"]] 100 | 101 | (s/keys :req-un [::x]) 102 | 42 103 | [["!!!42!!!\n"]] 104 | 105 | (s/keys :req-un [::x ::y]) 106 | {:y 42} 107 | [["!!!{:y 42}!!!\n"] 108 | ["{:y !!!42!!!}\n"]] 109 | 110 | (s/merge (s/keys :req-un [::x]) (s/keys :req-un [::y])) 111 | 42 112 | [["!!!42!!!\n"] 113 | ["!!!42!!!\n"]] 114 | 115 | (s/merge (s/keys :req-un [::x]) (s/keys :req-un [::y])) 116 | {:y 42} 117 | [["!!!{:y 42}!!!\n"] 118 | ["{:y !!!42!!!}\n"]] 119 | 120 | (s/cat :i integer? :s string?) 121 | 42 122 | [["!!!42!!!\n"]] 123 | 124 | (s/cat :i integer? :s string?) 125 | [1] 126 | [["[1 !!!...!!!]\n"]] 127 | 128 | (s/cat :i integer? :s string?) 129 | [1 "foo" 3 4] 130 | [["[1 \"foo\" !!!3!!! !!!4!!!]\n"]] 131 | 132 | (s/cat :i integer? :s string?) 133 | [1 :foo] 134 | [["[1 !!!:foo!!!]\n"]] 135 | 136 | (s/alt :i integer? :s string?) 137 | 42 138 | [["!!!42!!!\n"]] 139 | 140 | (s/alt :i integer? :s string?) 141 | [] 142 | [["[!!!...!!!]\n"]] 143 | 144 | (s/alt :i integer? :s string?) 145 | [:foo] 146 | [["[!!!:foo!!!]\n"] 147 | ["[!!!:foo!!!]\n"]] 148 | 149 | (s/alt :one integer? :two (s/cat :first integer? :second integer?)) 150 | [1 2 3 4] 151 | [["[1 2 !!!3!!! !!!4!!!]\n"]] 152 | 153 | (s/alt :two (s/cat :first integer? :second integer?) 154 | :three (s/cat :first integer? :second integer? :third integer?)) 155 | [1] 156 | [["[1 !!!...!!!]\n"]] 157 | 158 | (s/? integer?) 159 | 42 160 | [["!!!42!!!\n"]] 161 | 162 | (s/? integer?) 163 | [:foo] 164 | [["[!!!:foo!!!]\n"]] 165 | 166 | (s/? integer?) 167 | [1 2 3] 168 | [["[1 !!!2!!! !!!3!!!]\n"]] 169 | 170 | (s/? (s/cat :int integer? :str string?)) 171 | [1 "foo" 'bar] 172 | [["[1 \"foo\" !!!bar!!!]\n"]] 173 | 174 | (s/* integer?) 175 | 42 176 | [["!!!42!!!\n"]] 177 | 178 | (s/* (s/cat :i integer? :s string?)) 179 | [1 "foo" 2] 180 | [["[1 \"foo\" 2 !!!...!!!]\n"]] 181 | 182 | (s/* (s/cat :i integer? :s string?)) 183 | [1 "foo" 2 :bar] 184 | [["[1 \"foo\" 2 !!!:bar!!!]\n"]] 185 | 186 | (s/+ integer?) 187 | 42 188 | [["!!!42!!!\n"]] 189 | 190 | (s/+ integer?) 191 | [] 192 | [["[!!!...!!!]\n"]] 193 | 194 | (s/+ integer?) 195 | [:foo] 196 | [["[!!!:foo!!!]\n"]] 197 | 198 | (s/+ (s/cat :i integer? :s string?)) 199 | [1 "foo" 2] 200 | [["[1 \"foo\" 2 !!!...!!!]\n"]] 201 | 202 | (s/+ (s/cat :i integer? :s string?)) 203 | [1 "foo" 2 :bar] 204 | [["[1 \"foo\" 2 !!!:bar!!!]\n"]] 205 | 206 | (s/coll-of (s/int-in 0 5)) 207 | [0 :a 2] 208 | [["[0 !!!:a!!! 2]\n"]] 209 | 210 | (s/coll-of (s/int-in 0 5)) 211 | [0 5 2] 212 | [["[0 !!!5!!! 2]\n"]] 213 | 214 | (s/coll-of (s/double-in :min 0 :max 5)) 215 | [0.5 :a 4.5] 216 | [["[0.5 !!!:a!!! 4.5]\n"]] 217 | 218 | (s/coll-of (s/double-in :min 0 :max 5)) 219 | [0.5 10.5 4.5] 220 | [["[0.5 !!!10.5!!! 4.5]\n"]] 221 | 222 | (s/coll-of (s/inst-in #inst "2016-01-01" #inst "2017-01-01")) 223 | [#inst "2016-01-01" nil] 224 | [["[#inst \"2016-01-01T00:00:00.000-00:00\" !!!nil!!!]\n"]] 225 | 226 | (s/coll-of (s/inst-in #inst "2016-01-01" #inst "2017-01-01")) 227 | [#inst "2018-01-01"] 228 | [["[!!!#inst \"2018-01-01T00:00:00.000-00:00\"!!!]\n"]] 229 | 230 | (s/fspec :args (s/cat :x int?) :ret keyword?) 231 | identity 232 | #?(:clj [["!!!#function[core/identity]!!!\n" "!!!0!!!\n"]] 233 | :cljs [["!!!#function[cljs/core/identity]!!!\n" "!!!0!!!\n"]]) 234 | 235 | (s/fspec :args (s/cat :x int?) :ret string?) 236 | name 237 | #?(:clj [["!!!#function[core/name]!!!\n"]] 238 | :cljs [["!!!#function[cljs/core/name]!!!\n"]]) 239 | 240 | ::shape 241 | {:type :rectangle} 242 | [["!!!{:type :rectangle}!!!\n"]] 243 | 244 | ::shape 245 | {:type :circle :radius "100"} 246 | [["{:type :circle, :radius !!!\"100\"!!!}\n"]] 247 | 248 | (s/coll-of (s/with-gen (s/spec integer?) #(gen/return 1))) 249 | [0 :foo 2] 250 | [["[0 !!!:foo!!! 2]\n"]])) 251 | -------------------------------------------------------------------------------- /test/cljc/pinpointer/trace_test.cljc: -------------------------------------------------------------------------------- 1 | (ns pinpointer.trace-test 2 | (:require [clojure.test :refer [deftest are]] 3 | [clojure.spec.alpha :as s] 4 | [pinpointer.trace :as trace])) 5 | 6 | (defmulti shape-spec :type) 7 | (s/def ::shape (s/multi-spec shape-spec :type)) 8 | 9 | (s/def ::id integer?) 10 | 11 | (deftest trace-test 12 | (are [spec input expected] 13 | (= expected (trace/traces (s/explain-data spec input))) 14 | (s/map-of keyword? (s/coll-of (s/spec integer?))) 15 | {:a [1 2] :b ["3"]} 16 | [[[{:spec `(s/map-of keyword? (s/coll-of (s/spec integer?))) 17 | :val {:a [1 2] :b ["3"]} 18 | :steps [:b 1]} 19 | {:spec `(s/coll-of (s/spec integer?)) :val ["3"] :steps [0]} 20 | {:spec `(s/spec integer?) :val "3" :steps []} 21 | {:spec `integer? :val "3" :steps []}]]] 22 | 23 | (s/cat :first integer? :second integer?) 24 | [1] 25 | [[[{:spec `(s/cat :first integer? :second integer?) :val [1] :steps []} 26 | {:spec `integer? :val [1] :steps [] :reason "Insufficient input"}]]] 27 | 28 | (s/cat :first integer? :second integer?) 29 | [1 2 3] 30 | [[[{:spec `(s/cat :first integer? :second integer?) 31 | :val [1 2 3] 32 | :steps [2] 33 | :reason "Extra input"}]]] 34 | 35 | ::shape 36 | {:type :circle} 37 | [[[{:spec `(s/multi-spec shape-spec :type) 38 | :val {:type :circle} 39 | :steps [] 40 | :reason "no method"}]]] 41 | 42 | (s/and string? 43 | (s/conformer seq) 44 | (s/coll-of #{\a \b \c})) 45 | "abcdab" 46 | [[[{:spec `(s/and string? 47 | (s/conformer seq) 48 | (s/coll-of #{\a \b \c})) 49 | :val "abcdab" 50 | ;; TODO: the following field value looks somewhat weird, 51 | ;; so we may need to reconsider the result spec in the future 52 | :steps [3]}] 53 | [{:spec `(s/and string? 54 | (s/conformer seq) 55 | (s/coll-of #{\a \b \c})) 56 | :val '(\a \b \c \d \a \b) 57 | :steps []} 58 | {:spec `(s/coll-of #{\a \b \c}) 59 | :val '(\a \b \c \d \a \b) 60 | :steps [3]} 61 | {:spec #{\a \b \c} :val \d :steps []}]]] 62 | 63 | ;; eval is necessary to test the following case, but I don't 64 | ;; know how we can prepare it for CLJS at the moment. 65 | #?@(:clj 66 | ((s/and (s/map-of ::id (s/keys :req-un [::id])) 67 | (s/coll-of (s/spec (fn [[id m]] (= id (:id m)))))) 68 | {1 {:id 1} 2 {:id 3}} 69 | [[[{:spec `(s/and (s/map-of ::id (s/keys :req-un [::id])) 70 | (s/coll-of 71 | (s/spec (fn [[~'id ~'m]] (= ~'id (:id ~'m)))))) 72 | :val {1 {:id 1} 2 {:id 3}} 73 | :steps []} 74 | {:spec `(s/coll-of (s/spec (fn [[~'id ~'m]] (= ~'id (:id ~'m))))) 75 | :val {1 {:id 1} 2 {:id 3}} 76 | :steps [1]}] 77 | [{:spec `(s/coll-of (s/spec (fn [[~'id ~'m]] (= ~'id (:id ~'m))))) 78 | :val [[1 {:id 1}] [2 {:id 3}]] 79 | :steps [1]} 80 | {:spec `(s/spec (fn [[~'id ~'m]] (= ~'id (:id ~'m)))) 81 | :val [2 {:id 3}] 82 | :steps []} 83 | {:spec `(fn [[~'id ~'m]] (= ~'id (:id ~'m))) 84 | :val [2 {:id 3}] 85 | :steps []}]]])) 86 | 87 | )) 88 | -------------------------------------------------------------------------------- /test/cljs/pinpointer/runner.cljs: -------------------------------------------------------------------------------- 1 | (ns pinpointer.runner 2 | (:require [doo.runner :refer-macros [doo-tests]] 3 | pinpointer.core-test 4 | pinpointer.formatter-test 5 | pinpointer.trace-test)) 6 | 7 | (doo-tests 'pinpointer.trace-test 8 | 'pinpointer.formatter-test 9 | 'pinpointer.core-test) 10 | --------------------------------------------------------------------------------