From 4566ec69d269ecba8d9386a131b932ee5244aa8e Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 5 May 2024 09:23:58 -0500 Subject: enhancements, functions, tests, and misc enhancements - move accent-company to C-` and ensure it's on for org mode - re-enable narrow-to-region - turn on network repos by default - remove setq in company's use-package custom clause - increase company delay to .7 secs - recipe templates should have visibility show all - move video recordings code to separate module - move geiser-guile to prog-lisp functions - improve cj/reformat-region-or-buffer via restriction - improvements to test-format-region - adding tests for clear-blank-lines - Add prepend-lines and replace-fraction-glyphs functions - add cj/clear-blank-lines function - create cj/load-all-tests utility function tests - add keybinding for ert-run-tests-interactively - ensure ert libraries are available to load-all-tests - remove running the tests when evaluating the buffer - fix clear-blank-lines and adding better tests misc - updated packages - adding the luddite blog to elfeed - more abbrevs --- tests/test-clear-blank-lines.el | 47 ++++++++++++ tests/test-fixup-whitespace.el | 159 ++++++++++++++++++++++++++++++++++++++++ tests/test-format-region.el | 129 +++++++++++++++++++++++--------- tests/test-title-case-region.el | 44 +++++++++++ 4 files changed, 346 insertions(+), 33 deletions(-) create mode 100644 tests/test-clear-blank-lines.el create mode 100644 tests/test-fixup-whitespace.el create mode 100644 tests/test-title-case-region.el (limited to 'tests') diff --git a/tests/test-clear-blank-lines.el b/tests/test-clear-blank-lines.el new file mode 100644 index 000000000..2190aba00 --- /dev/null +++ b/tests/test-clear-blank-lines.el @@ -0,0 +1,47 @@ +;;; test-clear-blank-lines.el --- -*- lexical-binding: t; -*- + +;;; Commentary: +;; + +;;; Code: + +(require 'ert) +(add-to-list 'load-path (concat user-emacs-directory "modules")) +(require 'custom-functions) + +(ert-deftest test-cj/clear-blank-lines-region () + (let ((testdata "Some\n\n\n\nText") + (expected "Some\nText") + (actual)) + (with-temp-buffer + (insert testdata) + (cj/clear-blank-lines (point-min) (point-max)) + (setq actual (buffer-string)) + (message "buffer is:\n'%s'" actual) + (should (string= actual expected))))) + +(ert-deftest test-cj/clear-blank-lines-region-multiple-lines () + (let ((testdata "Some\n\n\n\nText") + (expected "Some\n\n\n\nText") + (midpoint) + (actual)) + (with-temp-buffer + (insert testdata) + (insert "\n") + (setq midpoint (point)) + (insert testdata) + (cj/clear-blank-lines (point-min) midpoint) + (setq actual (buffer-substring (- (point-max) + (length testdata)) (point-max))) + (message "buffer is:\n'%s'" (buffer-string)) + (should (string= actual expected))))) + +(ert-deftest test-cj/clear-blank-lines-negative () + (with-temp-buffer + (insert "Some\nText") + (cj/clear-blank-lines (point-min) (point-max)) + (should (equal (buffer-string) "Some\nText")))) + + +(provide 'test-clear-blank-lines) +;;; test-clear-blank-lines.el ends here. diff --git a/tests/test-fixup-whitespace.el b/tests/test-fixup-whitespace.el new file mode 100644 index 000000000..0126801ad --- /dev/null +++ b/tests/test-fixup-whitespace.el @@ -0,0 +1,159 @@ +;;; test-fixup-whitespace.el --- -*- lexical-binding: t; -*- + +;;; Commentary: +;; Test cj/fixup-whitespace-line-or-region in custom-functions.el + +;; The function under test should: +;; - ensure there is exactly one space between words +;; - remove tab characters +;; - remove leading and trailing whitespace +;; - operate on a line, or a region, if selected + +;;; Code: + + +(require 'ert) +(add-to-list 'load-path (concat user-emacs-directory "modules")) +(require 'custom-functions) + +(ert-deftest test-cj/fixup-whitespace-positive-first-line-only () + "Test a positive case with two lines. +Both lines have whitespace at the beginning and the end. This tests that when +this function is called on the first line, only that line is affected." + (let ((testdata " Hello, world! \n Foo bar ") + (expected "Hello, world!\n Foo bar ") + (actual)) + (with-temp-buffer + (insert testdata) + (goto-char (point-min)) + (cj/fixup-whitespace-line-or-region) + (setq actual (buffer-string)) + (should (string= actual expected))))) + +(ert-deftest test-cj/fixup-whitespace-positive-first-line-only-tabs () + "Test a positive case with two lines. +Both lines have extraneous whitespace at the beginning and the end, includuing +tabs. This tests that when this function is called on the first line, only that +line is affected." + (let ((testdata " Hello,\t world! \n Foo\tbar ") + (expected "Hello, world!\n Foo\tbar ") + (actual)) + (with-temp-buffer + (insert testdata) + (goto-char (point-min)) + (cj/fixup-whitespace-line-or-region) + (setq actual (buffer-string)) + (should (string= actual expected))))) + +(ert-deftest test-cj/fixup-whitespace-positive-first-line-only-tabs2 () + "Test a positive case with two lines. +Both lines have extraneous whitespace at the beginning and the end, includuing +tabs. This tests that when this function is called on the first line, only that +line is affected." + (let ((testdata "\t Hello,\tworld! \n Foo\t bar\t ") + (expected "Hello, world!\n Foo\t bar\t ") + (actual)) + (with-temp-buffer + (insert testdata) + (goto-char (point-min)) + (cj/fixup-whitespace-line-or-region) + (setq actual (buffer-string)) + (should (string= actual expected))))) + +(ert-deftest test-cj/fixup-whitespace-negative-first-line-only () + "Test a negative case with two lines. +Only the second line has whitespace at the beginning and the end. This tests +that when this function is called on the first line, neither line changes." + (let ((testdata "Hello, world!\n Foo bar ") + (expected "Hello, world!\n Foo bar ") + (actual)) + (with-temp-buffer + (insert testdata) + (goto-char (point-min)) + (cj/fixup-whitespace-line-or-region) + (setq actual (buffer-string)) + (should (string= actual expected))))) + +(ert-deftest test-cj/fixup-whitespace-positive-second-line-only () + "Test a positive case with two lines. +Both lines have whitespace at the beginning and the end. This tests that when +function is called on the second line, only that line is affected." + (let ((testdata " Hello, world! \n Foo bar ") + (expected " Hello, world! \nFoo bar") + (actual)) + (with-temp-buffer + (insert testdata) + (goto-char (point-min)) + (forward-line) + (cj/fixup-whitespace-line-or-region) + (setq actual (buffer-string)) + (should (string= actual expected))))) + +(ert-deftest test-cj/fixup-whitespace-negative-second-line-only () + "Test a negative case with two lines. +Only the first line has whitespace at the beginning and the end. This tests +that when this function is called on the first line, neither line changes." + (let ((testdata " Hello, world! \nFoo bar") + (expected " Hello, world! \nFoo bar") + (actual)) + (with-temp-buffer + (insert testdata) + (goto-char (point-min)) + (forward-line) + (cj/fixup-whitespace-line-or-region) + (setq actual (buffer-string)) + (should (string= actual expected))))) + +(ert-deftest test-cj/fixup-whitespace-positive-region () + "Test a positive case with a region. +Two lines have whitespace at the beginning, the middle, and the end. This tests +that when this function is called with a region, all whitespace is cleaned up as +expected." + (let ((testdata " Hello, world! \n Foo bar ") + (expected "Hello, world!\nFoo bar") + (actual)) + (with-temp-buffer + (insert testdata) + (goto-char (point-min)) + (set-mark (point)) + (goto-char (point-max)) + (cj/fixup-whitespace-line-or-region t) + (setq actual (buffer-string)) + (should (string= actual expected))))) + +(ert-deftest test-cj/fixup-whitespace-positive-region-tabs () + "Test a positive case with a region and tabs. +Two lines have extraneous whitespace at the beginning, the middle, and the end. +This tests that when this function is called with a region, all whitespace is +cleaned up as expected." + (let ((testdata " \t \t Hello, world! \n Foo\t bar ") + (expected "Hello, world!\nFoo bar") + (actual)) + (with-temp-buffer + (insert testdata) + (goto-char (point-min)) + (set-mark (point)) + (goto-char (point-max)) + (cj/fixup-whitespace-line-or-region t) + (setq actual (buffer-string)) + (should (string= actual expected))))) + +(ert-deftest test-cj/fixup-whitespace-negative-region () + "Test a negative case with a region. +Two lines are inserted, neither of which have extraneous whitespace. This tests +that when this function is called with a region, there's no unwanted +side-effects and nothing changes." + (let ((testdata "Hello, world!\nFoo bar") + (expected "Hello, world!\nFoo bar") + (actual)) + (with-temp-buffer + (insert testdata) + (goto-char (point-min)) + (set-mark (point)) + (goto-char (point-max)) + (cj/fixup-whitespace-line-or-region t) + (setq actual (buffer-string)) + (should (string= actual expected))))) + +(provide 'test-fixup-whitespace) +;;; test-fixup-whitespace.el ends here. diff --git a/tests/test-format-region.el b/tests/test-format-region.el index b1d1532c7..25d2e52e8 100644 --- a/tests/test-format-region.el +++ b/tests/test-format-region.el @@ -1,47 +1,110 @@ -;;; test-format-region.el --- tests for cj/format-region-or-buffer -*- lexical-binding: t; -*- +;;; test-format-region.el --- -*- lexical-binding: t; -*- ;;; Commentary: -;; Some basic tests for the custom function cj/format-region-or-buffer in custom-functions.el +;; Some basic tests for the custom function cj/format-region-or-buffer in +;; custom-functions.el ;;; Code: (add-to-list 'load-path (concat user-emacs-directory "modules")) (require 'custom-functions) -;; ----------------------------- Utility Functions ----------------------------- - -(defun buffer-string-no-properties () - "Return the contents of the current buffer without any text properties." - (buffer-substring-no-properties (point-min) (point-max))) ;; ----------------------------------- Tests ----------------------------------- -(ert-deftest cj/format-region-or-buffer-test/region () - "Test cj/format-region-or-buffer on a selected region." - (with-temp-buffer - (insert " line with leading spaces and a \n tab\there") - (goto-char (point-min)) - (push-mark (point) t t) - (goto-char (point-max)) - (cj/format-region-or-buffer) - ;; expected: trailing whitespace and leading spaces are removed, tabs are replaced by spaces - (should (string= (buffer-string-no-properties) "line with leading spaces and a\ntab here")))) - -(ert-deftest cj/format-region-or-buffer-test/whole-buffer () - "Test cj/format-region-or-buffer on an entire buffer." - (with-temp-buffer - (insert " \n\t\n line with leading spaces and a \n tab\there") - ;; expected: trailing and leading whitespace of buffer and lines are removed, tabs are replaced by spaces - (cj/format-region-or-buffer) - (should (string= (buffer-string-no-properties) "\nline with leading spaces and a\ntab here")))) - -(ert-deftest cj/format-region-or-buffer-test/extreme-buffer-size () - "Tests cj/format-region-or-buffer on an very large buffer." - (with-temp-buffer - (insert (make-string most-positive-fixnum ?\s)) - (cj/format-region-or-buffer) - ;; expected: even large buffers should not cause an error - (should (string= (buffer-string-no-properties) "")))) +(defvar test-format-rob-text-data + '((" spaces in front\nspaces behind " . + "spaces in front\nspaces behind") + ("\t tabs and spaces in front\ntabs and spaces behind\t " . + "tabs and spaces in front\ntabs and spaces behind"))) + +(defvar test-format-rob-elisp-data + '(("(defun existential ()\n(if (eq (+ 3 4) 7)\n(order)\n(chaos)))" . + "(defun existential ()\n (if (eq (+ 3 4) 7)\n (order)\n (chaos)))"))) + + +(ert-deftest test-format-rob-positive-text-region () + "Test cj/format-region-or-buffer on a selected region. +This tests " + (dolist (data-pair test-format-rob-text-data) + (let* ((testdata (car data-pair)) + (expected (cdr data-pair)) + (actual)) + (with-temp-buffer + (insert testdata) + (goto-char (point-min)) + (set-mark (point)) + (goto-char (point-max)) + (cj/format-region-or-buffer) + (setq actual (buffer-string)) + (should (string= actual expected)))))) + +(ert-deftest test-format-rob-positive-text-buffer () + "Test cj/format-region-or-buffer on the entire buffer. +This is the same as testing the region without setting a region in the temp +buffer." + (dolist (data-pair test-format-rob-text-data) + (let* ((testdata (car data-pair)) + (expected (cdr data-pair)) + (actual)) + (with-temp-buffer + (insert testdata) + (cj/format-region-or-buffer) + (setq actual (buffer-string)) + (should (string= actual expected)))))) + +(ert-deftest test-format-rob-positive-region-text-multiple-paragraphs () + "Test cj/format-region-or-buffer on the entire buffer." + (dolist (data-pair test-format-rob-text-data) + (let ((testdata (car data-pair)) + (expected1 (cdr data-pair)) + (expected2 (car data-pair)) + (actual1) + (actual2)) + (with-temp-buffer + ;; insert data twice with newline char in between + (insert testdata) + (insert"\n") + (insert testdata) + + ;; select the first set of data + (goto-char (point-min)) + (set-mark (point)) + (forward-line 2) + + ;; run format and return to top + (cj/format-region-or-buffer) + (message "buffer is:\n'%s'" (buffer-string)) + + ;; assert the first set is formatted + (goto-char (point-min)) + (setq actual1 (buffer-substring (point-min) (line-end-position 2))) + (should (string= actual1 expected1)) + + ;; assert the second set is unformatted + (goto-char (point-min)) + (setq actual2 (buffer-substring (line-beginning-position 3) (point-max))) + (should (string= actual2 expected2)))))) + +(ert-deftest test-format-rob-positive-elisp-region () + "Test cj/format-region-or-buffer on a selected region. +This tests that emacs-lisp specific formatting is applied." + (ws-butler-mode nil) + (dolist (data-pair test-format-rob-elisp-data) + (let* ((testdata (car data-pair)) + (expected (cdr data-pair)) + (actual)) + (with-temp-buffer + (emacs-lisp-mode) + (insert testdata) + (goto-char (point-min)) + (set-mark (point)) + (goto-char (point-max)) + (message "buffer before:\n'%s'" (buffer-string)) + (cj/format-region-or-buffer) + (message "buffer after:\n'%s'" (buffer-string)) + (setq actual (buffer-string)) + (should (string= actual expected)))))) (provide 'test-format-region) ;;; test-format-region.el ends here. diff --git a/tests/test-title-case-region.el b/tests/test-title-case-region.el new file mode 100644 index 000000000..ffab0c241 --- /dev/null +++ b/tests/test-title-case-region.el @@ -0,0 +1,44 @@ +;;; test-title-case-region.el --- -*- lexical-binding: t; -*- + +;;; Commentary: +;; Tests for the title-case region function in custom-functions.el + +;; Note on Title Case +;; Title case is a capitalization convention where major words are +;; capitalized,and most minor words are lowercase. Nouns,verbs (including +;; linking verbs), adjectives, adverbs,pronouns,and all words of four letters or +;; more are considered major words. Short (i.e., three letters or fewer) +;; conjunctions, short prepositions,and all articles are considered minor +;; words." + +;; positive case (single line, all lowercase, no skip words) +;; positive case (six lines, mixed case, skip words) +;; negative case (single line, all skip-words) +;; negative case (a long empty string) + + +;;; Code: + +(require 'ert) +(add-to-list 'load-path (concat user-emacs-directory "modules")) +(require 'custom-functions) + +(ert-deftest test-cj/fixup-whitespace-positive-first-line-only () + "Test a positive case with two lines. +Both lines have whitespace at the beginning and the end. This tests that when +this function is called on the first line, only that line is affected." + (let ((testdata " Hello, world! \n Foo bar ") + (expected "Hello, world!\n Foo bar ") + (actual)) + (with-temp-buffer + (insert testdata) + (goto-char (point-min)) + (cj/fixup-whitespace-line-or-region) + (setq actual (buffer-string)) + (should (string= actual expected))))) + + + + +(provide 'test-title-case-region) +;;; test-title-case-region.el ends here. -- cgit v1.2.3