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

4652ef9762ecb4302957ab480a959d6de61db78b
[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 using parens and control statements" do
104     it "avoids ambiguity by using extra indentation" do
105       vim.feedkeys 'iif (111 and\<CR>'
106       if shiftwidth == 4
107         indent.should == shiftwidth * 2
108       else
109         indent.should == 4
110       end
111       vim.feedkeys '222):\<CR>'
112       indent.should == shiftwidth
113       vim.feedkeys 'pass\<CR>'
114       indent.should == 0
115     end
116
117     it "still aligns parens properly if not ambiguous" do
118       vim.feedkeys 'iwhile (111 and\<CR>'
119       indent.should == 7
120       vim.feedkeys '222):\<CR>'
121       indent.should == shiftwidth
122       vim.feedkeys 'pass\<CR>'
123       indent.should == 0
124     end
125
126     it "still handles multiple parens correctly" do
127       vim.feedkeys 'iif (111 and (222 and 333\<CR>'
128       indent.should == 13
129       vim.feedkeys 'and 444\<CR>'
130       indent.should == 13
131       vim.feedkeys ')\<CR>'
132       if shiftwidth == 4
133         indent.should == shiftwidth * 2
134       else
135         indent.should == 4
136       end
137       vim.feedkeys 'and 555):\<CR>'
138       indent.should == shiftwidth
139       vim.feedkeys 'pass\<CR>'
140       indent.should == 0
141     end
142   end
143
144   describe "when a line breaks with a manual '\\'" do
145     it "indents shiftwidth spaces on normal line" do
146         vim.feedkeys 'ivalue = test + \\\\\<CR>'
147         indent.should == shiftwidth
148     end
149
150     it "indents 2x shiftwidth spaces for control structures" do
151         vim.feedkeys 'iif somevalue == xyz and \\\\\<CR>'
152         indent.should == shiftwidth * 2
153     end
154
155     it "indents relative to line above" do
156         vim.feedkeys 'i\<TAB>value = test + \\\\\<CR>'
157         indent.should == shiftwidth * 2
158     end
159   end
160
161   describe "when current line is dedented compared to previous line" do
162      before { vim.feedkeys 'i\<TAB>\<TAB>if x:\<CR>return True\<CR>\<ESC>' }
163      it "and current line has a valid indentation (Part 1)" do
164         vim.feedkeys '0i\<TAB>if y:'
165         proposed_indent.should == -1
166      end
167
168      it "and current line has a valid indentation (Part 2)" do
169         vim.feedkeys '0i\<TAB>\<TAB>if y:'
170         proposed_indent.should == -1
171      end
172
173      it "and current line has an invalid indentation" do
174         vim.feedkeys 'i    while True:\<CR>'
175         indent.should == previous_indent + shiftwidth
176      end
177   end
178
179   describe "when an 'if' is followed by" do
180      before { vim.feedkeys 'i\<TAB>\<TAB>if x:\<CR>' }
181      it "an elif, it lines up with the 'if'" do
182         vim.feedkeys 'elif y:'
183         indent.should == shiftwidth * 2
184      end
185
186      it "an 'else', it lines up with the 'if'" do
187         vim.feedkeys 'else:'
188         indent.should == shiftwidth * 2
189      end
190   end
191
192   describe "when a 'for' is followed by" do
193      before { vim.feedkeys 'i\<TAB>\<TAB>for x in y:\<CR>' }
194      it "an 'else', it lines up with the 'for'" do
195         vim.feedkeys 'else:'
196         indent.should == shiftwidth * 2
197      end
198   end
199
200   describe "when an 'else' is followed by" do
201      before { vim.feedkeys 'i\<TAB>\<TAB>else:\<CR>XXX\<CR>' }
202      it "a 'finally', it lines up with the 'else'" do
203         vim.feedkeys 'finally:'
204         indent.should == shiftwidth * 2
205      end
206   end
207
208
209   describe "when a 'try' is followed by" do
210      before { vim.feedkeys 'i\<TAB>\<TAB>try:\<CR>' }
211      it "an 'except', it lines up with the 'try'" do
212         vim.feedkeys 'except:'
213         indent.should == shiftwidth * 2
214      end
215
216      it "an 'else', it lines up with the 'try'" do
217         vim.feedkeys 'else:'
218         indent.should == shiftwidth * 2
219      end
220
221      it "a 'finally', it lines up with the 'try'" do
222         vim.feedkeys 'finally:'
223         indent.should == shiftwidth * 2
224      end
225   end
226
227   describe "when an 'except' is followed by" do
228      before { vim.feedkeys 'i\<TAB>\<TAB>except:\<CR>' }
229      it "an 'else', it lines up with the 'except'" do
230         vim.feedkeys 'else:'
231         indent.should == shiftwidth * 2
232      end
233
234      it "another 'except', it lines up with the previous 'except'" do
235         vim.feedkeys 'except:'
236         indent.should == shiftwidth * 2
237      end
238
239      it "a 'finally', it lines up with the 'except'" do
240         vim.feedkeys 'finally:'
241         indent.should == shiftwidth * 2
242      end
243   end
244
245   def shiftwidth
246     @shiftwidth ||= vim.echo("exists('*shiftwidth') ? shiftwidth() : &sw").to_i
247   end
248   def tabstop
249     @tabstop ||= vim.echo("&tabstop").to_i
250   end
251   def indent
252     vim.echo("indent('.')").to_i
253   end
254   def previous_indent
255     pline = vim.echo("line('.')").to_i - 1
256     vim.echo("indent('#{pline}')").to_i
257   end
258   def proposed_indent
259     line = vim.echo("line('.')")
260     col = vim.echo("col('.')")
261     indent_value = vim.echo("GetPythonPEPIndent(line('.'))").to_i
262     vim.command("call cursor(#{line}, #{col})")
263     return indent_value
264   end
265 end
266
267 describe "vim when using width of 4" do
268   before {
269     vim.command("set sw=4 ts=4 sts=4 et")
270   }
271
272   it_behaves_like "vim"
273 end
274
275 describe "vim when using width of 8" do
276   before {
277     vim.command("set sw=8 ts=8 sts=8 et")
278   }
279
280   it_behaves_like "vim"
281 end