Skip to content

Commit

Permalink
Made link? more consistent with click & renamed -in? to some-
Browse files Browse the repository at this point in the history
  • Loading branch information
glenjamin committed Dec 3, 2014
1 parent 1ad9f60 commit ad40eed
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 56 deletions.
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -170,13 +170,13 @@ The api namespace for testing is ```kerodon.test```. This uses the same machine
The main function is ```has```. It allows the verifications to compose using ->. It requires one of the verification functions, and an optional error message.

You can use ```status?``` to validate the status code of the last response.
You can use ```text?``` or ```regex?``` to validate the text in the page.
You can use ```text-in?``` or ```regex-in?``` to look for text anywhere in the page.
You can use ```text?``` or ```regex?``` to validate the text in the page/element.
You can use ```some-text?``` or ```some-regex?``` to look for text anywhere in the page/element.
You can use ```value?``` to validate the value of a field. The
selector can be the text or css of a label with a for element, or the
css of the field itself.
You can use ```attr?``` to validate an attribute's value.
You can use ```link?``` to look for an anchor tag with matching href, or (optionally) text.
You can use ```link?``` to look for an anchor tag with matching text, href or both.
You can use ```heading?``` to look for a heading (h1 - h6) with matching text.

```clojure
Expand All @@ -186,7 +186,7 @@ You can use ```heading?``` to look for a heading (h1 - h6) with matching text.
"page is found")
(has (text? "hello world")
"page says hello world")
(has (regex? ".*world.*")
(has (some-text? "world")
"page includes 'world'"))

(-> (session ring-app)
Expand All @@ -202,9 +202,9 @@ You can use ```heading?``` to look for a heading (h1 - h6) with matching text.

(-> (session ring-app)
(visit "/welcome")
(has (link? "/session/new")
(has (link? "Login")
"page has link to login")
(has (link? :text "Login")
(has (link? :href "/session/new")
"login link text is 'Login'"))
```

Expand Down
42 changes: 23 additions & 19 deletions src/kerodon/test.clj
Expand Up @@ -36,39 +36,43 @@
~expected
(~'regex? ~expected)))

(defmacro regex-in? [expected]
(defmacro some-regex? [expected]
`(validate re-find?
#(apply str (enlive/texts (:enlive %)))
~expected
(~'regex-in? ~expected)))
(~'some-regex? ~expected)))

(defmacro text? [expected]
`(validate =
#(apply str (enlive/texts (:enlive %)))
~expected
(~'text? ~expected)))

(defmacro text-in? [expected]
(defmacro some-text? [expected]
`(validate #(.contains %1 %2)
#(apply str (enlive/texts (:enlive %)))
~expected
(~'text-in? ~expected)))
(~'some-text? ~expected)))

(defmacro link? [key & [val]]
`(validate #(let [href# (:href %2)
text# (:text %2)]
(some (fn [link#]
(or (and href# (= href# (:href link#)))
(and text# (= text# (:text link#)))))
%1))
#(map (fn [link#]
{:href (get-in link# [:attrs :href])
:text (apply str (enlive/texts (:content link#)))})
(enlive/select (:enlive %) [:a]))
(if (nil? ~val)
{:href ~key}
{~key ~val})
(~'link? ~key ~val)))
(defn submap?
"Checks whether m contains all entries in sub."
[^java.util.Map m ^java.util.Map sub]
(.containsAll (.entrySet m) (.entrySet sub)))

(defmacro link? [text & [href]]
(let [expected (if (nil? href)
(list 'link? text)
(list 'link? text href))]
`(validate (fn [coll# search#] (some #(submap? %1 search#) coll#))
#(map (fn [link#]
{:href (get-in link# [:attrs :href])
:text (apply str (enlive/texts (:content link#)))})
(enlive/select (:enlive %) [:a]))
(cond
(nil? ~href) {:text ~text}
(= :href ~text) {:href ~href}
:else {:text ~text :href ~href})
~expected)))

(defmacro heading? [expected]
`(validate #(some (partial = %2) %1)
Expand Down
68 changes: 37 additions & 31 deletions test/kerodon/unit/test.clj
Expand Up @@ -94,26 +94,26 @@
#(text? "yes")
state)))))

(deftest test-text-in?
(testing "text-in?"
(deftest test-some-text?
(testing "some-text?"
(let [state {:enlive (parse [:p "this is a test"])}]
(testing "fails if text is not found"
(check-report {:type :fail
:expected '(text-in? "foo")
:expected '(some-text? "foo")
:actual "this is a test"}
#(text-in? "foo")
#(some-text? "foo")
state))
(testing "fails if content is subset of matcher"
(check-report {:type :fail
:expected '(text-in? "and this is a test")
:expected '(some-text? "and this is a test")
:actual "this is a test"}
#(text-in? "and this is a test")
#(some-text? "and this is a test")
state))
(testing "passes if text is found"
(check-report {:type :pass
:expected '(text-in? "is a")
:expected '(some-text? "is a")
:actual "this is a test"}
#(text-in? "is a")
#(some-text? "is a")
state)))))

(deftest test-regex?
Expand All @@ -132,20 +132,20 @@
#(regex? "f.*r")
state)))))

(deftest test-regex-in?
(testing "regex-in?"
(deftest test-some-regex?
(testing "some-regex?"
(let [state {:enlive (parse [:p "Account Number: #12345"])}]
(testing "fails if regex is not found"
(check-report {:type :fail
:expected '(regex-in? "\\d{6}")
:expected '(some-regex? "\\d{6}")
:actual "Account Number: #12345"}
#(regex-in? "\\d{6}")
#(some-regex? "\\d{6}")
state))
(testing "passes if regex is found"
(check-report {:type :pass
:expected '(regex-in? "\\d{5}")
:expected '(some-regex? "\\d{5}")
:actual "Account Number: #12345"}
#(regex-in? "\\d{5}")
#(some-regex? "\\d{5}")
state)))))

(deftest test-attr?
Expand Down Expand Up @@ -181,35 +181,41 @@
(deftest test-link?
(testing "link?"
(let [state {:enlive (parse [:p "Click " [:a {:href "/foo"} "here"] " to login"])}]
(testing "fails if link is not found"
(check-report {:type :fail
:expected '(link? "/bar" nil)
:actual '({:href "/foo" :text "here"})}
#(link? "/bar")
state))
(testing "passes if link is found"
(testing "matches only text by default"
(check-report {:type :pass
:expected '(link? "/foo" nil)
:actual [{:href "/foo" :text "here"}]}
#(link? "/foo")
state))
(testing "matches only href by default"
(check-report {:type :fail
:expected '(link? "here" nil)
:expected '(link? "here")
:actual [{:href "/foo" :text "href"}]}
#(link? "here")
state))
(testing "fails if link is not found"
(check-report {:type :fail
:expected '(link? "there")
:actual '({:href "/foo" :text "here"})}
#(link? "there")
state))
(testing "can match href explicitly"
(check-report {:type :pass
:expected '(link? :href "/foo")
:actual [{:href "/foo" :text "href"}]}
#(link? :href "/foo")
state))
(testing "can match text instead of href"
(testing "fails if href not found"
(check-report {:type :fail
:expected '(link? :href "/bar")
:actual [{:href "/foo" :text "href"}]}
#(link? :href "/bar")
state))
(testing "can match both at once"
(check-report {:type :pass
:expected '(link? :text "here")
:expected '(link? "here" "/foo")
:actual [{:href "/foo" :text "here"}]}
#(link? "here" "/foo")
state))
(testing "fails if not both matching"
(check-report {:type :fail
:expected '(link? "here" "/bar")
:actual [{:href "/foo" :text "here"}]}
#(link? :text "here")
#(link? "here" "/bar")
state)))))

(deftest test-heading?
Expand Down

0 comments on commit ad40eed

Please sign in to comment.