Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 如何让REPL识别映射中的测试?_Clojure_Read Eval Print Loop - Fatal编程技术网

Clojure 如何让REPL识别映射中的测试?

Clojure 如何让REPL识别映射中的测试?,clojure,read-eval-print-loop,Clojure,Read Eval Print Loop,我的地图定义如下: "Arcane Golem" {:name "Arcane Golem" :attack 4 :health 4 :mana-cost 3 :type :minion :set :classic :rarity :rare :description "Battlecry: Give your opponent a Mana Cryst

我的地图定义如下:

"Arcane Golem"
    {:name        "Arcane Golem"
    :attack      4
    :health      4
    :mana-cost   3
    :type        :minion
    :set         :classic
    :rarity      :rare
    :description "Battlecry: Give your opponent a Mana 
Crystal."
    :battlecry   (fn battlecry [state minion]
                     {:test (fn []
                                (as-> (create-game [{:minions [(create-minion "Arcane Golem" :id "ag")]}]) $
                                      (battlecry $ (get-minion $ "ag"))
                                      (contains? (get-in $[:players "p1" :hand]) "Mana Crystal")))}
                     (-> (get-opponent state (:id minion))
                     (:id)
                     (add-card-to-hand state (create-card "Mana Crystal"))))}

此映射本身是一个较大映射(称为卡定义)中的键值对。正如你们所看到的,我已经在下面为战斗呐喊函数编写了一个测试;但是,当我启动REPL并运行此映射名称空间中的所有测试时,它会显示
使用0个断言运行了0个测试。
如何让REPL识别此测试?

您可以同时使用
和test

; with-test is the same as using {:test #((is...)(is...))} in the meta data of the function.

(:use 'clojure.test)

(with-test
    (defn my-function [x y]
      (+ x y))
  (is (= 4 (my-function 2 2)))
  (is (= 7 (my-function 3 4))))

(test #'my-function)            ;(test (var my-function))
=> :ok
注意:当使用带有测试的
时,仍然必须使用
defn
将函数定义为全局变量(参见示例)。测试机器将找不到作为映射键值的匿名
fn

应该将函数定义为独立变量,然后在映射中包含对它的引用:

{:battlecry my-function}    ; for example
话虽如此,大多数人(包括我自己)更喜欢有一个单独的测试名称空间,以防止测试扰乱源代码。我喜欢将其组织为:

flintstones.core           ; main namespace
tst.flintstones.core       ; the unit test namespace
然后将它们放置在项目目录的
/src
/test
子目录中:

src/flintstones/core.clj            ; main namespace
test/tst/flintstones/core.clj       ; the unit tests

但还有其他可能性。另请参见。

您可以同时将
与test一起使用来

; with-test is the same as using {:test #((is...)(is...))} in the meta data of the function.

(:use 'clojure.test)

(with-test
    (defn my-function [x y]
      (+ x y))
  (is (= 4 (my-function 2 2)))
  (is (= 7 (my-function 3 4))))

(test #'my-function)            ;(test (var my-function))
=> :ok
注意:当使用带有测试的
时,仍然必须使用
defn
将函数定义为全局变量(参见示例)。测试机器将找不到作为映射键值的匿名
fn

应该将函数定义为独立变量,然后在映射中包含对它的引用:

{:battlecry my-function}    ; for example
话虽如此,大多数人(包括我自己)更喜欢有一个单独的测试名称空间,以防止测试扰乱源代码。我喜欢将其组织为:

flintstones.core           ; main namespace
tst.flintstones.core       ; the unit test namespace
然后将它们放置在项目目录的
/src
/test
子目录中:

src/flintstones/core.clj            ; main namespace
test/tst/flintstones/core.clj       ; the unit tests
但还有其他可能性。另见