From 2f38b3ae8e449b623a420c51555586c184b39473 Mon Sep 17 00:00:00 2001 From: Thomas Stratmann Date: Sun, 25 Nov 2018 01:56:02 +0100 Subject: [PATCH 1/4] basic folding test --- spec/folding/basic_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 spec/folding/basic_spec.rb diff --git a/spec/folding/basic_spec.rb b/spec/folding/basic_spec.rb new file mode 100644 index 00000000..cb3c200d --- /dev/null +++ b/spec/folding/basic_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec::Matchers.define :fold_lines do |lines| + file = ".fixture.ex" + + + match do |code| + File.write(file, code) + VIM.edit file + VIM.command("set foldmethod=syntax") + + VIM.normal("zO") + VIM.normal("zM") + VIM.normal("dd") + VIM.write + + written = IO.read(file) + code.lines.count - written.lines.count == lines + end +end + +describe 'Basic folding' do + it 'blah' do + content = <<~EOF + defmodule M do + end + EOF + + expect(content).to fold_lines(2) + end +end + From 27fcbaaaa2493b55802c17e0bc191507ff9a611d Mon Sep 17 00:00:00 2001 From: Thomas Stratmann Date: Sun, 25 Nov 2018 13:11:20 +0100 Subject: [PATCH 2/4] move matcher over, extract buffer method, example dsl --- spec/folding/basic_spec.rb | 31 +++++++------------------------ spec/spec_helper.rb | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/spec/folding/basic_spec.rb b/spec/folding/basic_spec.rb index cb3c200d..440404ed 100644 --- a/spec/folding/basic_spec.rb +++ b/spec/folding/basic_spec.rb @@ -2,33 +2,16 @@ require 'spec_helper' -RSpec::Matchers.define :fold_lines do |lines| - file = ".fixture.ex" - - - match do |code| - File.write(file, code) - VIM.edit file - VIM.command("set foldmethod=syntax") - - VIM.normal("zO") - VIM.normal("zM") - VIM.normal("dd") - VIM.write - - written = IO.read(file) - code.lines.count - written.lines.count == lines - end -end - describe 'Basic folding' do - it 'blah' do - content = <<~EOF - defmodule M do + def self.it_folds_lines(content, lines, tags = nil) + it("folds #{lines} lines on \n#{content}", tags) do + expect(content).to fold_lines(lines, tags) end - EOF + end - expect(content).to fold_lines(2) + it_folds_lines(<<~EOF, 2) + defmodule M do end + EOF end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cd02e09e..9b63225a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -61,6 +61,16 @@ def syntax(content, pattern) syngroups.gsub!(/["'\[\]]/, '').split(', ') end + def fold_and_delete(content) + with_file content do + @vim.command("set foldmethod=syntax") + + @vim.normal("zO") + @vim.normal("zM") + @vim.normal("dd") + end + end + private def with_file(content = nil) @@ -193,6 +203,16 @@ def self.new end end +RSpec::Matchers.define :fold_lines do |lines| + buffer = Buffer.new(VIM, :ex) + + match do |code| + after = buffer.fold_and_delete(code) + + code.lines.count - after.lines.count == lines + end +end + Vimrunner::RSpec.configure do |config| config.reuse_server = true From c93a84670171d316640e80e2028758bec9f5f689 Mon Sep 17 00:00:00 2001 From: Thomas Stratmann Date: Sun, 25 Nov 2018 13:49:20 +0100 Subject: [PATCH 3/4] support folding on lower lines, give failure message --- spec/spec_helper.rb | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9b63225a..42291a77 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -61,12 +61,14 @@ def syntax(content, pattern) syngroups.gsub!(/["'\[\]]/, '').split(', ') end - def fold_and_delete(content) + def fold_and_delete(content, opts) + start_line = opts[:on_line] with_file content do @vim.command("set foldmethod=syntax") @vim.normal("zO") - @vim.normal("zM") + @vim.normal("#{start_line}G") + @vim.normal("zc") @vim.normal("dd") end end @@ -203,14 +205,29 @@ def self.new end end -RSpec::Matchers.define :fold_lines do |lines| +RSpec::Matchers.define :fold_lines do |lines, opts| buffer = Buffer.new(VIM, :ex) + opts ||= {} + start_line = opts[:on_line] ||= 1 + + after = nil match do |code| - after = buffer.fold_and_delete(code) + after = buffer.fold_and_delete(code, opts) code.lines.count - after.lines.count == lines end + + failure_message do |code| + <<~EOF + expected + #{code} + to fold #{lines} lines at line #{start_line}, + but after folding at line #{start_line} and deleting a line I got + #{after} + back + EOF + end end Vimrunner::RSpec.configure do |config| From 12c7d514e506b402b9c6bae460675f053b084f0a Mon Sep 17 00:00:00 2001 From: Thomas Stratmann Date: Sun, 25 Nov 2018 13:50:29 +0100 Subject: [PATCH 4/4] add a few examples --- spec/folding/basic_spec.rb | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/spec/folding/basic_spec.rb b/spec/folding/basic_spec.rb index 440404ed..b3e2406f 100644 --- a/spec/folding/basic_spec.rb +++ b/spec/folding/basic_spec.rb @@ -12,6 +12,47 @@ def self.it_folds_lines(content, lines, tags = nil) it_folds_lines(<<~EOF, 2) defmodule M do end + "not in fold" + EOF + + it_folds_lines(<<~EOF, 4) + defmodule M do + def some_func do + end + end + "not in fold" + EOF + + it_folds_lines(<<~EOF, 2, on_line: 2) + defmodule M do + def some_func do + end + end + "not in fold" + EOF + + it_folds_lines(<<~EOF, 2) + if true do + end + "not in fold" + EOF + + it_folds_lines(<<~EOF, 3, on_line: 3) + if true do + nil + else + nil + end + "not in fold" + EOF + + it_folds_lines(<<~EOF, 5, skip: "broken") + if true do + nil + else + nil + end + "not in fold" EOF end