简体   繁体   English

当结果是一个函数时,无法使测试为真(编译时出现语法错误.... 没有这样的 var:...)

[英]Can't get test to true when the result is a function (Syntax error compiling at .... No such var:...)

Happens when I want to test a function where the result is another function.当我想测试结果是另一个函数的函数时会发生这种情况。 I have something like this:我有这样的事情:

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)))))

And in the test ns:在测试 ns 中:

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}))

the result in the repl is: 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]}

I know that I have to put f/index-forward instead of the result of the repl [flexsearch.core/index-forward], but it doesn't work with f/init/fn--14976 (No such var: f/init/fn--14976)我知道我必须把 f/index-forward 而不是 repl [flexsearch.core/index-forward] 的结果,但它不适用于 f/init/fn--14976(没有这样的 var:f /init/fn--14976)

I supouse that is a trick with the vars but i dont know how it really works.我认为这是 vars 的一个技巧,但我不知道它是如何工作的。 Any reading you can provide i will be gratefull您能提供的任何阅读资料,我将不胜感激

---EDIT--- The f/index-forward and f/encoder-icase notations works fine. ---编辑--- f/index-forward 和 f/encoder-icase 符号工作正常。

---EDIT 2--- i've defined: ---编辑 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)))))

the I get a similar ":tokenizer #function[flexsearch.core/spliter/fn--34857]," that I used in the test and it also failed –我在测试中使用了类似的“:tokenizer #function[flexsearch.core/spliter/fn--34857]”,但它也失败了 –

I think the "No such var" error is happening because the tokenizer is an anonymous function.认为发生“No such var”错误是因为标记器是一个匿名函数。

If you had the default tokenizer defined as a non-anonymous function in flexsearch.core and then used that name in the test, it would work.如果您在flexsearch.core中将默认标记器定义为非匿名函数,然后在测试中使用该名称,它就会起作用。

However, in general, you cannot compare two functions for equality - as @cfrick says.但是,一般来说,您不能比较两个函数的相等性 - 正如@cfrick 所说。 When you are comparing maps, where some of the values are functions, you are still comparing functions.当您比较地图时,其中一些值是函数,您仍在比较函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编译测试平台时出现“错误:无法解析警报的所有参数” - “Error: Can't resolve all parameters for Alert” when compiling a testbed 无法获得功能的测试范围 - Can't get test coverage for function 编译Google Test时出现g ++致命错误 - g++ fatal error when compiling Google Test 使用导入的 SASS 文件测试组件时出现语法错误 - Syntax Error when test component with SASS file imported Spring 反应性测试不会导致预期错误 - Spring Reactive test doesn't result in an expected error 无法使用控制器测试通过Jasmine来获取指令:错误:[$ injector:unpr]未知提供程序:utilsProvider &lt;-utils - Can't get a directive using a controller test to pass Jasmine : Error: [$injector:unpr] Unknown provider: utilsProvider <- utils 无法在 SonarQube 中获得单元测试成功 - Can't get Unit Test Success in SonarQube boost :: test中是否有一个函数可以返回错误值? - Is there a function in boost::test that can return error value? 如何编写单元测试来验证编译错误? - How write a unit test for verifying compiling error? TypeError:undefined不是一个用业力测试的函数,但是我从函数本身得到了预期的结果 - TypeError: undefined is not a function with karma test but I get the expected result from the function itself
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM