如何让 REPL 识别地图中的测试?
How to get REPL to recognize tests within a map?
我有一个地图定义如下:
"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 和 运行 这个地图命名空间中的所有测试时,它说 Ran 0 tests with 0 assertions.
我怎样才能让 REPL 识别这个测试?
您可以同时使用with-test
到define a function and a unit 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
注意:当使用 with-test
时,该函数仍必须使用 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
但还有其他的可能性。另见 the Clojure Cookbook discussion on testing.
我有一个地图定义如下:
"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 和 运行 这个地图命名空间中的所有测试时,它说 Ran 0 tests with 0 assertions.
我怎样才能让 REPL 识别这个测试?
您可以同时使用with-test
到define a function and a unit 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
注意:当使用 with-test
时,该函数仍必须使用 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
但还有其他的可能性。另见 the Clojure Cookbook discussion on testing.