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
|
;;; Tests for gnuplot-mode.
;;; Currently these attempt to cover the correct identification of
;;; string and comment syntax.
(require 'gnuplot)
(require 'ert)
(eval-when-compile (require 'cl))
;; Hide an annoying interactive message during batch testing
(when (require 'nadvice nil t)
(advice-add
'message
:around
(lambda (orig-message format-string &rest args)
(unless (string= format-string
"gnuplot-mode %s (gnuplot %s) -- report bugs with %S")
(apply orig-message format-string args)))))
(eval-and-compile
(defvar gnuplot-string-test-contexts
'("%s"
"print %s"
"print %s, 2+3"
"print %s, 'another string'"
"print %s, \"another string\""
"print 'another string', %s"
"\"double-quoted string\" %s"
"\"double-quoted string\"%s"
"%s \"double-quoted string\""
"%s\"double-quoted string\""
"'single-quoted string' %s"
"%s 'single-quoted string'"
"'single-quoted string' %s 'single-quoted string'")))
(defun gnuplot-test-string-in-context (string context)
"Test syntax-propertizing of STRING in CONTEXT in gnuplot-mode.
STRING contains text representing a Gnuplot string literal.
CONTEXT is a context is a context to place the literal within,
represented by a format-string with a single %s placeholder.
Returns non-nil if STRING is correctly recognised as a single
string by `scan-sexps'."
(destructuring-bind (prologue epilogue)
(split-string context "%s")
(with-temp-buffer
(gnuplot-mode)
(let (start end)
(save-excursion
(insert prologue)
(setq start (point))
(insert string)
(setq end (point))
(insert epilogue))
(when (fboundp 'syntax-propertize)
(syntax-propertize (point-max)))
(string= (buffer-substring start (scan-sexps start 1))
string)))))
(defmacro gnuplot-test-string (name string)
"Define an `ert' test to check syntax recognition of STRING in gnuplot-mode.
The test checks that STRING is correctly recognised as a single
string-literal in multiple different contexts, as determined by
`gnuplot-string-test-contexts'."
(declare (indent 1))
`(ert-deftest ,name ()
,string
,@(loop for context in gnuplot-string-test-contexts
collect
`(should (gnuplot-test-string-in-context ,string ,context)))))
;;;; Tests for double-quoted strings
(gnuplot-test-string gnuplot-double-quoted-string
"\"double-quoted string\"")
(gnuplot-test-string gnuplot-double-quoted-with-single-quotes
"\"double-quoted 'with single quotes' embedded\"")
(gnuplot-test-string gnuplot-double-quoted-with-single-quotes-2
"\"'single quotes inside double quotes'\"")
(gnuplot-test-string gnuplot-double-quoted-escapes
"\"double-quoted \\\\ string \\\" with embedded \\\" escapes\"")
(gnuplot-test-string gnuplot-double-quoted-escapes-2
"\"escaped quote before closing quote \\\"\"")
(gnuplot-test-string gnuplot-double-quoted-escapes-3
"\"escaped backslash before closing quote \\\\\"")
(gnuplot-test-string gnuplot-double-quoted-escapes-4
"\"\\\" escaped quote after opening quote\"")
(gnuplot-test-string gnuplot-double-quoted-escapes-5
"\"\\\\ escaped backslash after opening quote\"")
(gnuplot-test-string gnuplot-double-quoted-escapes-6
"\"\\\\\\\" escaped backslashes + escaped quotes (1) \\\\\\\"\"")
(gnuplot-test-string gnuplot-double-quoted-empty
"\"\"")
(gnuplot-test-string gnuplot-double-quoted-string-containing-escaped-quotes
"\"\\\"\\\"\"")
(gnuplot-test-string gnuplot-newline-terminated-double-quoted-string
"\"newline-terminated
")
(gnuplot-test-string gnuplot-double-quoted-with-embedded-newlines
"\"string \\
with embedded \\
newlines\"")
(gnuplot-test-string gnuplot-newline-terminated-double-quoted-string-with-newline
;; with newlines
"\"newline-terminated string \\
with newlines
")
;;;; Tests for single-quoted strings
(gnuplot-test-string gnuplot-single-quoted-strings
"'single-quoted string'")
(gnuplot-test-string gnuplot-single-quoted-empty
"''")
(gnuplot-test-string gnuplot-single-quoted-with-double-quotes
"'a single-quoted string \"containing a double-quoted string\"'")
(gnuplot-test-string gnuplot-single-quoted-quotes
"'embedded '' quote '' characters'")
(gnuplot-test-string gnuplot-single-quoted-quotes-2
"'embedded '' quote '' characters'''")
(gnuplot-test-string gnuplot-single-quoted-quotes-3
"' '''")
(gnuplot-test-string gnuplot-single-quoted-backslashes
"'embedded \\ backslashes \\'")
(gnuplot-test-string gnuplot-single-quoted-backslashes-2
"'multiple \\ embedded \\\\ backslashes \\\\\\'")
(gnuplot-test-string gnuplot-single-quoted-trailing-backslash
"'trailing backslash\\'")
(gnuplot-test-string gnuplot-single-quoted-newline-terminated
"'newline terminated\n")
(gnuplot-test-string gnuplot-single-quoted-newline-terminated-quotes
"'embedded '' escapes \\ ending at newline ''\n")
(gnuplot-test-string gnuplot-single-quoted-embedded-newlines
"'string \\\n with embedded \\\nnewlines'")
(gnuplot-test-string gnuplot-single-quoted-embedded-newlines-backslashes
"'string \\\\\n with \\\\\\\n multiple \\\\\\\\\n backslashes'")
(gnuplot-test-string gnuplot-single-quoted-newline-terminated-embedded-newline
"'newline-terminated string \\\n with newlines\n")
;;;; Comment syntax
(eval-and-compile
(defvar gnuplot-comment-test-contexts
'("%s"
"\n%s"
"\n\n%s\n\n"
"print 'single-quoted string' %s"
"print \"double-quoted string\" %s"
"print 'single-quoted string # with hash mark' %s"
"print \"double-quoted string # with hash mark\" %s"
"plot sin(x), cos(x) %s"
"plot sin(x)
%s
plot cos(x)"
"# one-line comment
%s"
"# multi-line \\
comment
%s")
"List of contexts in which to test syntax recognition of comments."))
(defun gnuplot-test-comment-in-context (comment context)
"Non-nil if COMMENT is correctly recognised within CONTEXT in gnuplot-mode."
(destructuring-bind (prologue epilogue)
(split-string context "%s")
(with-temp-buffer
(gnuplot-mode)
(let (start end)
(save-excursion
(insert prologue)
(setq start (point))
(insert comment)
(setq end (point))
(insert epilogue))
(when (fboundp 'syntax-propertize)
(syntax-propertize (point-max)))
(goto-char (1+ start))
(flet ((in-comment-p (position)
(nth 4 (syntax-ppss position))))
(and
(not (in-comment-p start))
(loop for position from (1+ start) upto end
always (in-comment-p position))
(or (= end (point-max))
(not (in-comment-p (1+ end))))))))))
(defmacro gnuplot-test-comment (name comment)
"Define an `ert' test to check syntax recognition of COMMENT in gnuplot-mode.
The test checks that STRING is correctly recognised as a single
string-literal in multiple different contexts, as determined by
`gnuplot-string-test-contexts'."
(declare (indent 1))
`(ert-deftest ,name ()
,comment
,@(loop for context in gnuplot-comment-test-contexts
collect
`(should (gnuplot-test-comment-in-context ,comment ,context)))))
(gnuplot-test-comment gnuplot-comment-simple
"# a simple one-line comment")
(gnuplot-test-comment gnuplot-comment-multiline
"# a comment\
continued \
over multiple lines")
(gnuplot-test-comment gnuplot-comment-with-hashes
"# a comment # with more # hash # characters #")
(gnuplot-test-comment gnuplot-comment-multiline-with-hashes
"# a comment \
# continued # over \
mutliple # lines #")
(gnuplot-test-comment gnuplot-comment-with-single-quotes
"# a comment 'containing a single-quoted string'")
(gnuplot-test-comment gnuplot-comment-with-single-quotes
"# a comment \"containing a double-quoted string\"")
(gnuplot-test-comment gnuplot-comment-multiline-with-quotes
"# a continued \
'comment' \
\"containing strings\"")
|