1 ;;; semantic-utest.el --- Tests for semantic's parsing system.
3 ;;; Copyright (C) 2003, 2004, 2007 Eric M. Ludlam
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; X-RCS: $Id: semantic-utest.el,v 1.1 2007-11-26 15:10:44 michaels Exp $
8 ;; This file is not part of GNU Emacs.
10 ;; Semantic is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This software is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; Semantic's parsing and partial parsing system is pretty complex.
28 ;; These unit tests attempt to emulate semantic's partial reparsing
29 ;; and full reparsing system, and anything else I may feel the urge
30 ;; to write a test for.
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 (defvar semantic-utest-C-buffer-contents
38 "/* Test file for C language for Unit Tests */
51 float funp1(char arg11, char arg12);
53 char fun2(int arg_21, int arg_22) /*1*/
55 struct mystruct1 *ms1 = malloc(sizeof(struct mystruct1));
57 char sv = calc_sv(var1);
61 } else if (arg_21 == 0) {
63 } else if (arg_22 == 0) {
69 printf(\"SV = %d\\n\", sv);
77 "Contents of a C buffer initialized by this unit test.
78 Be sure to change `semantic-utest-C-name-contents' when you
79 change this variable.")
81 (defvar semantic-utest-C-h-buffer-contents
82 "/* Test file for C language header file for Unit Tests */
87 "Contents of a C header file buffer initialized by this unit test.")
90 (defvar semantic-utest-C-filename "/tmp/sutest.c"
91 "File to open and erase during this test for C.")
93 (defvar semantic-utest-C-filename-h
94 (concat (file-name-sans-extension semantic-utest-C-filename)
96 "Header file filename for C")
99 (defvar semantic-utest-C-name-contents
102 nil (overlay 48 66 "sutest.c"))
103 ("sutest.h" include nil nil (overlay 67 86 "sutest.c"))
108 (reparse-symbol classsubparts)
109 (overlay 109 120 "sutest.c"))
112 (reparse-symbol classsubparts)
113 (overlay 123 135 "sutest.c"))
116 (reparse-symbol classsubparts)
117 (overlay 138 151 "sutest.c")))
119 nil (overlay 88 154 "sutest.c"))
122 nil (overlay 156 165 "sutest.c"))
124 (:prototype-flag t :arguments
127 (reparse-symbol arg-sub-list)
128 (overlay 179 190 "sutest.c"))
131 (reparse-symbol arg-sub-list)
132 (overlay 191 202 "sutest.c")))
134 nil (overlay 167 203 "sutest.c"))
139 (reparse-symbol arg-sub-list)
140 (overlay 215 226 "sutest.c"))
143 (reparse-symbol arg-sub-list)
144 (overlay 227 238 "sutest.c")))
146 nil (overlay 205 566 "sutest.c")))
147 "List of expected tag names for C.")
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 ;; Data for Python tests
154 (defvar semantic-utest-Python-buffer-contents
166 ; "pyhon test case. notice that python is indentation sensitive
169 (defvar semantic-utest-Python-name-contents
173 (reparse-symbol function_parameters)
174 (overlay 10 11 "tst.py"))
176 (reparse-symbol function_parameters)
177 (overlay 12 13 "tst.py"))
179 (reparse-symbol function_parameters)
180 (overlay 14 15 "tst.py"))))
181 nil (overlay 1 31 "tst.py"))
185 (reparse-symbol function_parameters)
186 (overlay 41 42 "tst.py"))
188 (reparse-symbol function_parameters)
189 (overlay 43 44 "tst.py"))
191 (reparse-symbol function_parameters)
192 (overlay 45 46 "tst.py"))))
193 nil (overlay 32 65 "tst.py")))
195 "List of expected tag names for Python.")
199 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
200 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
201 ;; Data for Java tests
203 (defvar semantic-utest-Java-buffer-contents
206 void fun1(int a,int b){
210 void fun2(int a,int b){ //1
218 (defvar semantic-utest-Java-name-contents
225 (reparse-symbol formal_parameters)
226 (overlay 30 35 "JavaTest.java"))
229 (reparse-symbol formal_parameters)
230 (overlay 36 41 "JavaTest.java")))
232 (reparse-symbol class_member_declaration)
233 (overlay 20 61 "JavaTest.java"))
238 (reparse-symbol formal_parameters)
239 (overlay 75 80 "JavaTest.java"))
242 (reparse-symbol formal_parameters)
243 (overlay 81 86 "JavaTest.java")))
245 (reparse-symbol class_member_declaration)
246 (overlay 65 110 "JavaTest.java")))
248 nil (overlay 2 113 "JavaTest.java")))
249 "List of expected tag names for Java."
253 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 ;; Data for Javascript tests
257 (defvar semantic-utest-Javascript-buffer-contents
263 function fun2(a,b){ //1
270 (defvar semantic-utest-Javascript-name-contents
274 (reparse-symbol FormalParameterList)
275 (overlay 15 16 "tst.js"))
277 (reparse-symbol FormalParameterList)
278 (overlay 18 19 "tst.js"))))
279 nil (overlay 1 39 "tst.js"))
283 (reparse-symbol FormalParameterList)
284 (overlay 55 56 "tst.js"))
286 (reparse-symbol FormalParameterList)
287 (overlay 57 58 "tst.js"))))
288 nil (overlay 41 82 "tst.js")))
290 "List of expected tag names for Javascript.")
294 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
295 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
296 ;; Data for Makefile tests
298 (defvar semantic-utest-Makefile-buffer-contents
311 (defvar semantic-utest-Makefile-name-contents
312 '(("t1" function nil nil (overlay 1 9 "Makefile"))
316 nil (overlay 18 28 "Makefile")))
317 "List of expected tag names for Makefile.")
320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
322 ;; Data for Scheme tests
324 (defvar semantic-utest-Scheme-buffer-contents
332 (defvar semantic-utest-Scheme-name-contents
334 (:default-value ("2"))
335 nil (overlay 3 18 "tst.scm"))
337 (:default-value ("3"))
338 nil (overlay 21 55 "tst.scm")))
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343 ;; Data for Html tests
345 (defvar semantic-utest-Html-buffer-contents
355 (defvar semantic-utest-Html-name-contents
358 (("hello" section nil nil (overlay 21 24 "tst.html"))))
359 nil (overlay 10 15 "tst.html")))
363 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
364 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
365 ;; Data for Csharp C# tests
367 (defvar semantic-utest-Csharp-buffer-contents
370 int fun1(int a, int b) {
372 int fun2(int a, int b) {
377 (defvar semantic-utest-Csharp-name-contents
384 (reparse-symbol formal_parameters)
385 (overlay 30 35 "tst.cs"))
388 (reparse-symbol formal_parameters)
389 (overlay 37 42 "tst.cs")))
391 (reparse-symbol class_member_declaration)
392 (overlay 21 61 "tst.cs"))
397 (reparse-symbol formal_parameters)
398 (overlay 73 78 "tst.cs"))
401 (reparse-symbol formal_parameters)
402 (overlay 80 85 "tst.cs")))
404 (reparse-symbol class_member_declaration)
405 (overlay 64 104 "tst.cs")))
407 nil (overlay 1 106 "tst.cs")))
411 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
416 (defun semantic-utest-makebuffer (filename contents)
417 (let ((buff (find-file-noselect filename)))
421 ;(semantic-fetch-tags) ;JAVE could this go here?
426 (defun semantic-utest-C ()
427 "Run semantic's C unit test."
430 (let ((buff (semantic-utest-makebuffer semantic-utest-C-filename semantic-utest-C-buffer-contents))
431 (buff2 (semantic-utest-makebuffer semantic-utest-C-filename-h semantic-utest-C-h-buffer-contents))
433 (semantic-fetch-tags)
436 ;; Turn off a range of modes
437 (semantic-idle-scheduler-mode -1)
439 ;; Turn on some modes
440 (semantic-highlight-edits-mode 1)
442 ;; Update tags, and show it.
443 (semantic-fetch-tags)
444 (switch-to-buffer buff)
448 (message "First parsing test.")
449 (semantic-utest-verify-names semantic-utest-C-name-contents)
451 (message "Invalid tag test.")
452 (semantic-utest-last-invalid semantic-utest-C-name-contents '("fun2") "/\\*1\\*/" "/* Deleted this line */")
453 (semantic-utest-verify-names semantic-utest-C-name-contents)
456 ;; (kill-buffer buff)
457 ;; (kill-buffer buff2)
459 (message "All C tests passed.")
465 (defun semantic-utest-generic (testname filename contents name-contents names-removed killme insertme)
466 "generic unit test according to template, should work for languages withouth .h files, python javascript java."
468 (let ((buff (semantic-utest-makebuffer filename contents))
470 ;; Turn off a range of modes
471 (semantic-idle-scheduler-mode -1)
473 ;; Turn on some modes
474 (semantic-highlight-edits-mode 1)
476 ;; Update tags, and show it.
477 (semantic-fetch-tags)
478 (switch-to-buffer buff)
482 (message "First parsing test %s." testname)
483 (semantic-utest-verify-names name-contents)
485 (message "Invalid tag test %s." testname)
486 (semantic-utest-last-invalid name-contents names-removed killme insertme)
487 (semantic-utest-verify-names name-contents)
490 ;; (kill-buffer buff)
491 ;; (kill-buffer buff2)
493 (message "All %s tests passed." testname)
496 (defun semantic-utest-Python()
498 (semantic-utest-generic "Python" "/tmp/pytest.py" semantic-utest-Python-buffer-contents semantic-utest-Python-name-contents '("fun2") "#1" "#deleted line") )
501 (defun semantic-utest-Javascript()
503 (semantic-utest-generic "Javascript" "/tmp/javascripttest.js" semantic-utest-Javascript-buffer-contents semantic-utest-Javascript-name-contents '("fun2") "//1" "//deleted line")
506 (defun semantic-utest-Java()
508 (semantic-utest-generic "Java" "/tmp/JavaTest.java" semantic-utest-Java-buffer-contents semantic-utest-Java-name-contents '("fun2") "//1" "//deleted line")
511 (defun semantic-utest-Makefile()
513 (semantic-utest-generic "Makefile" "/tmp/Makefile" semantic-utest-Makefile-buffer-contents semantic-utest-Makefile-name-contents '("fun2") "#1" "#deleted line")
516 (defun semantic-utest-Scheme()
518 (semantic-utest-generic "Scheme" "/tmp/tst.scm" semantic-utest-Scheme-buffer-contents semantic-utest-Scheme-name-contents '("fun2") ";1" ";deleted line")
522 (defun semantic-utest-Html()
524 (semantic-utest-generic "HTML" "/tmp/tst.html" semantic-utest-Html-buffer-contents semantic-utest-Html-name-contents '("fun2") "<!--1-->" "<!--deleted line-->")
527 ;look at http://mfgames.com/linux/csharp-mode
528 (defun semantic-utest-Csharp() ;; hmm i dont even know how to edit a scharp file. need a csharp mode implementation i suppose
530 (semantic-utest-generic "C#" "/tmp/csharptest.cs" semantic-utest-Csharp-buffer-contents semantic-utest-Csharp-name-contents '("fun2") "//1" "//deleted line")
533 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
536 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
539 ;-export([hello_world/0]).
542 ; io:format("Hello World ~n").
543 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
544 (defun semantic-utest-Erlang()
546 (semantic-utest-generic "Erlang" "/tmp/tst.erl" semantic-utest-Erlang-buffer-contents semantic-utest-Erlang-name-contents '("fun2") "//1" "//deleted line")
549 ;texi is also supported
550 (defun semantic-utest-Texi()
552 (semantic-utest-generic "texi" "/tmp/tst.texi" semantic-utest-Texi-buffer-contents semantic-utest-Texi-name-contents '("fun2") "//1" "//deleted line")
555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
557 (defun semantic-utest-main()
561 (semantic-utest-Python)
562 (semantic-utest-Java)
563 (semantic-utest-Javascript)
564 (semantic-utest-Makefile)
565 (semantic-utest-Scheme)
566 ;(semantic-utest-Html)
567 ;(semantic-utest-Csharp)
570 ;;; Buffer contents validation
572 (defun semantic-utest-match-attributes (attr1 attr2 skipnames)
573 "Compare attribute lists ATTR1 and ATTR2.
574 Argument SKIPNAMES is a list of names that may be child nodes to skip."
576 (while (and res attr1 attr2)
580 (cond ((and (listp (car attr1))
581 (semantic-tag-p (car (car attr1))))
582 ;; Compare the list of tags...
583 (semantic-utest-taglists-equivalent-p
584 (car attr2) (car attr1) skipnames)
587 (equal (car attr1) (car attr2)))))
590 (error "TAG INTERNAL DIFF: %S %S"
591 (car attr1) (car attr2)))
593 (setq attr1 (cdr attr1)
597 (defun semantic-utest-equivalent-tag-p (tag1 tag2 skipnames)
598 "Determine if TAG1 and TAG2 are the same.
599 SKIPNAMES includes lists of possible child nodes that should be missing."
600 (and (equal (semantic-tag-name tag1) (semantic-tag-name tag2))
601 (semantic-tag-of-class-p tag1 (semantic-tag-class tag2))
602 (semantic-utest-match-attributes
603 (semantic-tag-attributes tag1) (semantic-tag-attributes tag2)
607 (defun semantic-utest-taglists-equivalent-p (table names skipnames)
608 "Compare TABLE and NAMES, where skipnames allow list1 to be different.
609 SKIPNAMES is a list of names that should be skipped in the NAMES list."
610 (let ((SN skipnames))
612 (setq names (remove (car SN) names))
614 (while (and names table)
615 (if (not (semantic-utest-equivalent-tag-p (car names)
618 (error "Expected %s, found %s"
619 (semantic-format-tag-prototype (car names))
620 (semantic-format-tag-prototype (car table))))
621 (setq names (cdr names)
623 (when names (error "Items forgotten: %S" names))
624 (when table (error "Items extra: %S" table))
627 (defun semantic-utest-verify-names (name-contents &optional skipnames)
628 "Verify the names of the test buffer from NAME-CONTENTS.
629 Argument SKIPNAMES is a list of names that should be skipped
630 when analyzing the file.
632 JAVE this thing would need to be recursive to handle java and csharp"
633 (let ((names name-contents)
634 (table (semantic-fetch-tags))
636 (semantic-utest-taglists-equivalent-p table names skipnames)
639 ;;;;;;;;;;;;;;;;;;;;;;;;
640 ; JAVE redefine a new validation function
641 ; is not quite as good as the old one yet
642 (defun semantic-utest-verify-names-jave (name-contents &optional skipnames)
643 "JAVE version of `semantic-utest-verify-names'.
644 NAME-CONTENTS is a sample of the tags buffer to test against.
645 SKIPNAMES is a list of names to remove from NAME-CONTENTS"
646 (assert (semantic-utest-verify-names-2 name-contents (semantic-fetch-tags))
650 (defun semantic-utest-verify-names-2 (l1 l2)
651 (cond ( (and (consp l1) (equal (car l1) 'overlay))
656 (and (semantic-utest-verify-names-2 (car l1) (car l2)) (semantic-utest-verify-names-2 (cdr l1) (cdr l2))))
657 (t (error "internal error"))))
663 ;;; Kill indicator line
665 (defvar semantic-utest-last-kill-text nil
666 "The text from the last kill.")
668 (defvar semantic-utest-last-kill-pos nil
669 "The position of the last kill.")
671 (defun semantic-utest-kill-indicator ( killme insertme)
672 "Kill the line with KILLME on it and insert INSERTME in its place."
673 (goto-char (point-min))
674 ; (re-search-forward (concat "/\\*" indicator "\\*/")); JAVE this isnt generic enough for different lagnuages
675 (re-search-forward killme)
677 (setq semantic-utest-last-kill-pos (point))
678 (setq semantic-utest-last-kill-text
679 (buffer-substring (point) (point-at-eol)))
680 (delete-region (point) (point-at-eol))
685 (defun semantic-utest-unkill-indicator ()
686 "Unkill the last indicator."
687 (goto-char semantic-utest-last-kill-pos)
688 (delete-region (point) (point-at-eol))
689 (insert semantic-utest-last-kill-text)
696 (defun semantic-utest-last-invalid (name-contents names-removed killme insertme)
697 "Make the last fcn invalid."
698 (semantic-utest-kill-indicator killme insertme)
699 ; (semantic-utest-verify-names name-contents names-removed); verify its gone ;new validator doesnt handle skipnames yet
700 (semantic-utest-unkill-indicator);put back killed stuff
706 ;"#<overlay from \\([0-9]+\\) to \\([0-9]+\\) in \\([^>]*\\)>"
707 ;#<overlay from \([0-9]+\) to \([0-9]+\) in \([^>]*\)>
708 ;(overlay \1 \2 "\3")
712 ;; these are some unit tests for cedet that I got from Eric and modified a bit for:
716 ;; I tried to generalize the structure of the tests a bit to make it easier to add languages
719 ;; Many items in the checklist look like:
721 ;; M-x global-semantic-highlight-edits-mode RET
722 ;; - Edit a file. See the highlight of newly inserted text.
723 ;; - Customize `semantic-edits-verbose-flag' to be non-nil.
724 ;; - Wait for the idle scheduler, it should clean up the edits.
725 ;; - observe messages from incremental parser. Do they relate
727 ;; - M-x bovinate RET - verify your changes are reflected.
729 ;; It's all about watching the behavior. Timers go off, things get
730 ;; cleaned up, you type in new changes, etc. An example I tried to
731 ;; do is below, but covers only 1 language, and not very well at that.
732 ;; I seem to remember seeing a unit test framework going by one of the
733 ;; lists. I'm not sure if that would help.
735 ;; Another that might be automatable:
737 ;; M-x semantic-analyze-current-context RET
738 ;; - Do this in different contexts in your language
739 ;; files. Verify that reasonable results are returned
740 ;; such as identification of assignments, function arguments, etc.
742 ;; Anyway, those are some ideas. Any effort you put it will be helpful!