branch: elpa/nix-mode
commit 6445ebfad696bdfd1d7bc8ddd463772ba61763e8
Merge: 7f968e8a7f 02b59d9bcf
Author: Matthew Bauer <[email protected]>
Commit: GitHub <[email protected]>
Merge pull request #63 from etu/ert-indent-testing
Ert indent testing
---
tests/nix-mode-tests.el | 66 +++++++++++++++++++++++++++++++++++++++
tests/testcases/issue-60.1.nix | 7 +++++
tests/testcases/issue-60.2.nix | 8 +++++
tests/testcases/issue-60.3.nix | 8 +++++
tests/testcases/list-contents.nix | 14 +++++++++
5 files changed, 103 insertions(+)
diff --git a/tests/nix-mode-tests.el b/tests/nix-mode-tests.el
index d92a02fef5..7505908834 100644
--- a/tests/nix-mode-tests.el
+++ b/tests/nix-mode-tests.el
@@ -4,6 +4,7 @@
;;; Code:
+(require 'cl-lib)
(require 'ert)
(require 'nix-mode)
@@ -26,5 +27,70 @@
(nix-mode)
(eq (nix--get-string-type (nix--get-parse-state (point))) nil))))
+;;; Indentation tests
+
+(defvar nix-mode-test-dir (expand-file-name "testcases"
+ (if load-file-name
+ (file-name-directory
load-file-name)
+ default-directory))
+ "Directory containing the `nix-mode' testcase files.")
+
+;; Define macro to build indentation tests
+(cl-defmacro with-nix-mode-test ((file &key indent) &rest body)
+ "Set up environment for testing `nix-mode'.
+Execute BODY in a temporary buffer containing the contents of
+FILE, in `nix-mode'. All tests will use the `nix-indent-line'
+function to do the indentation tests."
+
+ `(with-temp-buffer
+ ;; Read test data file
+ (insert-file-contents (expand-file-name ,file nix-mode-test-dir))
+
+ ;; Store the file as a local variable and set the right indentation
function to use
+ (let ((raw-file (buffer-substring-no-properties (point-min) (point-max)))
+ (nix-indent-function 'nix-indent-line)
+ (inhibit-message t))
+ ;; Load up nix-mode
+ (nix-mode)
+
+ ;; If we're doing an indentation test
+ (if ,indent
+ (progn
+ ;; Indent the buffer
+ (indent-region (point-min) (point-max))
+
+ ;; Compare buffer to the stored buffer contents
+ (should (equal
+ (buffer-substring-no-properties (point-min) (point-max))
+ raw-file))))
+
+ ;; Go to beginning
+ (goto-char (point-min))
+
+ ;; Run additional tests
+ ,@body)))
+
+(ert-deftest nix-mode-indent-test-list-contents ()
+ "Proper indentation for items inside of a list."
+ (with-nix-mode-test ("list-contents.nix" :indent t)))
+
+(ert-deftest nix-mode-test-indent-issue-60-1 ()
+ "Proper indentation of attrsets inside of lists inside of attrsets.
+
+Related issue: https://github.com/NixOS/nix-mode/issues/60"
+ (with-nix-mode-test ("issue-60.1.nix" :indent t)))
+
+(ert-deftest nix-mode-test-indent-issue-60-2 ()
+ "Proper indentation of code inside of let blocks.
+
+Related issue: https://github.com/NixOS/nix-mode/issues/60"
+ (with-nix-mode-test ("issue-60.2.nix" :indent t)))
+
+(ert-deftest nix-mode-test-indent-issue-60-3 ()
+ "Proper indentation of import and newline after equal.
+
+Related issue: https://github.com/NixOS/nix-mode/issues/60"
+ (with-nix-mode-test ("issue-60.3.nix" :indent t)))
+
(provide 'nix-mode-tests)
;;; nix-mode-tests.el ends here
diff --git a/tests/testcases/issue-60.1.nix b/tests/testcases/issue-60.1.nix
new file mode 100644
index 0000000000..c575243447
--- /dev/null
+++ b/tests/testcases/issue-60.1.nix
@@ -0,0 +1,7 @@
+{
+ x86 = [
+ {
+ name = "t1.small.x86";
+ }
+ ];
+}
diff --git a/tests/testcases/issue-60.2.nix b/tests/testcases/issue-60.2.nix
new file mode 100644
index 0000000000..7a27cf793d
--- /dev/null
+++ b/tests/testcases/issue-60.2.nix
@@ -0,0 +1,8 @@
+let
+ x = [
+ (let y = 1; in y)
+ { foo = 1; }
+ [ 1 2 3 ]
+ x
+ ];
+in x
diff --git a/tests/testcases/issue-60.3.nix b/tests/testcases/issue-60.3.nix
new file mode 100644
index 0000000000..1cf7350a78
--- /dev/null
+++ b/tests/testcases/issue-60.3.nix
@@ -0,0 +1,8 @@
+let
+ mozilla-overlay =
+ import
+ (
+ builtins.fetchTarball
+ https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz
+ );
+in mozilla-overlay
diff --git a/tests/testcases/list-contents.nix
b/tests/testcases/list-contents.nix
new file mode 100644
index 0000000000..4c6630eb32
--- /dev/null
+++ b/tests/testcases/list-contents.nix
@@ -0,0 +1,14 @@
+[
+ 1
+ false
+ true
+ https://nixos.org/
+ {
+ attr = "set";
+ }
+ [
+ "nested"
+ "list"
+ ]
+ "string"
+]