Skip to content

Commit

Permalink
Convert uuid.fnl and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
russtoku committed Aug 20, 2024
1 parent b717186 commit 7ff078f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 35 deletions.
38 changes: 38 additions & 0 deletions fnl/conjure-spec/uuid_spec.fnl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(local {: describe : it} (require :plenary.busted))
(local assert (require :luassert.assert))
(local a (require :nfnl.core))
(local uuid (require :conjure.uuid))

(describe "uuid"
(fn []
;; Helper: Is word in xs?
(fn is-in [word xs]
(a.some (fn [x] (= word x)) xs))

(describe "turns a UUID"
(fn []
(it "into something human-readable"
(fn []
(assert.are.equals "Wirehaired Pointing Griffon"
(uuid.pretty "c7ef277c-160c-45f4-a5c3-03ac16a93788")
)))
(it "into something human-readable but wrong string"
(fn []
(assert.are.not.equals "Wirehaired Pointing Griffon"
(uuid.pretty "d7ef277c-160c-45f4-a5c3-03ac16a93788")
)))))

(describe "generated UUID"
(fn []
(it "is turned into something human-readable"
(fn []
(assert.are.equals true
(is-in (uuid.pretty (uuid.v4))
uuid.cats-and-dogs))))
(it "has the correct format"
(fn []
(assert.are.not.equals nil
(string.match (uuid.v4)
"^%x+-%x+-%x+-%x+-%x+$"))))))))


21 changes: 11 additions & 10 deletions fnl/conjure/uuid.fnl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
(import-macros {: module : def : defn : defonce : def- : defn- : defonce- : wrap-last-expr : wrap-module-body : deftest} :nfnl.macros.aniseed)

(module conjure.uuid
{autoload {a conjure.aniseed.core
str conjure.aniseed.string}})
(local {: autoload} (require :nfnl.module))
(local a (autoload :nfnl.core))
(local str (autoload :nfnl.string))

;; Adapted from https://gist.github.com/jrus/3197011

Expand All @@ -15,7 +13,7 @@
; end)
; end

(defn v4 []
(fn v4 []
(string.gsub
"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
"[xy]"
Expand All @@ -24,7 +22,7 @@
(or (and (= $1 "x") (math.random 0 0xf))
(math.random 8 0xb)))))

(def- cats-and-dogs
(local cats-and-dogs
["Blue Lacy" "Queensland Heeler" "Rhod Ridgeback" "Retriever" "Chinese Sharpei" "Black Mouth Cur" "Catahoula" "Staffordshire" "Affenpinscher"
"Afghan Hound" "Airedale Terrier" "Akita" "Australian Kelpie" "Alaskan Malamute" "English Bulldog" "American Bulldog" "American English Coonhound"
"American Eskimo Dog" "American Foxhound" "American Hairless Terrier" "American Staffordshire Terrier" "American Water Spaniel" "Anatolian Shepherd Dog"
Expand Down Expand Up @@ -63,7 +61,7 @@
"Siberian cat" "Singapura cat" "Snowshoe cat" "Sokoke" "Somali cat" "Sphynx cat" "Suphalak" "Thai cat" "Thai Lilac" "Tonkinese cat" "Toyger"
"Turkish Angora" "Turkish Van" "Ukrainian Levkoy"])

(defn pretty [id]
(fn pretty [id]
"Turns a UUID into something human readable. This MASSIVELY reduces the
entropy but it should be good enough for a bunch of various UI / UX cases."
(if (a.string? id)
Expand All @@ -73,6 +71,9 @@

(comment
(v4)
(pretty (v4)))
(pretty (v4))
)

*module*
{: v4
: pretty
: cats-and-dogs}
38 changes: 38 additions & 0 deletions lua/conjure-spec/uuid_spec.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 7 additions & 25 deletions lua/conjure/uuid.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7ff078f

Please sign in to comment.