   (:require [ahnentafel.server.pages :refer :all])
   (:require [ahnentafel.gedcom.data :as data])
   (:require [clojure.test :refer :all]))
+
+(defn get-page [page & args]
+  (apply str (apply page
+                    {:version "x.x.x"
+                    :gedcom-file "/path/to/test.ged"
+                     :get-data (fn [] 'fake-data)}
+                    args)))
+
+(defmethod assert-expr 'page-contains? [msg form]
+  (let [text (nth form 1)
+        msg (or msg (str "Expected to find '" text "' on the page."))]
+    `(let [result# (.contains ~'page ~text)]
+       (do-report {:message ~msg :expected '~form :actual ~'page
+                   :type (if result# :pass :fail)}))))
+
+(defmethod assert-expr 'page-not-contains? [msg form]
+  (let [text (nth form 1)
+        msg (or msg (str "Expected not to find '" text "' on the page."))]
+    `(let [result# (.contains ~'page ~text)]
+       (do-report {:message ~msg :expected '~form :actual ~'page
+                   :type (if result# :fail :pass)}))))
+
+(defmacro are-on-page [& texts]
+  `(are [text] (~'page-contains? text) ~@texts))
 
 (deftest home-page-test
   (with-redefs [data/header (constantly {:number-of-records 23

                       :family-as-child "@FAM1@"
                       :family-as-spouse "@FAM2@"
                       })]
        (let [page (apply str (record {:get-data (fn [] 'fake-data)} "@I23@"))]
          (is (= @trapped-query {:xref "@I23@"}))
          (is (.contains page "INDIVIDUAL"))
          (is (.contains page "Bob Smith (a.k.a. Robert Smith, The Guy from The Cure)"))
          (is (.contains page "Sex: M"))
          (is (.contains page "Born: 1 JAN 1970 00:00:00 near his mother"))
          (is (.contains page "Died: 1 JAN 2000 00:00:00 graveside"))
          (is (.contains page "Buried: 2 JAN 2000 00:00:00 6 feet under"))
          (is (.contains page "<a href=\"/records/@FAM1@\">Go To Family (where this person was a child)</a>"))
          (is (.contains page "<a href=\"/records/@FAM2@\">Go To Family (where this person was a spouse)</a>") page)))))

  (testing "individual record - with parts missing"
    (with-redefs [data/find-record
                  (constantly {:type :individual})]
      (let [page (apply str (record {:xref "@I23@" :get-data (constantly 'fake-data)}))]
        (doseq [x '("Name" "Sex" "Born" "Died" "Buried" "Go To Family")]
          (is (not (.contains page x))
              (str "Should not find '" x "' on page.")))))))

                                         :encoding "ANSEL"
                                         :submitter {:xref "@I31@"
                                                     :name "Fred Smith"}})]
    (let [page (get-page home)]
      (are-on-page
       "<title>Ahnentafel</title>"
       "23 records in test.ged."
       "id=\"home-contents\""
       "created by Author"
       "for Recipient"
       "on 13 SEP 2000"
       "GEDCOM version 5.5.1 (LINEAGE-LINKED ANSEL)"
       "Submitted by <a href=\"/records/@I31@\">Fred Smith</a>"
       "<span id=\"version\">x.x.x</span>"))))

(deftest page-not-found-test
  (let [page (get-page not-found {:uri "/unknown"})]
    (are-on-page
     "<title>Ahnentafel</title>"
     "/unknown not found."
     "<span id=\"version\">x.x.x</span>")))

(deftest record-page-test
  (testing "individual record - maximum data"
