Personal tools

Emacs/Automatic unit testing

From HaskellWiki

< Emacs
Revision as of 10:46, 18 May 2012 by Chrisdone (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Emacs for Haskell

Inferior Haskell processes
Automatic unit testing
Automatic building
API searching
Project navigation
Literate programming

One Approach

Here's a cute trick I've evolved:

I'm a great fan of unit test first, as described by eXtremeProgramming on TheOriginalWiki.

With the code below, I can press F12 and immediately run all of my unit tests, and immediately see whether they all passed or not. I've put all of my unit tests into their own file with a main function that runs the tests and gives an exitcode according to the test results. I've specified that the compile-command for that file compiles and runs the file.

This elisp code will run the compile command from the F12 key in emacs. The output will popup a new window twelve lines tall. If the compilation is successful (exitcode zero) the window goes away. If the exitcode is 1 or greater, the window stays so you can see the output.

(require 'compile)

;; this means hitting the compile button always saves the buffer
;; having to separately hit C-x C-s is a waste of time
(setq mode-compile-always-save-buffer-p t)
;; make the compile window stick at 12 lines tall
(setq compilation-window-height 12)

;; from enberg on #emacs
;; if the compilation has a zero exit code, 
;; the windows disappears after two seconds
;; otherwise it stays
(setq compilation-finish-function
      (lambda (buf str)
        (unless (string-match "exited abnormally" str)
          ;;no errors, make the compilation window go away in a few seconds
           "2 sec" nil 'delete-windows-on
           (get-buffer-create "*compilation*"))
          (message "No Compilation Errors!"))))

;; one-button testing, tada!
(global-set-key [f12] 'compile)

This Haskell code has some Emacs local variable settings at the bottom specifying what the compile-command should be for this buffer.

import HUnit
import System
myTestList = 
    TestList [
              "add numbers" ~: 5 ~=? (3 + 2)
             ,"add numbers" ~: 5 ~=? (3 + 3)
h = runTestTT myTestList
main = do c <- h
          putStr $ show c
          let errs = errors c
              fails = failures c
          System.exitWith (codeGet errs fails)
codeGet errs fails
 | fails > 0       = ExitFailure 2
 | errs > 0        = ExitFailure 1
 | otherwise       = ExitSuccess
-- Local Variables:
-- compile-command: "ghc --make -o Test_Demo -i/home/shae/src/haskell/libraries/ HUnitDemo.hs && ./Test_Demo"
-- End:

If you have any questions, ideas, or suggestions for this code, the maintainer would love to hear them.