summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/tests/mumamo-test.el
blob: ecbac10e6a38c87727ee08e53066a10c694a292f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
;;; mumamo-test.el --- Test routines for mumamo
;;
;; Author: Lennart Borgman
;; Created: Sat Mar 31 03:59:26 2007
;; Version: 0.1
;; Last-Updated:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; This file defines some test for mumamo.el and a the minor mode
;; `mumamu-test-mode' to bind the test functions to some keys for
;; convenient use. This will define F3 to run
;; `mumamo-test-create-chunk-at' and Shift-F3 to
;; `mumamo-test-create-chunks-at-all-points'.
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:

;;(eval-when-compile (require 'mumamo))
(eval-when-compile (require 'mumamo))
(require 'whelp)

;;;;;;; TESTS, run in fundamental-mode buffer

(defvar mumamo-test-mode-keymap
  (let ((map (make-sparse-keymap)))
    (define-key map [f11] 'goto-char)
    (define-key map [(meta f3)] 'mumamo-test-fontify-region)
    (define-key map [(shift f3)] 'mumamo-test-create-chunks-at-all-points)
    (define-key map [f3] 'mumamo-test-create-chunk-at-point)
    map))

(defvar mumamo-test-current-chunk-family nil)
(make-variable-buffer-local 'mumamo-test-current-chunk-family)

(define-minor-mode mumamo-test-mode
  "For testing creating mumamo-mode chunks.
When this mode is on the following keys are defined:

  \\{mumamo-test-mode-keymap}

"
  nil
  " MuMaMo-TEST"
  :keymap mumamo-test-mode-keymap
  (if mumamo-test-mode
      (progn
        (setq mumamo-test-current-chunk-family mumamo-current-chunk-family)
        (setq mumamo-use-condition-case nil)
        (setq mumamo-debugger nil)
        (run-with-idle-timer 0 nil 'mumamo-test-tell-bindings))
    (setq mumamo-use-condition-case t)
    (setq mumamo-debugger (default-value 'mumamo-debugger)))
  )

(defun mumamo-test-tell-bindings ()
  (save-match-data ;; runs in timer
    (let ((s "mumamo-test-mode is on, use F3/shift-F3 for simple testing"))
      (put-text-property 0 (length s)
                         'face 'font-lock-warning-face
                         s)
      (message "%s" s))))

;;(mumamo-test-mode 1)


;; (defun mumamo-test-fontify-buffer ()
;;   (interactive)
;;   (unless mumamo-current-chunk-family
;;     (mumamo-select-chunk-family))
;;   ;;(when mumamo-mode (mumamo-mode 0))
;;   (when mumamo-multi-major-mode (mumamo-turn-off-actions))
;;   (save-excursion
;;     (mumamo-remove-all-chunk-overlays)
;;     (mumamo-save-buffer-state nil
;;       (put-text-property (point-min) (point-max) 'face nil))
;;     (mumamo-fontify-buffer)))

(defun mumamo-test-create-chunk-at-point ()
  (interactive)
  (remove-hook 'post-command-hook 'mumamo-post-command t)
  (font-lock-mode -1)
  (setq fontification-functions nil)
  (save-excursion
    (mumamo-remove-all-chunk-overlays)
    (mumamo-save-buffer-state nil
      (remove-text-properties (point-min) (point-max) '(face nil syntax-table nil)))
    (let* ((mumamo-current-chunk-family mumamo-test-current-chunk-family)
           (here (point))
           chunk
           chunk2)
      (mumamo-save-buffer-state nil
        ;;(setq chunk (mumamo-create-chunk-at here)))
        (setq chunk (mumamo-find-chunks here "test1")))
      ;;(setq chunk2 (mumamo-get-chunk-at here))
      (setq chunk2 (mumamo-find-chunks here "set chunk2"))
      ;;(message "mumamo-test-create-chunk-at-point.chunk 1=%s" chunk)
      ;;(lwarn 'test-create-chunk-at :warning "chunk=%s, chunk2=%s" chunk chunk2)
      ;;(when (overlay-buffer chunk)
        (assert (eq chunk chunk2))
        ;;)
      ;;(message "mumamo-test-create-chunk-at-point.chunk 2=%s" chunk)
      ;;(syntax-ppss-flush-cache (1- (overlay-start chunk)))
      (syntax-ppss-flush-cache (overlay-start chunk))
      (let ((start (overlay-start chunk))
            (end   (overlay-end chunk)))
        ;;(setq syntax-ppss-last (cons 319 (parse-partial-sexp 1 1)))
        ;;(message "mumamo-test-create-chunk-at-point.chunk 2a=%s" chunk)
        (mumamo-save-buffer-state nil
          (mumamo-fontify-region-1 start end nil)))
      ;;(message "mumamo-test-create-chunk-at-point.chunk 3=%s" chunk)
      (unless mumamo-test-mode (mumamo-test-mode 1))
      ;;(message "mumamo-test-create-chunk-at-point.chunk 4=%s" chunk)
      chunk
      ;;(message "test 2.debugger=%s" debugger)
      ;;(mumamo-get-chunk-at here)
      (mumamo-find-chunks here "return value")
      )))

(defun mumamo-test-create-chunks-at-all-points ()
  (interactive)
  ;;(goto-char (point-min))
  (let (last-ovl
        this-ovl)
    (while (< (point) (point-max))
      ;;(setq this-ovl (mumamo-test-create-chunk-at-point))
      (setq this-ovl (mumamo-find-chunks (point) "test loop"))
      ;;(message "this-ovl=%s" this-ovl)
      (sit-for 0.005)
      ;;(sit-for 0)
      (when last-ovl
        (if (= (point) (overlay-end last-ovl))
            (assert (= (overlay-end last-ovl) (overlay-start this-ovl)))
          (assert (= (overlay-start last-ovl) (overlay-start this-ovl)))
          (assert (= (overlay-end last-ovl) (overlay-end this-ovl)))
          ))
      (if last-ovl
          (move-overlay last-ovl (overlay-start this-ovl) (overlay-end this-ovl))
        (setq last-ovl (make-overlay (overlay-start this-ovl) (overlay-end this-ovl))))
      (forward-char 1)
      )
    (message "No problems found")))

(defun mumamo-test-fontify-region ()
  (interactive)
  (let ((font-lock-mode t))
    ;;(mumamo-fontify-region-with (point-min) (point-max) nil 'php-mode nil)
    (mumamo-fontify-region (point-min) (point-max) t)))

;; Fix-me: can't byte compile:
;; (defun mumamo-test-easy-make ()
;;   (interactive)
;;   (let ((start-str "--Start Submode:")
;;         (end-str "--End Submode--")
;;         (start-reg nil))
;;     (setq start-reg
;;           ;; (rx
;;           ;;  (eval start-str)
;;           ;;  (0+ space)
;;           ;;  (submatch
;;           ;;   (0+ (any "a-z-")))
;;           ;;  (0+ space)
;;           ;;  "--"
;;           ;;  )
;;           (rx-to-string
;;            `(and
;;              ,start-str
;;              (0+ space)
;;              (submatch
;;               (0+ (any "a-z-")))
;;              (0+ space)
;;              "--"
;;              ))
;;           )
;;     (mumamo-easy-make-chunk-fun testchunk
;;       start-str
;;       start-reg
;;       end-str))
;;   (setq mumamo-current-chunk-family
;;         (list "testing"
;;               'text-mode
;;               (list
;;                'testchunk
;;                ))))

;; (defun mumamo-test-emb-perl ()
;;   (interactive)
;;   (let ((start-str "[-")
;;         (end-str "-]")
;;         (start-reg nil))
;;     (mumamo-easy-make-chunk-fun testchunk-ep
;;       start-str
;;       start-reg
;;       end-str))
;;   (setq mumamo-current-chunk-family
;;         (list "emb perl test"
;;               'perl-mode
;;               (list
;;                'testchunk-ep
;;                ))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; These are for testing bad initialization in mumamo. They can be
;; used for example with php-mode. (They are mainly for development
;; purposes.)
;;
;; (mumamo-bad-c-init)
(defun mumamo-bad-c-init() (/ 1 0))
(defun mumamo-setup-bad-c-init ()
  (interactive)
  (add-hook 'c-mode-common-hook 'mumamo-bad-c-init))
(defun mumamo-teardown-bad-c-init ()
  (interactive)
  (remove-hook 'c-mode-common-hook 'mumamo-bad-c-init))


;; (defmacro mumamo-get-backtrace (bodyform)
;;   "Evaluate BODYFORM, return backtrace as a string.
;; If there is an error in BODYFORM then return the backtrace as a
;; string, otherwise return nil."
;;   `(let* ((debugger-ret nil)
;;           (debugger (lambda (&rest debugger-args)
;;                       (message "DEBUGGER CALLED BEFORE")
;;                       (setq debugger-ret (with-output-to-string (backtrace)))
;;                       (message "DEBUGGER CALLED AFTER, debugger-ret=%s" debugger-ret)
;;                       ))
;;           (debug-on-error t)
;;           (debug-on-signal t)
;;           )
;;      (condition-case err
;;          (progn
;;            ,bodyform
;;            nil)
;;        (error
;;         (message "err=%S" err)
;;         (message "debugger-ret=%S\n\n\n" debugger-ret)
;;         (let* ((errmsg (error-message-string err))
;;                (debugger-lines (split-string debugger-ret "\n"))
;;                (dbg-ret (mapconcat 'identity (nthcdr 6 debugger-lines) "\n")))
;;           (concat errmsg "\n" dbg-ret))))))

;; (defun mumamo-test3-debug()
;;   (interactive)
;;   (message "%s"
;;            (mumamo-get-backtrace
;;             (mumamo-test-major-mode-init 'php-mode))))

;; (defun mumamo-test2-debug()
;;   (interactive)
;;   (mumamo-condition-case var
;;                          (mumamo-test-major-mode-init 'php-mode)
;;                          handlers))

(defun mumamo-test-debug()
  (interactive)
  (condition-case err
      (let ((debugger 'mumamo-debug)
            (debug-on-error t)
            (debug-on-signal t))
        ;;(message "here d")(sit-for 1)
        (mumamo-test-major-mode-init 'php-mode))
    (error (message "here 2 err=%S" err))))

(defun mumamo-debug (&rest debugger-args)
  (let ((s (with-output-to-string (backtrace))))
    (message "mumamo-debug: %s" s)))

;; (defun mumamo-bt-to-msg (msg)
;;   (mumamo-msgfntfy "%s: %s" msg
;;            (with-output-to-string
;;              (backtrace))))

(defun mumamo-test-major-mode-init (major)
  "Turn on major mode MAJOR in a temp buffer.
This function should be used after getting errors during
fontification where the message in the *Message* buffer tells
that you should call it to get a traceback.

Send the traceback you get, if any, together with the message in
the message buffer when reporting the error."
  (interactive "CMajor mode: ")
  (with-temp-buffer
    ;;(setq mumamo-explicitly-turned-on-off t)
    (setq debug-on-error t)
    (funcall major)))

(provide 'mumamo-test)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; mumamo-test.el ends here