如何模拟用户输入?

时间:2018-10-18 12:14:11

标签: clojure

我知道可以通过将(read-line)函数包装在with-in-str中,然后像这样通过编程方式传递您的输入来模拟用户的输入:

(with-in-str "punit naik" (println (read-line)))

这当然会打印punit naik到控制台。 但是问题是,我有一个函数递归运行,并不断在迭代中要求用户输入。 我想为该功能编写一个测试用例。我该如何实现?

1 个答案:

答案 0 :(得分:2)

read-line一次读取一行。您可以准备一个字符串,其中包含所有需要阅读的行,并用新行分隔:

(with-in-str
  "a\nb\nc"
  (loop []
    (if-let [line (read-line)]
      (do
        (println "Line was" line)
        (println "Recurring")
        (recur))
      (println "No more lines"))))

此打印:

Line was a
Recurring
Line was b
Recurring
Line was c
Recurring
No more lines