Clojure.spec - Why is it useful and when is it used?

I recently watched Rich Hickeys speak in Cojure Conj 2016 ( https://www.youtube.com/watch?v=oyLBGkS5ICk - here is a link in case someone missed it), and although it was very interesting, I really didn't understand the point in Clojure.Spec or when you used it. It seemed that most ideas, such as correspondence, reality, etc., already had similar functions in Clojure.

I studied Clojure for about 3 months, so maybe this is due to a lack of programming / Clojure experience.

Do Clojure.spec and Cljs.spec work similarly to Clojure and Cljs in that, although they are not 100% identical, they are based on the same basic principles.

thank

+4
source share
2 answers
  • Are you tired of documenting your programs?
  • Is there any prospect of doing more tests, causing procrastination?
  • When the boss says “test coverage,” do you suffer from fear?
  • Have you forgotten what your data means?

For smooth expression of tough specifications you need Clojure.Spec !


Clojure.spec gives you a single method for documenting, defining and automatically testing your programs and checking your live data.

He steals almost all his ideas. And he does nothing that you cannot do for yourself.

But in my - barely informed - opinion, it changes the efficiency of the specification, making it worthy of work. Game changer? - quite possible.

+8
source

clojure/conj , , , - . spec clojure; , .

, , . , , . spec, , ( ) , . , .

WORKFLOW:

, , - , . , (subs s start end) s , start end . , , , start end , end , start, . , ( , , , ):

(s/fdef clojure.core/subs

        :args (s/and (s/cat :s string? :start nat-int? :end (s/? nat-int?))
                     (fn [{:keys [s start end]}]
                       (if end
                         (<= 0 start end (count s))
                         (<= 0 start (count s)))))

        :ret string?

        :fn (fn [{{:keys [s start end]} :args, substring :ret}]
              (and (if end
                     (= (- end start) (count substring))
                     (= (- (count s) start) (count substring)))
                   (<= (count substring) (count s)))))

, args spec:

(s/exercise-fn `subs)

1000 ( , , - , :args; , ):

(stest/check `subs)

, subs, ? , spec, , :

(stest/instrument `subs)

, , - . , .

, spec'd, ( clojure):

(-> (stest/enumerate-namespace 'user) stest/check)

, , " ".

s/conform s/valid - . , , .

, ! !

+5

Source: https://habr.com/ru/post/1663453/


All Articles