(ns microformats.parser-expectations (:require [expectations :refer :all] [microformats.parser :refer :all] [clojure.zip :as z] [clojure.tools.trace :refer :all] [net.cgrand.enlive-html :refer [html-snippet]])) (untrace-ns 'microformats.parser) ;(trace-ns 'microformats.parser) (defn- snippets [html] (map z/xml-zip (html-snippet html))) (defn- snippet [html] (first (snippets html))) (expect [:location] (into [] ((classes-to-props "p-") ["someclass" "p-location" "someotherclass"]))) (expect [:location :name] (into [] ((classes-to-props "p-") ["someclass" "p-location" "someotherclass" "p-name"]))) (expect "Foo bar" (apply str (#'microformats.parser/node-to-html '("Foo " {:tag :strong, :attrs nil, :content ("bar")})))) (expect "665 3rd St. Suite 207" (->> "

665 3rd St. Suite 207

" snippet z/node :content (#'microformats.parser/node-to-text))) (expect {:tag :div :attrs {:class "h-card"} :content ["\n" {:tag :p :attrs nil :content [{:tag :a :attrs {:class "h-org"} :content nil}]} "\n"]} (z/node (snippet "

"))) (expect "http://example.com" (get-base-url (snippet ""))) (expect "/" (normalise-url (snippet "") "/")) (expect "http://example.com/" (normalise-url (snippet "") "/")) (expect {:name '("Name")} (parse-p (snippet "

Name

"))) (expect {:name '("Name")} (parse-p (snippet "

Not this name

"))) (expect {:name '("Name Endname")} (parse-p (snippet "

Name (this should not be included) Endname

"))) (expect {:name '("Example User")} (parse-p (snippet "

Example User

"))) (expect {:nickname '("exuser")} (parse-p (snippet "

exuser

"))) (expect {:name '("Example User")} (parse-p (snippet "\"Example"))) (expect {:name '("Example User")} (parse-p (snippet "@example"))) (expect {:name '("@example")} (parse-p (snippet "@example"))) (expect {:name '("Example User")} (parse-p (snippet ""))) (expect {:name '("Example User")} (parse-p (snippet "Example User"))) (expect {:name '("")} (parse-p (snippet "
"))) (expect {:name '("")} (parse-p (snippet "
"))) (expect {:url '("http://example.com")} (parse-u (snippet "Awesome example website"))) (expect {:photo '("http://example.com/someimage.png")} (parse-u (snippet ""))) (expect {:photo '("http://example.com/someimage.png")} (parse-u (snippet ""))) (expect {:photo '("http://example.com/someimage.png")} (parse-u (snippet ""))) (expect {:photo '("http://example.com/someimage.png")} (parse-u (snippet ""))) (expect {:photo '("http://example.com/someimage.png")} (parse-u (snippet "http://example.com/someimage.png"))) (expect {:photo '("http://example.com/someimage.png")} (parse-u (snippet ""))) (expect {:photo '("http://example.com/someimage.png")} (parse-u (snippet "http://example.com/someimage.png"))) (expect {:start '("2012-08-05T14:50")} (parse-dt (snippet ""))) (expect {:start '("2012-08-05T14:50")} (parse-dt (snippet ""))) (expect {:start '("2012-08-05T14:50")} (parse-dt (snippet ""))) (expect {:end '("2012-08-05T18:00")} (parse-dt (snippet ""))) (expect {:start '("2012-08-05T14:50")} (parse-dt (snippet ""))) (expect {:start '("2012-08-05T14:50")} (parse-dt (snippet "2012-08-05T14:50"))) (expect {:start '("2012-08-05T14:50")} (parse-dt (snippet ""))) (expect {:start '("2012-08-05T14:50")} (parse-dt (snippet "2012-08-05T14:50"))) (expect {:start '("2012-08-05T14:50")} (parse-dt (snippet ""))) (expect {:start '("2012-08-05T14:50")} (parse-dt (snippet "2012-08-05T14:50"))) (expect {:content '({:html "Here is a load of embedded markup" :value "Here is a load of embedded markup"})} (parse-e (snippet "
Here is a load of embedded markup
"))) (expect {:author '("http://example.com/a")} (parse-rels (snippets "author a"))) (expect {:author '("http://example.com/a")} (parse-rels (snippets " author a"))) (expect {:author '("http://example.com/a" "http://example.com/b")} (parse-rels (snippets "author a author b"))) (expect {:items nil :rels {}} (parse "")) (expect {:items '({:properties {:name ("Example User")} :type ("h-card")}) :rels {}} (parse "

Example User

")) (expect {:items '({:properties {:name ("Example User")} :type ("h-card")}) :rels {}} (parse "

Example User

")) (expect {:items '({:properties {:name ("Example User")} :type ("h-card")}) :rels {}} (parse "

Example User

")) (expect {:items '({:properties {:name ("Example User") :url ("http://example.com")} :type ("h-card")}) :rels {}} (parse "
Example User
")) (expect {:items '({:type ("h-card") :properties {:name ("Example User")}}) :rels {}} (parse "

Example User

")) (expect {:items '({:type ("h-card") :properties {:name ("Example User")}}) :rels {}} (parse "

Example User

")) (expect {:items '({:type ("h-card") :properties {:name ("Example User") :url ("http://example.com")}}) :rels {}} (parse "
Example User")) (expect {:items '({:type ("h-card") :properties {:name ("Example User") :photo ("http://example.com/me.png")}}) :rels {}} (parse "\"Example")) (expect {:items '({:type ("h-card") :properties {:name ("Example User")}}) :rels {}} (parse "Incorrect")) (expect {:items '({:type ("h-card") :properties {:name ("Example User")}}) :rels {}} (parse "

\"Example

")) (expect {:items '({:type ("h-card") :properties {:name ("Example User")}}) :rels {}} (parse "

Wrong

")) (expect {:items '({:type ("h-card") :properties {:name ("Example User")}}) :rels {}} (parse "
\"Example
")) (expect {:items '({:type ("h-card") :properties {:name ("Example User")}}) :rels {}} (parse "

Wrong

")) (expect {:items '({:type ("h-card") :properties {:photo ("http://example.com/me.png")}}) :rels {}} (parse "")) (expect {:items '({:type ("h-card") :properties {:name ("Example User") :photo ("http://example.com/me.png")}}) :rels {}} (parse "
\"Example
")) (expect {:items '({:type ("h-card") :properties {:photo ("http://example.com/me.png")}}) :rels {}} (parse "
")) (expect {:items '({:type ("h-card") :properties {:photo ("http://example.com/me.png")}}) :rels {}} (parse "
")) (expect {:items '({:type ("h-card") :properties {:name ("Example User") :photo ("http://example.com/me.png")}}) :rels {}} (parse "
\"Example
")) (expect {:items '({:type ("h-card") :properties {:name ("Example User") :url ("http://example.com")}}) :rels {}} (parse "
Example User
")) (expect {:items '({:type ("h-card") :properties {:name ("Example") :url ("http://example.com" "http://myblog.com")}}) :rels {}} (parse "

Example

My Website My Blog
")) (expect {:items '({:type ("h-card") :properties {:name ("Example User") :url ("http://example.com" "http://myblog.com")}}) :rels {}} (parse "
Example User My Blog
")) (expect {:items '({:type ("h-card") :properties {:name ("Example") :tel ("01234567890")}}) :rels {}} (parse "

Example

01234567890


")) (expect {:items '({:type ("h-adr"), :properties {:street-address ("665 3rd St."), :extended-address ("Suite 207"), :locality ("San Francisco"), :region ("CA"), :postal-code ("94107"), :country-name ("U.S.A."), :name ("665 3rd St. Suite 207 San Francisco, CA 94107 U.S.A.")}}) :rels {}} (parse "

665 3rd St. Suite 207 San Francisco, CA 94107 U.S.A.

")) (expect {:items '({:type ("h-card" "h-org") :properties {:name ("Example")}}) :rels {}} (parse "

Example

")) (expect {:items '({:type ("h-card") :properties {:name ("John Doe") :org ({:value "Example" :type ("h-card" "h-org") :properties {:name ("Example")}})}}) :rels {}} (parse "
John Doe Example
")) (expect {:items '({:type ("h-card") :properties {:name ("Example User") :url ("http://example.com/")}}) :rels {}} (parse "
Example User
")) (expect {:items '({:type ("h-card") :properties {:name ("Example User") :url ("http://example.com/")}}) :rels {}} (parse "
Example User
")) (expect {:items '({:type ("h-card") :properties {:name ("Example User") :photo ("http://example.com/me.png")}}) :rels {}} (parse "
\"Example
")) (expect {:items '({:type ("h-card") :properties {:name ("Example User") :photo ("http://example.com/me.png")}}) :rels {}} (parse "
\"Example
")) (expect {:items '({:type ("h-card"), :properties {:photo ("http://blog.mozilla.org/press/files/2012/04/mitchell-baker.jpg"), :url ("http://blog.lizardwrangler.com/" "https://twitter.com/MitchellBaker"), :name ("Mitchell Baker"), }}) :rels {}} (parse "
Mitchell Baker MitchellBaker
"))