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

Indent correctly after lines with '#'s in strings
[etc/vim.git] / spec / indent / indent_spec.rb
1 require "spec_helper"
2
3 describe "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 '#' is contained in a string that is followed by a colon" do
71     it "indents by one level" do
72         vim.feedkeys 'iif "some#thing" == "test":#test\<CR>pass'
73         indent.should == shiftwidth
74     end
75   end
76
77   describe "when '#' is not contained in a string and is followed by a colon" do
78     it "does not indent" do
79         vim.feedkeys 'iif "some#thing" == "test"#:test\<CR>'
80         indent.should == 0
81     end
82   end
83
84   describe "when using simple control structures" do
85       it "indents shiftwidth spaces" do
86           vim.feedkeys 'iwhile True:\<CR>pass'
87           indent.should == shiftwidth
88       end
89   end
90
91   describe "when a line breaks with a manual '\\'" do
92     it "indents shiftwidth spaces on normal line" do
93         vim.feedkeys 'ivalue = test + \\\\\<CR>'
94         indent.should == shiftwidth
95     end
96
97     it "indents 2x shiftwidth spaces for control structures" do
98         vim.feedkeys 'iif somevalue == xyz and \\\\\<CR>'
99         indent.should == shiftwidth * 2
100     end
101
102     it "indents relative to line above" do
103         vim.feedkeys 'i\tvalue = test + \\\\\<CR>'
104         indent.should == shiftwidth * 2
105     end
106   end
107
108   describe "when current line is dedented compared to previous line" do
109      before { vim.feedkeys 'i\<TAB>\<TAB>if x:\<CR>return True\<CR>\<ESC>' }
110      it "and current line has a valid indentation (Part 1)" do
111         vim.feedkeys '0i\<TAB>if y:'
112         proposed_indent.should == -1
113      end
114
115      it "and current line has a valid indentation (Part 2)" do
116         vim.feedkeys '0i\<TAB>\<TAB>if y:'
117         proposed_indent.should == -1
118      end
119
120      it "and current line has an invalid indentation" do
121         vim.feedkeys 'i    while True:\<CR>'
122         indent.should == previous_indent + shiftwidth
123      end
124   end
125
126   def shiftwidth
127     @shiftwidth ||= vim.echo("exists('*shiftwidth') ? shiftwidth() : &sw").to_i
128   end
129   def tabstop
130     @tabstop ||= vim.echo("&tabstop").to_i
131   end
132   def indent
133     vim.echo("indent('.')").to_i
134   end
135   def previous_indent
136     pline = vim.echo("line('.')").to_i - 1
137     vim.echo("indent('#{pline}')").to_i
138   end
139   def proposed_indent
140     line = vim.echo("line('.')")
141     col = vim.echo("col('.')")
142     indent_value = vim.echo("GetPythonPEPIndent(line('.'))").to_i
143     vim.command("call cursor(#{line}, #{col})")
144     return indent_value
145   end
146 end
147