Note: This project is currently in a work in progress so not all languages or sections are complete.

Test Driven Development with Clojure

Installing Homebrew

We're going to start by installing Homebrew which will make installing everything else we need much easier.

Open a Terminal window and run the following commands:

1
2
xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing Clojure

Now we can easily install Clojure with Homebrew using the following:

1
brew install clojure

Creating the Project

Now we'll use Leiningen to initialize a project for us.

1
2
3
brew intall leiningen
lein new default piglatin
cd piglatin

Dependency Management

When building software you will frequently use libraries and tools from a third party. Since they have their own development lifecycles and versions we want to be able to define a specific version of that dependency to use in our software. We want to do that with a tool and a file.

For Clojure we'll use the tool Leiningen and our dependencies will be defined in the file project.clj.

1
# leiningen was already installed above

Adding a Testing Library

We're going to use the testing library clojure.test to write our tests.

Edit the file project.clj and add clojure.test as a testing dependency:

1

Then we retrieve clojure.test by running lein deps.

Finally we initialize clojure.test by running .

The First Test

Create the file test/piglatin/core_test.clj with the following contents:

1
2
3
4
5
6
7
(ns piglatin.core-test
  (:require [clojure.test :refer :all]
            [piglatin.core :refer :all]))

(deftest word-starting-with-a-vowel
  (testing "translate_word"
    (is (= "appleyay" (translate_word "apple")))))

Then run the test with the command lein test to see the results:

1
Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol: translate_word in this context, compiling:(piglatin/core_test.clj:7:23)

Comments