From 6e5c360342153414c0b6c3019f9cc4e68466acd2 Mon Sep 17 00:00:00 2001 From: Max Penet Date: Sun, 24 Dec 2023 00:50:29 +0100 Subject: [PATCH] fix count/num-compare output depending on s-exp format --- src/exoscale/lingo.cljc | 64 ++++++++++++++----------- src/exoscale/lingo/impl.cljc | 10 ++-- test/exoscale/lingo/test/core_test.cljc | 12 +++++ 3 files changed, 52 insertions(+), 34 deletions(-) diff --git a/src/exoscale/lingo.cljc b/src/exoscale/lingo.cljc index f55181b..a50a191 100644 --- a/src/exoscale/lingo.cljc +++ b/src/exoscale/lingo.cljc @@ -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? diff --git a/src/exoscale/lingo/impl.cljc b/src/exoscale/lingo/impl.cljc index 6081cc0..b8585f8 100644 --- a/src/exoscale/lingo/impl.cljc +++ b/src/exoscale/lingo/impl.cljc @@ -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 @@ -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)))) diff --git a/test/exoscale/lingo/test/core_test.cljc b/test/exoscale/lingo/test/core_test.cljc index 93b1839..8975228 100644 --- a/test/exoscale/lingo/test/core_test.cljc +++ b/test/exoscale/lingo/test/core_test.cljc @@ -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 %))) ""