Skip to content

Commit

Permalink
Initial version of expound alpha2 working with spec2
Browse files Browse the repository at this point in the history
  • Loading branch information
bhb committed Mar 2, 2020
1 parent 1bd1313 commit 90daaaf
Show file tree
Hide file tree
Showing 15 changed files with 6,680 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ jobs:
- v1-dependencies-{{ checksum "project.clj" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: lein with-profile test-common,clj-1.9.0,spec-0.2.168 test
# spec2 is not compatible with 1.9.0
# - run: lein with-profile test-common,clj-1.9.0,spec-0.2.168 test
- run: lein with-profile test-common,clj-1.10.0,spec-0.2.176 test

workflows:
Expand Down
3 changes: 3 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/alpha.spec {:git/url "https://github.com/clojure/spec-alpha2.git"
:sha "b644e4d8c5553e10544d920306690fffe9b53e15"} }
:aliases {;; clj -Atest
:test {:extra-paths ["test"]
:extra-deps {com.cognitect/test-runner {:git/url "https://github.com/cognitect-labs/test-runner.git"
Expand Down
59 changes: 59 additions & 0 deletions doc/spec2_bugs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Spec2 bugs

A list of bugs in spec2. I will report them as spec2 gets more stable.

### Using binding in spec fn

```clojure
;; make sure `n` is not defined
(let [n 1]
(s/def ::foobar #(< n %))
)
;; Expected: no error
;; Actual: 'Unable to resolve symbol: n in this context'
```

Note: this might be doable with a different strategy e.g.

```clojure
(let [max-v 10]
(s2/register ::foo
(s2/resolve-spec `(s2/int-in 1 (inc ~max-v))))
(s2/valid? ::foo 20))
```

### Bug with s/nest

```clojure
(s/def :alt-spec/one-many-int (s/cat :bs (s/alt :one int?
:many (s/nest (s/+ int?)))))

(s/explain :alt-spec/one-many-int [["2"]])
;; Attempting to call unbound fn: #'clojure.core/unquote
```

### Bug with using symbols in specs e.g.

```
> (s/def ::is-foo #{foo})
:expound.alpha2.core-test/is-foo
> (s/form ::is-foo)
#{foo}
> (s/explain ::is-foo 'foo)
Success!
nil
> (s/def ::is-or #{or})
:expound.alpha2.core-test/is-or
> (s/form ::is-or)
#{clojure.core/or}
> (s/explain ::is-or 'or)
or - failed: #{clojure.core/or} spec: :expound.alpha2.core-test/is-or
nil
```

From Alex Miller: "there is actually a known issue around sets of symbols (kind of a collision with symbol as function reference, which need qualification)"

### Bug with Clojure 1.9.0

`lein with-profile test-common,clj-1.9.0 test` fails (but moving to 1.10.0 works)

2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
:check {:global-vars {*unchecked-math* :warn-on-boxed
*warn-on-reflection* true}}
:kaocha [:test-common
{:dependencies [[lambdaisland/kaocha "0.0-565"]
{:dependencies [[lambdaisland/kaocha "0.0-590"]
[lambdaisland/kaocha-cloverage "0.0-41"]]}]
:test-common {:dependencies [[org.clojure/test.check "0.10.0-alpha3"]
[pjstadig/humane-test-output "0.9.0"]
Expand Down
93 changes: 93 additions & 0 deletions src/expound/alpha2/ansi.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
(ns ^:no-doc expound.alpha2.ansi
(:require [clojure.string :as string]))

;; Copied from strictly-specking, since I see no reason
;; to deviate from the colors displayed in figwheel
;; https://github.com/bhauman/strictly-specking/blob/f102c9bd604f0c238a738ac9e2b1f6968fdfd2d8/src/strictly_specking/ansi_util.clj

(def sgr-code
"Map of symbols to numeric SGR (select graphic rendition) codes."
{:none 0
:bold 1
:underline 3
:blink 5
:reverse 7
:hidden 8
:strike 9
:black 30
:red 31
:green 32
:yellow 33
:blue 34
:magenta 35
:cyan 36
:white 37
:fg-256 38
:fg-reset 39
:bg-black 40
:bg-red 41
:bg-green 42
:bg-yellow 43
:bg-blue 44
:bg-magenta 45
:bg-cyan 46
:bg-white 47
:bg-256 48
:bg-reset 49})

(def ^:dynamic *enable-color* false)

(defn esc
"Returns an ANSI escope string which will apply the given collection of SGR
codes."
[codes]
(let [codes (map sgr-code codes codes)
codes (string/join \; codes)]
(str \u001b \[ codes \m)))

(defn escape
"Returns an ANSI escope string which will enact the given SGR codes."
[& codes]
(esc codes))

(defn sgr
"Wraps the given string with SGR escapes to apply the given codes, then reset
the graphics."
[string & codes]
(str (esc codes) string (escape :none)))

(def ansi-code? sgr-code)

(def ^:dynamic *print-styles*
{:highlight [:bold]
:good [:green]
:good-pred [:green]
:good-key [:green]
:bad [:red]
:bad-value [:red]
:error-key [:red]
:focus-key [:bold]
:correct-key [:green]
:header [:cyan]
:footer [:cyan]
:warning-key [:bold]
:focus-path [:magenta]
:message [:magenta]
:pointer [:magenta]
:none [:none]})

(defn resolve-styles [styles]
(if-let [res (not-empty
(mapcat #(or
(when-let [res (*print-styles* %)]
res)
[%])
styles))]
res
;; fall back to bright
[:bold]))

(defn color [s & styles]
(if *enable-color*
(apply sgr s (resolve-styles styles))
s))
Loading

0 comments on commit 90daaaf

Please sign in to comment.