Working on legacy code Part 1

The project I’m working on right now is sb-cover. I want to integrate it with lisp-unit-with-fixture.

For me, to understand code, it is best if I create coverage tests to ensure that when I change code, I don’t change the codes behavior.

Looking at the design of the application I noticed that the coverage data model and the view (HTML) is intermingled:

        (format stream "<table class='summary'>")
        (format stream "<tr class='head-row'><td></td><td class='main-head' colspan='3'>Expression</td><td class='main-head' colspan='3'>Branch</td></tr>")
        (format stream "<tr class='head-row'>~{<td width='80px'>~A</td>~}</tr>"
                (list "Source file"
                      "Covered" "Total" "%"
                      "Covered" "Total" "%")

So to start you grab the low-lying fruit and encapsulate it:

(defun write-header (stream)
  (format stream "<table class='summary'>")
  (format stream "<tr class='head-row'><td></td><td class='main-head' colspan='3'>Expression</td><td class='main-head' colspan='3'>Branch</td></tr>")
  (format stream "<tr class='head-row'>~{<td width='80px'>~A</td>~}</tr>"
          (list "Source file"
                "Covered" "Total" "%"
                "Covered" "Total" "%"))

The function ‘write-header’ outputs a string. Let’s pin this down with an unit test:

(test-fixture html-stream-writing
    (:setup
      ((header "<table class='summary'> ;....
  (:tests
    (should-write-html
     (assert-equal header (with-output-to-string (out)
                            (write-header out)))))

And that is how you start to get your code under control.

Expression Branch
Source file Covered Total % Covered Total %
\_Holder\WorkSpace\gWeb\sb-cover-dev\src\
cover.lisp 201 1047 19.2 2 84 2.4
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: