ruby-beautify 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/README.md +5 -0
- data/bin/rbeautify +29 -0
- data/lib/app.rb +79 -0
- data/lib/beautifier.rb +168 -0
- data/lib/cli.rb +81 -0
- data/lib/filemagic.rb +13 -0
- data/lib/rbeautify.rb +27 -0
- data/lib/rbeautify/block_end.rb +23 -0
- data/lib/rbeautify/block_matcher.rb +153 -0
- data/lib/rbeautify/block_start.rb +119 -0
- data/lib/rbeautify/config/ruby.rb +131 -0
- data/lib/rbeautify/language.rb +37 -0
- data/lib/rbeautify/line.rb +53 -0
- data/license.txt +77 -0
- data/rbeautify.gemspec +15 -0
- data/spec/fixtures/ruby.yml +408 -0
- data/spec/rbeautify/block_matcher_spec.rb +89 -0
- data/spec/rbeautify/block_start_spec.rb +51 -0
- data/spec/rbeautify/config/ruby_spec.rb +183 -0
- data/spec/rbeautify/line_spec.rb +73 -0
- data/spec/rbeautify_spec.rb +1 -0
- data/spec/spec_helper.rb +124 -0
- metadata +95 -0
@@ -0,0 +1,89 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe RBeautify::BlockMatcher do
|
4
|
+
|
5
|
+
describe 'class' do
|
6
|
+
describe '#parse' do
|
7
|
+
before(:each) do
|
8
|
+
@ruby = RBeautify::Language.language(:ruby)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should not match de' do
|
12
|
+
RBeautify::BlockMatcher.parse(@ruby, nil, 0, 'de foo', 0).should be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should match def' do
|
16
|
+
block = RBeautify::BlockMatcher.parse(@ruby, nil, 0, 'def foo', 0)
|
17
|
+
block.should_not be_nil
|
18
|
+
block.name.should == :standard
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should be nested block' do
|
22
|
+
block = RBeautify::BlockMatcher.parse(@ruby, nil, 0, 'if {', 0)
|
23
|
+
block.should_not be_nil
|
24
|
+
block.name.should == :curly_bracket
|
25
|
+
block.parent.should_not be_nil
|
26
|
+
block.parent.name.should == :if
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should be nested block (taking into account ends)' do
|
30
|
+
block = RBeautify::BlockMatcher.parse(@ruby, nil, 0, 'if {}', 0)
|
31
|
+
block.should_not be_nil
|
32
|
+
block.name.should == :if
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should be deeply nested block (taking into account ends)' do
|
36
|
+
block = RBeautify::BlockMatcher.parse(@ruby, nil, 0, 'def foo(bar = {})', 0)
|
37
|
+
block.should_not be_nil
|
38
|
+
block.name.should == :standard
|
39
|
+
block.parent.should be_nil
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should current block if no started or ended blocks' do
|
43
|
+
block = RBeautify::BlockStart.new(@ruby.matcher(:standard), nil, 0, 0, 'def', ' foo')
|
44
|
+
RBeautify::BlockMatcher.parse(@ruby, block, 0, 'a = 3', 0).should == block
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should be newly started block if ends and starts' do
|
48
|
+
current_block = RBeautify::BlockStart.new(@ruby.matcher(:if), nil, 0, 0, 'if', ' foo')
|
49
|
+
block = RBeautify::BlockMatcher.parse(@ruby, current_block, 0, 'else', 0)
|
50
|
+
block.should_not be_nil
|
51
|
+
block.name.should == :if
|
52
|
+
block.parent.should be_nil
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should be parent block if current block ends' do
|
56
|
+
parent_block = RBeautify::BlockStart.new(@ruby.matcher(:standard), nil, 0, 0, 'class', ' Foo')
|
57
|
+
child_block = RBeautify::BlockStart.new(@ruby.matcher(:standard), parent_block, 0, 0, 'def', ' foo')
|
58
|
+
RBeautify::BlockMatcher.parse(@ruby, child_block, 0, 'end', 0).should == parent_block
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should remove two blocks if top of stack ends implicitly' do
|
62
|
+
parent_block = RBeautify::BlockStart.new(@ruby.matcher(:case), nil, 0, 0, 'case', ' foo')
|
63
|
+
child_block = RBeautify::BlockStart.new(@ruby.matcher(:inner_case), parent_block, 0, 0, 'when', '2')
|
64
|
+
RBeautify::BlockMatcher.parse(@ruby, child_block, 0, 'end', 0).should be_nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#can_nest?' do
|
70
|
+
before(:each) do
|
71
|
+
@language = mock(RBeautify::Language)
|
72
|
+
end
|
73
|
+
|
74
|
+
it { RBeautify::BlockMatcher.new(@language, :foo, /foo/, /bar/).should be_can_nest(nil) }
|
75
|
+
|
76
|
+
it { RBeautify::BlockMatcher.new(@language, :foo, /foo/, /bar/).should be_can_nest(mock('block_start', :parse_content? => true)) }
|
77
|
+
|
78
|
+
it { RBeautify::BlockMatcher.new(@language, :foo, /foo/, /bar/).should_not be_can_nest(mock('block_start', :parse_content? => false)) }
|
79
|
+
|
80
|
+
it { RBeautify::BlockMatcher.new(@language, :foo, /foo/, /bar/, :nest_except => [:bar]).should be_can_nest(nil) }
|
81
|
+
|
82
|
+
it { RBeautify::BlockMatcher.new(@language, :foo, /foo/, /bar/, :nest_except => [:foo]).should be_can_nest(mock('block_start', :name => :bar, :parse_content? => true)) }
|
83
|
+
|
84
|
+
it { RBeautify::BlockMatcher.new(@language, :foo, /foo/, /bar/, :nest_except => [:foo]).should_not be_can_nest(mock('block_start', :name => :bar, :parse_content? => false)) }
|
85
|
+
|
86
|
+
it { RBeautify::BlockMatcher.new(@language, :foo, /foo/, /bar/, :nest_except => [:bar]).should_not be_can_nest(mock('block_start', :name => :bar, :parse_content? => true)) }
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe RBeautify::BlockStart do
|
4
|
+
before(:each) do
|
5
|
+
@ruby = RBeautify::Language.language(:ruby)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'class' do
|
9
|
+
describe '#first_common_ancestor' do
|
10
|
+
before(:each) do
|
11
|
+
@grand_parent = RBeautify::BlockStart.new(@ruby.matcher(:standard), nil, 0, 0, 'class', ' Foo')
|
12
|
+
@parent = RBeautify::BlockStart.new(@ruby.matcher(:standard), @grand_parent, 0, 0, 'def', ' foo')
|
13
|
+
@first = RBeautify::BlockStart.new(@ruby.matcher(:standard), @parent, 0, 0, 'def', ' foo')
|
14
|
+
@second = RBeautify::BlockStart.new(@ruby.matcher(:standard), @parent, 0, 0, 'def', ' bar')
|
15
|
+
@unrelated = RBeautify::BlockStart.new(@ruby.matcher(:standard), nil, 0, 0, 'class', ' Bar')
|
16
|
+
end
|
17
|
+
|
18
|
+
it { RBeautify::BlockStart.first_common_ancestor(@grand_parent, nil).should be_nil }
|
19
|
+
it { RBeautify::BlockStart.first_common_ancestor(@grand_parent, @unrelated).should be_nil }
|
20
|
+
it { RBeautify::BlockStart.first_common_ancestor(@grand_parent, @parent).should == @grand_parent }
|
21
|
+
it { RBeautify::BlockStart.first_common_ancestor(@grand_parent, @first).should == @grand_parent }
|
22
|
+
it { RBeautify::BlockStart.first_common_ancestor(@first, @second).should == @parent }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#strict_ancestor_of?' do
|
27
|
+
before(:each) do
|
28
|
+
@block = RBeautify::BlockStart.new(@ruby.matcher(:standard), nil, 0, 'def', ' foo', 0)
|
29
|
+
end
|
30
|
+
|
31
|
+
it { @block.should_not be_strict_ancestor_of(nil) }
|
32
|
+
it { @block.should_not be_strict_ancestor_of(@block) }
|
33
|
+
it { @block.should_not be_strict_ancestor_of(RBeautify::BlockStart.new(@ruby.matcher(:if), nil, 0, 0, 'if', ' foo') ) }
|
34
|
+
it { @block.should be_strict_ancestor_of(RBeautify::BlockStart.new(@ruby.matcher(:if), @block, 0, 0, 'if', ' foo') ) }
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#total_indent_size' do
|
39
|
+
before(:each) do
|
40
|
+
@block = RBeautify::BlockStart.new(@ruby.matcher(:standard), nil, 0, 'def', ' foo', 0)
|
41
|
+
end
|
42
|
+
|
43
|
+
it { RBeautify::BlockStart.new(@ruby.matcher(:standard), nil, 0, 0, 'def', ' foo') .total_indent_size.should == 2 }
|
44
|
+
|
45
|
+
it 'should sum with parents total indent size' do
|
46
|
+
parent = mock('parent_start_block', :total_indent_size => 4)
|
47
|
+
RBeautify::BlockStart.new(@ruby.matcher(:standard), parent, 0, 0, 'def', ' foo') .total_indent_size.should == 6
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,183 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper.rb'
|
2
|
+
|
3
|
+
run_fixtures_for_language(:ruby)
|
4
|
+
|
5
|
+
describe 'Ruby' do
|
6
|
+
|
7
|
+
describe 'matchers' do
|
8
|
+
before(:each) do
|
9
|
+
@ruby = RBeautify::Language.language(:ruby)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'standard' do
|
13
|
+
before(:each) do
|
14
|
+
@matcher = @ruby.matcher(:standard)
|
15
|
+
@current_block = RBeautify::BlockStart.new(@matcher, nil, 0, 0, '', '')
|
16
|
+
end
|
17
|
+
|
18
|
+
it { @matcher.parse_block_start('class Foo; end', nil, 0, 0).should be_block_start_like(:standard, 0, 'class', ' Foo; end') }
|
19
|
+
it { @matcher.parse_block_start('module Foo', nil, 0, 0).should be_block_start_like(:standard, 0, 'module', ' Foo') }
|
20
|
+
it { @matcher.parse_block_start('def foo()', nil, 0, 0).should be_block_start_like(:standard, 0, 'def', ' foo()') }
|
21
|
+
it { @matcher.parse_block_start('end Foo', nil, 0, 0).should be_nil }
|
22
|
+
|
23
|
+
it { @current_block.parse_block_end('end', 0).should be_block_end_like(@current_block, 0, 'end', '') }
|
24
|
+
it { @current_block.parse_block_end(';end', 0).should be_block_end_like(@current_block, 0, ';end', '') }
|
25
|
+
it { @current_block.parse_block_end('; end', 0).should be_block_end_like(@current_block, 1, ' end', '') }
|
26
|
+
it { @current_block.parse_block_end('rescue', 0).should be_block_end_like(@current_block, 0, 'rescue', '') }
|
27
|
+
it { @current_block.parse_block_end('ensure', 0).should be_block_end_like(@current_block, 0, 'ensure', '') }
|
28
|
+
it { @current_block.parse_block_end('}', 0).should be_nil }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'if' do
|
32
|
+
before(:each) do
|
33
|
+
@matcher = @ruby.matcher(:if)
|
34
|
+
@current_block = RBeautify::BlockStart.new(@matcher, nil, 0, 0, 'if', ' foo')
|
35
|
+
end
|
36
|
+
|
37
|
+
it { @matcher.should be_end_can_also_be_start}
|
38
|
+
|
39
|
+
it { @matcher.parse_block_start('if foo', nil, 0, 0).should be_block_start_like(:if, 0, 'if', ' foo') }
|
40
|
+
it { @matcher.parse_block_start('then foo = bar', nil, 0, 0).should be_block_start_like(:if, 0, 'then', ' foo = bar') }
|
41
|
+
it { @matcher.parse_block_start('if_foo', nil, 0, 0).should be_nil }
|
42
|
+
|
43
|
+
it { @current_block.parse_block_end('end', 0).should be_block_end_like(@current_block, 0, 'end', '') }
|
44
|
+
it { @current_block.parse_block_end('then', 0).should be_block_end_like(@current_block, 0, 'then', '') }
|
45
|
+
it { @current_block.parse_block_end('else', 0).should be_block_end_like(@current_block, 0, 'else', '') }
|
46
|
+
it { @current_block.parse_block_end('a = 3', 0).should be_nil }
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'curly_bracket' do
|
50
|
+
before(:each) do
|
51
|
+
@matcher = @ruby.matcher(:curly_bracket)
|
52
|
+
@current_block = RBeautify::BlockStart.new(@matcher, nil, 0, 0, '{', '')
|
53
|
+
end
|
54
|
+
|
55
|
+
it { @matcher.parse_block_start('{', nil, 0, 0).should be_block_start_like(:curly_bracket, 0, '{', '') }
|
56
|
+
it { @matcher.parse_block_start('end', nil, 0, 0).should be_nil }
|
57
|
+
|
58
|
+
it { @current_block.parse_block_end('}', 0).should be_block_end_like(@current_block, 0, '}', '') }
|
59
|
+
it { @current_block.parse_block_end('end', 0).should be_nil }
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'double_quote' do
|
64
|
+
before(:each) do
|
65
|
+
@matcher = @ruby.matcher(:double_quote)
|
66
|
+
@current_block = RBeautify::BlockStart.new(@matcher, nil, 0, 0, '"', 'foo"')
|
67
|
+
end
|
68
|
+
|
69
|
+
it { @matcher.parse_block_start('a = "foo"', nil, 0, 0).should be_block_start_like(:double_quote, 4, '"', 'foo"') }
|
70
|
+
it { @matcher.parse_block_start('a = 2', nil, 0, 0).should be_nil }
|
71
|
+
|
72
|
+
it { @current_block.parse_block_end(' bar"', 0).should be_block_end_like(@current_block, 4, '"', '') }
|
73
|
+
it { @current_block.parse_block_end(' " + bar + "', 0).should be_block_end_like(@current_block, 1, '"', ' + bar + "') }
|
74
|
+
it { @current_block.parse_block_end('\\\\"', 0).should be_block_end_like(@current_block, 2, '"', '') }
|
75
|
+
it { @current_block.parse_block_end('\\" more string"', 0).should be_block_end_like(@current_block, 14, '"', '') }
|
76
|
+
it { @current_block.parse_block_end('a = 2', 0).should be_nil }
|
77
|
+
it { @current_block.parse_block_end('\\"', 0).should be_nil }
|
78
|
+
it { @current_block.parse_block_end('\\\\\\"', 0).should be_nil }
|
79
|
+
end
|
80
|
+
|
81
|
+
describe 'single_quote' do
|
82
|
+
before(:each) do
|
83
|
+
@matcher = @ruby.matcher(:single_quote)
|
84
|
+
@current_block = RBeautify::BlockStart.new(@matcher, nil, 0, 0, "'", "foo'")
|
85
|
+
end
|
86
|
+
|
87
|
+
it { @matcher.should_not be_end_can_also_be_start}
|
88
|
+
it { @matcher.parse_block_start("describe '#foo?' do", nil, 0, 0).should be_block_start_like(:single_quote, 9, "'", "#foo?' do") }
|
89
|
+
it { @matcher.parse_block_start('a = 2', nil, 0, 0).should be_nil }
|
90
|
+
|
91
|
+
it { @current_block.parse_block_end("#foo?' do", 9).should be_block_end_like(@current_block, 14, "'", ' do')}
|
92
|
+
it { @current_block.parse_block_end('a = 2', 0).should be_nil }
|
93
|
+
end
|
94
|
+
|
95
|
+
describe 'continuing_line' do
|
96
|
+
before(:each) do
|
97
|
+
@matcher = @ruby.matcher(:continuing_line)
|
98
|
+
@current_block = RBeautify::BlockStart.new(@matcher, nil, 0, 8, ',', '')
|
99
|
+
end
|
100
|
+
|
101
|
+
it { @matcher.parse_block_start('foo :bar,', nil, 0, 0).should be_block_start_like(:continuing_line, 8, ',', '') }
|
102
|
+
it { @matcher.parse_block_start('a = 3 &&', nil, 0, 0).should be_block_start_like(:continuing_line, 6, '&&', '') }
|
103
|
+
it { @matcher.parse_block_start('a = 3 ||', nil, 0, 0).should be_block_start_like(:continuing_line, 6, '||', '') }
|
104
|
+
it { @matcher.parse_block_start('a = 3 +', nil, 0, 0).should be_block_start_like(:continuing_line, 6, '+', '') }
|
105
|
+
it { @matcher.parse_block_start('a = 3 -', nil, 0, 0).should be_block_start_like(:continuing_line, 6, '-', '') }
|
106
|
+
it { @matcher.parse_block_start("a \\", nil, 0, 0).should be_block_start_like(:continuing_line, 2, '\\', '') }
|
107
|
+
it { @matcher.parse_block_start('a ?', nil, 0, 0).should be_block_start_like(:continuing_line, 1, ' ?', '') }
|
108
|
+
it { @matcher.parse_block_start('a ? # some comment', nil, 0, 0).should be_block_start_like(:continuing_line, 1, ' ? # some comment', '') }
|
109
|
+
it { @matcher.parse_block_start('a = 3', nil, 0, 0).should be_nil }
|
110
|
+
it { @matcher.parse_block_start('a = foo.bar?', nil, 0, 0).should be_nil }
|
111
|
+
it { @matcher.parse_block_start('# just a comment', nil, 0, 0).should be_nil }
|
112
|
+
|
113
|
+
it { @current_block.parse_block_end('a = 3', 0).should be_block_end_like(@current_block, 0, '', 'a = 3') }
|
114
|
+
it { @current_block.parse_block_end('foo :bar,', 0).should be_nil }
|
115
|
+
it { @current_block.parse_block_end('a = 3 &&', 0).should be_nil }
|
116
|
+
it { @current_block.parse_block_end('a = 3 +', 0).should be_nil }
|
117
|
+
it { @current_block.parse_block_end("a \\", 0).should be_nil }
|
118
|
+
it { @current_block.parse_block_end('# just a comment', 0).should be_nil }
|
119
|
+
it { @current_block.parse_block_end('#', 0).should be_nil }
|
120
|
+
end
|
121
|
+
|
122
|
+
describe 'round_bracket' do
|
123
|
+
before(:each) do
|
124
|
+
@matcher = @ruby.matcher(:round_bracket)
|
125
|
+
@current_block = RBeautify::BlockStart.new(@matcher, nil, 0, 0, '(', '')
|
126
|
+
end
|
127
|
+
|
128
|
+
it { @matcher.parse_block_start('a = (foo,', nil, 0, 0).should be_block_start_like(:round_bracket, 4, '(', 'foo,') }
|
129
|
+
it { @matcher.parse_block_start('anything else', nil, 0, 0).should be_nil }
|
130
|
+
|
131
|
+
it { @current_block.parse_block_end('foo)', 0).should be_block_end_like(@current_block, 3, ')', '') }
|
132
|
+
it { @current_block.parse_block_end('a = 3', 0).should be_nil }
|
133
|
+
end
|
134
|
+
|
135
|
+
describe 'comment' do
|
136
|
+
before(:each) do
|
137
|
+
@matcher = @ruby.matcher(:comment)
|
138
|
+
@current_block = RBeautify::BlockStart.new(@matcher, nil, 0, 8, '#', ' comment')
|
139
|
+
end
|
140
|
+
|
141
|
+
it { @matcher.parse_block_start('anything # comment', nil, 0, 0).should be_block_start_like(:comment, 8, ' #', ' comment') }
|
142
|
+
it { @matcher.parse_block_start('#', nil, 0, 0).should be_block_start_like(:comment, 0, '#', '') }
|
143
|
+
it { @matcher.parse_block_start('anything else', nil, 0, 0).should be_nil }
|
144
|
+
|
145
|
+
it { @current_block.parse_block_end('anything', 0).should be_block_end_like(@current_block, 8, '', '') }
|
146
|
+
it { @current_block.parse_block_end('', 0).should be_block_end_like(@current_block, 0, '', '') }
|
147
|
+
end
|
148
|
+
|
149
|
+
describe 'begin' do
|
150
|
+
before(:each) do
|
151
|
+
@matcher = @ruby.matcher(:begin)
|
152
|
+
@current_block = RBeautify::BlockStart.new(@matcher, nil, 0, 0, 'begin', '')
|
153
|
+
end
|
154
|
+
|
155
|
+
it { @matcher.parse_block_start('begin', nil, 0, 0).should be_block_start_like(:begin, 0, 'begin', '') }
|
156
|
+
it { @matcher.parse_block_start('beginning', nil, 0, 0).should be_nil }
|
157
|
+
|
158
|
+
it { @current_block.parse_block_end('rescue', 0).should be_block_end_like(@current_block, 0, 'rescue', '') }
|
159
|
+
it { @current_block.parse_block_end('ensure', 0).should be_block_end_like(@current_block, 0, 'ensure', '') }
|
160
|
+
it { @current_block.parse_block_end('else', 0).should be_block_end_like(@current_block, 0, 'else', '') }
|
161
|
+
it { @current_block.parse_block_end('end', 0).should be_block_end_like(@current_block, 0, 'end', '') }
|
162
|
+
end
|
163
|
+
|
164
|
+
describe 'regex' do
|
165
|
+
before(:each) do
|
166
|
+
@matcher = @ruby.matcher(:regex)
|
167
|
+
@current_block = RBeautify::BlockStart.new(@matcher, nil, 0, 0, '/', 'foo/')
|
168
|
+
end
|
169
|
+
|
170
|
+
it { @matcher.parse_block_start('/foo/', nil, 0, 0).should be_block_start_like(:regex, 0, '/', 'foo/') }
|
171
|
+
it { @matcher.parse_block_start(', /foo/', nil, 0, 0).should be_block_start_like(:regex, 0, ', /', 'foo/') }
|
172
|
+
it { @matcher.parse_block_start('foo = /bar/', nil, 0, 0).should be_block_start_like(:regex, 4, '= /', 'bar/') }
|
173
|
+
it { @matcher.parse_block_start('foo =~ /bar/', nil, 0, 0).should be_block_start_like(:regex, 5, '~ /', 'bar/') }
|
174
|
+
it { @matcher.parse_block_start('1/2', nil, 0, 0).should be_nil }
|
175
|
+
it { @matcher.parse_block_start('anything else', nil, 0, 0).should be_nil }
|
176
|
+
|
177
|
+
it { @current_block.parse_block_end('foo/', 0).should be_block_end_like(@current_block, 3, '/', '') }
|
178
|
+
it { @current_block.parse_block_end('foo/', 0).should be_block_end_like(@current_block, 3, '/', '') }
|
179
|
+
it { @current_block.parse_block_end('', 0).should be_nil }
|
180
|
+
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe RBeautify::Line do
|
4
|
+
|
5
|
+
describe '#format' do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@language = mock(RBeautify::Language)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should just strip with empty stack' do
|
12
|
+
RBeautify::BlockMatcher.stub!(:parse => nil)
|
13
|
+
RBeautify::Line.new(@language, ' a = 3 ', 0).format.should == "a = 3"
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should indent with existing indent' do
|
17
|
+
current_block = mock('block_start', :total_indent_size => 2, :format_content? => true, :strict_ancestor_of? => false)
|
18
|
+
RBeautify::BlockStart.stub(:first_common_ancestor => current_block)
|
19
|
+
RBeautify::BlockMatcher.stub!(:parse => current_block)
|
20
|
+
RBeautify::Line.new(@language, ' a = 3 ', 0, current_block).format.should == ' a = 3'
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'leave empty lines blank' do
|
24
|
+
current_block = mock('block_start', :format_content? => true)
|
25
|
+
RBeautify::BlockMatcher.stub!(:parse => current_block)
|
26
|
+
RBeautify::Line.new(@language, ' ', 0, current_block).format.should == ''
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should remove indent with match to end of block' do
|
30
|
+
current_block = mock('block_start', :format_content? => true, :indent_end_line? => false)
|
31
|
+
RBeautify::BlockStart.stub(:first_common_ancestor => nil)
|
32
|
+
RBeautify::BlockMatcher.stub!(:parse => nil)
|
33
|
+
RBeautify::Line.new(@language, ' end ', 0, current_block).format.should == 'end'
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should not remove indent with match to end of block if indent_end_line? is true' do
|
37
|
+
current_block = mock('block_start', :total_indent_size => 2, :format_content? => true, :indent_end_line? => true)
|
38
|
+
RBeautify::BlockMatcher.stub!(:parse => nil)
|
39
|
+
RBeautify::Line.new(@language, ' end ', 0, current_block).format.should == ' end'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should leave indent at old stack level with match of new block' do
|
43
|
+
current_block = mock('current_block_start', :total_indent_size => 2, :format_content? => true)
|
44
|
+
new_block = mock('new_block_start', :format_content? => true, :strict_ancestor_of? => false)
|
45
|
+
RBeautify::BlockStart.stub(:first_common_ancestor => current_block)
|
46
|
+
RBeautify::BlockMatcher.stub!(:parse => new_block)
|
47
|
+
RBeautify::Line.new(@language, 'class Foo', 0, current_block).format.should == ' class Foo'
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should remove indent if a block ends and starts' do
|
51
|
+
current_block = mock('current_block_start', :format_content? => true)
|
52
|
+
new_block = mock('new_block_start', :format_content? => true, :strict_ancestor_of? => false)
|
53
|
+
RBeautify::BlockStart.stub(:first_common_ancestor => nil)
|
54
|
+
RBeautify::BlockMatcher.stub!(:parse => new_block)
|
55
|
+
RBeautify::Line.new(@language, ' else ', 0, current_block).format.should == 'else'
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should not change when format is false' do
|
59
|
+
current_block = mock('block_start', :format_content? => false)
|
60
|
+
RBeautify::BlockMatcher.stub!(:parse => current_block)
|
61
|
+
RBeautify::Line.new(@language, ' some content after program has finished. ', 0, current_block).format.should ==
|
62
|
+
" some content after program has finished. "
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should leave indent with match to end of block (but no format)' do
|
66
|
+
current_block = mock('block_start', :format_content? => false)
|
67
|
+
RBeautify::BlockMatcher.stub!(:parse => nil)
|
68
|
+
RBeautify::Line.new(@language, ' "', 0, current_block).format.should == ' "'
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
begin
|
2
|
+
require 'rspec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems'
|
5
|
+
require 'rspec'
|
6
|
+
end
|
7
|
+
require 'yaml'
|
8
|
+
require File.dirname(__FILE__) + '/../lib/rbeautify.rb'
|
9
|
+
|
10
|
+
module RBeautifyMatchers
|
11
|
+
# Adds more descriptive failure messages to the dynamic be_valid matcher
|
12
|
+
class BeBlockStartLike #:nodoc:
|
13
|
+
def initialize(block_matcher_name, offset, match, after_match)
|
14
|
+
# triggers the standard Spec::Matchers::Be magic, as if the original Spec::Matchers#method_missing had fired
|
15
|
+
@block_matcher_name = block_matcher_name
|
16
|
+
@offset = offset
|
17
|
+
@match = match
|
18
|
+
@after_match = after_match
|
19
|
+
end
|
20
|
+
|
21
|
+
def matches?(target_block)
|
22
|
+
@target_block = target_block
|
23
|
+
return !target_block.nil? &&
|
24
|
+
(expected_string == got_string)
|
25
|
+
end
|
26
|
+
|
27
|
+
def failure_message
|
28
|
+
"expected\n#{expected_string} but got\n#{got_string}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def negative_failure_message
|
32
|
+
"expected to be different from #{expected_string}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def expected_string
|
36
|
+
"name: #{@block_matcher_name}, offset: #{@offset}, match: '#{@match}', after_match: '#{@after_match}'"
|
37
|
+
end
|
38
|
+
|
39
|
+
def got_string
|
40
|
+
"name: #{@target_block.block_matcher.name}, offset: #{@target_block.offset}, match: '#{@target_block.match}', after_match: '#{@target_block.after_match}'"
|
41
|
+
end
|
42
|
+
|
43
|
+
def description
|
44
|
+
"block start with"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class BeBlockEndLike #:nodoc:
|
49
|
+
def initialize(block_start, offset, match, after_match)
|
50
|
+
# triggers the standard Spec::Matchers::Be magic, as if the original Spec::Matchers#method_missing had fired
|
51
|
+
@block_start = block_start
|
52
|
+
@offset = offset
|
53
|
+
@match = match
|
54
|
+
@after_match = after_match
|
55
|
+
end
|
56
|
+
|
57
|
+
def matches?(target_block)
|
58
|
+
@target_block = target_block
|
59
|
+
expected_string == got_string
|
60
|
+
end
|
61
|
+
|
62
|
+
def failure_message
|
63
|
+
"expected\n#{expected_string} but got\n#{got_string}"
|
64
|
+
end
|
65
|
+
|
66
|
+
def negative_failure_message
|
67
|
+
"expected to be different from #{expected_string}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def expected_string
|
71
|
+
"block_end: #{@block_start.name}, offset: #{@offset}, match: '#{@match}', after_match: '#{@after_match}'"
|
72
|
+
end
|
73
|
+
|
74
|
+
def got_string
|
75
|
+
if @target_block.nil?
|
76
|
+
'nil'
|
77
|
+
else
|
78
|
+
"block_end: #{@target_block.block_start.name}, offset: #{@target_block.offset}, match: '#{@target_block.match}', after_match: '#{@target_block.after_match}'"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def description
|
83
|
+
"block end with"
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
def be_block_start_like(block_matcher, offset, match, after_match)
|
89
|
+
BeBlockStartLike.new(block_matcher, offset, match, after_match)
|
90
|
+
end
|
91
|
+
|
92
|
+
def be_block_end_like(block_start, offset, match, after_match)
|
93
|
+
BeBlockEndLike.new(block_start, offset, match, after_match)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
RSpec.configure do |config|
|
98
|
+
config.include(RBeautifyMatchers)
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
def run_fixtures_for_language(language)
|
103
|
+
fixtures = YAML.load_file(File.dirname(__FILE__) + "/fixtures/#{language}.yml")
|
104
|
+
|
105
|
+
describe language do
|
106
|
+
fixtures.each do |fixture|
|
107
|
+
it "should #{fixture['name']}" do
|
108
|
+
input = fixture['input']
|
109
|
+
output = fixture['output'] || input
|
110
|
+
debug = fixture['debug'] || false
|
111
|
+
|
112
|
+
if fixture['pending']
|
113
|
+
pending fixture['pending'] do
|
114
|
+
RBeautify.beautify_string(language, input).should == output
|
115
|
+
end
|
116
|
+
else
|
117
|
+
RBeautify::BlockMatcher.debug = debug
|
118
|
+
RBeautify.beautify_string(language, input).should == output
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|