I’m reading the guide on how to do instrumenting of functions via
spec, but I am kind of stuck. The
spam function is just made up, its contents is not important.
I got the
:args to check for the types, but I really wanted to compare the length of the data to the
stop parameter. My attempts there failed to compile.
:ret in the spec does not seem to do anything. I can change the
:ret int? to
:ret string? to no effect.
I want the spec to fail if the data is too short.
Anybody got any ideas?
I got a working version that uses
:pre in the function itself. But I wanted the check in the spec.
(ns test-fdef (:require [clojure.spec.alpha :as s] [clojure.spec.test.alpha :as stest])) (s/fdef spam :args (s/cat :start int? :stop int? :buf bytes?) :fn #(>= (-> % :args :buf alength) (* 3 (-> % :args :stop))) :ret int?) (defn spam [start stop data] (println start stop data) 1) (stest/instrument `spam) (spam 1 10 (byte-array [1 2 3])) ; I want this to fail (passes) (spam 1 2 (byte-array [1 2 1 2 1 2])) ; I want this to pass