]> git.madduck.net Git - etc/vim.git/blob - spec/indent/indent_spec.rb

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

prevent tests being shadowed by first line rule
[etc/vim.git] / spec / indent / indent_spec.rb
1 require "spec_helper"
2
3 shared_examples_for "vim" do
4
5   before(:each) {
6     # clear buffer
7     vim.normal 'gg"_dG'
8
9     # Insert two blank lines.
10     # The first line is a corner case in this plugin that would shadow the
11     # correct behaviour of other tests. Thus we explicitly jump to the first
12     # line when we require so.
13     vim.feedkeys 'i\<CR>\<CR>\<ESC>'
14   }
15
16   describe "when using the indent plugin" do
17     it "sets the indentexpr and indentkeys options" do
18       vim.command("set indentexpr?").should include "GetPythonPEPIndent("
19       vim.command("set indentkeys?").should include "=elif"
20     end
21
22     it "sets autoindent and expandtab" do
23       vim.command("set autoindent?").should match(/\s*autoindent/)
24       vim.command("set expandtab?").should match(/\s*expandtab/)
25     end
26   end
27
28   describe "when entering the first line" do
29     before { vim.feedkeys '0ggipass' }
30
31     it "does not indent" do
32       proposed_indent.should == 0
33       indent.should == 0
34     end
35
36     it "does not indent when using '=='" do
37       vim.normal "=="
38       indent.should == 0
39     end
40   end
41
42   describe "when after a '(' that is at the end of its line" do
43     before { vim.feedkeys 'itest(\<CR>' }
44
45     it "indents by one level" do
46       proposed_indent.should == shiftwidth
47       vim.feedkeys 'something'
48       indent.should == shiftwidth
49       vim.normal '=='
50       indent.should == shiftwidth
51     end
52
53     it "puts the closing parenthesis at the same level" do
54       vim.feedkeys ')'
55       indent.should == 0
56     end
57   end
58
59   describe "when after an '(' that is followed by something" do
60     before { vim.feedkeys 'itest(something,\<CR>' }
61
62     it "lines up on following lines" do
63       indent.should == 5
64       vim.feedkeys 'more,\<CR>'
65       indent.should == 5
66     end
67
68     it "lines up the closing parenthesis" do
69       vim.feedkeys ')'
70       indent.should == 5
71     end
72
73     it "does not touch the closing parenthesis if it is already indented further" do
74       vim.feedkeys '  )'
75       indent.should == 7
76     end
77   end
78
79   describe "when after multiple parens of different types" do
80     it "indents by one level" do
81       vim.feedkeys 'if({\<CR>'
82       indent.should == shiftwidth
83     end
84
85     it "lines up with the last paren" do
86       vim.feedkeys 'ifff({123: 456,\<CR>'
87       indent.should == 5
88     end
89   end
90
91   describe "when '#' is contained in a string that is followed by a colon" do
92     it "indents by one level" do
93         vim.feedkeys 'iif "some#thing" == "test":#test\<CR>pass'
94         indent.should == shiftwidth
95     end
96   end
97
98   describe "when '#' is not contained in a string and is followed by a colon" do
99     it "does not indent" do
100         vim.feedkeys 'iif "some#thing" == "test"#:test\<CR>'
101         indent.should == 0
102     end
103   end
104
105   describe "when using simple control structures" do
106       it "indents shiftwidth spaces" do
107           vim.feedkeys 'iwhile True:\<CR>pass'
108           indent.should == shiftwidth
109       end
110   end
111
112   describe "when writing an 'else' block" do
113     it "aligns to the preceeding 'for' block" do
114       vim.feedkeys 'ifor x in "abc":\<CR>pass\<CR>else:'
115       indent.should == 0
116     end
117
118     it "aligns to the preceeding 'if' block" do
119       vim.feedkeys 'ifor x in "abc":\<CR>if True:\<CR>pass\<CR>else:'
120       indent.should == shiftwidth
121     end
122   end
123
124   describe "when using parens and control statements" do
125     it "avoids ambiguity by using extra indentation" do
126       vim.feedkeys 'iif (111 and\<CR>'
127       if shiftwidth == 4
128         indent.should == shiftwidth * 2
129       else
130         indent.should == 4
131       end
132       vim.feedkeys '222):\<CR>'
133       indent.should == shiftwidth
134       vim.feedkeys 'pass\<CR>'
135       indent.should == 0
136     end
137
138     it "still aligns parens properly if not ambiguous" do
139       vim.feedkeys 'iwhile (111 and\<CR>'
140       indent.should == 7
141       vim.feedkeys '222):\<CR>'
142       indent.should == shiftwidth
143       vim.feedkeys 'pass\<CR>'
144       indent.should == 0
145     end
146
147     it "still handles multiple parens correctly" do
148       vim.feedkeys 'iif (111 and (222 and 333\<CR>'
149       indent.should == 13
150       vim.feedkeys 'and 444\<CR>'
151       indent.should == 13
152       vim.feedkeys ')\<CR>'
153       if shiftwidth == 4
154         indent.should == shiftwidth * 2
155       else
156         indent.should == 4
157       end
158       vim.feedkeys 'and 555):\<CR>'
159       indent.should == shiftwidth
160       vim.feedkeys 'pass\<CR>'
161       indent.should == 0
162     end
163   end
164
165   describe "when a line breaks with a manual '\\'" do
166     it "indents shiftwidth spaces on normal line" do
167         vim.feedkeys 'ivalue = test + \\\\\<CR>'
168         indent.should == shiftwidth
169     end
170
171     it "indents 2x shiftwidth spaces for control structures" do
172         vim.feedkeys 'iif somevalue == xyz and \\\\\<CR>'
173         indent.should == shiftwidth * 2
174     end
175
176     it "indents relative to line above" do
177         vim.feedkeys 'i\<TAB>value = test + \\\\\<CR>'
178         indent.should == shiftwidth * 2
179     end
180   end
181
182   describe "when current line is dedented compared to previous line" do
183      before { vim.feedkeys 'i\<TAB>\<TAB>if x:\<CR>return True\<CR>\<ESC>' }
184      it "and current line has a valid indentation (Part 1)" do
185         vim.feedkeys '0i\<TAB>if y:'
186         proposed_indent.should == -1
187      end
188
189      it "and current line has a valid indentation (Part 2)" do
190         vim.feedkeys '0i\<TAB>\<TAB>if y:'
191         proposed_indent.should == -1
192      end
193
194      it "and current line has an invalid indentation" do
195         vim.feedkeys 'i    while True:\<CR>'
196         indent.should == previous_indent + shiftwidth
197      end
198   end
199
200   describe "when an 'if' is followed by" do
201      before { vim.feedkeys 'i\<TAB>\<TAB>if x:\<CR>' }
202      it "an elif, it lines up with the 'if'" do
203         vim.feedkeys 'elif y:'
204         indent.should == shiftwidth * 2
205      end
206
207      it "an 'else', it lines up with the 'if'" do
208         vim.feedkeys 'else:'
209         indent.should == shiftwidth * 2
210      end
211   end
212
213   describe "when a 'for' is followed by" do
214      before { vim.feedkeys 'i\<TAB>\<TAB>for x in y:\<CR>' }
215      it "an 'else', it lines up with the 'for'" do
216         vim.feedkeys 'else:'
217         indent.should == shiftwidth * 2
218      end
219   end
220
221   describe "when an 'else' is followed by" do
222      before { vim.feedkeys 'i\<TAB>\<TAB>else:\<CR>XXX\<CR>' }
223      it "a 'finally', it lines up with the 'else'" do
224         vim.feedkeys 'finally:'
225         indent.should == shiftwidth * 2
226      end
227   end
228
229
230   describe "when a 'try' is followed by" do
231      before { vim.feedkeys 'i\<TAB>\<TAB>try:\<CR>' }
232      it "an 'except', it lines up with the 'try'" do
233         vim.feedkeys 'except:'
234         indent.should == shiftwidth * 2
235      end
236
237      it "an 'else', it lines up with the 'try'" do
238         vim.feedkeys 'else:'
239         indent.should == shiftwidth * 2
240      end
241
242      it "a 'finally', it lines up with the 'try'" do
243         vim.feedkeys 'finally:'
244         indent.should == shiftwidth * 2
245      end
246   end
247
248   describe "when an 'except' is followed by" do
249      before { vim.feedkeys 'i\<TAB>\<TAB>except:\<CR>' }
250      it "an 'else', it lines up with the 'except'" do
251         vim.feedkeys 'else:'
252         indent.should == shiftwidth * 2
253      end
254
255      it "another 'except', it lines up with the previous 'except'" do
256         vim.feedkeys 'except:'
257         indent.should == shiftwidth * 2
258      end
259
260      it "a 'finally', it lines up with the 'except'" do
261         vim.feedkeys 'finally:'
262         indent.should == shiftwidth * 2
263      end
264   end
265
266   def shiftwidth
267     @shiftwidth ||= vim.echo("exists('*shiftwidth') ? shiftwidth() : &sw").to_i
268   end
269   def tabstop
270     @tabstop ||= vim.echo("&tabstop").to_i
271   end
272   def indent
273     vim.echo("indent('.')").to_i
274   end
275   def previous_indent
276     pline = vim.echo("line('.')").to_i - 1
277     vim.echo("indent('#{pline}')").to_i
278   end
279   def proposed_indent
280     line = vim.echo("line('.')")
281     col = vim.echo("col('.')")
282     indent_value = vim.echo("GetPythonPEPIndent(line('.'))").to_i
283     vim.command("call cursor(#{line}, #{col})")
284     return indent_value
285   end
286 end
287
288 describe "vim when using width of 4" do
289   before {
290     vim.command("set sw=4 ts=4 sts=4 et")
291   }
292
293   it_behaves_like "vim"
294 end
295
296 describe "vim when using width of 8" do
297   before {
298     vim.command("set sw=8 ts=8 sts=8 et")
299   }
300
301   it_behaves_like "vim"
302 end