Find file
Fetching contributors…
Cannot retrieve contributors at this time
101 lines (84 sloc) 3.06 KB
(:use [clojure.test]
(:require [clojure.xml :as xml]
[ :as zip]))
(defn parse-str [s]
(zip/xml-zip (xml/parse (new org.xml.sax.InputSource
(new s)))))
(def atom1 (parse-str "<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns=''>
<title type='text'>n01senet</title>
<link rel='alternate' type='text/html' href=''/>
<title type='text'>clojure is the best lisp yet</title>
<title type='text'>experimenting with vnc</title>
(deftest test-simple-single-function filter
(is (= (xml-> atom1 #(:tag (zip/node %)))
(deftest test-two-stage-filter
(testing "using helpful query prediates"
(is (= (xml-> atom1 (tag= :title) text)
(testing "keyword shortcut"
(is (= (xml-> atom1 :title text)
(deftest test-multi-stage-filter
(testing "basic functionality"
(is (= (xml-> atom1 :entry :author :name text)
'("Chouser" "agriffis"))))
(testing "subquery specified using a vector"
(is (= (xml-> atom1 :entry [:author :name (text= "agriffis")]
:id text)
(testing "with string shortcut"
(is (= (xml-> atom1 :entry [:author :name "agriffis"] :id text)
(deftest test-xml1->
(is (= (xml1-> atom1 :entry :author :name text)
(deftest test-attribute-access
(is (= (xml-> atom1 :title (attr :type))
(deftest test-attribute-filtering
(is (= (xml-> atom1 :link [(attr= :rel "alternate")] (attr :type))
;; This was in the original code under a comment, but is fails
#_(deftest test-ancestors
(testing "basic function"
(is (= (xml-> atom1 descendants :id "2" ancestors zip/node #(:tag %))
'(:id :entry :feed))))
(testing "with non-auto tag= (:entry), followed by auto tag= (:id)"
(is (= (xml-> atom1 descendants :name "Chouser" ancestors
:entry :id text)
(defrecord Node [tag attrs content])
(def record
(Node. :root {}
[(Node. :entry {} [(Node. :id {} ["1"])
(Node. :author {:type "text"} ["John"])])
(Node. :entry {} [(Node. :id {} ["2"])
(Node. :author {:type "text"} ["Jane"])])])))
(deftest test-records
(is (= (xml-> record :entry :author text)
'("John" "Jane"))))
(def atom2 (parse-str "<feed>
(deftest dzip-3-first-node
(is (not= (xml-> atom2 :feed ) '())))
(deftest dzip-3-second-node
(is (= (xml-> atom2 :foo text) '("bar"))))