diff options
| author | Michael R. Mauger | 2019-04-24 20:59:25 -0400 |
|---|---|---|
| committer | Michael R. Mauger | 2019-04-24 20:59:25 -0400 |
| commit | a1386fa6a7698c04902354cd5fefb39056b0a901 (patch) | |
| tree | 9d4ee7ae5b4d3cc13e0b88a445ec2e1c35e36117 | |
| parent | 2bf957394cdcb93396966d3289f5e200886cb424 (diff) | |
| download | emacs-a1386fa6a7698c04902354cd5fefb39056b0a901.tar.gz emacs-a1386fa6a7698c04902354cd5fefb39056b0a901.zip | |
* lisp/progmodes/sql.el
(sql-is-sqli-buffer-p): New function.
(sql-generate-unique-sqli-buffer-name): Refactor and use it.
(sql-product-interactive): Simplify name logic.
* test/lisp/progmodes/sql-tests.el
(sql-tests-placeholder-filter-harness): New macro.
(sql-tests-placeholder-filter-simple)
(sql-tests-placeholder-filter-ampersand)
(sql-tests-placeholder-filter-period): Refactored tests and use macro.
(sql-tests-buffer-naming-harness): New macro.
(sql-tests-buffer-naming-default)
(sql-tests-buffer-naming-multiple)
(sql-tests-buffer-naming-explicit)
(sql-tests-buffer-naming-universal-argument)
(sql-tests-buffer-naming-existing): New tests.
| -rw-r--r-- | lisp/progmodes/sql.el | 46 | ||||
| -rw-r--r-- | test/lisp/progmodes/sql-tests.el | 167 |
2 files changed, 166 insertions, 47 deletions
diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el index 28261ef74b2..2d33b3130cd 100644 --- a/lisp/progmodes/sql.el +++ b/lisp/progmodes/sql.el | |||
| @@ -1423,6 +1423,15 @@ specified, it's `sql-product' or `sql-connection' must match." | |||
| 1423 | (and (stringp connection) | 1423 | (and (stringp connection) |
| 1424 | (string= connection sql-connection)))))))) | 1424 | (string= connection sql-connection)))))))) |
| 1425 | 1425 | ||
| 1426 | (defun sql-is-sqli-buffer-p (buffer) | ||
| 1427 | "Return non-nil if buffer is a SQLi buffer." | ||
| 1428 | (when buffer | ||
| 1429 | (setq buffer (get-buffer buffer)) | ||
| 1430 | (and buffer | ||
| 1431 | (buffer-live-p buffer) | ||
| 1432 | (with-current-buffer buffer | ||
| 1433 | (derived-mode-p 'sql-interactive-mode))))) | ||
| 1434 | |||
| 1426 | ;; Keymap for sql-interactive-mode. | 1435 | ;; Keymap for sql-interactive-mode. |
| 1427 | 1436 | ||
| 1428 | (defvar sql-interactive-mode-map | 1437 | (defvar sql-interactive-mode-map |
| @@ -3550,24 +3559,29 @@ server/database name." | |||
| 3550 | "Generate a new, unique buffer name for a SQLi buffer. | 3559 | "Generate a new, unique buffer name for a SQLi buffer. |
| 3551 | 3560 | ||
| 3552 | Append a sequence number until a unique name is found." | 3561 | Append a sequence number until a unique name is found." |
| 3553 | (let ((base-name (when (stringp base) | 3562 | (let ((base-name (substring-no-properties |
| 3554 | (substring-no-properties | 3563 | (if base |
| 3555 | (or base | 3564 | (if (stringp base) |
| 3556 | (sql-get-product-feature product :name) | 3565 | base |
| 3566 | (format "%S" base)) | ||
| 3567 | (or (sql-get-product-feature product :name) | ||
| 3557 | (symbol-name product))))) | 3568 | (symbol-name product))))) |
| 3558 | buf-fmt-1st buf-fmt-rest) | 3569 | buf-fmt-1st |
| 3570 | buf-fmt-rest) | ||
| 3559 | 3571 | ||
| 3560 | ;; Calculate buffer format | 3572 | ;; Calculate buffer format |
| 3561 | (if base-name | 3573 | (if (string-blank-p base-name) |
| 3562 | (setq buf-fmt-1st (format "*SQL: %s*" base-name) | 3574 | (setq buf-fmt-1st "*SQL*" |
| 3563 | buf-fmt-rest (format "*SQL: %s-%%d*" base-name)) | 3575 | buf-fmt-rest "*SQL-%d*") |
| 3564 | (setq buf-fmt-1st "*SQL*" | 3576 | (setq buf-fmt-1st (format "*SQL: %s*" base-name) |
| 3565 | buf-fmt-rest "*SQL-%d*")) | 3577 | buf-fmt-rest (format "*SQL: %s-%%d*" base-name))) |
| 3566 | 3578 | ||
| 3567 | ;; See if we can find an unused buffer | 3579 | ;; See if we can find an unused buffer |
| 3568 | (let ((buf-name buf-fmt-1st) | 3580 | (let ((buf-name buf-fmt-1st) |
| 3569 | (i 1)) | 3581 | (i 1)) |
| 3570 | (while (sql-buffer-live-p buf-name) | 3582 | (while (if (sql-is-sqli-buffer-p buf-name) |
| 3583 | (comint-check-proc buf-name) | ||
| 3584 | (buffer-live-p (get-buffer buf-name))) | ||
| 3571 | ;; Check a sequence number on the BASE | 3585 | ;; Check a sequence number on the BASE |
| 3572 | (setq buf-name (format buf-fmt-rest i) | 3586 | (setq buf-name (format buf-fmt-rest i) |
| 3573 | i (1+ i))) | 3587 | i (1+ i))) |
| @@ -4670,13 +4684,13 @@ the call to \\[sql-product-interactive] with | |||
| 4670 | (read-string | 4684 | (read-string |
| 4671 | "Buffer name (\"*SQL: XXX*\"; enter `XXX'): " | 4685 | "Buffer name (\"*SQL: XXX*\"; enter `XXX'): " |
| 4672 | (sql-make-alternate-buffer-name product)))) | 4686 | (sql-make-alternate-buffer-name product)))) |
| 4673 | ((or (string-prefix-p " " new-name) | ||
| 4674 | (string-match-p "\\`[*].*[*]\\'" new-name)) | ||
| 4675 | new-name) | ||
| 4676 | ((stringp new-name) | 4687 | ((stringp new-name) |
| 4677 | (sql-generate-unique-sqli-buffer-name product new-name)) | 4688 | (if (or (string-prefix-p " " new-name) |
| 4689 | (string-match-p "\\`[*].*[*]\\'" new-name)) | ||
| 4690 | new-name | ||
| 4691 | (sql-generate-unique-sqli-buffer-name product new-name))) | ||
| 4678 | (t | 4692 | (t |
| 4679 | (sql-generate-unique-sqli-buffer-name product nil))))) | 4693 | (sql-generate-unique-sqli-buffer-name product new-name))))) |
| 4680 | 4694 | ||
| 4681 | ;; Set SQLi mode. | 4695 | ;; Set SQLi mode. |
| 4682 | (let ((sql-interactive-product product)) | 4696 | (let ((sql-interactive-product product)) |
diff --git a/test/lisp/progmodes/sql-tests.el b/test/lisp/progmodes/sql-tests.el index 5ac34907c2d..ad1f7976526 100644 --- a/test/lisp/progmodes/sql-tests.el +++ b/test/lisp/progmodes/sql-tests.el | |||
| @@ -271,37 +271,142 @@ Perform ACTION and validate results" | |||
| 271 | (should-not (sql-get-product-feature 'd :Z)))) | 271 | (should-not (sql-get-product-feature 'd :Z)))) |
| 272 | 272 | ||
| 273 | ;;; SQL Oracle SCAN/DEFINE | 273 | ;;; SQL Oracle SCAN/DEFINE |
| 274 | (ert-deftest sql-tests-placeholder-filter () | 274 | (defmacro sql-tests-placeholder-filter-harness (orig repl outp) |
| 275 | "Test that placeholder relacement is as expected." | 275 | "Set-up and tear-down of testing of placeholder filter. |
| 276 | (let ((syntab (syntax-table)) | 276 | |
| 277 | (sql-oracle-scan-on t) | 277 | The placeholder in ORIG will be replaced by REPL which should |
| 278 | (placeholder-value "")) | 278 | yield OUTP." |
| 279 | (set-syntax-table sql-mode-syntax-table) | 279 | |
| 280 | 280 | (declare (indent 0)) | |
| 281 | (cl-letf | 281 | `(let ((syntab (syntax-table)) |
| 282 | (((symbol-function 'read-from-minibuffer) | 282 | (sql-oracle-scan-on t)) |
| 283 | (lambda (&rest _) placeholder-value))) | 283 | (set-syntax-table sql-mode-syntax-table) |
| 284 | 284 | ||
| 285 | (setq placeholder-value "XX") | 285 | (cl-letf |
| 286 | (should (equal | 286 | (((symbol-function 'read-from-minibuffer) |
| 287 | (sql-placeholders-filter "select '&x' from dual;") | 287 | (lambda (&rest _) ,repl))) |
| 288 | "select 'XX' from dual;")) | 288 | |
| 289 | 289 | (should (equal (sql-placeholders-filter ,orig) ,outp))) | |
| 290 | (setq placeholder-value "&Y") | 290 | |
| 291 | (should (equal | 291 | (set-syntax-table syntab))) |
| 292 | (sql-placeholders-filter "select '&x' from dual;") | 292 | |
| 293 | "select '&Y' from dual;")) | 293 | (ert-deftest sql-tests-placeholder-filter-simple () |
| 294 | (should (equal | 294 | "Test that placeholder relacement of simple replacement text." |
| 295 | (sql-placeholders-filter "select '&x' from dual;") | 295 | (sql-tests-placeholder-filter-harness |
| 296 | "select '&Y' from dual;")) | 296 | "select '&x' from dual;" "XX" |
| 297 | (should (equal | 297 | "select 'XX' from dual;")) |
| 298 | (sql-placeholders-filter "select '&x.' from dual;") | 298 | |
| 299 | "select '&Y' from dual;")) | 299 | (ert-deftest sql-tests-placeholder-filter-ampersand () |
| 300 | (should (equal | 300 | "Test that placeholder relacement of replacement text with ampersand." |
| 301 | (sql-placeholders-filter "select '&x.y' from dual;") | 301 | (sql-tests-placeholder-filter-harness |
| 302 | "select '&Yy' from dual;"))) | 302 | "select '&x' from dual;" "&Y" |
| 303 | 303 | "select '&Y' from dual;") | |
| 304 | (set-syntax-table syntab))) | 304 | |
| 305 | (sql-tests-placeholder-filter-harness | ||
| 306 | "select '&x' from dual;" "Y&" | ||
| 307 | "select 'Y&' from dual;") | ||
| 308 | |||
| 309 | (sql-tests-placeholder-filter-harness | ||
| 310 | "select '&x' from dual;" "Y&Y" | ||
| 311 | "select 'Y&Y' from dual;")) | ||
| 312 | |||
| 313 | (ert-deftest sql-tests-placeholder-filter-period () | ||
| 314 | "Test that placeholder relacement of token terminated by a period." | ||
| 315 | (sql-tests-placeholder-filter-harness | ||
| 316 | "select '&x.' from dual;" "&Y" | ||
| 317 | "select '&Y' from dual;") | ||
| 318 | |||
| 319 | (sql-tests-placeholder-filter-harness | ||
| 320 | "select '&x.y' from dual;" "&Y" | ||
| 321 | "select '&Yy' from dual;") | ||
| 322 | |||
| 323 | (sql-tests-placeholder-filter-harness | ||
| 324 | "select '&x..y' from dual;" "&Y" | ||
| 325 | "select '&Y.y' from dual;")) | ||
| 326 | |||
| 327 | ;; Buffer naming | ||
| 328 | (defmacro sql-tests-buffer-naming-harness (product &rest action) | ||
| 329 | "Set-up and tear-down of test of buffer naming. | ||
| 330 | |||
| 331 | The ACTION will be tested after set-up of PRODUCT." | ||
| 332 | |||
| 333 | (declare (indent 1)) | ||
| 334 | `(let (new-bufs) | ||
| 335 | (cl-letf | ||
| 336 | (((symbol-function 'make-comint-in-buffer) | ||
| 337 | (lambda (_name buffer _program &optional _startfile &rest _switches) | ||
| 338 | (let ((b (get-buffer-create buffer))) | ||
| 339 | (message ">>make-comint-in-buffer %S" b) | ||
| 340 | (cl-pushnew b new-bufs) ;; Keep track of what we create | ||
| 341 | b)))) | ||
| 342 | |||
| 343 | (let (,(intern (format "sql-%s-login-params" product))) | ||
| 344 | ,@action) | ||
| 345 | |||
| 346 | (let (kill-buffer-query-functions) ;; Kill what we create | ||
| 347 | (mapc #'kill-buffer new-bufs))))) | ||
| 348 | |||
| 349 | (ert-deftest sql-tests-buffer-naming-default () | ||
| 350 | "Test buffer naming." | ||
| 351 | (sql-tests-buffer-naming-harness sqlite | ||
| 352 | (sql-sqlite) | ||
| 353 | (message ">> %S" (current-buffer)) | ||
| 354 | (should (equal (buffer-name) "*SQL: SQLite*")))) | ||
| 355 | |||
| 356 | (ert-deftest sql-tests-buffer-naming-multiple () | ||
| 357 | "Test buffer naming of multiple buffers." | ||
| 358 | (sql-tests-buffer-naming-harness sqlite | ||
| 359 | (sql-sqlite) | ||
| 360 | (should (equal (buffer-name) "*SQL: SQLite*")) | ||
| 361 | |||
| 362 | (switch-to-buffer "*scratch*") | ||
| 363 | |||
| 364 | (sql-sqlite) | ||
| 365 | (should (equal (buffer-name) "*SQL: SQLite*")))) | ||
| 366 | |||
| 367 | (ert-deftest sql-tests-buffer-naming-explicit () | ||
| 368 | "Test buffer naming with explicit name." | ||
| 369 | (sql-tests-buffer-naming-harness sqlite | ||
| 370 | (sql-sqlite "A") | ||
| 371 | (should (equal (buffer-name) "*SQL: A*")) | ||
| 372 | |||
| 373 | (switch-to-buffer "*scratch*") | ||
| 374 | |||
| 375 | (sql-sqlite "A") | ||
| 376 | (should (equal (buffer-name) "*SQL: A*")))) | ||
| 377 | |||
| 378 | (ert-deftest sql-tests-buffer-naming-universal-argument () | ||
| 379 | "Test buffer naming with explicit name." | ||
| 380 | (sql-tests-buffer-naming-harness sqlite | ||
| 381 | (cl-letf | ||
| 382 | (((symbol-function 'read-string) | ||
| 383 | (lambda (_prompt &optional _initial-input _history _default-value _inherit-input-method) | ||
| 384 | "1"))) | ||
| 385 | (sql-sqlite '(4)) | ||
| 386 | (should (equal (buffer-name) "*SQL: 1*"))) | ||
| 387 | |||
| 388 | (switch-to-buffer "*scratch*") | ||
| 389 | |||
| 390 | (cl-letf | ||
| 391 | (((symbol-function 'read-string) | ||
| 392 | (lambda (_prompt &optional _initial-input _history _default-value _inherit-input-method) | ||
| 393 | "2"))) | ||
| 394 | (sql-sqlite '(16)) | ||
| 395 | (should (equal (buffer-name) "*SQL: 2*"))))) | ||
| 396 | |||
| 397 | (ert-deftest sql-tests-buffer-naming-existing () | ||
| 398 | "Test buffer naming with an existing non-SQLi buffer." | ||
| 399 | (sql-tests-buffer-naming-harness sqlite | ||
| 400 | (get-buffer-create "*SQL: exist*") | ||
| 401 | |||
| 402 | (cl-letf | ||
| 403 | (((symbol-function 'read-string) | ||
| 404 | (lambda (_prompt &optional _initial-input _history _default-value _inherit-input-method) | ||
| 405 | "exist"))) | ||
| 406 | (sql-sqlite '(4)) | ||
| 407 | (should (equal (buffer-name) "*SQL: exist-1*"))) | ||
| 408 | |||
| 409 | (kill-buffer "*SQL: exist*"))) | ||
| 305 | 410 | ||
| 306 | 411 | ||
| 307 | (provide 'sql-tests) | 412 | (provide 'sql-tests) |