Hello.
I tried to construct a small example to understand and to illustrate how
stest/instrument with the :replace option works.
My attempt to construct a small working example was to expand on the
ranged-rand example in the clojure.spec Guide.
I created another function called ranged-const that always returns the value of
the start argument. I wanted to see if the ranged-const function could become
a mock to the ranged-rand function.
Here is the behavior that I am seeing. The mocked ranged-rand should be
returning 2 but it always returns 5 instead. Secondly, I would have expected
to see the printed side-effect of [:ranged-const 2 5].
spec-demo.core=> (stest/instrument `ranged-rand {:replace {`ranged-rand
::ranged-const}})
[spec-demo.core/ranged-rand]
spec-demo.core=> (ranged-const 2 5)
[:ranged-const 2 5]
2
spec-demo.core=> (ranged-rand 2 5)
5
spec-demo.core=> (ranged-rand 2 5)
5
Any suggestions?
Best regards,
Joe N.
Here is a complete log:
$ cat .boot/boot.properties
#http://boot-clj.com
#Sat Sep 17 23:34:35 CDT 2016
BOOT_EMIT_TARGET=no
BOOT_CLOJURE_NAME=org.clojure/clojure
BOOT_VERSION=2.6.0
BOOT_CLOJURE_VERSION=1.9.0-alpha14
$ boot repl
nREPL server started on port 53962 on host 127.0.0.1 - nrepl://127.0.0.1:53962
REPL-y 0.3.7, nREPL 0.2.12
Clojure 1.9.0-alpha14
Java HotSpot(TM) 64-Bit Server VM 1.8.0_102-b14
Exit: Control+D or (exit) or (quit)
Commands: (user/help)
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Find by Name: (find-name "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Examples from clojuredocs.org: [clojuredocs or cdoc]
(user/clojuredocs name-here)
(user/clojuredocs "ns-here" "name-here")
boot.user=> (ns spec-demo.core
#_=> (:require [clojure.spec :as s]
#_=> [clojure.spec.test :as stest])
#_=> (:gen-class))
nil
spec-demo.core=> (defn ranged-rand
#_=> "Returns random int in range start <= rand < end"
#_=> [start end]
#_=> (println [:ranged-rand start end]) ; DEBUG
#_=> (+ start (long (rand (- end start)))))
#'spec-demo.core/ranged-rand
spec-demo.core=> (s/fdef ranged-rand
#_=> :args (s/and (s/cat :start int? :end int?)
#_=> #(< (:start %) (:end %)))
#_=> :ret int?
#_=> :fn (s/and #(>= (:ret %) (-> % :args :start))
#_=> #(< (:ret %) (-> % :args :end))))
spec-demo.core/ranged-rand
spec-demo.core=> (defn ranged-const
#_=> "Returns const int equal to start such that start <= const <
end"
#_=> [start end]
#_=> (println [:ranged-const start end]) ; DEBUG
#_=> start)
#'spec-demo.core/ranged-const
spec-demo.core=> (s/fdef ranged-const
#_=> :args (s/and (s/cat :start int? :end int?)
#_=> #(< (:start %) (:end %)))
#_=> :ret int?
#_=> :fn #(= (:ret %) (-> % :args :start)))
spec-demo.core/ranged-const
spec-demo.core=> (ranged-rand 2 5)
[:ranged-rand 2 5]
3
spec-demo.core=> (ranged-rand 2 5)
[:ranged-rand 2 5]
4
spec-demo.core=> (ranged-rand 2 5)
[:ranged-rand 2 5]
2
spec-demo.core=> (ranged-const 2 5)
[:ranged-const 2 5]
2
spec-demo.core=> (ranged-const 2 5)
[:ranged-const 2 5]
2
spec-demo.core=> (ranged-const 2 5)
[:ranged-const 2 5]
2
spec-demo.core=> (stest/instrument `ranged-rand {:replace {`ranged-rand
::ranged-const}})
[spec-demo.core/ranged-rand]
spec-demo.core=> (ranged-const 2 5)
[:ranged-const 2 5]
2
spec-demo.core=> (ranged-rand 2 5)
5
spec-demo.core=> (ranged-rand 2 5)
5
spec-demo.core=> (ranged-rand 2 5)
5
spec-demo.core=>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.