Created
March 8, 2012 03:07
-
-
Save hugoduncan/1998319 to your computer and use it in GitHub Desktop.
multi-version dispatched functions
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
(ns version-dispatch.dispatch-test | |
(:use | |
clojure.test | |
version-dispatch.dispatch)) | |
(def os-hierarchy | |
(-> (make-hierarchy) | |
(derive :linux :os) | |
;; base distibutions | |
(derive :rh-base :linux) | |
(derive :debian-base :linux) | |
(derive :arch-base :linux) | |
(derive :suse-base :linux) | |
(derive :bsd-base :linux) | |
(derive :gentoo-base :linux) | |
;; distibutions | |
(derive :centos :rh-base) | |
(derive :rhel :rh-base) | |
(derive :amzn-linux :rh-base) | |
(derive :fedora :rh-base) | |
(derive :debian :debian-base) | |
(derive :ubuntu :debian-base) | |
(derive :jeos :debian-base) | |
(derive :arch :arch-base) | |
(derive :gentoo :gentoo-base) | |
(derive :darwin :bsd-base) | |
(derive :osx :bsd-base))) | |
(deftest range-compare-test | |
(is (neg? (range-compare ["1.2" "1.3"] ["1.0" "1.4"]))) | |
(is (pos? (range-compare ["1.0" "1.4"] ["1.2" "1.3"]))) | |
(is (zero? (range-compare ["1.2" "1.3"] ["1.2" "1.3"]))) | |
(is (neg? (range-compare ["1.2" "1.3"] ["1.2" "1.4"]))) | |
(is (neg? (range-compare ["1.3" "1.4"] ["1.2" "1.4"])))) | |
(deftest version-less-test | |
(is (neg? (version-less "1.2" "1.3"))) | |
(is (pos? (version-less "1.3" "1.2"))) | |
(is (zero? (version-less "1.2" "1.2")))) | |
(deftest compare-match-test | |
(testing "strings" | |
(is (false? (compare-match | |
os-hierarchy | |
[{:os :ubuntu :os-version "1.2" :version "1.1"} :unused] | |
[{:os :ubuntu :os-version "1.2" :version "1.1"} :unused]))) | |
(is (true? (compare-match | |
os-hierarchy | |
[{:os :ubuntu :os-version "1.2" :version "1.1"} :unused] | |
[{:os :ubuntu :os-version "1.3" :version "1.1"} :unused]))) | |
(is (false? (compare-match | |
os-hierarchy | |
[{:os :ubuntu :os-version "1.3" :version "1.1"} :unused] | |
[{:os :ubuntu :os-version "1.2" :version "1.1"} :unused]))))) | |
(defmulti-version os-ver [os os-ver ver arg] #'os-hierarchy) | |
(multi-version-method | |
os-ver {:os :rhel :os-version "1.0" :version nil} | |
[id os-version version arg] | |
[arg 1]) | |
(multi-version-method | |
os-ver {:os :rh-base :os-version ["2.0" nil] :version "2.1"} | |
[os os-version version arg] | |
[arg 2]) | |
(multi-version-method | |
os-ver {:os :centos :os-version [nil "1.0"] :version [nil "3.1"]} | |
[os os-version version arg] | |
[arg 3]) | |
(multi-version-method | |
os-ver {:os :debian :os-version ["1.0" "2.0"] :version ["4.1" "4.3"]} | |
[os os-version version arg] | |
[arg 4]) | |
(multi-version-method | |
os-ver {:os :ubuntu :os-version ["1.0" "2.0"] :version ["4.1" "4.3"]} | |
[os os-version version arg] | |
[arg 5]) | |
(multi-version-method | |
os-ver {:os :ubuntu :os-version ["1.2" "1.3"] :version ["4.1" "4.3"]} | |
[os os-version version arg] | |
[arg 6]) | |
(multi-version-method | |
os-ver {:os :ubuntu :os-version ["1.1" "1.4"] :version ["4.1" "4.3"]} | |
[os os-version version arg] | |
[arg 7]) | |
(deftest basic | |
(is (:hierarchy (meta #'os-ver))) | |
(is (:methods (meta #'os-ver))) | |
(testing "basic dispatch" | |
(is (= [::arg 1] (os-ver :rhel "1.0" "9.9" ::arg))) | |
(is (= [::arg 2] (os-ver :rhel "3.0" "2.1" ::arg))) | |
(is (= [::arg 3] (os-ver :centos "0.9.1" "3.1" ::arg))) | |
(is (= [::arg 4] (os-ver :debian "1.9.1" "4.3" ::arg)))) | |
(testing "overlapped dispatch" | |
(is (= [::arg 6] (os-ver :ubuntu "1.25" "4.2" ::arg))))) |
Author
hugoduncan
commented
Mar 8, 2012
An open question for me here is version normalization. If someone says this is ubuntu oniric, how do we go from this to a version number that can be compared to.
The other question is about whether / how something like this could be extended to packages, and then relationships between versions. E.g. this version of this requires such version of that...
The final question is how to use this to generate version numbers. For example, I want to download a file based on a particular version of OS. I need the version number with certain format (e.g. codename instead of version number).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment