Codebase list kitchensink-clojure / c68ec3c
Merge pull request #84 from aperiodic/rand-strs (maint) Add `rand-str` function. Ryan Senior authored 7 years ago GitHub committed 7 years ago
2 changed file(s) with 59 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
77 (:import [org.ini4j Ini Config BasicProfileSection]
88 [javax.naming.ldap LdapName]
99 [java.io StringWriter Reader File])
10 (:require [clojure.test]
11 [clojure.tools.logging :as log]
10 (:require [clojure.tools.logging :as log]
1211 [clojure.string :as string]
1312 [clojure.tools.cli :as cli]
1413 [clojure.java.io :as io]
258257 (if (empty? selected)
259258 (last weights-and-values)
260259 (-> (first selected) second))))
260
261 (def ascii-character-sets
262 (let [concatv (comp vec concat)
263 ALPHA (mapv char (range 65 91))
264 alpha (mapv char (range 97 123))
265 digits (mapv char (range 48 58))
266 symbols (concatv (map char (range 33 48))
267 (map char (range 91 97))
268 (map char (range 123 127)))]
269 {:alpha (concatv alpha ALPHA)
270 :alpha-lower alpha
271 :alpha-upper ALPHA
272 :alpha-digits (concatv alpha ALPHA digits)
273 :alpha-digits-symbols (concatv alpha ALPHA digits symbols)
274 :symbols symbols
275 :digits digits}))
276
277 (defn rand-str
278 "Produces a random string of length n, drawn from the given collection of
279 characters. The following keywords may be used in place of a character
280 collection:
281 :alpha - [a-zA-Z]
282 :alpha-lower - [a-z]
283 :alpha-upper - [A-Z]
284 :alpha-digits - [a-zA-Z0-9]
285 :alpha-digits-symbols - all printable ASCII characters besides space
286 :symbols - all visible, non-alpha-numeric ASCII characters (no space)
287 :digits - [0-9]
288 If no character collection or keyword is provided, :alpha-digits-symbols is
289 used by default."
290 ([n] (rand-str :alpha-digits-symbols n))
291 ([characters n]
292 (let [char-coll (cond
293 (and (keyword? characters) (contains? ascii-character-sets characters))
294 (get ascii-character-sets characters)
295
296 (keyword? characters)
297 (throw (IllegalArgumentException.
298 (str characters " is not a recognized character collection keyword")))
299
300 :else (vec characters))]
301 (apply str (repeatedly n #(rand-nth char-coll))))))
261302
262303 ;; ## Collection operations
263304
203203 (testing "a weight is not numeric"
204204 (is (thrown? AssertionError (rand-weighted-selection :foo :bar)))))))
205205
206 (deftest rand-str-test
207 (testing "rand-str"
208 (testing "throws an IllegalArgumentException when given an unknown characters keyword"
209 (is (thrown-with-msg? IllegalArgumentException #":CJK" (rand-str :CJK 42))))
210
211 (doseq [[kw cs] ascii-character-sets
212 :let [cs (set cs)]]
213 (testing (str "recognizes the " kw " character set keyword")
214 (dotimes [_ 10]
215 (is (every? cs (rand-str kw 1000))))))
216
217 (testing "uses collections of strings & characters as character sets"
218 (let [as ["a" \a]]
219 (dotimes [_ 100]
220 (is (every? #(= % \a) (rand-str as 100))))))))
221
206222 (deftest excludes?-test
207223 (testing "should return true if coll does not contain key"
208224 (is (excludes? {:foo 1} :bar)))