Skip to content

Commit

Permalink
fix count/num-compare output depending on s-exp format
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenet committed Dec 23, 2023
1 parent 0429d0d commit 6e5c360
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 34 deletions.
64 changes: 35 additions & 29 deletions src/exoscale/lingo.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -375,41 +375,47 @@
(s/cat :op #{'= '< '> '<= '>= 'not=}
:_ ::count+arg
:x any?)
:count-2 (s/cat :op #{'= '< '> '<= '>= 'not=}
:x number?
:_ ::count+arg)))
(fn [[_ {:keys [op x]}] _opts]
:count-2
(s/cat :op #{'= '< '> '<= '>= 'not=}
:x number?
:_ ::count+arg)))

(fn [[t {:keys [op x]}] _opts]
(impl/format "should contain %s %s %s"
(case op
not= "not ="
= "exactly"
> "more than"
< "less than"
>= "at least"
<= "at most")
(case [t op]
([:count-1 #?(:cljs 'not= :clj not=)] [:count-2 #?(:cljs 'not= :clj not=)]) "not ="
([:count-1 #?(:cljs '= :clj =)] [:count-2 #?(:cljs '= :clj =)]) "exactly"
([:count-1 #?(:cljs '> :clj >)] [:count-2 #?(:cljs '< :clj <)]) "more than"
([:count-1 #?(:cljs '< :clj <)] [:count-2 #?(:cljs '> :clj >)]) "less than"
([:count-1 #?(:cljs '>= :clj >=)] [:count-2 #?(:cljs '<= :clj <=)]) "at least"
([:count-1 #?(:cljs '<= :clj <=)] [:count-2 #?(:cljs '>= :clj >=)]) "at most")
x
(if (= 1 x)
"element"
"elements"))))

(set-pred-error! (s/def :exoscale.lingo.pred/num-compare
(s/or :count-1
(s/cat :op #{'= '< '> '<= '>= 'not=}
:_ simple-symbol?
:x any?)
:count-2 (s/cat :op #{'= '< '> '<= '>= 'not=}
:x any?
:_ simple-symbol?)))
(fn [[_ {:keys [op x]}] _opts]
(impl/format "should %s %s"
(case op
not= "not be equal to"
= "be equal to"
> "be greater than"
< "be less than"
>= "be at least"
<= "be at most")
x)))
(let [x [:a :b]]
(cond)

(set-pred-error! (s/def :exoscale.lingo.pred/num-compare
(s/or :count-1
(s/cat :op #{'= '< '> '<= '>= 'not=}
:_ simple-symbol?
:x any?)
:count-2
(s/cat :op #{'= '< '> '<= '>= 'not=}
:x any?
:_ simple-symbol?)))
(fn [[t {:keys [op x]}] _opts]
(impl/format "should %s %s"
(case [t op]
([:count-1 #?(:cljs 'not= :clj not=)] [:count-2 #?(:cljs 'not= :clj not=)]) "not be equal to"
([:count-1 #?(:cljs '= :clj =)] [:count-2 #?(:cljs '= :clj =)]) "be equal to"
([:count-1 #?(:cljs '> :clj >)] [:count-2 #?(:cljs '< :clj <)]) "be greater than"
([:count-1 #?(:cljs '< :clj <)] [:count-2 #?(:cljs '> :clj >)]) "be less than"
([:count-1 #?(:cljs '>= :clj >=)] [:count-2 #?(:cljs '<= :clj <=)]) "be at least"
([:count-1 #?(:cljs '<= :clj <=)] [:count-2 #?(:cljs '>= :clj >=)]) "be at most")
x))))

(set-pred-error! (s/def :exoscale.lingo.pred/int-in-range
(s/or :_ (s/cat :_ #{#?(:clj 'clojure.spec.alpha/int-in-range?
Expand Down
10 changes: 5 additions & 5 deletions src/exoscale/lingo/impl.cljc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(ns exoscale.lingo.impl
(:refer-clojure :exclude [format])
(:require [clojure.walk :as walk]
(:require #?@(:cljs [[goog.string]
[goog.string.format]])
[clojure.spec.alpha :as s]
[exoscale.lingo.utils :as u]
#?@(:cljs [[goog.string]
[goog.string.format]])))
[clojure.walk :as walk]
[exoscale.lingo.utils :as u]))

(def format
#?(:clj clojure.core/format
Expand Down Expand Up @@ -138,7 +138,7 @@
pbs)]
(-> (first pbs)
(select-keys [:path :via :val :in])
(assoc :pred (list 'contains-keys? '% missing-keys)
(assoc :pred (list 'contains-keys? '% missing-keys)
:exoscale.lingo.explain.pred/spec :exoscale.lingo.pred/contains-keys
:exoscale.lingo.explain.pred/vals {:keys missing-keys}))))
(vals mk-by-path))))
Expand Down
12 changes: 12 additions & 0 deletions test/exoscale/lingo/test/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,22 @@
""
"\"\" is invalid - should contain more than 3 elements\n"

(s/and string? #(< 3 (count %)))
""
"\"\" is invalid - should contain more than 3 elements\n"

(s/and string? #(> 3 (count %)))
"asdfasf"
"\"asdfasf\" is invalid - should contain less than 3 elements\n"

(s/def ::cnt #(> (count %) 3))
""
"\"\" is an invalid :exoscale.lingo.test.core-test/cnt - should contain more than 3 elements\n"

(s/def ::cnt #(< (count %) 3))
"asdf"
"\"asdf\" is an invalid :exoscale.lingo.test.core-test/cnt - should contain less than 3 elements\n"

;; test the original unchanged msg
(s/and string? #(pos? (count %)))
""
Expand Down

0 comments on commit 6e5c360

Please # to comment.