~ubuntu-branches/ubuntu/hardy/python-docutils/hardy

« back to all changes in this revision

Viewing changes to tools/editors/emacs/tests/tests-runner.el

  • Committer: Bazaar Package Importer
  • Author(s): martin f. krafft
  • Date: 2006-07-10 11:45:05 UTC
  • mfrom: (2.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20060710114505-otkhqcslevewxmz5
Tags: 0.4-3
Added build dependency on python-central (closes: #377580).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;; Authors: Martin Blais <blais@furius.ca>
 
2
;; Date: $Date: 2005/04/01 23:19:41 $
 
3
;; Copyright: This module has been placed in the public domain.
 
4
;;
 
5
;; Simple generic test runner for test scripts.
 
6
;;
 
7
;; Run this with::
 
8
;;
 
9
;;    emacs --script <file>.el
 
10
;;
 
11
;;
 
12
;; There are mainly two useful functions from this pacakge:
 
13
;;
 
14
;; 1. regression-test-compare-expect-values : used to compare expected output
 
15
;;    values from running a function;
 
16
;;
 
17
;; 2. regression-test-compare-expect-buffer : used to compare expected output
 
18
;;    buffer contents after running the function.
 
19
;;
 
20
;; regression-test-compare-expect-values test format
 
21
;; -------------------------------------------------
 
22
;;
 
23
;; The tests are a list of tuples, with the following entries:
 
24
;;
 
25
;; - a SYMBOL that uniquely identifies the test.
 
26
;;
 
27
;; - the input buffer CONTENTS to prepare and run the test on.  If char @ is
 
28
;;   present in the buffer, it is removed and the cursor is placed at that
 
29
;;   position before running the tested function.
 
30
;;
 
31
;; - the expected OUTPUT value that the function should return.  If the actual
 
32
;;   output is different from this, the test will fail.
 
33
;;
 
34
;; - an optional list of INPUT ARGUMENTS that the test function is called with
 
35
;;   for this test.
 
36
;; 
 
37
;; regression-test-compare-expect-buffer test format
 
38
;; -------------------------------------------------
 
39
;;
 
40
;; - a SYMBOL that uniquely identifies the test.
 
41
;;
 
42
;; - the input buffer CONTENTS to prepare and run the test on.  Here too, char @
 
43
;;   is present in the buffer, it is removed and the cursor is placed at that
 
44
;;   position before running the tested function.
 
45
;;
 
46
;; - the EXPECTED buffer contents after the function has been run.
 
47
;;   Additionally, if char @ is present, it is checked that the cursor is
 
48
;;   located at that position in the buffer after the function is run (this is
 
49
;;   optional).
 
50
;;
 
51
;; - an optional list of PREFIX ARGUMENTS, which indicates to the test program
 
52
;;   to set those prefix arguments before running the given function.  If there
 
53
;;   are multiple prefix args, the function is invoked many times.
 
54
;;
 
55
 
 
56
 
 
57
(require 'cl)
 
58
 
 
59
(defvar regression-point-char "@"
 
60
  "Special character used to mark the position of point in input
 
61
  text and expected text.")
 
62
 
 
63
(defun regression-test-loop (suitename testfun testlist fun &optional continue)
 
64
  "Loop over a series of tests in a buffer and run the 'testfun'
 
65
function."
 
66
 
 
67
  (message (format "\n\n   Test Suite: %s\n\n" suitename))
 
68
 
 
69
  (let ((buf (get-buffer-create "regression-tests"))
 
70
        errtxt
 
71
        )
 
72
    (dolist (curtest testlist)
 
73
 
 
74
      ;; Print current text.
 
75
      (message (format "========= %s" (prin1-to-string (car curtest))))
 
76
 
 
77
      ;; Prepare a buffer with the starting text, and move the cursor where
 
78
      ;; the special character is located.
 
79
      (switch-to-buffer buf)
 
80
      (erase-buffer)
 
81
      (insert (cadr curtest))
 
82
 
 
83
      (if (not (search-backward regression-point-char nil t))
 
84
          (error (concat "Error: Badly formed test input, missing "
 
85
                         "the cursor position marker.")))
 
86
 
 
87
      (delete-char 1)
 
88
 
 
89
      (setq errtxt (funcall testfun
 
90
                            (car curtest)
 
91
                            (caddr curtest)
 
92
                            (cadddr curtest)))
 
93
 
 
94
      (if errtxt
 
95
          (if continue
 
96
              (progn (message errtxt)
 
97
                     (message "(Continuing...)"))
 
98
            (error errtxt)))
 
99
    ))
 
100
  (message "Done."))
 
101
 
 
102
 
 
103
(defun regression-compare-buffers (testname expected testargs)
 
104
  "Compare the buffer and expected text and return actual
 
105
contents if they do not match."
 
106
  
 
107
  ;; Run the section title update command n times.
 
108
  (dolist (x (or testargs (list nil)))
 
109
    (let ((current-prefix-arg x))
 
110
      (funcall fun)))
 
111
 
 
112
  ;; Compare the buffer output with the expected text.
 
113
  (let* (;; Get the actual buffer contents.
 
114
         (actual (buffer-string))
 
115
         ;; Get the expected location of point
 
116
         (exppoint (string-match regression-point-char expected))
 
117
         
 
118
         (expected-clean (if exppoint
 
119
                             (concat (substring expected 0 exppoint)
 
120
                                     (substring expected (+ 1 exppoint)))
 
121
                           expected))
 
122
 
 
123
         ;; Adjust position of point vs. string index.
 
124
         (exppoint (and exppoint (+ exppoint 1)))
 
125
 
 
126
         )
 
127
 
 
128
    (if (not (string= expected-clean actual))
 
129
        ;; Error! Test failed.
 
130
        (format "Error: Test %s failed: \nexpected\n%s\ngot\n%s"
 
131
                testname 
 
132
                (prin1-to-string expected-clean) 
 
133
                (prin1-to-string actual))
 
134
      (if (and exppoint (not (equal exppoint (point))))
 
135
          ;; Error! Test failed, final position of cursor is not the same.
 
136
          (format "Error: Test %s failed: cursor badly placed." testname))
 
137
    )))
 
138
 
 
139
(defun regression-test-compare-expect-buffer 
 
140
  (suitename testlist fun &optional continue)
 
141
  "Run the regression tests for the expected buffer contents."
 
142
  (regression-test-loop 
 
143
   suitename 'regression-compare-buffers testlist fun continue))
 
144
 
 
145
 
 
146
(defun regression-compare-values (testname expected testargs)
 
147
  "Compare the buffer and expected text and return actual
 
148
contents if they do not match."
 
149
 
 
150
  (let (actual)
 
151
    ;; Run the section title update command n times.
 
152
    (setq actual (apply fun testargs))
 
153
    
 
154
    ;; Compare the buffer output with the expected text.
 
155
    (if (not (equal actual expected))
 
156
        ;; Error! Test failed.
 
157
        (format "Error: Test %s failed: expected '%s' got '%s'."
 
158
                testname
 
159
                (prin1-to-string expected)
 
160
                (prin1-to-string actual))
 
161
    )))
 
162
 
 
163
(defun regression-test-compare-expect-values 
 
164
  (suitename testlist fun &optional continue)
 
165
  "Run the regression tests for expected values comparison."
 
166
  (regression-test-loop 
 
167
   suitename 'regression-compare-values testlist fun continue))