blob: f127b1965f173369c8aa899c0a66778b2becc4c7 (
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
|
;;; coq-tests.el --- integration tests -*- lexical-binding: t; -*-
;;; Commentary:
;;
;;; Eval this to run the tests interactively <C-x C-e>
;;
;; (progn (load-file "coq-tests.el") (call-interactively #'ert))
(setq debug-on-error t) ; open the debugger on error -- may be commented-out
; (require 'ert-async)
;(setq ert-async-timeout 2)
;;; Code:
; Exemple de code Lisp qui lance des commandes au prouveur en arrière-plan
; (defun has-error ()
; "True if error in Proof"
; (eq 'error proof-shell-last-output-kind))
;
; (defun test-process-invisible-split ()
; (proof-shell-invisible-command "split."
; 'waitforit
; #'proof-done-invisible
; 'no-error-display 'no-response-display 'no-goals-display))
;
; (defun test-process-invisible-tactics-then-reset-and-insert ()
; (interactive)
; (let ((reset-cmd ; store backtracking info before proof search
; (format "Backtrack %s %s %s . "
; (int-to-string coq-last-but-one-statenum)
; (int-to-string coq-last-but-one-proofnum)
; 0)))
; ;; Toy example of proof search
; (while (not (has-error))
; (message "Trying split.")
; (test-process-invisible-split))
; ;; Reset to the previous state
; (proof-shell-invisible-command reset-cmd 'waitforit #'proof-done-invisible)
; ;; Insert (and re-process) the found script
; (proof-insert-sendback-command "split.\nsplit.\nsplit.\nQed.")))
;
; (test-process-invisible-tactics-then-reset-and-insert)
(defconst coq-test-file-prefix "coq_test_")
(defun get-string-from-file (filePath)
"Return filePath's file content."
(with-temp-buffer
(insert-file-contents filePath)
(buffer-string)))
(defun read-lines (filePath)
"Return a list of lines of a file at filePath."
(with-temp-buffer
(insert-file-contents filePath)
(split-string (buffer-string) "\n" t)))
(defun coq-test-init ()
"Ensure `coq' is loaded."
(unless (featurep 'coq)
(add-to-list 'load-path
(locate-dominating-file buffer-file-name "proof-general.el"))
(load "proof-general")
(proofgeneral "coq")))
(defun coq-test-exit ()
"Exit the Coq process."
(proof-shell-exit t))
; (coq-test-on-file nil (message (buffer-file-name)) (message "OK") 42)
;; DEFINITION OF MOCKS, SEE `coq-mock' BELOW
;; Another solution would consist in using el-mock, mentioned in:
;; https://www.gnu.org/software/emacs/manual/html_mono/ert.html#Mocks-and-Stubs
(defun mock-proof-display-three-b (&rest rest)
(message (concat "Skipping proof-display-three-b on input: "
(pp-to-string rest)))
; Result:
nil)
;; AVOID THE FOLLOWING ERROR:
;; Hit M-x proof-layout-windows to reset layout
;; Debugger entered--Lisp error: (error "Window #<window 6 on *goals*> too small for splitting")
;; signal(error ("Window #<window 6 on *goals*> too small for splitting"))
;; error("Window %s too small for splitting" #<window 6 on *goals*>)
;; split-window(nil nil)
;; split-window-vertically()
;; proof-safe-split-window-vertically()
;; proof-select-three-b(nil #<buffer *goals*> #<buffer *response*> smart)
;; proof-display-three-b(smart)
;; proof-layout-windows()
;; proof-multiple-frames-enable()
;; proof-shell-start()
;; proof-shell-ready-prover()
(defun coq-mock (f)
(require 'pg-response) ; load the feature defining proof-display-three-b first
(cl-letf (;((symbol-function 'foo) #'mock-foo)
((symbol-function 'proof-display-three-b) #'mock-proof-display-three-b))
(funcall f)))
;; Run <C-x C-e> on:
;; (coq-mock #'main)
(defun coq-test-cmd (cmd)
;;(coq-test-on-file)
;;(coq-test-init)
(proof-shell-invisible-command
cmd
'waitforit
#'proof-done-invisible
'no-error-display 'no-response-display 'no-goals-display))
(defun coq-fixture-on-file (file body)
"Fixture to setup the test env: open FILE if non-nil, or a temp file
then evaluate the BODY function and finally tear-down (exit Coq)."
;; AVOID THE FOLLOWING ERROR:
;; Starting: -emacs
;; Debugger entered--Lisp error: (wrong-type-argument stringp nil)
;; file-name-directory(nil)
;; scomint-exec-1("coq" #<buffer *coq*> nil ("-emacs"))
;; scomint-exec(#<buffer *coq*> "coq" nil nil ("-emacs"))
;; scomint-make-in-buffer("coq" nil nil nil "-emacs")
;; apply(scomint-make-in-buffer "coq" nil nil nil "-emacs")
;; scomint-make("coq" nil nil "-emacs")
;; apply(scomint-make ("coq" nil nil "-emacs"))
;; proof-shell-start()
;; proof-shell-ready-prover()
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; For info on macros: https://mullikine.github.io/posts/macro-tutorial
;;; (pp (macroexpand '(macro args)))
(save-excursion
(let* ((openfile (or file
(concat (make-temp-file coq-test-file-prefix) ".v")))
;; if FILE is nil, create a temporary Coq file, removed in the end
(rmfile (unless file openfile))
(buffer (find-file openfile)))
(message "Opening file %s ..." file)
(unwind-protect
(progn
(coq-test-init)
(set-buffer buffer)
(coq-mock body))
(kill-buffer buffer)
(when rmfile (message "Removing file %s ..." rmfile))
(ignore-errors (delete-file rmfile))))))
(defun coq-test-001 ()
;; TODO: retrieve the test status, maybe by changing the function above
(coq-test-cmd (process-list)))
;; TODO: Use https://github.com/rejeep/ert-async.el
;; and/or ERT https://www.gnu.org/software/emacs/manual/html_node/ert/index.html
(ert-deftest coq-test-running ()
(coq-fixture-on-file nil
(lambda ()
(coq-test-cmd "Print 0.")
;; (should (process-list)) ; wouldn't be a strong enough assert.
(should (get-process "coq")))))
(ert-deftest coq-test-print ()
(coq-fixture-on-file "./test1.v"
(lambda ()
(coq-test-cmd (car (read-lines "./test1.v")))
;; TODO/Cyril: call (proof-goto-point (point-max))
(should (equal "trois is defined"
(string-trim
(setq temp
(with-current-buffer "*response*"
(buffer-substring-no-properties (point-min) (point-max))))))))))
;; TODO
;; (ert-deftest coq-test-lemma ()
;; (coq-init-exit
;; (lambda ()
;; (coq-test-cmd (car (read-lines "./test1.v")))
;; (should (equal "trois is defined"
;; (with-current-buffer "*response*"
;; (goto-char (point-min))
;; (buffer-substring-no-properties (line-beginning-position) (line-end-position) )
;; )
;; )))))
;; (defun coq-test-main ()
;; (coq-mock #'coq-test-001))
;; ;(ignore-errors (coq-test-exit))
;; (coq-test-main)
(provide 'coq-tests)
;;; learn-ocaml-tests.el ends here
|