我正在测试一些音频行为,我需要用户判断事情是否通过.我想请用户在leiningen测试中做出回应.但是,有一些事情发生在读取线上,阻止了这一点.
这是使用"lein new foo"创建新的clojure项目并编辑foo/test/foo/core_test.clj文件之后的一些示例测试代码:
(ns foo.core-test
(:use clojure.test
foo.core))
(deftest a-test
(testing "FIXME, what a fail."
(let [_ (println "enter something")
yn (read-line)]
(println yn)
(is (= yn "y")))))
Run Code Online (Sandbox Code Playgroud)
这就是"lein测试"中发生的事情
lein test foo.core-test
enter something
hi
there
what
is
going on?
^C
Run Code Online (Sandbox Code Playgroud)
只有control-C停止(读取线路)呼叫.
我在Java 1.6.0_35 Java HotSpot(TM)64位服务器VM上使用Clojure 1.4.0和Leiningen 2.0.0-preview7
关于如何让读取线在测试中工作的任何想法?
我还应该注意(read-line)在我的"lein repl"中运行良好...
> lein repl
nREPL server started on port 54398
REPL-y 0.1.0-beta8
Clojure 1.4.0
Exit: Control+D or (exit) or (quit)
Commands: (user/help)
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
(user/sourcery function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Examples from clojuredocs.org: [clojuredocs or cdoc]
(user/clojuredocs name-here)
(user/clojuredocs "ns-here" "name-here")
user=> (println (read-line))
hi
hi
nil
user=> (read-line)
ho
"ho"
Run Code Online (Sandbox Code Playgroud)
更新:
我打算给@DaoWen赢得胜利.阅读链接和谷歌搜索,斯坦丁只是leiningen b0rken.有足够的箍跳,也许我可以想出来,但对话框的建议似乎是最好的前进道路.这里删除一些测试代码只是为了完整性,以防其他人.
(ns foo.core-test
(:use clojure.test
foo.core))
(import 'javax.swing.JOptionPane)
(defn ask-yn
"return 0 on pass, 1 on fail"
[prompt]
(JOptionPane/showConfirmDialog nil prompt "User Input" JOptionPane/YES_NO_OPTION))
(deftest a-test
(testing "a-test"
(let [yn (ask-yn "did a-test pass?")]
(is (= yn 0)))))
(deftest b-test
(testing "b-test"
(let [yn (ask-yn "did b-test pass?")]
(is (= yn 0)))))
Run Code Online (Sandbox Code Playgroud)
我知道这不能解决您的read-line问题,但也许您可以通过使用 aJOptionPane来解决它。如果我是正在收听多个声音文件的用户,我宁愿只单击“是”或“否”,而不必键入两者之一。
(import 'javax.swing.JOptionPane)
(JOptionPane/showConfirmDialog nil "Did the test pass?" "User Input" JOptionPane/YES_NO_OPTION)
Run Code Online (Sandbox Code Playgroud)
更新:
我以为我以前见过这样的东西:
为什么使用 lein run 按 ENTER 键后 read-line 不返回(看起来像是挂起),但使用 lein repl 却可以?
看来您应该尝试跑步,lein trampoline test而不仅仅是lein test为了解决stdin问题。