XEmacs 21.4.4 "Artificial Intelligence".
[chise/xemacs-chise.git.1] / tests / automated / syntax-tests.el
1 ;; Copyright (C) 1999 Free Software Foundation, Inc.
2
3 ;; Author: Yoshiki Hayashi  <t90553@mail.ecc.u-tokyo.ac.jp>
4 ;; Maintainer: Yoshiki Hayashi  <t90553@mail.ecc.u-tokyo.ac.jp>
5 ;; Created: 1999
6 ;; Keywords: tests
7
8 ;; This file is part of XEmacs.
9
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 ;; 02111-1307, USA.
24
25 ;;; Synched up with: Not in FSF.
26
27 ;;; Commentary:
28
29 ;; Test syntax related functions.
30 ;; Right now it tests scan_words using forward-word and backward-word.
31 ;; See test-harness.el for instructions on how to run these tests.
32
33 ;;; Notation
34 ;; W:   word constituent character.
35 ;; NW:  non word constituent character.
36 ;; -!-: current point.
37 ;; EOB: end of buffer
38 ;; BOB: beginning of buffer.
39
40 ;; Algorithm of scan_words is simple.  It just searches SW and then
41 ;; moves to NW.  When with MULE, it also stops at word boundary.  Word
42 ;; boundary is tricky and listing all possible cases will be huge.
43 ;; Those test are omitted here as it doesn't affect core
44 ;; functionality.
45
46 (defun test-forward-word (string stop)
47   (goto-char (point-max))
48   (let ((point (point)))
49     (insert string)
50     (goto-char point)
51     (forward-word 1)
52     (Assert (eq (point) (+ point stop)))))
53
54 (with-temp-buffer
55   ;; -!- W NW
56   (test-forward-word "W " 1)
57   (test-forward-word "WO " 2)
58   ;; -!- W EOB
59   (test-forward-word "W" 1)
60   (test-forward-word "WO" 2)
61   ;; -!- NW EOB
62   (test-forward-word " " 1)
63   (test-forward-word " !" 2)
64   ;; -!- NW W NW
65   (test-forward-word " W " 2)
66   (test-forward-word " WO " 3)
67   (test-forward-word " !W " 3)
68   (test-forward-word " !WO " 4)
69   ;; -!- NW W EOB
70   (test-forward-word " W" 2)
71   (test-forward-word " WO" 3)
72   (test-forward-word " !W" 3)
73   (test-forward-word " !WO" 4))
74
75 (defun test-backward-word (string stop)
76   (goto-char (point-min))
77   (insert string)
78   (let ((point (point)))
79     (backward-word 1)
80     (Assert (eq (point) (- point stop)))))
81
82 (with-temp-buffer
83   ;; NW W -!-
84   (test-backward-word " W" 1)
85   (test-backward-word " WO" 2)
86   ;; BOB W -!-
87   (test-backward-word "W" 1)
88   (test-backward-word "WO" 2)
89   ;; BOB NW -!-
90   ;; -!-NW EOB
91   (test-backward-word " " 1)
92   (test-backward-word " !" 2)
93   ;; NW W NW -!-
94   (test-backward-word " W " 2)
95   (test-backward-word " WO " 3)
96   (test-backward-word " W !" 3)
97   (test-backward-word " WO !" 4)
98   ;; BOB W NW -!-
99   (test-backward-word "W " 2)
100   (test-backward-word "WO " 3)
101   (test-backward-word "W !" 3)
102   (test-backward-word "WO !" 4))
103
104 ;; Works like test-forward-word, except for the following:
105 ;; after <string> is inserted, the syntax-table <apply-syntax>
106 ;; is applied to position <apply-pos>.
107 ;; <apply-pos> can be in the form (start . end), or can be a
108 ;; character position.
109 (defun test-syntax-table (string apply-pos apply-syntax stop)
110   (goto-char (point-max))
111   (unless (consp apply-pos)
112         (setq apply-pos `(,apply-pos . ,(+ 1 apply-pos))))
113   (let ((point (point)))
114         (insert string)
115         (put-text-property (+ point (car apply-pos)) (+ point (cdr apply-pos))
116                                            'syntax-table apply-syntax)
117         (goto-char point)
118         (forward-word 1)
119         (Assert (eq (point) (+ point stop)))))
120
121 ;; test syntax-table extents
122 (with-temp-buffer
123   ;; Apply punctuation to word
124   (test-syntax-table "WO" 1 `(,(syntax-string-to-code ".")) 1)
125   ;; Apply word to punctuation
126   (test-syntax-table "W." 1 `(,(syntax-string-to-code "w")) 2))
127
128 ;; Test forward-comment at buffer boundaries
129 (with-temp-buffer
130   (c-mode)
131   (insert "// comment\n")
132   (forward-comment -2)
133   (Assert (eq (point) (point-min)))
134
135   (let ((point (point)))
136         (insert "/* comment */")
137         (goto-char point)
138         (forward-comment 2)
139         (Assert (eq (point) (point-max)))
140
141         ;; this last used to crash
142         (parse-partial-sexp point (point-max))))