Clojure Geek

Writing about learning Clojure

First clojure.test tests

Anybody that knows me for 5 minutes knows I love writing unit tests. I’ve been told “you’re sick” by friends as they shake their heads at me..

I got lots of good ideas for simplifiying (and making more idomatic) the code in my last post so I wanted to rewrite parts of the code to be use those ideas. Perfect time to practice writing clojure tests!

My first test:

1
2
3
4
5
6
7
(ns recipe-api.sample-data-test
  (:require [clojure.test :refer :all]
            [recipe-api.sample-data :refer :all]))

(deftest test-testify-data
  (testing "Adding TEST in front of :name value"
    (is (= {:name "TEST hello"} (testify-data {:name "hello"})))))

Using the suggestions I rewrote testify-data as

1
2
(defn testify-data [data]
  (update-in data [:name] #(str "TEST " %)))

Alot better than what I had:

1
2
(defn testify-data [data]
  (merge data (hash-map :name (str "TEST " (:name data)))))

Then refactoring

1
2
(defn testify-all-data [datas]
  (map #(testify-data %) datas))

into, since the anonymous function is redundant there:

1
2
(defn testify-all-data [datas]
  (map testify-data datas))

And the test for that:

1
2
3
4
5
6
(deftest test-testify-all-data
  (testing "Adding TEST in front of a bunch of data"
    (is (= [{:name "TEST hello"}
            {:name "TEST bye"}]
         (testify-all-data [{:name "hello"}
                            {:name "bye"}])))))

Yay much better!

Thanks lemonodor and Gary Verhaegen for your feedback on my last post.

Any suggestions?

Comments