Skip to content

Commit

Permalink
Fix Uri interface and protocol implementations
Browse files Browse the repository at this point in the history
The Uri type was missing implementations for several of the methods
that are part of the protocols and interfaces that Uri declares as
implementing. The missing functions are:

* count from IPersistentCollection
* empty from IPersistentCollection
* cons from IPersistentCollection (used by conj)
* without from IPersistentMap (used by dissoc)
* iterator from Iterable which IPersistentMap implies (used by reduce)

There was also a problem with the implementation for entryAt which was
not returning the expected IMapEntry type causing a runtime error when
it was called (which find does).

All these problems are now fixed and tested with unit tests.
  • Loading branch information
griff committed Jan 30, 2015
1 parent 2c6d88a commit e1a83b1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
23 changes: 22 additions & 1 deletion src/org/bovinegenius/exploding_fish.clj
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
:query (query self value)
:fragment (fragment self value)
(Uri. (assoc data key value) metadata)))
(entryAt [self key] (data key))
(entryAt [_ key] (find data key))

clojure.lang.ILookup
(valAt [self key] (get data key))
Expand All @@ -179,6 +179,21 @@
(seq [self] (seq data))

clojure.lang.IPersistentMap
(without [self key]
(condp = key
:scheme (scheme self nil)
:scheme-relative (scheme-relative self nil)
:authority (authority self nil)
:user-info (user-info self nil)
:host (host self nil)
:port (port self nil)
:path (path self nil)
:query (query self nil)
:fragment (fragment self nil)
(Uri. (dissoc data key) metadata)))

Iterable
(iterator [_] (.iterator data))

clojure.lang.IFn
(invoke [self key]
Expand All @@ -198,6 +213,12 @@
(Integer. (.hashCode (str self)))) 2)))

clojure.lang.IPersistentCollection
(count [_] (count data))
(empty [_] (Uri. {} metadata))
(cons [self o]
(cond (instance? java.util.Map$Entry o) (assoc self (key o) (val o))
(vector? o) (assoc self (nth o 0) (nth o 1))
:else (reduce conj self o)))
(equiv [self other] (.equals self other))

clojure.lang.IObj
Expand Down
65 changes: 36 additions & 29 deletions test/org/bovinegenius/uri_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,37 @@

(deftest uri-test
(let [uri-string "http://www.fred.net/"]
(is (= (into {} (seq (uri uri-string)))
(into {} (seq (uri (URI. uri-string))))
(into {} (seq (uri (URL. uri-string))))
(into {} (seq (uri {:host "www.fred.net",
:path "/",
:authority "www.fred.net",
:scheme "http",
:scheme-relative "//www.fred.net/"})))
(is (= [:host "www.fred.net"] (find (uri uri-string) :host)))
(is (= 5 (count (uri uri-string))))
(is (= (uri {}) (empty (uri uri-string))))
(is (= (into {} (uri uri-string))
(into {} (uri (URI. uri-string)))
(into {} (uri (URL. uri-string)))
(into {} (uri {:host "www.fred.net"
:path "/"
:authority "www.fred.net"
:scheme "http"
:scheme-relative "//www.fred.net/"}))
(into {} (conj (uri "http://example.com/test") {:host "www.fred.net" :path "/"}))
(into {} (conj (uri "http://example.com/") [:host "www.fred.net"]))
(into {} (conj (uri "http://www.fred.net/test") (find {:path "/"} :path)))
(into {} (dissoc (uri "http://www.fred.net/#muh") :fragment))
{:host "www.fred.net",
:path "/",
:authority "www.fred.net",
:scheme "http",
:scheme-relative "//www.fred.net/"})))
(let [uri-string "http://www.domain.net/with?query=and#fragment"]
(is (= (into {} (seq (uri uri-string)))
(into {} (seq (uri (URI. uri-string))))
(into {} (seq (uri (URL. uri-string))))
(into {} (seq (uri {:host "www.domain.net",
:query "query=and",
:path "/with",
:authority "www.domain.net",
:scheme "http",
:scheme-relative "//www.domain.net/with?query=and",
:fragment "fragment"})))
(is (= (into {} (uri uri-string))
(into {} (uri (URI. uri-string)))
(into {} (uri (URL. uri-string)))
(into {} (uri {:host "www.domain.net"
:query "query=and"
:path "/with"
:authority "www.domain.net"
:scheme "http"
:scheme-relative "//www.domain.net/with?query=and"
:fragment "fragment"}))
{:host "www.domain.net",
:query "query=and",
:path "/with",
Expand All @@ -39,17 +46,17 @@
:scheme-relative "//www.domain.net/with?query=and",
:fragment "fragment"})))
(let [uri-string "http://www.domain.net:8080/with?query=and#fragment"]
(is (= (into {} (seq (uri uri-string)))
(into {} (seq (uri (URI. uri-string))))
(into {} (seq (uri (URL. uri-string))))
(into {} (seq (uri {:port 8080,
:host "www.domain.net",
:query "query=and",
:path "/with",
:authority "www.domain.net:8080",
:scheme "http",
:scheme-relative "//www.domain.net:8080/with?query=and",
:fragment "fragment"})))
(is (= (into {} (uri uri-string))
(into {} (uri (URI. uri-string)))
(into {} (uri (URL. uri-string)))
(into {} (uri {:port 8080
:host "www.domain.net"
:query "query=and"
:path "/with"
:authority "www.domain.net:8080"
:scheme "http"
:scheme-relative "//www.domain.net:8080/with?query=and"
:fragment "fragment"}))
{:port 8080,
:host "www.domain.net",
:query "query=and",
Expand Down

0 comments on commit e1a83b1

Please sign in to comment.