-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves issue "Invalid regex pattern in cljc file #21"
- Loading branch information
Showing
6 changed files
with
192 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
(ns tupelo.uuid | ||
(:use tupelo.core) | ||
(:refer-clojure :exclude [rand]) | ||
(:require | ||
[clj-uuid :as uuid] | ||
[clojure.core :exclude [rand]] | ||
[schema.core :as s] | ||
[tupelo.schema :as tsk] | ||
[tupelo.string :as str] | ||
[tupelo.core.impl :as impl]) | ||
(:import | ||
[java.util UUID])) | ||
|
||
(def const-null-str | ||
"A null UUID string '00000000-0000-0000-0000-000000000000' " | ||
"00000000-0000-0000-0000-000000000000") | ||
|
||
(def const-dummy-str | ||
"A dummy UUID string '00000000-0000-0000-0000-000000000000' " | ||
"cafebabe-1953-0510-0970-0123456789ff") | ||
|
||
; #todo add code & tests for cljs | ||
#?(:clj | ||
(do | ||
|
||
(def uuid-str? ; #todo add tests for cljc | ||
"Returns true iff the string shows a valid UUID-like pattern of hex digits. Does not | ||
distinguish between UUID subtypes." | ||
impl/uuid-str?) | ||
|
||
(def null-str | ||
"Returns a null UUID string '00000000-0000-0000-0000-000000000000' " | ||
(const->fn const-null-str)) | ||
|
||
(def dummy-str | ||
"Returns a dummy UUID string 'cafebabe-1953-0510-0970-0123456789ff' " | ||
(const->fn const-dummy-str)) | ||
|
||
(def const-null-obj | ||
"A null UUID object '00000000-0000-0000-0000-000000000000' " | ||
(UUID/fromString const-null-str)) | ||
|
||
(def const-dummy-obj | ||
"A dummy UUID object '00000000-0000-0000-0000-000000000000' " | ||
(UUID/fromString const-dummy-str)) | ||
|
||
(def null | ||
"Returns a null UUID object '00000000-0000-0000-0000-000000000000' " | ||
(const->fn const-null-obj)) | ||
|
||
(def dummy | ||
"Returns a dummy UUID object 'cafebabe-1953-0510-0970-0123456789ff'" | ||
(const->fn const-dummy-obj)) | ||
|
||
;----------------------------------------------------------------------------- | ||
(s/defn rand :- UUID | ||
"Returns a random uuid as a UUID object" | ||
[] (uuid/v4)) | ||
|
||
(s/defn rand-str :- s/Str | ||
"Returns a random uuid as a String" | ||
[] (str (tupelo.uuid/rand))) | ||
|
||
;----------------------------------------------------------------------------- | ||
(def ^:no-doc uuid-counter (atom nil)) ; uninitialized | ||
(defn counted-reset! [] (reset! uuid-counter 0)) | ||
(counted-reset!) ; initialize upon load | ||
|
||
(defn counted-str [] | ||
(let [cnt (swap-out! uuid-counter inc) | ||
uuid-str (format "%08x-aaaa-bbbb-cccc-ddddeeeeffff" cnt)] | ||
uuid-str)) | ||
|
||
(defn counted [] | ||
(UUID/fromString (counted-str))) | ||
|
||
;----------------------------------------------------------------------------- | ||
(defmacro with-null | ||
"For testing, replace all calls to uuid/rand with uuid/null" | ||
[& forms] | ||
`(with-redefs [rand null] | ||
~@forms)) | ||
|
||
(defmacro with-counted | ||
"For testing, replace all calls to uuid/rand with uuid/counted" | ||
[& forms] | ||
`(with-redefs [rand counted] | ||
(counted-reset!) | ||
~@forms)) | ||
|
||
)) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
(ns tst.tupelo.uuid | ||
(:use tupelo.core | ||
tupelo.test) | ||
(:refer-clojure :exclude [rand]) | ||
(:require | ||
[tupelo.uuid :as uuid])) | ||
|
||
; #todo add tests for cljs | ||
#?(:clj | ||
(do | ||
|
||
(verify | ||
(is= "00000000-0000-0000-0000-000000000000" | ||
(uuid/null-str) | ||
(str (uuid/null))) | ||
(is= "cafebabe-1953-0510-0970-0123456789ff" | ||
(uuid/dummy-str) | ||
(str (uuid/dummy))) | ||
|
||
(is (uuid? (uuid/null))) | ||
(is (uuid? (uuid/dummy))) | ||
(is (uuid/uuid-str? (uuid/null-str))) | ||
(is (uuid/uuid-str? (uuid/dummy-str))) | ||
|
||
(isnt (uuid/uuid-str? "cafebabe-1953-0510-0970-0123456789fff")) | ||
(isnt (uuid/uuid-str? "cafebabe-1953-0510-09700-123456789ff")) | ||
(isnt (uuid/uuid-str? "cafebabe-1953-0510-066x-0123456789ff")) | ||
(isnt (uuid/uuid-str? "cafebabe-1953-0510-0123456789ff")) | ||
(isnt (uuid/uuid-str? "cafebabe-1953-0510|0970-0123456789ff")) | ||
(isnt (uuid/uuid-str? 5)) | ||
(isnt (uuid/uuid-str? :nope)) | ||
(isnt (uuid/uuid-str? nil)) | ||
|
||
; we return uuids as an object or a string | ||
(is= java.util.UUID (type (uuid/rand))) | ||
(is (string? (uuid/rand-str))) | ||
(is (with-exception-default false | ||
(dotimes [i 99] ; 2 uuids are never equal | ||
(assert (not= (uuid/rand) (uuid/rand))) | ||
(assert (not= (uuid/rand-str) (uuid/rand-str)))) | ||
true)) ; if no failures, we pass the test | ||
|
||
; demonstrate uuid/with-null usage for testing | ||
(uuid/with-null | ||
(is= (uuid/rand-str) "00000000-0000-0000-0000-000000000000") | ||
(is= (uuid/rand-str) "00000000-0000-0000-0000-000000000000") | ||
(is= (uuid/rand-str) "00000000-0000-0000-0000-000000000000")) | ||
|
||
; demonstrate uuid/with-counted for testing | ||
(uuid/with-counted | ||
(is= (uuid/rand-str) "00000000-aaaa-bbbb-cccc-ddddeeeeffff") | ||
(is= (uuid/rand-str) "00000001-aaaa-bbbb-cccc-ddddeeeeffff") | ||
(is= (uuid/rand-str) "00000002-aaaa-bbbb-cccc-ddddeeeeffff") | ||
(let [r1 (uuid/rand) | ||
r2 (uuid/rand-str)] | ||
(is= (type r1) java.util.UUID) | ||
(is= (type r2) java.lang.String) | ||
(is= (str r1) "00000003-aaaa-bbbb-cccc-ddddeeeeffff") | ||
(is= r2 "00000004-aaaa-bbbb-cccc-ddddeeeeffff") | ||
(is (uuid/uuid-str? r2)))) | ||
|
||
; demonstrate uuid/counted (manual) | ||
(uuid/counted-reset!) | ||
(is= (uuid/counted-str) "00000000-aaaa-bbbb-cccc-ddddeeeeffff") | ||
(is= (uuid/counted-str) "00000001-aaaa-bbbb-cccc-ddddeeeeffff") | ||
(is= (uuid/counted-str) "00000002-aaaa-bbbb-cccc-ddddeeeeffff") | ||
(uuid/counted-reset!) | ||
(is= (uuid/counted-str) "00000000-aaaa-bbbb-cccc-ddddeeeeffff") | ||
(is= (uuid/counted-str) "00000001-aaaa-bbbb-cccc-ddddeeeeffff") | ||
) | ||
|
||
)) |