返回值不是(quote <value>)</value>

时间:2014-07-07 00:04:27

标签: testing clojure quote

我正在尝试4clojure上遇到的挑战,并且卡在以下代码上

(ns com.koddsson.for-clojure
  (:use [clojure.test :only [is deftest run-tests]]))

(defn my-flatten
  ([x] (if (not (and (seq? x) (vector? x)))
           x                      ; If x is not a sequence nor a vector
           (map my-flatten x))))  ; else recursivly apply the flatten function

(deftest test28
  (is (= (my-flatten '((1 2) 3 [4 [5 6]])) '(1 2 3 4 5 6)))
  (is (= (my-flatten ["a" ["b"] "c"]) '("a" "b" "c")))
  (is (= (my-flatten '((((:a))))) '(:a))))

(run-tests)

它会生成以下输出。

λ bubblegum 20 → λ git master* → lein exec -p 28.clj

Testing com.koddsson.for-clojure

FAIL in (test28) (28.clj:10)
expected: (= (my-flatten (quote ((1 2) 3 [4 [5 6]]))) (quote (1 2 3 4 5 6)))
  actual: (not (= ((1 2) 3 [4 [5 6]]) (1 2 3 4 5 6)))

FAIL in (test28) (28.clj:11)
expected: (= (my-flatten ["a" ["b"] "c"]) (quote ("a" "b" "c")))
  actual: (not (= ["a" ["b"] "c"] ("a" "b" "c")))

FAIL in (test28) (28.clj:12)
expected: (= (my-flatten (quote ((((:a)))))) (quote (:a)))
  actual: (not (= ((((:a)))) (:a)))

Ran 1 tests containing 3 assertions.
3 failures, 0 errors.

它似乎给出了正确的返回值,但格式错误。有什么想法吗?

1 个答案:

答案 0 :(得分:7)

您的测试输出实际上显示您获得正确的返回值。

FAIL in (test28) (28.clj:10)
expected: (= (my-flatten (quote ((1 2) 3 [4 [5 6]]))) (quote (1 2 3 4 5 6)))
  actual: (not (= ((1 2) 3 [4 [5 6]]) (1 2 3 4 5 6)))
                  ^^^^^^^^^^^^^^^^^^^
                  output of my-flatten

您应该在REPL中验证您的my-flatten返回上面标记的输出。实际上,您的功能本质上是身份功能。

您编写的代码有三个问题。

  1. 条件(and (seq? x) (vector? x))永远不会成立。将鼠标悬停在扰流板上。

      

    您应该将and更改为or或改为使用sequential?

  2. 通过上述修复,由于map返回序列,您的结构现在将更改为序列结构。这些序列需要递归连接。将鼠标悬停在扰流板上。

      

    map更改为mapcat

  3. 现在代码暂时中断了。您需要保护基本案例不受串联操作的影响。将鼠标悬停在扰流板上。

      

    将基本案例返回值x包装在集合中,例如[x]