As an introductory reading, we received a Justification and Review along with a Guide that should provide you with information on why and how.
If you need a slightly complicated example, we can take a string->semantic-version
function leiningen.release
:
(defn string->semantic-version [version-string]
"Create map representing the given version string. Returns nil if the
string does not follow guidelines setforth by Semantic Versioning 2.0.0,
http://semver.org/"
;; <MajorVersion>.<MinorVersion>.<PatchVersion>[-<Qualifier>][-SNAPSHOT]
(if-let [[_ major minor patch qualifier snapshot]
(re-matches
#"(\d+)\.(\d+)\.(\d+)(?:-(?!SNAPSHOT)([^\-]+))?(?:-(SNAPSHOT))?"
version-string)]
(->> [major minor patch]
(map #(Integer/parseInt %))
(zipmap [:major :minor :patch])
(merge {:qualifier qualifier
:snapshot snapshot}))))
, . :
(ns leiningen.core.spec.util
(:require
[clojure.spec :as spec]
[clojure.spec.gen :as gen]
[miner.strgen :as strgen]
[clojure.spec.test :as test]
[leiningen.release :as release]))
(defmacro stregex
"Defines a spec which matches a string based on a given string
regular expression. This the classical type of regex as in the
clojure regex literal
[string-regex]
`(spec/with-gen
(spec/and string?
(spec/def ::semantic-version-string
(stregex
(spec/def ::non-blank-string
(spec/and string?
(spec/def ::natural-number
(spec/int-in 0 Integer/MAX_VALUE))
(spec/def ::release/major ::natural-number)
(spec/def ::release/minor ::natural-number)
(spec/def ::release/patch ::natural-number)
(spec/def ::release/qualifier ::non-blank-string)
(spec/def ::release/snapshot
(spec/def ::release/semantic-version-map
(spec/keys :req-un [::release/major ::release/minor ::release/patch
::release/qualifier ::release/snapshot]))
spec:
(spec/fdef release/string->semantic-version
:args (spec/cat :version-str ::release/semantic-version-string)
:ret ::release/semantic-version-map)
Clojure Spec , , , :
(test/check `release/version-map->string)
=> ({:spec #object[clojure.spec$fspec_impl$reify__14248 0x16c2555 "clojure.spec$fspec_impl$reify__14248@16c2555"],
:clojure.spec.test.check/ret {:result true,
:num-tests 1000,
:seed 1491922864713},
:sym leiningen.release/version-map->string})
, 1000 , , .