-
Notifications
You must be signed in to change notification settings - Fork 24
/
test.cljs
176 lines (157 loc) · 6.14 KB
/
test.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns orchestra-cljs.spec.test
(:require-macros [orchestra-cljs.spec.test :as m
:refer [instrument unstrument with-instrument-disabled]])
(:require
[goog.object :as gobj]
[goog.userAgent.product :as product]
[clojure.string :as string]
[cljs.stacktrace :as st]
[cljs.pprint :as pp]
[cljs.spec.alpha :as s]
[cljs.spec.gen.alpha :as gen]))
(defn distinct-by
([f coll]
(let [step (fn step [xs seen]
(lazy-seq
((fn [[x :as xs] seen]
(when-let [s (seq xs)]
(let [v (f x)]
(if (contains? seen v)
(recur (rest s) seen)
(cons x (step (rest s) (conj seen v)))))))
xs seen)))]
(step coll #{}))))
(defn- no-fspec
[v spec]
(ex-info (str "Fn at " v " is not spec'ed.")
{:var v :spec spec ::s/failure :no-fspec}))
(defn- no-args-spec
[v spec]
(ex-info (str "Args for " v " are not spec'ed.")
{:var v :spec spec ::s/failure :no-args-spec}))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; instrument ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:private ^:dynamic *instrument-enabled*
"if false, instrumented fns call straight through"
true)
(defn get-host-port []
(if (not= "browser" *target*)
{}
{:host (.. js/window -location -host)
:port (.. js/window -location -port)}))
(defn get-ua-product []
(if (not= "browser" *target*)
(keyword *target*)
(cond
product/SAFARI :safari
product/CHROME :chrome
product/FIREFOX :firefox
product/IE :ie)))
(defn get-env []
{:ua-product (get-ua-product)})
(defn- find-caller [st]
(letfn [(search-spec-fn [frame]
(when frame
(let [s (:function frame)]
(and (string? s) (not (string/blank? s))
(re-find #"cljs\.spec\.test\.spec_checking_fn" s)))))]
(->> st
(drop-while #(not (search-spec-fn %)))
(drop-while search-spec-fn)
first)))
;; TODO: check ::caller result in other browsers - David
(defn- spec-checking-fn
[v f raw-fn-spec]
(let [fn-spec (@#'s/maybe-spec raw-fn-spec)
conform! (fn [v role spec data data-key]
(let [conformed (s/conform spec data)]
(if (= ::s/invalid conformed)
(let [caller (find-caller
(st/parse-stacktrace
(get-host-port)
(.-stack (js/Error.))
(get-env) nil))
via (if-some [n (#'s/spec-name spec)]
[n]
[])
ed (merge (assoc (s/explain-data* spec [role]
via
[]
data)
data-key data
::s/failure :instrument)
(when caller
{::caller caller}))]
(throw (ex-info
(str "Call to " v " did not conform to spec:\n"
(with-out-str (s/explain-out ed)))
ed)))
conformed)))]
(doto
(fn
[& args]
(if *instrument-enabled*
(with-instrument-disabled
(let [cargs (when (:args fn-spec)
(conform! v :args (:args fn-spec) args ::s/args))
ret (binding [*instrument-enabled* true]
(apply f args))]
(when (:ret fn-spec)
(conform! v :ret (:ret fn-spec) ret ::s/ret))
(when-let [spec (:fn fn-spec)]
(if (nil? cargs)
(throw (no-args-spec v fn-spec))
(conform! v :fn spec {:ret ret :args cargs} ::s/fn)))
ret))
(apply f args)))
(gobj/extend f))))
(defonce ^:private instrumented-vars (atom {}))
(defn- instrument-choose-fn
"Helper for instrument."
[f spec sym {over :gen :keys [stub replace]}]
(if (some #{sym} stub)
(-> spec (s/gen over) gen/generate)
(get replace sym f)))
(defn- instrument-choose-spec
"Helper for instrument"
[spec sym {overrides :spec}]
(get overrides sym spec))
(defn instrument-1*
[s v opts]
(let [spec (s/get-spec v)
{:keys [raw wrapped]} (get @instrumented-vars v)
current @v
to-wrap (if (= wrapped current) raw current)
ospec (or (instrument-choose-spec spec s opts)
(throw (no-fspec v spec)))
ofn (instrument-choose-fn to-wrap ospec s opts)
checked (spec-checking-fn v ofn ospec)]
(swap! instrumented-vars assoc v {:raw to-wrap :wrapped checked})
checked))
(defn unstrument-1*
[s v]
(when v
(when-let [{:keys [raw wrapped]} (get @instrumented-vars v)]
(swap! instrumented-vars dissoc v)
(let [current @v]
(when (= wrapped current)
raw)))))
(defn- fn-spec-name?
[s]
(symbol? s))
(defn instrumentable-syms
"Given an opts map as per instrument, returns the set of syms
that can be instrumented."
([] (instrumentable-syms nil))
([opts]
(assert (every? ident? (keys (:gen opts))) "instrument :gen expects ident keys")
(reduce into #{} [(filter fn-spec-name? (keys (s/registry)))
(keys (:spec opts))
(:stub opts)
(keys (:replace opts))])))