aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rwxr-xr-xtest/shared/run-func.bash (renamed from test/formatters/run-func.bash)10
-rw-r--r--test/unit/apheleia-unit-test.el97
2 files changed, 103 insertions, 4 deletions
diff --git a/test/formatters/run-func.bash b/test/shared/run-func.bash
index 621edde..f5d6f96 100755
--- a/test/formatters/run-func.bash
+++ b/test/shared/run-func.bash
@@ -2,11 +2,13 @@
set -euo pipefail
+func="$1"
+shift
+
# Avoid using git to get project directory, due to
# https://github.com/radian-software/apheleia/pull/89#issuecomment-1107319617
-cd "$(dirname "$0")"
-repo="$(cd ../.. && pwd)"
+cd "$(dirname "$0")/../.."
-exec emacs --batch -L "${repo}" -L . -l apheleia-ft \
- --eval "(setq debug-on-error t)" -f "$1"
+exec emacs --batch -L . "$@" \
+ --eval "(setq debug-on-error t)" -f "${func}"
diff --git a/test/unit/apheleia-unit-test.el b/test/unit/apheleia-unit-test.el
new file mode 100644
index 0000000..c5d54cc
--- /dev/null
+++ b/test/unit/apheleia-unit-test.el
@@ -0,0 +1,97 @@
+;; -*- lexical-binding: t -*-
+
+;; `apheleia-unit-tests' - unit tests using ERT.
+
+(require 'apheleia)
+
+;; Using buttercup because ert makes it really hard to write tabular
+;; tests that report enough context to debug when they fail.
+(require 'buttercup)
+
+(require 'map)
+
+(defun apheleia-unit-find-vars (form)
+ (cond
+ ((symbolp form)
+ (list form))
+ ((listp form)
+ (mapcan #'apheleia-unit-find-vars (cdr form)))))
+
+(describe "apheleia--edit-distance-table"
+ (cl-flet ((table-error
+ (before-str after-str expected-table)
+ (let* ((hash (apheleia--edit-distance-table before-str after-str))
+ (table
+ (mapcar
+ (lambda (i2)
+ (mapcar
+ (lambda (i1)
+ (gethash (cons i1 i2) hash))
+ (number-sequence 0 (length before-str))))
+ (number-sequence 0 (length after-str)))))
+ (unless (equal table expected-table)
+ table))))
+ (cl-macrolet ((testcases
+ (description &rest specs)
+ `(it ,description
+ ,@(mapcar
+ (lambda (spec)
+ `(expect
+ (table-error ,@spec)
+ :to-be nil))
+ specs))))
+ (testcases
+ "computes the example from apheleia-dp file header"
+ ("hello" "heo"
+ '((0 1 2 3 4 5)
+ (1 0 1 2 3 4)
+ (2 1 0 1 2 3)
+ (3 2 1 1 2 2)))))))
+
+(describe "apheleia--align-point"
+ (cl-flet ((alignment-error
+ (before-spec after-spec)
+ (let* ((before-pos (string-match "|" before-spec))
+ (after-pos (string-match "|" after-spec))
+ (before-str (replace-regexp-in-string "|" "" before-spec))
+ (after-str (replace-regexp-in-string "|" "" after-spec))
+ (real-after-pos (apheleia--align-point before-str after-str before-pos)))
+ (unless (= after-pos real-after-pos)
+ (concat (substring after-str 0 real-after-pos) "|"
+ (substring after-str real-after-pos))))))
+ (cl-macrolet ((testcases
+ (description &rest specs)
+ `(it ,description
+ ,@(mapcar
+ (lambda (spec)
+ (cl-destructuring-bind (before-spec after-spec) spec
+ `(expect
+ (alignment-error ,before-spec ,after-spec)
+ :to-be nil)))
+ specs))))
+ (testcases
+ "does normal alignments"
+ ("hel|lo"
+ "he|o")
+ ("hello| world"
+ "helo| word")
+ ("hello | world"
+ "hello|world"))
+ (testcases
+ "solves issue #2"
+ (" if (node.type === \"CallExpression\" && (node.callee.type === \"Import\" @@ (node.callee.type === \"Identifier\" && node.callee.name === \"require\"))) {
+ //|
+ }
+"
+ " if (
+ node.type === \"CallExpression\" &&
+ (node.callee.type === \"Import\" @@
+ (node.callee.type === \"Identifier\" && node.callee.name === \"require\"))
+ ) {
+ //|
+ }
+"))
+ (testcases
+ "solves issue #290"
+ (" | <div class=\"left-[40rem] fixed inset-y-0 right-0 z-0 hidden lg:block xl:left-[50rem]\">\n <svg\n"
+ "|<div class=\"left-[40rem] fixed inset-y-0 right-0 z-0 hidden lg:block xl:left-[50rem]\">\n <svg")))))