Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to spec a function which have no arguments in Clojure?

Tags:

clojure

I want to write some spec's for a small side project of mine and being worried how to write a spec for a function which does not provide any arguments to pass into it.

I want to write the spec for this particular function:

(defn get-total-pages []
  (int (Math/ceil (/ (count (get-posts)) posts-per-page))))

It calculates the total number of pages assuming posts-per-page posts at one page. The function itself works well, but when adding the following spec to it:

(s/fdef get-total-pages
  :args ()
  :ret int?)

I can not perform any stest/check on it without the warning:

:clojure.spec.test.check/ret {:result #error {
 :cause "Unable to construct gen at: [] for: clojure.spec.alpha$spec_impl$reify__2059@1232bda3"
...

When not using the :args mapping it all, spec tell me that :args is missing, so here my final question: How can I spec a function which do not offer arguments?

like image 536
Mo0812 Avatar asked Jan 25 '23 02:01

Mo0812


1 Answers

The :args list for a function of no arguments can be spec'd as an empty s/cat spec:

(s/fdef get-total-pages
  :args (s/cat)
  :ret int?)
like image 150
Taylor Wood Avatar answered Feb 16 '23 01:02

Taylor Wood