From 9a065943c9b159ac1ed3bb6a147de3665e807155 Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 27 Jul 2022 11:02:30 +0200 Subject: [PATCH] Add unittests for predicates --- src/main/cljc/dda/c4k_common/predicate.cljc | 27 +++++++++++++++++-- .../cljc/dda/c4k_common/predicate_test.cljc | 13 +++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/test/cljc/dda/c4k_common/predicate_test.cljc diff --git a/src/main/cljc/dda/c4k_common/predicate.cljc b/src/main/cljc/dda/c4k_common/predicate.cljc index 07ccf7c..2917577 100644 --- a/src/main/cljc/dda/c4k_common/predicate.cljc +++ b/src/main/cljc/dda/c4k_common/predicate.cljc @@ -1,4 +1,8 @@ -(ns dda.c4k-common.predicate) +(ns dda.c4k-common.predicate + (:require + [clojure.string :as str] + #?(:clj [clojure.edn :as edn] + :cljs [cljs.reader :as edn]))) (defn bash-env-string? [input] @@ -10,6 +14,10 @@ (and (string? input) (some? (re-matches #"(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)" input)))) +(defn list-of-separated-by? + [input spec-function separator] + (every? true? (map spec-function (str/split input separator)))) + (defn letsencrypt-issuer? [input] (contains? #{"prod" "staging"} input)) @@ -20,4 +28,19 @@ (defn pvc-storage-class-name? [input] - (contains? #{:manual :local-path} input)) \ No newline at end of file + (contains? #{:manual :local-path} input)) + +(defn port-number? + [input] + (and (integer? input) + (> input 0) + (<= input 65535))) + +(defn host-and-port-string? + [input] + (and (string? input) + (let [split-string (str/split input #":")] + (and (= (count split-string) 2) + (fqdn-string? (first split-string)) + (port-number? (edn/read-string (second split-string))))))) + diff --git a/src/test/cljc/dda/c4k_common/predicate_test.cljc b/src/test/cljc/dda/c4k_common/predicate_test.cljc new file mode 100644 index 0000000..abe2c61 --- /dev/null +++ b/src/test/cljc/dda/c4k_common/predicate_test.cljc @@ -0,0 +1,13 @@ +(ns dda.c4k-common.predicate-test + (:require + #?(:clj [clojure.test :refer [deftest is are testing run-tests]] + :cljs [cljs.test :refer-macros [deftest is are testing run-tests]]) + [clojure.spec.test.alpha :as st] + [dda.c4k-common.predicate :as cut])) + + +(deftest test-bash-env-string? + (is (true? (cut/bash-env-string? "abcd"))) + (is (false? (cut/bash-env-string? "$abdc"))) + (is (false? (cut/bash-env-string? "\"abdc")))) +