Skip to content

Instantly share code, notes, and snippets.

@arnaudbos
Created June 25, 2020 15:27
Show Gist options
  • Save arnaudbos/43a1928854ec44886590aa51860d47ff to your computer and use it in GitHub Desktop.
Save arnaudbos/43a1928854ec44886590aa51860d47ff to your computer and use it in GitHub Desktop.
I got sidetracked
(defonce full-date-pattern (DateTimeFormatter/ofPattern "yyyy/MM/dd"))
(defonce birthday-pattern (DateTimeFormatter/ofPattern "MM/dd"))
(defn is-birthday? [day birthday]
(string/ends-with?
birthday
(.format day birthday-pattern)))
(with-test
#'is-birthday?
(->>
(repeatedly #(let [year (+ 1000 (rand-int 1020))
month (format "%02d" (inc (rand-int 12)))
day (inc (rand-int 31))
day (if (and (= month 2) (> day 28)) 28 day)
other-day (if (= (rand-int 2) 0) day (inc (rand-int 31)))
day (format "%02d" day)
other-day (format "%02d" other-day)]
(vector
[year month day]
[year month other-day])))
(take 1000)
(map (fn [[[year1 month1 day1] [year2 month2 day2]]]
[(str year1 "/" month1 "/" day1)
(str year2 "/" month2 "/" day2)]))
(map (fn [[d1 d2]]
(let [date1 (LocalDate/parse d1 full-date-pattern)
date2 (LocalDate/parse d2 full-date-pattern)]
[date1 (.format date2 birthday-pattern) (= 0 (compare date1 date2))])))
(mapv
(fn [[day bday expected]]
(let [day-str (.format day birthday-pattern)
actual (is-birthday? day bday)]
(testing (str day-str " should be" (when expected " be " " not be ") "considered to be the same day as " bday)
(if expected
(is actual)
(is (not actual)))))))))
(test-ns *ns*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment