Skip to content

Commit

Permalink
Merge pull request #63 from etu/ert-indent-testing
Browse files Browse the repository at this point in the history
Ert indent testing
  • Loading branch information
matthewbauer committed Jan 3, 2019
2 parents 7f968e8 + 02b59d9 commit 6445ebf
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/nix-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

;;; Code:

(require 'cl-lib)
(require 'ert)
(require 'nix-mode)

Expand All @@ -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
7 changes: 7 additions & 0 deletions tests/testcases/issue-60.1.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
x86 = [
{
name = "t1.small.x86";
}
];
}
8 changes: 8 additions & 0 deletions tests/testcases/issue-60.2.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let
x = [
(let y = 1; in y)
{ foo = 1; }
[ 1 2 3 ]
x
];
in x
8 changes: 8 additions & 0 deletions tests/testcases/issue-60.3.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let
mozilla-overlay =
import
(
builtins.fetchTarball
https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz
);
in mozilla-overlay
14 changes: 14 additions & 0 deletions tests/testcases/list-contents.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
1
false
true
https://nixos.org/
{
attr = "set";
}
[
"nested"
"list"
]
"string"
]

0 comments on commit 6445ebf

Please sign in to comment.