Skip to content

Commit 94ff85c

Browse files
author
Gaspar Chilingarov
committed
Add tests for Ctags transformer for Elixir (for Tagbar integration).
1 parent f277d13 commit 94ff85c

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

Gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source 'https://rubygems.org'
2+
3+
ruby '2.3.1'
4+
5+
gem 'rspec'
6+
gem 'vimrunner'
7+
gem 'pry-byebug'
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'transforms ctags output' do
6+
it "generates trace file" do
7+
correct_content = <<~EOF
8+
Server /tmp/v3hvNb9/5.ex /^defmodule PusherProxy.Proxy.Server do$/;" m line:3
9+
start_link/2 /tmp/v3hvNb9/5.ex /^ def start_link(name, kw_args\\\\[]) do$/;" f line:59
10+
add_worker/2 /tmp/v3hvNb9/5.ex /^ def add_worker(pid, worker_pid) do$/;" f line:63
11+
add_worker/3 /tmp/v3hvNb9/5.ex /^ def add_worker(pid, worker_pid, options) do$/;" f line:66
12+
more_work_available?/3 /tmp/v3hvNb9/5.ex /^ def more_work_available?(pid, worker_pid, since_rev_id) do$/;" f line:68
13+
init(kw_args) /tmp/v3hvNb9/5.ex /^ def init(kw_args) do$/;" O line:73
14+
handle_cast({:add_worker, pid}, state) /tmp/v3hvNb9/5.ex /^ def handle_cast({:add_worker, pid}, state) do$/;" O line:86
15+
handle_cast({:decoder_ready, decoder_pid}, state) /tmp/v3hvNb9/5.ex /^ def handle_cast({:decoder_ready, decoder_pid}, state) do$/;" O line:101
16+
handle_cast({:data, _data, false, _rev, _pid}, state) /tmp/v3hvNb9/5.ex /^ def handle_cast({:data, _data, false, _rev, _pid}, state) do$/;" O line:126
17+
handle_cast({:data, data, true, _rev, _pid}, state) /tmp/v3hvNb9/5.ex /^ def handle_cast({:data, data, true, _rev, _pid}, state) do$/;" O line:141
18+
handle_cast({:more_work_available?, worker_pid, known_max_rev}, state) /tmp/v3hvNb9/5.ex /^ def handle_cast({:more_work_available?, worker_pid, known_max_rev}, state) do$/;" O line:172
19+
handle_cast(:cleanup_ets, state) /tmp/v3hvNb9/5.ex /^ def handle_cast(:cleanup_ets, state) do$/;" O line:188
20+
handle_info({:DOWN, _, :process, pid, reason}, state) /tmp/v3hvNb9/5.ex /^ def handle_info({:DOWN, _, :process, pid, reason}, state) do$/;" O line:208
21+
handle_info(ev, state) /tmp/v3hvNb9/5.ex /^ def handle_info(ev, state) do$/;" O line:219
22+
EOF
23+
24+
25+
expect(<<~EOF).to be_ctags_output(correct_content)
26+
Server /tmp/v3hvNb9/5.ex /^defmodule PusherProxy.Proxy.Server do$/;" m line:3
27+
start_link /tmp/v3hvNb9/5.ex /^ def start_link(name, kw_args\\\\[]) do$/;" f line:59
28+
add_worker /tmp/v3hvNb9/5.ex /^ def add_worker(pid, worker_pid) do$/;" f line:63
29+
add_worker /tmp/v3hvNb9/5.ex /^ def add_worker(pid, worker_pid1) do$/;" f line:64
30+
add_worker /tmp/v3hvNb9/5.ex /^ def add_worker(pid, worker_pid2) do$/;" f line:65
31+
add_worker /tmp/v3hvNb9/5.ex /^ def add_worker(pid, worker_pid, options) do$/;" f line:66
32+
more_work_available? /tmp/v3hvNb9/5.ex /^ def more_work_available?(pid, worker_pid, since_rev_id) do$/;" f line:68
33+
init(kw_args) /tmp/v3hvNb9/5.ex /^ def init(kw_args) do$/;" O line:73
34+
init /tmp/v3hvNb9/5.ex /^ def init(kw_args) do$/;" f line:73
35+
handle_cast({:add_worker, pid}, state) /tmp/v3hvNb9/5.ex /^ def handle_cast({:add_worker, pid}, state) do$/;" O line:86
36+
handle_cast /tmp/v3hvNb9/5.ex /^ def handle_cast({:add_worker, pid}, state) do$/;" f line:86
37+
handle_cast({:decoder_ready, decoder_pid}, state) /tmp/v3hvNb9/5.ex /^ def handle_cast({:decoder_ready, decoder_pid}, state) do$/;" O line:101
38+
handle_cast /tmp/v3hvNb9/5.ex /^ def handle_cast({:decoder_ready, decoder_pid}, state) do$/;" f line:101
39+
handle_cast({:data, _data, false, _rev, _pid}, state) /tmp/v3hvNb9/5.ex /^ def handle_cast({:data, _data, false, _rev, _pid}, state) do$/;" O line:126
40+
handle_cast /tmp/v3hvNb9/5.ex /^ def handle_cast({:data, _data, false, _rev, _pid}, state) do$/;" f line:126
41+
handle_cast({:data, data, true, _rev, _pid}, state) /tmp/v3hvNb9/5.ex /^ def handle_cast({:data, data, true, _rev, _pid}, state) do$/;" O line:141
42+
handle_cast /tmp/v3hvNb9/5.ex /^ def handle_cast({:data, data, true, _rev, _pid}, state) do$/;" f line:141
43+
handle_cast({:more_work_available?, worker_pid, known_max_rev}, state) /tmp/v3hvNb9/5.ex /^ def handle_cast({:more_work_available?, worker_pid, known_max_rev}, state) do$/;" O line:172
44+
handle_cast /tmp/v3hvNb9/5.ex /^ def handle_cast({:more_work_available?, worker_pid, known_max_rev}, state) do$/;" f line:172
45+
handle_cast(:cleanup_ets, state) /tmp/v3hvNb9/5.ex /^ def handle_cast(:cleanup_ets, state) do$/;" O line:188
46+
handle_cast /tmp/v3hvNb9/5.ex /^ def handle_cast(:cleanup_ets, state) do$/;" f line:188
47+
handle_info({:DOWN, _, :process, pid, reason}, state) /tmp/v3hvNb9/5.ex /^ def handle_info({:DOWN, _, :process, pid, reason}, state) do$/;" O line:208
48+
handle_info /tmp/v3hvNb9/5.ex /^ def handle_info({:DOWN, _, :process, pid, reason}, state) do$/;" f line:208
49+
handle_info(ev, state) /tmp/v3hvNb9/5.ex /^ def handle_info(ev, state) do$/;" O line:219
50+
handle_info /tmp/v3hvNb9/5.ex /^ def handle_info(ev, state) do$/;" f line:219
51+
EOF
52+
end
53+
end

spec/spec_helper.rb

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# frozen_string_literal: true
2+
3+
require 'rspec/expectations'
4+
require 'tmpdir'
5+
require 'vimrunner'
6+
require 'vimrunner/rspec'
7+
require 'pry'
8+
9+
class Buffer
10+
def initialize(vim, type)
11+
#@mix_file = "mix.exs"
12+
#@test_helper = "test/test_helper.exs"
13+
#
14+
#write_mix_exs
15+
16+
if type == :ctags
17+
@file = "fixture.#{type}"
18+
else
19+
puts "do not know type #{type}"
20+
exit!(123)
21+
end
22+
23+
@vim = vim
24+
end
25+
26+
def get_ctags_convertion(content, leave='v')
27+
File.write(@file, content) if content
28+
29+
@vim.command "let b:content=readfile('#{@file}')"
30+
@vim.command 'echo join(vimide#elixir#tagbarFilter(b:content), "\n")'
31+
end
32+
33+
def messages_clear
34+
@vim.command ':messages clear'
35+
end
36+
37+
def messages
38+
@vim.command ':messages'
39+
end
40+
41+
private
42+
43+
def with_file(content = nil)
44+
edit_file(content)
45+
46+
yield if block_given?
47+
48+
@vim.write
49+
IO.read(@file)
50+
end
51+
52+
def edit_file(content)
53+
File.write(@file, content) if content
54+
55+
@vim.edit @file
56+
@vim.normal ':set ft=elixir<CR>'
57+
end
58+
59+
end
60+
61+
class Differ
62+
def self.diff(result, expected)
63+
instance.diff(result, expected)
64+
end
65+
66+
def self.instance
67+
@instance ||= new
68+
end
69+
70+
def initialize
71+
@differ = RSpec::Support::Differ.new(
72+
object_preparer: -> (object) do
73+
RSpec::Matchers::Composable.surface_descriptions_in(object)
74+
end,
75+
color: RSpec::Matchers.configuration.color?
76+
)
77+
end
78+
79+
def diff(result, expected)
80+
@differ.diff_as_object(result, expected)
81+
end
82+
end
83+
84+
RSpec::Matchers.define :be_ctags_output do |expected_result, options='v'|
85+
buffer = Buffer.new(VIM, :ctags)
86+
87+
expected_result = expected_result.gsub(/\n$/, '')
88+
89+
match do |code|
90+
#result = buffer.get_ctags_convertion(code)
91+
#p "-<<<<--------------------------"
92+
#p result
93+
#p "---------------------------"
94+
#p expected_result
95+
#p "->>>>--------------------------"
96+
#
97+
#byebug
98+
99+
buffer.get_ctags_convertion(code) == expected_result
100+
end
101+
102+
failure_message do |code|
103+
buffer.messages_clear
104+
result = buffer.get_ctags_convertion(code)
105+
messages = buffer.messages
106+
107+
<<~EOM
108+
Vim echo messages:
109+
#{messages}
110+
111+
Diff:
112+
#{Differ.diff(result, expected_result)}
113+
EOM
114+
end
115+
end
116+
117+
Vimrunner::RSpec.configure do |config|
118+
config.reuse_server = true
119+
120+
config.start_vim do
121+
VIM = Vimrunner.start_gvim
122+
VIM.add_plugin(File.expand_path('..', __dir__), 'autoload/vimide.vim')
123+
VIM.add_plugin(File.expand_path('..', __dir__), 'autoload/vimide/elixir.vim')
124+
VIM
125+
end
126+
end
127+
128+
RSpec.configure do |config|
129+
config.order = :random
130+
131+
# Run a single spec by adding the `focus: true` option
132+
config.filter_run_including focus: true
133+
config.run_all_when_everything_filtered = true
134+
end

0 commit comments

Comments
 (0)