Fork me on GitHub
#clojure-spec
<
2016-12-26
>
roelof16:12:32

I have this error message :

(clojure.spec.gen/generate (clojure.spec/gen :paintings2.routes.home/page))
RuntimeException Var clojure.test.check.generators/large-integer is not on the classpath  clojure.spec.gen/dynaload (gen.clj:21)
 

roelof17:12:00

on this code :

(ns paintings2.routes.home
  (:require [paintings2.layout :as layout]
            [compojure.core :refer [defroutes GET]]
            [ring.util.http-response :as response]
            [ :as io]
            [compojure.route :refer [resources]]
            [environ.core :refer [env]]
            [paintings2.api-get :as api]
            [clj-http.client :as client]
            [clojure.spec :as s]))

(defn ->long [s] (try (Long/parseLong s) (catch Exception _ ::s/invalid)))

(s/def ::page (s/and (s/conformer ->long) (s/int-in 1 471)))

(s/def ::optional-page (s/nilable ::page))

(defn page-check [page] (let [page page page-num (or (s/conform ::optional-page page) 1)] page-num))

(s/fdef page-check
        :args (string? (::page :page))
        :ret  (number? (::page :page))
        :fn  (s/valid? true?  (::page :page)  )
       ) 

joshjones17:12:21

and did you put [org.clojure/test.check "0.9.0"] in your project dependencies?

roelof17:12:34

and I have this in my projects.clj :

:profiles {:dev {:dependencies [[org.clojure/test.check "0.9.0"]
                                   ]}} 

joshjones17:12:11

Why not put it in your main :dependencies? Why in a profile?

roelof17:12:14

see my last response, @joshjones

roelof17:12:32

o, then I misunderstood you

roelof17:12:41

moment, I will change it

joshjones17:12:19

(defproject your-project "1.0"
  :description "spec playground"
  :url ""
  :license {:name "Eclipse Public License"
            :url ""}
  :dependencies [[org.clojure/clojure "1.9.0-alpha14"]
                 [org.clojure/test.check "0.9.0"]])

roelof17:12:57

oke, now I see another error message :

ExceptionInfo Unable to construct gen at: [] for: (conformer ->long)  clojure.core/ex-info (core.clj:4725) 

joshjones17:12:10

now you're getting somewhere -- i get why you want to use a conformer, but i've not used a conformer to generate data before, nor have i seen any info on doing so -- only seen info on conforming/validating using a conformer

joshjones17:12:04

so you just want to generate numbers in a certain range?

roelof17:12:36

yes, I want to check if the url parameter is between the 1 and 471 or is not there

roelof17:12:54

is the last case the pagenumber schould be set to 1

joshjones17:12:40

and the input can be either a string or a number, right?

joshjones17:12:20

the way you've written the spec for ::page, I do not think there's enough info there for it to do any data generation

roelof17:12:28

oke, what I try to do it this : The user can use this url : http://localhost:3000/

roelof17:12:47

and then my code schould take that the pagenumber is 1

roelof17:12:11

or the user can use this url : http://localhost/?page=n

roelof17:12:30

n will be a integer where my code makes a long of it

roelof17:12:44

@joshjones so far, clear ?

roelof17:12:23

Now I want to make a generator which makes several cases for n

roelof17:12:20

so n could be "a" then the outcome is invalid or "124" then the outcome is 124 or "500" then the outcome is also invalid

roelof17:12:48

so I want testcases that test this

joshjones17:12:47

ok -- what you want to actually generate is the entire URL?

joshjones17:12:59

you are using something already for routing, and for parsing the query string?

joshjones17:12:42

(defn gen-page-num
  []
  (gen/fmap #(str %) (s/gen (s/int-in 1 471))))

(s/def ::page
  (s/spec (s/and (s/conformer ->long) (s/int-in 1 471))
          :gen gen-page-num))

seancorfield17:12:14

You might want to keep this level of back and forth in the #beginners channel

roelof17:12:39

@seancorfield The last question is about spec because josh thinks that my fdef for testing and generating test-data is not good

joshjones17:12:37

@roelof Use the above generator and revised def for ::page and you'll be able to generate data using ::page

roelof18:12:39

and I can use that data for checking the page-check function

roelof18:12:17

@joshjones Am I right that I cannot check what happens if a user enters false data like this : ' /?pag="a"

seancorfield18:12:36

@roelof Although this is about clojure.spec, there are still a lot of beginner-level issues at play here so I’d suggest keeping to the #beginners channel where folks have opted into helping folks with this level of discussion, rather than swamping the other channels with long back and forth about syntax and basic errors.