]> 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:

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