Skip to content

Commit bc08780

Browse files
schnittchenkelvinst
authored andcommitted
Folding tests (elixir-editors#453)
* basic folding test * move matcher over, extract buffer method, example dsl * support folding on lower lines, give failure message * add a few examples
1 parent 12d3307 commit bc08780

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

spec/folding/basic_spec.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'Basic folding' do
6+
def self.it_folds_lines(content, lines, tags = nil)
7+
it("folds #{lines} lines on \n#{content}", tags) do
8+
expect(content).to fold_lines(lines, tags)
9+
end
10+
end
11+
12+
it_folds_lines(<<~EOF, 2)
13+
defmodule M do
14+
end
15+
"not in fold"
16+
EOF
17+
18+
it_folds_lines(<<~EOF, 4)
19+
defmodule M do
20+
def some_func do
21+
end
22+
end
23+
"not in fold"
24+
EOF
25+
26+
it_folds_lines(<<~EOF, 2, on_line: 2)
27+
defmodule M do
28+
def some_func do
29+
end
30+
end
31+
"not in fold"
32+
EOF
33+
34+
it_folds_lines(<<~EOF, 2)
35+
if true do
36+
end
37+
"not in fold"
38+
EOF
39+
40+
it_folds_lines(<<~EOF, 3, on_line: 3)
41+
if true do
42+
nil
43+
else
44+
nil
45+
end
46+
"not in fold"
47+
EOF
48+
49+
it_folds_lines(<<~EOF, 5, skip: "broken")
50+
if true do
51+
nil
52+
else
53+
nil
54+
end
55+
"not in fold"
56+
EOF
57+
end
58+

spec/spec_helper.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ def syntax(content, pattern)
6161
syngroups.gsub!(/["'\[\]]/, '').split(', ')
6262
end
6363

64+
def fold_and_delete(content, opts)
65+
start_line = opts[:on_line]
66+
with_file content do
67+
@vim.command("set foldmethod=syntax")
68+
69+
@vim.normal("zO")
70+
@vim.normal("#{start_line}G")
71+
@vim.normal("zc")
72+
@vim.normal("dd")
73+
end
74+
end
75+
6476
private
6577

6678
def with_file(content = nil)
@@ -193,6 +205,31 @@ def self.new
193205
end
194206
end
195207

208+
RSpec::Matchers.define :fold_lines do |lines, opts|
209+
buffer = Buffer.new(VIM, :ex)
210+
opts ||= {}
211+
start_line = opts[:on_line] ||= 1
212+
213+
after = nil
214+
215+
match do |code|
216+
after = buffer.fold_and_delete(code, opts)
217+
218+
code.lines.count - after.lines.count == lines
219+
end
220+
221+
failure_message do |code|
222+
<<~EOF
223+
expected
224+
#{code}
225+
to fold #{lines} lines at line #{start_line},
226+
but after folding at line #{start_line} and deleting a line I got
227+
#{after}
228+
back
229+
EOF
230+
end
231+
end
232+
196233
Vimrunner::RSpec.configure do |config|
197234
config.reuse_server = true
198235

0 commit comments

Comments
 (0)