Skip to content

Commit 5beefe3

Browse files
authored
Throw exception for unknown :fn/* function keywords (#670)
1 parent eaea79f commit 5beefe3

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

CHANGELOG.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ A release with an intentional breaking changes is marked with:
3333
** {issue}661[#661]: Fix `:fn/enabled`. ({person}dgr[@dgr])
3434
** {issue}663[#663]: `query` throws a more accurate exception with a more accurate error message when provided with an empty query vector. ({person}dgr[@dgr])
3535
** {issue}666[#666]: Previously, in some error conditions, Etaoin would throw a very generic `clojure.lang.Exception` object. Some of those cases have been replaced by throwing a map with Slingshot, providing more information about the problem. ({person}dgr[@dgr])
36+
** {issue}668[#668]: Throw an exception for unknown `:fn/*` keywords in map queries.
3637
* Docs
3738
** {issue}656[#656]: Correctly describe behavior when query's parameter is a string. The User Guide and `query` doc strings say that a string passed to `query` is interpreted as an XPath expression. In fact, `query` interprets this as either XPath or CSS depending on the setting of the driver's `:locator` parameter, which can be changed. ({person}dgr[@dgr])
3839
* Quality

src/etaoin/impl/xpath.clj

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns ^:no-doc etaoin.impl.xpath
22
"A special module to work with XPath language."
3-
(:require [clojure.string :as string]))
3+
(:require [clojure.string :as string]
4+
[slingshot.slingshot :refer [throw+]]))
45

56
(set! *warn-on-reflection* true)
67

@@ -49,7 +50,16 @@
4950

5051
(defmethod clause :default
5152
[[attr text]]
52-
(node-equals (format "@%s" (to-str attr)) text))
53+
;; If we fall through to the default case and yet the attribute
54+
;; looks like a query function, then the user probably has a typo
55+
;; in their program, so we throw.
56+
;; Else treat it like an attribute
57+
(if (and (keyword? attr) (= (namespace attr) "fn"))
58+
(throw+ {:type :etaoin/argument
59+
:message "Unknown query function"
60+
:fn attr
61+
:arg text})
62+
(node-equals (format "@%s" (to-str attr)) text)))
5363

5464
(defmethod clause :fn/text
5565
[[_ text]]

test/etaoin/api_test.clj

+4-1
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,10 @@
935935
;; 3. malformed XPath
936936
;; 4. malformed CSS
937937
;; 5. query isn't a string, map, or vector. Perhaps a list and set.
938-
;; 6. bad :fn/... keywords
938+
;; 6. unknown :fn/... keywords
939+
(testing "unknown :fn/* keywords"
940+
;; ":fn/indx" is probably a typo and the user really wants ":fn/index"
941+
(is (thrown+? [:type :etaoin/argument] (e/query *driver* {:tag :div :fn/indx 1}))))
939942
;; 7. vector queries with vector elements (vectors in vectors)
940943
))
941944

0 commit comments

Comments
 (0)