-
Notifications
You must be signed in to change notification settings - Fork 45
/
wgrep-test.el
157 lines (148 loc) · 5.81 KB
/
wgrep-test.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
(require 'ert)
(require 'dash)
(require 's)
(require 'wgrep-test-helper)
(ert-deftest wgrep-normal ()
:tags '(wgrep)
(wgrep-test-helper--default
(wgrep-test-helper-fixture "HOGE\nFOO\nBAZ\n"
(lambda (file)
(wgrep-test-helper--grep (concat "grep -nH -e FOO -C 1 " file))
(wgrep-change-to-wgrep-mode)
(goto-char (point-min))
;; header is readonly
(should (re-search-forward "^grep" nil t))
(should-error (delete-char 1) :type 'text-read-only)
;; search hit line (hit by -C option)
(should (re-search-forward "HOGE" nil t))
;; delete 1st line
(wgrep-mark-deletion)
(should (re-search-forward "FOO" nil t))
;; replace 2nd line
(replace-match "FOO2")
;; footer is readonly
(goto-char (point-max))
(should-error (delete-char -1) :type 'text-read-only)
;; apply to buffer
(wgrep-finish-edit)
;; save to file
(wgrep-save-all-buffers)
;; compare file contents is valid
(should (equal "FOO2\nBAZ\n" (wgrep-test-helper--get-contents file)))))))
(ert-deftest wgrep-normal-with-newline ()
:tags '(wgrep)
(wgrep-test-helper--default
(wgrep-test-helper-fixture "HOGE\n"
(lambda (file)
(wgrep-test-helper--grep (concat "grep -nH -e HOGE " file))
(wgrep-change-to-wgrep-mode)
(goto-char (point-min))
;; through the header
(should (re-search-forward (concat "^" (regexp-quote file) ":") nil t))
;; search hit line (hit by -C option)
(should (re-search-forward "HOGE" nil t))
(replace-match "FOO\nBAZ")
;; apply to buffer
(wgrep-finish-edit)
;; save to file
(wgrep-save-all-buffers)
;; compare file contents is valid
(should (equal "FOO\nBAZ\n" (wgrep-test-helper--get-contents file)))))))
(ert-deftest wgrep-bom-with-multibyte ()
:tags '(wgrep)
(wgrep-test-helper--default
(wgrep-test-helper-fixture '("あ\nい\nう\n" utf-8-with-signature)
(lambda (file)
(wgrep-test-helper--grep (concat "grep -nH -e 'あ' -A 2 " file))
(wgrep-change-to-wgrep-mode)
(goto-char (point-min))
;; BOM check is valid.
;; skip BOM by `.*'
(should (re-search-forward (concat (regexp-quote file) ":[0-9]+:.*\\(あ\\)$") nil t))
(replace-match "へのへのも" nil nil nil 1)
;; 2nd line
(should (re-search-forward (concat (regexp-quote file) "-[0-9]+-\\(い\\)$") nil t))
(replace-match "へじ" nil nil nil 1)
;; apply to buffer
(wgrep-finish-edit)
;; save to file
(wgrep-save-all-buffers)
;; compare file contents is valid
(should (equal "へのへのも\nへじ\nう\n" (wgrep-test-helper--get-contents file)))
))))
(ert-deftest wgrep-bom-with-unibyte ()
:tags '(wgrep)
(wgrep-test-helper--default
(wgrep-test-helper-fixture '("a\nb\n" utf-8-with-signature)
(lambda (file)
(wgrep-test-helper--grep (concat "grep -nH -e 'a' -A 2 " file))
(wgrep-change-to-wgrep-mode)
(goto-char (point-min))
;; BOM check is valid.
(should (re-search-forward (concat (regexp-quote file) ":[0-9]+:.*\\(a\\)$") nil t))
(replace-match "ABCD" nil nil nil 1)
;; apply to buffer
(wgrep-finish-edit)
;; save to file
(wgrep-save-all-buffers)
;; compare file contents is valid
(should (equal "ABCD\nb\n" (wgrep-test-helper--get-contents file)))))))
(ert-deftest wgrep-with-modify ()
:tags '(wgrep)
(wgrep-test-helper--default
(wgrep-test-helper-fixture "a\nb\nc\n"
(lambda (file)
(let (;; This test intended to check modified buffer is existing.
;; Keep that buffer is modifying while calling grep.
(grep-save-buffers nil))
(with-current-buffer (find-file-noselect file)
;; modify file buffer
(goto-char (point-min))
(and (re-search-forward "^a" nil t)
(replace-match "hoge"))
(and (re-search-forward "^b" nil t)
(replace-match "foo")))
(wgrep-test-helper--grep (concat "grep -nH -e 'a' -A 2 " file))
(wgrep-change-to-wgrep-mode)
(goto-char (point-min))
;; delete "a" line (failed when saving)
(should (re-search-forward (concat (regexp-quote file) ":[0-9]+:.*\\(a\\)$") nil t))
(wgrep-mark-deletion)
;; replace "b" line (failed when saving)
(should (re-search-forward (concat (regexp-quote file) "-[0-9]+-.*\\(b\\)$") nil t))
(replace-match "B" nil nil nil 1)
;; replace "c" line
(should (re-search-forward (concat (regexp-quote file) "-[0-9]+-.*\\(c\\)$") nil t))
(replace-match "C" nil nil nil 1)
;; apply to buffer
(wgrep-finish-edit)
;; save to file
(wgrep-save-all-buffers)
;; compare file contents is valid. (keep preceding file buffer's contents)
(should (equal "hoge\nfoo\nC\n" (wgrep-test-helper--get-contents file))))))))
(ert-deftest wgrep-with-readonly-file ()
:tags '(wgrep)
(wgrep-test-helper--default
(wgrep-test-helper-fixture "a\nb\nc\n"
(lambda (file)
;; make readonly
(set-file-modes file ?\400)
(wgrep-test-helper--grep (concat "grep -nH -e 'a' " file))
(wgrep-change-to-wgrep-mode)
(goto-char (point-min))
(should (re-search-forward (concat (regexp-quote file) ":[0-9]+:.*\\(a\\)$") nil t))
(replace-match "A" nil nil nil 1)
;; only check with no error
(wgrep-finish-edit)
;; TODO check result file is unchanged
))))
;; TODO (Not implemented testcase)
;; * wgrep-toggle-readonly-area
;; ** sort-lines
;; * wgrep-abort-changes
;; * wgrep-exit
;; * broken file contents (invalid coding system)
;; * new text contains newline
;; * wgrep-change-readonly-file
;; * test wgrep-*.el
(provide 'wgrep-test)