Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring to improve maintainability #32

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.swp
Gemfile.lock
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--colour
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source 'https://rubygems.org'

gem 'nokogiri', '1.6.7.2'

gem 'rspec', '3.4.0'
gem 'rspec-its', '1.2.0'
gem 'pry-nav', '0.2.4'
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,27 @@ HISTORY

INSTALL
-------
* Requires: gem install hpricot
* Install with pathogen: clone/submodule into vim/bundle
* **rbenv** users will need to install hpricot using system ruby `RBENV_VERSION=system sudo gem install hpricot`

# External dependencies

Because of ending of `hpricot` gem maintenance was made decision about moving into `nokogiri`. So this gem is required for `vim-rspec` work.
You may install it manually by run `gem install nokogiri` or go into plugin folder and run `bundle`.

# VIM plugin managers

## Pathogen
Install with pathogen: clone/submodule into vim/bundle

## VIM-PLUG
Added into `~/.vimrc` the following line:

```
call plug#begin('~/.vim/plugged')

Plug '<path-to-repo>'

call plug#end()
```

USAGE
-----
Expand Down
33 changes: 0 additions & 33 deletions plugin/lib/context_renderer.rb

This file was deleted.

35 changes: 15 additions & 20 deletions plugin/lib/failure_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
# -*- encoding : utf-8 -*-
require_relative 'string_util'

class FailureRenderer
include StringUtil

def initialize(failure)
@failure = failure
attr_reader :example

def initialize(example)
@example = example
end

def render
puts failure_message
puts failure_location
puts backtrace_details
end

private

def indent(msg)
" #{msg}"
def failure_message
indent(unescape(example.failure_message))
end

def failure_location
unescape(
(@failure/"div[@class='backtrace']/pre").inner_html.split("\n").map { |line| "#{indent(line.strip)}" }.join("\n")
example.failure_location.split("\n")
.map { |line| "#{indent(line.strip)}" }
.join("\n")
)
end

def failure_message
indent(unescape((@failure/"div[@class~='message']/pre").inner_html.gsub(/\n/,'').gsub(/\s+/,' ')))
end

def backtrace_details
unescape(
backtrace_lines.map do |elem|
example.backtrace_lines.map do |elem|
linenum = elem[1]
code = elem[3].chomp
code = strip_html_spans(code)
" #{linenum}: #{code}\n"
end.join
)
end

def backtrace_lines
(@failure/"pre[@class='ruby']/code").inner_html.scan(/(<span class="linenum">)(\d+)(<\/span>)(.*)/).reject { |line| line[3] =~ ignore_line_if_matches }
end

def ignore_line_if_matches
/install syntax to get syntax highlighting/
end

end
37 changes: 37 additions & 0 deletions plugin/lib/rspec_context_renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# Renders example group
#
# Parameters:
# example_group - an html representation of an rspec example_group from rspec output
#
class RSpecContextRenderer
attr_reader :example_group


def initialize(example_group)
@example_group = example_group
end

def render
render_example_group_header
render_specs
puts " "
end

private

def render_example_group_header
puts "[#{example_group.header_text}]"
end

def render_specs
example_group.examples.each do |example|
render_spec_descriptor(example)
FailureRenderer.new(example).render if example.failure?
end
end

def render_spec_descriptor(example)
puts "#{example.status_sign} #{example.header_text}"
end
end
26 changes: 26 additions & 0 deletions plugin/lib/rspec_example_group_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'nokogiri'
require_relative 'rspec_example_result'

class RspecExampleGroupResult
attr_reader :context

def initialize(context)
@context = context
end

def header_text
fetch_header_node.inner_html
end

def examples
context.css('dd').map do |context|
RspecExampleResult.new(context)
end
end

private

def fetch_header_node
context.css('dl dt').first
end
end
56 changes: 56 additions & 0 deletions plugin/lib/rspec_example_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'nokogiri'
require_relative './string_util.rb'

class RspecExampleResult
include ::StringUtil

CLASSES_TO_SIGN_MAPPING = {"passed"=>"+","failed"=>"-","not_implemented"=>"#"}

attr_reader :context

def initialize(context)
@context = context
end

def failure?
context['class'] =~ /failed/
end

def header_text
context.css('span:first').first.inner_html
end

def html_class
context['class'].gsub(/(?:example|spec) /,'')
end

def failure_message
context.css('div.message > pre').first.inner_html
.gsub(/\n/,'').gsub(/\s+/,' ')
end

def failure_location
unescape(
context.css('div.backtrace > pre').first.inner_html.split("\n").map do |line|
"#{indent(line.strip)}"
end.join("\n")
)
context.css('div.backtrace > pre').first.inner_html
end

def backtrace_lines
context.css('pre.ruby > code').first.inner_html
.scan(/(<span class="linenum">)(\d+)(<\/span>)(.*)/)
.reject { |line| line[3] =~ ignore_line_if_matches }
end

def status_sign
CLASSES_TO_SIGN_MAPPING[html_class]
end

private

def ignore_line_if_matches
/install syntax to get syntax highlighting/
end
end
54 changes: 54 additions & 0 deletions plugin/lib/rspec_test_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'nokogiri'
require_relative 'rspec_example_group_result'

class RspecTestResult
attr_reader :html, :doc

def initialize(html)
@html = html
@doc = Nokogiri::HTML(html)
end

def duration_str
fetch_duration_node.inner_html.scan(/".*"/).first.gsub(/<\/?strong>/,"").gsub(/\"/,'')
end

def examples_count
fetch_counts_str.match(/(\d+) example/)[1].to_i rescue 0
end

def failures_count
fetch_counts_str.match(/(\d+) failure/)[1].to_i rescue 0
end

def pending_count
fetch_counts_str.match(/(\d+) pending/)[1].to_i rescue 0
end

def example_groups
doc.css('div.example_group').map do |context|
RspecExampleGroupResult.new(context)
end
end

private

def fetch_counts_str
fetch_counts_node
.inner_html.scan(/".*"/).first
end

def fetch_counts_node
fetch_script_node_with_inner_pattern(/totals/)
end

def fetch_duration_node
fetch_script_node_with_inner_pattern(/duration/)
end

def fetch_script_node_with_inner_pattern(pattern)
doc.css("script")
.select { |script| script.inner_html =~ pattern }
.first
end
end
4 changes: 4 additions & 0 deletions plugin/lib/string_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ def strip_html_spans(code)
def unescape(html)
CGI.unescapeHTML(html)
end

def indent(msg)
" #{msg}"
end
end
Loading