我想测试一个函数,它的结果是另一个函数。我的代码如下
ns flexsearch.core
(defn init [{:keys [tokenizer split indexer filter] :as options}]
(let [encoder (get-encoder (:encoder options))]
(assoc (merge {:ids {} :data {}} options)
:indexer (get-indexer indexer)
:encoder encoder
:tokenizer (if (fn? tokenizer) tokenizer #(string/split % (or split #"\W+")))
:filter (set (mapv encoder filter)))))
在测试命名空间中
ns flexsearch.core-test
[flexsearch.core :as f]
(def split #"\W+")
(is (= (f/init {:tokenizer false :split split :indexer :forward :filter #{"and" "or"}})
{:ids {},
:data {},
:tokenizer f/init/fn--14976,
:split #"\W+",
:indexer f/index-forward,
:filter #{"or" "and"},
:encoder f/encoder-icase}))
在repl中的结果是
{:ids {},
:data {},
:tokenizer #function[flexsearch.core/init/fn--14976],
:split #"\W+",
:indexer #function[flexsearch.core/index-forward],
:filter #{"or" "and"},
:encoder #function[flexsearch.core/encoder-icase]}
我知道我应该使用f/index-forward而不是repl的结果[flexsearch.core/index-forward],但我用f/init/fn--14976(没有这样的变量: f/init/fn--14976)尝试后不起作用
我觉得这可能是一个关于变量的技巧,但我不清楚它实际的工作原理。如果您能提供一些阅读材料,我将不胜感激
---编辑--- f/index-forward和f/encoder-icase的表示法工作正常。
---编辑 2--- 我已经定义了
(defn spliter [split] (fn [x] (string/split x (or split #"\W+"))))
and used it on:
(defn init [{:keys [tokenizer split indexer filter] :as options}]
(let [encoder (get-encoder (:encoder options))]
(assoc (merge {:ids {} :data {}} options)
:indexer (get-indexer indexer)
:encoder encoder
:tokenizer (if (fn? tokenizer) tokenizer (spliter split))
:filter (set (mapv encoder filter)))))
我得到了一个类似 ":tokenizer #function[flexsearch.core/spliter/fn--34857,]" 的结果,我在测试中使用了这个结果,但它也失败了——