Add unittests for predicates

This commit is contained in:
erik 2022-07-27 11:02:30 +02:00
parent 16c440b872
commit 9a065943c9
2 changed files with 38 additions and 2 deletions

View file

@ -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? (defn bash-env-string?
[input] [input]
@ -10,6 +14,10 @@
(and (string? input) (and (string? input)
(some? (re-matches #"(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)" 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? (defn letsencrypt-issuer?
[input] [input]
(contains? #{"prod" "staging"} input)) (contains? #{"prod" "staging"} input))
@ -20,4 +28,19 @@
(defn pvc-storage-class-name? (defn pvc-storage-class-name?
[input] [input]
(contains? #{:manual :local-path} input)) (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)))))))

View file

@ -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"))))