branch: externals/sql-indent
commit 30cf40ab5806c1da22549fec469235c67b0cb68e
Author: Alex Harsanyi <[email protected]>
Commit: Alex Harsanyi <[email protected]>

    Update unit test suite to run independent of user settings
    
    Previously, the test suite depended on whether the tabs are expanded into
    spaces or not and other user settings.
---
 sql-indent-test.el     | 92 ++++++++++++++++++++++++++++++--------------------
 test-data/pr17-syn.eld |  3 +-
 test-data/pr17.sql     | 30 ++++++++--------
 3 files changed, 71 insertions(+), 54 deletions(-)

diff --git a/sql-indent-test.el b/sql-indent-test.el
index 8f0028a..39b9d4f 100644
--- a/sql-indent-test.el
+++ b/sql-indent-test.el
@@ -1,4 +1,4 @@
-;;; sql-indent-test.el --- Test utilities for sql-indent.el. -*- 
lexical-binding: t -*-
+;;; sql-indent-test.el --- Automated tests for sql-indent.el. -*- 
lexical-binding: t -*-
 ;; Copyright (C) 2017 Alex Harsanyi
 ;;
 ;; Author: Alex Harsanyi ([email protected])
@@ -27,7 +27,15 @@
 ;;
 ;;     M-x ert RET "^sqlind-" RET
 ;;
-;; There are two types of tests,
+;; Tests can also be run in batch mode using the following command:
+;;
+;;    emacs -batch -Q --no-site-file -L . -l sql-indent-test.el -f 
ert-run-tests-batch-and-exit
+;;
+;; The above command used '-Q' and '--no-site-file options', making sure that
+;; the tests are run in a "standard" environment, regardless of what packages
+;; and settings are present in your personal init and site-init files.
+;;
+;;;; There are two types of tests,
 ;;
 ;; * SYNTAX CHECKS check if the syntax in an SQL file is correctly
 ;;   indentified.  These tests are independent of indentation preferences (see
@@ -41,10 +49,12 @@
 ;; prepared separately using `sqlind-collect-syntax-from-buffer' and
 ;; `sqlind-collect-indentation-offsets-from-buffer'.
 ;;
+;;;; Preparing new tests
+;;
 ;; To create a syntax check file, open an *ielm* buffer (M-x ielm RET) and
 ;; run:
 ;;
-;; (sqlind-collect-syntax-from-buffer (find-file "./test-data/pr7.sql"))
+;; (sqlind-collect-syntax-from-buffer (find-file-noselect 
"./test-data/pr7.sql"))
 ;;
 ;; The function will output a set of syntax definitions.  Put these into an
 ;; .eld file.
@@ -52,7 +62,7 @@
 ;; To create an indentation offsets file, run:
 ;;
 ;; (sqlind-collect-indentation-offsets-from-buffer
-;;   (find-file "./test-data/pr7.sql")
+;;   (find-file-noselect "./test-data/pr7.sql")
 ;;   sqlind-indentation-left-offsets-alist
 ;;   2)
 ;;
@@ -61,6 +71,7 @@
 ;;
 ;; See the end of file for examples on how to put together the actual tests.
 
+;;; Code
 (require 'ert)
 (require 'sql-indent)
 (require 'sql-indent-left)
@@ -71,32 +82,30 @@
 (defun sqlind-collect-syntax-from-buffer (buffer)
   (let ((result '()))
     (with-current-buffer buffer
-      (save-excursion
-        ;; NOTE: we indent the buffer according to the default rules first, as
-        ;; this affects anchor points.  We could get rid of this if we write a
-        ;; smarter `sqlind-ert-check-line-syntax'
-        (sqlind-ert-indent-buffer
-         (default-value 'sqlind-indentation-offsets-alist)
-         (default-value 'sqlind-basic-offset))
-        (goto-char (point-min))
+      ;; NOTE: we indent the buffer according to the default rules first, as
+      ;; this affects anchor points.  We could get rid of this if we write a
+      ;; smarter `sqlind-ert-check-line-syntax'
+      (sqlind-ert-indent-buffer
+       (default-value 'sqlind-indentation-offsets-alist)
+       (default-value 'sqlind-basic-offset))
+      (goto-char (point-min))
+      (let ((syn (sqlind-syntax-of-line)))
+        (setq result (cons syn result)))
+      (while (= (forward-line 1) 0)
         (let ((syn (sqlind-syntax-of-line)))
-          (setq result (cons syn result)))
-        (while (= (forward-line 1) 0)
-          (let ((syn (sqlind-syntax-of-line)))
-            (setq result (cons syn result))))))
+          (setq result (cons syn result)))))
     (reverse result)))
 
 (defun sqlind-collect-indentation-offsets-from-buffer (buffer rules 
basic-offset)
   (let ((result '()))
     (with-current-buffer buffer
-      (save-excursion
-        (sqlind-ert-indent-buffer
-         (or rules (default-value 'sqlind-indentation-offsets-alist))
-         (or basic-offset (default-value 'sqlind-basic-offset)))
-        (goto-char (point-min))
-        (setq result (cons (current-indentation) result))
-        (while (= (forward-line 1) 0)
-          (setq result (cons (current-indentation) result)))))
+      (sqlind-ert-indent-buffer
+       (or rules (default-value 'sqlind-indentation-offsets-alist))
+       (or basic-offset (default-value 'sqlind-basic-offset)))
+      (goto-char (point-min))
+      (setq result (cons (current-indentation) result))
+      (while (= (forward-line 1) 0)
+        (setq result (cons (current-indentation) result))))
     (reverse result)))
 
 
@@ -111,7 +120,12 @@ inddent the whole buffer."
     (setq sqlind-indentation-offsets-alist rules))
   (when basic-offset
     (setq sqlind-basic-offset basic-offset))
+  ;; To ensure we are consistent in our offsets regardless of he users
+  ;; personal tab choices, setup spaces only indentation for this buffer.
+  (setq indent-tabs-mode nil)
+  (untabify (point-min) (point-max))
   (indent-region (point-min) (point-max))
+  ;; (save-buffer) ; if you want to see the result of this command
   (set-buffer-modified-p nil))
 
 (defun sqlind-ert-check-line-syntax (expected)
@@ -144,20 +158,21 @@ previously saved syntax data in DATA-FILE.  An error is 
signaled
 if there is a mismatch."
   (let ((syntax-data (sqlind-ert-read-data data-file)))
     (with-current-buffer (find-file sql-file)
-      (save-excursion
-        ;; NOTE: indent the buffer according to default rules first -- this
-        ;; affects anchor points.
-        (sqlind-ert-indent-buffer
-         (default-value 'sqlind-indentation-offsets-alist)
-         (default-value 'sqlind-basic-offset))
-        (goto-char (point-min))
-        (should (consp syntax-data))    ; "premature end of syntax-data"
+      (sqlind-minor-mode 1)             ; ensure this is enabled
+      ;; NOTE: indent the buffer according to default rules first -- this
+      ;; affects anchor points.
+      ;; (message "sql-product: %s" sql-product)
+      (sqlind-ert-indent-buffer
+       (default-value 'sqlind-indentation-offsets-alist)
+       (default-value 'sqlind-basic-offset))
+      (goto-char (point-min))
+      (should (consp syntax-data))    ; "premature end of syntax-data"
+      (sqlind-ert-check-line-syntax (car syntax-data))
+      (setq syntax-data (cdr syntax-data))
+      (while (= (forward-line 1) 0)
+        (should (consp syntax-data))  ; "premature end of syntax-data"
         (sqlind-ert-check-line-syntax (car syntax-data))
-        (setq syntax-data (cdr syntax-data))
-        (while (= (forward-line 1) 0)
-          (should (consp syntax-data))  ; "premature end of syntax-data"
-          (sqlind-ert-check-line-syntax (car syntax-data))
-          (setq syntax-data (cdr syntax-data)))))))
+        (setq syntax-data (cdr syntax-data))))))
 
 (defun sqlind-ert-check-line-indentation (expected)
   "Check that the current line has EXPECTED indentation.
@@ -180,6 +195,8 @@ information read from DATA-FILE (as generated by
 `sqlind-collect-indentation-offsets-from-buffer')"
   (let ((indentation-data (sqlind-ert-read-data data-file)))
     (with-current-buffer (find-file sql-file)
+      (sqlind-minor-mode 1)
+      ;; (message "sql-product: %s" sql-product)
       (sqlind-ert-indent-buffer rules basic-offset)
       (goto-char (point-min))
       (should (consp indentation-data))    ; "premature end of indentation-data
@@ -258,3 +275,4 @@ information read from DATA-FILE (as generated by
 (ert-deftest sqlind-ert-pr19 ()
   (sqlind-ert-check-file-syntax "test-data/pr19.sql" "test-data/pr19-syn.eld"))
 
+;;; sql-indent-test.el ends here
diff --git a/test-data/pr17-syn.eld b/test-data/pr17-syn.eld
index 9d34a47..6530fdc 100644
--- a/test-data/pr17-syn.eld
+++ b/test-data/pr17-syn.eld
@@ -211,5 +211,4 @@
   (toplevel . 1))
  ((comment-start . 1)
   (toplevel . 1))
- ((toplevel . 1)))
- 
+ ((toplevel . 1)))
\ No newline at end of file
diff --git a/test-data/pr17.sql b/test-data/pr17.sql
index 08f57d2..5715fc0 100644
--- a/test-data/pr17.sql
+++ b/test-data/pr17.sql
@@ -6,14 +6,14 @@ set linesize 2500
 select sysdate from dual;
 
 select col1, 'a long line of text ending with a single word'
-          || col2
-          || col3
-          || 'some text' as composed_column,
+         || col2
+         || col3
+         || 'some text' as composed_column,
        col4
-    || col5 as composed_column2
+         || col5 as composed_column2
   from   my_table
  where  cond1 = fct1
-             || 'another text'
+        || 'another text'
     and    cond2 = 2;
 
 select atc.column_name,
@@ -40,23 +40,23 @@ select atc.column_name,
                            and    atc1.owner       = atc.owner
                            and    atc1.table_name  = atc.table_name
                            and    acc1.column_name = acc.column_name)
- ;
+        ;
 
 delete from my_table mt
  where  col_1    = v_col1
-    and   (col_2    = v_col2
-           or col_3 = v_col3)
-    and   col_42    = '42'
- ;
+        and   (col_2    = v_col2
+               or col_3 = v_col3)
+        and   col_42    = '42'
+        ;
 
 update my_table
    set    col1_has_a_long_name = value1,
           col2_is_short        = value2
  where cond1 is not null
-   and  (   col_2 = v_col2
-         or col_3 = v_col3)
-   and   col_42   = '42'
- ;
+       and  (   col_2 = v_col2
+             or col_3 = v_col3)
+       and   col_42   = '42'
+       ;
 
 insert into xyzxx
             ( aaa, xxx, bbb, ccc,
@@ -88,7 +88,7 @@ select aaa,
           xxx
  order by xxx desc,
           aaa asc
- ;
+          ;
 -- Local Variables:
 -- sql-product: oracle
 -- End:

Reply via email to