test_parser 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
2
  .bundle
3
+ .rvmrc
3
4
  Gemfile.lock
4
5
  pkg/*
data/.rvmrc.example ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.2-p180@test_parser"
8
+
9
+ #
10
+ # First we attempt to load the desired environment directly from the environment
11
+ # file. This is very fast and efficicent compared to running through the entire
12
+ # CLI and selector. If you want feedback on which environment was used then
13
+ # insert the word 'use' after --create as this triggers verbose mode.
14
+ #
15
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
16
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]] ; then
17
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
18
+ else
19
+ # If the environment file has not yet been created, use the RVM CLI to select.
20
+ rvm --create use "$environment_id"
21
+ fi
22
+
23
+ #
24
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
25
+ # it be automatically loaded. Uncomment the following and adjust the filename if
26
+ # necessary.
27
+ #
28
+ # filename=".gems"
29
+ # if [[ -s "$filename" ]] ; then
30
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
31
+ # fi
32
+
33
+ #
34
+ # If you use bundler and would like to run bundle each time you enter the
35
+ # directory, you can uncomment the following code.
36
+ #
37
+ # # Ensure that Bundler is installed. Install it if it is not.
38
+ # if ! command -v bundle >/dev/null; then
39
+ # printf "The rubygem 'bundler' is not installed. Installing it now.\n"
40
+ # gem install bundler
41
+ # fi
42
+ #
43
+ # # Bundle while reducing excess noise.
44
+ # printf "Bundling your gems. This may take a few minutes on a fresh clone.\n"
45
+ # bundle | grep -v '^Using ' | grep -v ' is complete' | sed '/^$/d'
46
+ #
47
+
data/bin/test_parser CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "test_parser"
4
- require 'yaml'
3
+ require "test_parser/command_line"
5
4
 
6
- $stdout << TestParser.all_tests(ARGV.last).to_yaml
5
+ TestParser::CommandLine.new(ARGV).run
data/lib/test_parser.rb CHANGED
@@ -1,25 +1,22 @@
1
- require 'test_parser/common'
2
- require 'test_parser/minitest'
3
- require 'test_parser/rspec'
1
+ require 'test_parser/parser/common'
2
+ require 'test_parser/parser/minitest'
3
+ require 'test_parser/parser/rspec'
4
4
 
5
5
  module TestParser
6
6
 
7
+ DEFAULT_PARSERS = [Parser::MiniTest, Parser::RSpec]
8
+
7
9
  def all_tests(path, options = {})
8
- frameworks = options[:frameworks] ||= [:rspec, :minitest]
9
- path = sanitize_path(path)
10
- frameworks.collect_concat do |framework|
11
- send "#{framework}_tests", path, options[framework]
12
- end
13
- end
10
+ parsers = options[:parsers] ||= DEFAULT_PARSERS
11
+ path = sanitize_path(path)
12
+ libs = options[:libs] || []
14
13
 
15
- def minitest_tests(path, options = nil)
16
- options ||= {}
17
- MiniTest.find_tests!(path, options)
18
- end
14
+ libs << 'spec'
15
+ add_libs_to_load_path(path, libs)
19
16
 
20
- def rspec_tests(path, options = nil)
21
- options ||= {}
22
- RSpec.find_tests!(path, options)
17
+ parsers.collect_concat do |parser|
18
+ parser.find_tests(path, options[parser.type] || {})
19
+ end
23
20
  end
24
21
 
25
22
  def require_all(path, glob)
@@ -28,8 +25,19 @@ module TestParser
28
25
 
29
26
  def sanitize_path(path)
30
27
  return path if path.is_a? Pathname
28
+
31
29
  Pathname.new(path).expand_path
32
30
  end
33
31
 
34
32
  extend self
33
+
34
+ private
35
+
36
+ def add_libs_to_load_path(path, libs)
37
+ libs.each do |lib|
38
+ dir = (path + lib).expand_path.to_s
39
+ $LOAD_PATH.unshift(dir) unless $LOAD_PATH.include?(dir)
40
+ end
41
+ end
42
+
35
43
  end
@@ -0,0 +1,101 @@
1
+ require 'optparse'
2
+ require 'test_parser'
3
+
4
+ require 'test_parser/test_formatter/yaml'
5
+
6
+ module TestParser
7
+ class CommandLine
8
+
9
+ attr_reader :stdout, :stderr
10
+
11
+ def initialize(options, stdout = $stdout, stderr = $stderr)
12
+ @stdout, @stderr = stdout, stderr
13
+ @registered_values = []
14
+ @libs = []
15
+ configure_options(options)
16
+ end
17
+
18
+ def run
19
+ tests = TestParser.all_tests(test_suite_path, parse_options)
20
+ output_stream << format_tests(tests)
21
+ end
22
+
23
+ def parse_options
24
+ {
25
+ :libs => @libs
26
+ }
27
+ end
28
+
29
+ def test_suite_path
30
+ @test_suite_path || '.'
31
+ end
32
+
33
+ def output_stream
34
+ $stdout
35
+ end
36
+
37
+ def format_tests(tests)
38
+ @formater ||= yaml_formatter
39
+ @formater.format(tests)
40
+ end
41
+
42
+ private
43
+
44
+ def yaml_formatter
45
+ TestFormatter::YAML
46
+ end
47
+
48
+ def configure_options(options)
49
+ OptionParser.new do |parser|
50
+ parser.banner = "Usage: test_parser [options] [directory]"
51
+
52
+ parser.on('-I DIRECTORY', 'specify $LOAD_PATH directory (may be used more than once)') do |dir|
53
+ append_libs(dir)
54
+ end
55
+
56
+ parser.on('-f', '--format FORMATTER', 'Choose a formatter',
57
+ ' y[aml] - YAML format') do |o|
58
+ set_formater(o)
59
+ end
60
+
61
+ parser.on_tail('-h', '--help', "This is it!") do
62
+ puts parser
63
+ exit
64
+ end
65
+
66
+ end.parse!(options)
67
+
68
+ extract_test_suite_path(options)
69
+ end
70
+
71
+ def append_libs(libs)
72
+ @libs += libs.split(':')
73
+ end
74
+
75
+ def extract_test_suite_path(args)
76
+ @test_suite_path = args.pop
77
+
78
+ unless args.empty?
79
+ log_err "Unknown use for values: #{args.inspect}"
80
+ end
81
+ end
82
+
83
+ def puts(*args)
84
+ stdout.puts *args
85
+ end
86
+
87
+ def log_err(*args)
88
+ stderr.puts *args
89
+ end
90
+
91
+ def set_formater(format)
92
+ case format
93
+ when /y(aml)?/i
94
+ @formatter = yaml_formatter
95
+ else
96
+ log_err "Unknown formatter #{format}"
97
+ end
98
+ end
99
+
100
+ end
101
+ end
@@ -0,0 +1,17 @@
1
+ require 'test_parser/test_information'
2
+
3
+ module TestParser
4
+ module Parser
5
+ module Common
6
+
7
+ def build_test
8
+ TestInformation.new(parser_type, identification, snippet_source)
9
+ end
10
+
11
+ def parser_type
12
+ self.class.type
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,51 @@
1
+ require 'minitest/unit'
2
+
3
+ require 'test_parser/parser/common'
4
+ require 'test_parser/extensions/minitest'
5
+ require 'test_parser/snippet_source/method'
6
+
7
+ module TestParser
8
+ module Parser
9
+ class MiniTest
10
+
11
+ attr_reader :klass, :test_method
12
+
13
+ def self.type
14
+ :minitest
15
+ end
16
+
17
+ def self.find_tests(path, options = {})
18
+ glob = options[:glob] || 'test/**/*_test.rb'
19
+
20
+ ::MiniTest::Unit.dont_install_at_exit!
21
+
22
+ TestParser.require_all(path, glob)
23
+
24
+ ::MiniTest::Unit::TestCase.test_suites.collect_concat do |klass|
25
+ klass.test_methods.map do |test|
26
+ new(klass, test).build_test
27
+ end
28
+ end
29
+ end
30
+
31
+ include Common
32
+
33
+ def initialize(klass, test_method)
34
+ @klass, @test_method = klass, test_method
35
+ end
36
+
37
+ def identification
38
+ "#{klass.name}##{test_method}"
39
+ end
40
+
41
+ def file_name
42
+ klass.instance_method(test_method).source_location.first
43
+ end
44
+
45
+ def snippet_source
46
+ @snippet_source ||= SnippetSource::Method.new(file_name, test_method)
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,78 @@
1
+ require 'rspec/core'
2
+
3
+ require 'test_parser/parser/common'
4
+ require 'test_parser/extensions/rspec'
5
+ require 'test_parser/snippet_source/line_number'
6
+
7
+ module TestParser
8
+ module Parser
9
+ class RSpec
10
+
11
+ attr_reader :example
12
+
13
+ def self.type
14
+ :rspec2
15
+ end
16
+
17
+ def self.find_tests(path, options = {})
18
+ glob = options[:glob] || 'spec/**/*_spec.rb'
19
+
20
+ ::RSpec.with_world ::RSpec::Core::World.new do |world|
21
+
22
+ TestParser.require_all(path, glob)
23
+
24
+ groups = world.example_groups
25
+ examples = groups.collect_concat(&:descendant_filtered_examples)
26
+ examples.map do |example|
27
+ new(example).build_test
28
+ end
29
+ end
30
+ end
31
+
32
+ include Common
33
+
34
+ def initialize(example)
35
+ @example = example
36
+ end
37
+
38
+ def identification
39
+ @identification ||= path_to_example.join('/')
40
+ end
41
+
42
+ def snippet_source
43
+ @snippet_source ||= SnippetSource::LineNumber.new(file_name, line_number)
44
+ end
45
+
46
+ def snippet
47
+ @snippet ||= snippet_source.snippet
48
+ end
49
+
50
+ def file_name
51
+ example.file_path
52
+ end
53
+
54
+ def line_number
55
+ example.metadata[:line_number]
56
+ end
57
+
58
+ private
59
+
60
+ def path_to_example
61
+ example_groups_names + [example_description]
62
+ end
63
+
64
+ def example_groups_names
65
+ example.example_group.ancestors.map(&:display_name).reverse
66
+ end
67
+
68
+ def example_description
69
+ unless example.description.empty?
70
+ example.description
71
+ else
72
+ snippet.get_block.to_code
73
+ end
74
+ end
75
+
76
+ end
77
+ end
78
+ end
@@ -1,10 +1,17 @@
1
1
  require 'ruby2ruby'
2
2
 
3
3
  module TestParser
4
- class Snippet < Struct.new(:sexp)
4
+ class Snippet
5
+
6
+ attr_reader :sexp
7
+
8
+ def initialize(sexp)
9
+ raise ArgumentError unless sexp
10
+ @sexp = sexp
11
+ end
5
12
 
6
13
  def to_code
7
- @to_code ||= Ruby2Ruby.new.process(self.sexp.deep_clone)
14
+ @to_code ||= Ruby2Ruby.new.process(sexp.deep_clone)
8
15
  end
9
16
 
10
17
  def get_block
@@ -0,0 +1,13 @@
1
+ require 'test_parser/source_code'
2
+
3
+ module TestParser
4
+ module SnippetSource
5
+ module Common
6
+
7
+ def source_code
8
+ SourceCode.for(file)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'common'
2
+
3
+ module TestParser
4
+ module SnippetSource
5
+ class LineNumber
6
+
7
+ attr_reader :file, :line_number
8
+
9
+ include Common
10
+
11
+ def initialize(file, line_number)
12
+ @file, @line_number = file, line_number
13
+ end
14
+
15
+ def snippet
16
+ source_code.extract_code_from_line(line_number)
17
+ rescue NoCodeInLineError
18
+ raise "File: #{@file}:#{@line_number}"
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'common'
2
+
3
+ module TestParser
4
+ module SnippetSource
5
+ class Method
6
+
7
+ attr_reader :file, :method_name
8
+
9
+ include Common
10
+
11
+ def initialize(file, method_name)
12
+ @file, @method_name = file, method_name
13
+ end
14
+
15
+ def snippet
16
+ source_code.extract_method(method_name)
17
+ end
18
+
19
+ end
20
+ end
21
+ end
22
+
@@ -3,6 +3,10 @@ require 'test_parser/snippet'
3
3
 
4
4
  module TestParser
5
5
 
6
+ SourceCodeError = Class.new(StandardError)
7
+ NoMethodFoundError = Class.new(SourceCodeError)
8
+ NoCodeInLineError = Class.new(SourceCodeError)
9
+
6
10
  class SourceCode < Snippet
7
11
 
8
12
  def self.source_codes
@@ -26,15 +30,36 @@ module TestParser
26
30
  name = name.to_sym
27
31
  definitions = sexp.enum_for(:each_of_type, :defn)
28
32
  method_sexp = definitions.find {|def_sexp| def_sexp[1] == name }
33
+
34
+ raise NoMethodFoundError unless method_sexp
35
+
29
36
  Snippet.new(method_sexp)
30
37
  end
31
38
 
32
39
  def extract_code_from_line(line_number)
33
- method_calls = sexp.enum_for(:each_of_type, :iter)
34
- code_sexp = method_calls.find {|sexp| sexp[1].line == line_number }
40
+
41
+ code_sexp = find_method_call_with_block(line_number)
42
+ code_sexp ||= find_method_call_without_block(line_number)
43
+
44
+ raise NoCodeInLineError unless code_sexp
45
+
35
46
  Snippet.new(code_sexp)
36
47
  end
37
48
 
49
+ private
50
+
51
+ def find_method_call_with_block(line_number)
52
+ method_calls = sexp.enum_for(:each_of_type, :iter)
53
+ method_calls.find {|sexp| sexp[1].line == line_number }
54
+ end
55
+
56
+ def find_method_call_without_block(line_number)
57
+ method_calls = sexp.enum_for(:each_of_type, :call)
58
+ method_calls.find do |sexp|
59
+ sexp[3][1] && sexp[3][1].line == line_number
60
+ end
61
+ end
62
+
38
63
  end
39
64
 
40
65
  end
@@ -0,0 +1,11 @@
1
+ require 'yaml'
2
+
3
+ module TestFormatter
4
+ module YAML
5
+ extend self
6
+
7
+ def format(tests)
8
+ tests.to_yaml
9
+ end
10
+ end
11
+ end
@@ -1,14 +1,20 @@
1
1
  module TestParser
2
- class TestInformation < Struct.new(:type, :identification, :file, :extras)
2
+ class TestInformation
3
+
4
+ attr_reader :type, :identification, :snippet_source
5
+
6
+ def initialize(type, identification, snippet_source)
7
+ @type = type
8
+ @identification = identification
9
+ @snippet_source = snippet_source
10
+ end
3
11
 
4
12
  def snippet
5
- source_code = SourceCode.for(file)
6
- case type
7
- when :rspec2
8
- source_code.extract_code_from_line(extras[:line_number])
9
- when :minitest
10
- source_code.extract_method(extras[:method_name])
11
- end
13
+ snippet_source.snippet
14
+ end
15
+
16
+ def file
17
+ snippet_source.file
12
18
  end
13
19
 
14
20
  end
@@ -1,3 +1,3 @@
1
1
  module TestParser
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -2,6 +2,7 @@ require 'spec_helper'
2
2
  require 'test_parser/source_code'
3
3
 
4
4
  describe TestParser::SourceCode do
5
+
5
6
  include FakeFS::SpecHelpers
6
7
 
7
8
  before(:each) do
@@ -18,13 +19,16 @@ describe TestParser::SourceCode do
18
19
  end
19
20
 
20
21
  it 'has the sexp of the original file' do
21
- sexp = RubyParser.new.parse standard_class_source
22
- TestParser::SourceCode.for('standard_class_source.rb').sexp.should == sexp
22
+ sexp = RubyParser.new.parse standard_class_source
23
+ source_code = TestParser::SourceCode.for('standard_class_source.rb')
24
+
25
+ source_code.sexp.should == sexp
23
26
  end
24
27
 
25
28
  describe '.extract_method' do
26
29
  it 'finds the method definition from the method name' do
27
- snippet = TestParser::SourceCode.for('standard_class_source.rb').extract_method('method')
30
+ source_code = TestParser::SourceCode.for('standard_class_source.rb')
31
+ snippet = source_code.extract_method('method')
28
32
  snippet.to_code.should match_code(<<-METHOD)
29
33
  def method(some)
30
34
  puts some
@@ -35,13 +39,19 @@ describe TestParser::SourceCode do
35
39
 
36
40
  describe '.extract_code_from_line' do
37
41
  it 'finds the line with its block' do
38
- snippet = TestParser::SourceCode.for('dsl_source.rb').extract_code_from_line(2)
42
+ source_code = TestParser::SourceCode.for('dsl_source.rb')
43
+ snippet = source_code.extract_code_from_line(2)
39
44
  snippet.to_code.should match_code(<<-METHOD)
40
45
  method 'ble' do
41
46
  testing_something
42
47
  end
43
48
  METHOD
44
49
  end
50
+ it 'finds the line without its block' do
51
+ source_code = TestParser::SourceCode.for('dsl_source.rb')
52
+ snippet = source_code.extract_code_from_line(5)
53
+ snippet.to_code.should match_code(%{pending 'ble'})
54
+ end
45
55
  end
46
56
 
47
57
  def standard_class_source
@@ -60,6 +70,7 @@ describe TestParser::SourceCode do
60
70
  method 'ble' do
61
71
  testing_something
62
72
  end
73
+ pending "ble"
63
74
  end
64
75
  SOURCE
65
76
  end
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,8 @@ require 'bundler'
2
2
  Bundler.setup
3
3
  Bundler.require :default, :test
4
4
 
5
- Dir[File.join(File.dirname(__FILE__), 'support', '**', '*.rb')].each {|f| require f }
5
+ support_glob = File.join(File.dirname(__FILE__), 'support', '**', '*.rb')
6
+ Dir[support_glob].each {|f| require f }
6
7
 
7
8
  require 'fakefs/spec_helpers'
8
9
 
@@ -1,10 +1,13 @@
1
1
  RSpec::Matchers.define :match_code do |expected|
2
- expected_sexp = RubyParser.new.parse(expected)
3
2
 
4
3
  match do |actual|
5
- actual_sexp = actual.sexp if actual.respond_to? :sexp
6
- actual_sexp ||= RubyParser.new.parse(actual)
7
- expected_sexp == actual_sexp
4
+ sexp_from(expected) == sexp_from(actual)
5
+ end
6
+
7
+ def sexp_from(obj)
8
+ return obj.sexp if obj.respond_to? :sexp
9
+
10
+ RubyParser.new.parse(obj)
8
11
  end
9
12
 
10
13
  end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ require 'test_parser/parser/minitest'
4
+
5
+ describe TestParser::Parser::MiniTest do
6
+
7
+ include TestParser::Parser
8
+
9
+ describe '.find_tests!' do
10
+
11
+ context 'within test_project' do
12
+
13
+ before(:all) do
14
+ @tests = MiniTest.find_tests path_for_test_project
15
+ @test_identifications = @tests.map(&:identification)
16
+ end
17
+
18
+ it 'finds all the tests' do
19
+ @tests.should have(2).tests
20
+ end
21
+
22
+ it 'finds normally declared test' do
23
+ @test_identifications.should include('TestSomething#test_foo')
24
+ test = @tests[@test_identifications.index('TestSomething#test_foo')]
25
+
26
+ expected_file = 'test/example_test.rb'
27
+
28
+ test.file.should == path_for(expected_file)
29
+ test.snippet.should match_code(snippet_for(expected_file, 'test_foo'))
30
+ end
31
+
32
+ it 'finds module shared tests' do
33
+ @test_identifications.should include('TestSomething#test_truth')
34
+ test = @tests[@test_identifications.index('TestSomething#test_truth')]
35
+
36
+ expected_file = 'test/test_helper.rb'
37
+
38
+ test.file.should == path_for(expected_file)
39
+ test.snippet.should match_code(snippet_for(expected_file, 'test_truth'))
40
+ end
41
+
42
+ def path_for(file)
43
+ path_for_test_project(file).to_s
44
+ end
45
+
46
+ def snippet_for(file, method_name)
47
+ TestParser::SourceCode.for(path_for(file)).extract_method(method_name)
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ require 'test_parser/parser/rspec'
4
+
5
+ describe TestParser::Parser::RSpec do
6
+ describe '.find_tests' do
7
+ context 'within test_project' do
8
+
9
+ include TestParser::Parser
10
+
11
+ before(:all) do
12
+ @test_suite = RSpec.find_tests(path_for_test_project)
13
+ @test_names = @test_suite.map(&:identification)
14
+ end
15
+
16
+ it 'finds all the test_suite' do
17
+ @test_suite.should have(2).test_suite
18
+ end
19
+
20
+ it 'finds nested test_suite' do
21
+ @test_names.should include('Life/with death sentence/should(be_alive)')
22
+ test = @test_suite[@test_names.index('Life/with death sentence/should(be_alive)')]
23
+
24
+ test.file.should == path_for('spec/example_spec.rb')
25
+ test.snippet.should match_code(snippet_for('spec/example_spec.rb', 11))
26
+ end
27
+
28
+ it 'finds not nested test_suite' do
29
+ @test_names.should include('Life/actually lives')
30
+ test = @test_suite[@test_names.index('Life/actually lives')]
31
+
32
+ test.file.should == path_for('spec/example_spec.rb')
33
+ test.snippet.should match_code(snippet_for('spec/example_spec.rb', 5))
34
+ end
35
+
36
+ def path_for(file)
37
+ path_for_test_project(file).to_s
38
+ end
39
+
40
+ def snippet_for(file, line_number)
41
+ TestParser::SourceCode.for(path_for(file)).extract_code_from_line(line_number)
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -3,58 +3,30 @@ require 'spec_helper'
3
3
  require 'test_parser'
4
4
 
5
5
  describe TestParser do
6
-
7
- describe ".minitest_tests" do
8
- it 'delegates test finding to TestParser::MiniTest' do
9
- TestParser::MiniTest.should_receive(:find_tests!).
10
- with(path_for_test_project, {}).and_return []
11
-
12
- TestParser.minitest_tests(path_for_test_project)
13
- end
14
- it 'passes extra options to TestParser::Minitest' do
15
- TestParser::MiniTest.should_receive(:find_tests!).
16
- with(path_for_test_project, {:some_option => true}).and_return []
17
-
18
- TestParser.minitest_tests(path_for_test_project, {:some_option => true})
19
- end
20
- end
21
-
22
- describe ".rspec_tests" do
23
- it 'delegates test finding to TestParser::RSpec' do
24
- TestParser::RSpec.should_receive(:find_tests!).
25
- with(path_for_test_project, {}).and_return []
26
-
27
- TestParser.rspec_tests(path_for_test_project)
28
- end
29
- it 'passes extra options to TestParser::RSpec' do
30
- TestParser::RSpec.should_receive(:find_tests!).
31
- with(path_for_test_project, {:some_option => true}).and_return []
32
-
33
- TestParser.rspec_tests(path_for_test_project, {:some_option => true})
34
- end
35
- end
36
-
6
+
37
7
  describe ".all_tests" do
8
+
38
9
  it "collects all the test from the default frameworks" do
39
- stub_frameworks
10
+ stub_frameworks!
40
11
 
41
12
  tests = TestParser.all_tests(path_for_test_project)
42
- tests.should == [:rspec, :minitest]
13
+ tests.should =~ [:rspec, :minitest]
43
14
  end
44
15
 
45
- context "when :frameworks are specified" do
46
- it 'collects only for the given frameworks' do
47
- stub_frameworks
16
+ context "when :parsers are specified" do
17
+ it 'collects only for the given parsers' do
18
+ stub_frameworks!
48
19
 
49
- options = {:frameworks => [:rspec]}
20
+ options = {:parsers => [TestParser::Parser::RSpec]}
50
21
  tests = TestParser.all_tests(path_for_test_project, options)
51
22
  tests.should == [:rspec]
52
23
  end
53
24
  end
54
25
 
55
- def stub_frameworks
56
- TestParser::RSpec.stub(:find_tests!) { [:rspec]}
57
- TestParser::MiniTest.stub(:find_tests!) {[:minitest]}
26
+ def stub_frameworks!
27
+ TestParser::Parser::RSpec.stub(:find_tests) { [:rspec]}
28
+ TestParser::Parser::MiniTest.stub(:find_tests) {[:minitest]}
58
29
  end
30
+
59
31
  end
60
32
  end
data/test/rspec_test.rb CHANGED
@@ -14,11 +14,10 @@ class RSpecTest < MiniTest::Unit::TestCase
14
14
  include ProjectsPaths
15
15
 
16
16
  def test_rspec_parsing
17
- tests = TestParser::RSpec.find_tests!(path_for_test_project)
17
+ tests = TestParser::Parser::RSpec.find_tests(path_for_test_project)
18
18
  test = tests.first
19
19
  assert_equal 'Life/actually lives', test.identification
20
20
  assert_equal path_for_test_project('spec/example_spec.rb').to_s, test.file
21
- assert_equal 5, test.extras[:line_number]
22
21
  end
23
22
 
24
23
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: test_parser
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Juan Manuel Barreneche
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-20 00:00:00 -03:00
13
+ date: 2011-02-27 00:00:00 -03:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -69,25 +69,31 @@ extra_rdoc_files: []
69
69
  files:
70
70
  - .gitignore
71
71
  - .rvmrc
72
+ - .rvmrc.example
72
73
  - Gemfile
73
74
  - Rakefile
74
75
  - bin/test_parser
75
76
  - lib/test_parser.rb
76
- - lib/test_parser/common.rb
77
+ - lib/test_parser/command_line.rb
77
78
  - lib/test_parser/extensions/minitest.rb
78
79
  - lib/test_parser/extensions/rspec.rb
79
- - lib/test_parser/minitest.rb
80
- - lib/test_parser/rspec.rb
80
+ - lib/test_parser/parser/common.rb
81
+ - lib/test_parser/parser/minitest.rb
82
+ - lib/test_parser/parser/rspec.rb
81
83
  - lib/test_parser/snippet.rb
84
+ - lib/test_parser/snippet_source/common.rb
85
+ - lib/test_parser/snippet_source/line_number.rb
86
+ - lib/test_parser/snippet_source/method.rb
82
87
  - lib/test_parser/source_code.rb
88
+ - lib/test_parser/test_formatter/yaml.rb
83
89
  - lib/test_parser/test_information.rb
84
90
  - lib/test_parser/version.rb
85
- - spec/minitest_spec.rb
86
- - spec/rspec_spec.rb
87
91
  - spec/source_code_spec.rb
88
92
  - spec/spec_helper.rb
89
93
  - spec/support/matchers/match_code.rb
90
94
  - spec/support/projects_paths.rb
95
+ - spec/test_parser/minitest_spec.rb
96
+ - spec/test_parser/rspec_spec.rb
91
97
  - spec/test_parser_spec.rb
92
98
  - test/rspec_test.rb
93
99
  - test_parser.gemspec
@@ -126,11 +132,11 @@ signing_key:
126
132
  specification_version: 3
127
133
  summary: Retrieves information of your test suite
128
134
  test_files:
129
- - spec/minitest_spec.rb
130
- - spec/rspec_spec.rb
131
135
  - spec/source_code_spec.rb
132
136
  - spec/spec_helper.rb
133
137
  - spec/support/matchers/match_code.rb
134
138
  - spec/support/projects_paths.rb
139
+ - spec/test_parser/minitest_spec.rb
140
+ - spec/test_parser/rspec_spec.rb
135
141
  - spec/test_parser_spec.rb
136
142
  - test/rspec_test.rb
@@ -1,17 +0,0 @@
1
- require 'test_parser/source_code'
2
- require 'test_parser/test_information'
3
-
4
- module TestParser
5
-
6
- module Common
7
-
8
- def build_test(extras = {})
9
- TestInformation.new(parser_type, test_identification, test_file_name, extras)
10
- end
11
-
12
- def test_source_code
13
- SourceCode.for(test_file_name)
14
- end
15
-
16
- end
17
- end
@@ -1,48 +0,0 @@
1
- require 'minitest/unit'
2
- require_relative 'extensions/minitest'
3
-
4
- require 'test_parser'
5
-
6
- module TestParser
7
- class MiniTest
8
- attr_reader :klass, :test_method
9
-
10
- def self.find_tests!(path, options = {})
11
- glob = options[:glob] || 'test/**/*_test.rb'
12
-
13
- ::MiniTest::Unit.dont_install_at_exit!
14
-
15
- TestParser.require_all(path, glob)
16
-
17
- ::MiniTest::Unit::TestCase.test_suites.collect_concat do |klass|
18
- klass.test_methods.map do |test|
19
- new(klass, test).test_info
20
- end
21
- end
22
- end
23
-
24
- include Common
25
-
26
- def parser_type
27
- :minitest
28
- end
29
-
30
- def initialize(klass, test_method)
31
- @klass, @test_method = klass, test_method
32
- end
33
-
34
- def test_info
35
- build_test(:method_name => test_method)
36
- end
37
-
38
- def test_identification
39
- "#{klass.name}##{test_method}"
40
- end
41
-
42
- def test_file_name
43
- klass.instance_method(test_method).source_location.first
44
- end
45
-
46
- end
47
-
48
- end
@@ -1,68 +0,0 @@
1
- require 'rspec/core'
2
- require_relative 'extensions/rspec'
3
-
4
- require 'test_parser'
5
-
6
- module TestParser
7
- class RSpec
8
- attr_reader :example
9
-
10
- def self.find_tests!(path, options = {})
11
- glob = options[:glob] || 'spec/**/*_spec.rb'
12
-
13
- ::RSpec.with_world ::RSpec::Core::World.new do |world|
14
-
15
- TestParser.require_all(path, glob)
16
-
17
- groups = world.example_groups
18
- examples = groups.collect_concat(&:descendant_filtered_examples)
19
- examples.map do |example|
20
- new(example).test_info
21
- end
22
- end
23
- end
24
-
25
- include Common
26
-
27
- def initialize(example)
28
- @example = example
29
- end
30
-
31
- def parser_type
32
- :rspec2
33
- end
34
-
35
- def test_info
36
- build_test(:line_number => line_number)
37
- end
38
-
39
- def test_snippet
40
- @test_snippet ||= test_source_code.extract_code_from_line(line_number)
41
- end
42
-
43
- def test_file_name
44
- example.file_path
45
- end
46
-
47
- def test_identification
48
- (example_group_names.reverse + [example_description]).join('/')
49
- end
50
-
51
- def line_number
52
- example.metadata[:line_number]
53
- end
54
-
55
- def example_group_names
56
- example.example_group.ancestors.map(&:display_name)
57
- end
58
-
59
- def example_description
60
- unless example.description.empty?
61
- example.description
62
- else
63
- test_snippet.get_block.to_code
64
- end
65
- end
66
-
67
- end
68
- end
@@ -1,39 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'test_parser'
4
- describe TestParser::MiniTest do
5
- before(:all) do
6
- MiniTest::Unit::TestCase.reset
7
- end
8
- after(:all) do
9
- MiniTest::Unit::TestCase.reset
10
- end
11
-
12
- describe '.find_tests!' do
13
-
14
- context 'within test_project' do
15
- before(:all) do
16
- @tests = TestParser::MiniTest.find_tests! path_for_test_project
17
- @test_identifications = @tests.map(&:identification)
18
- end
19
-
20
- it 'finds all the tests' do
21
- @tests.should have(2).tests
22
- end
23
-
24
- it 'finds normally declared test' do
25
- @test_identifications.should include('TestSomething#test_foo')
26
- test = @tests[@test_identifications.index('TestSomething#test_foo')]
27
- test.file.should == (path_for_test_project('test/example_test.rb')).to_s
28
- test.extras[:method_name].should == 'test_foo'
29
- end
30
-
31
- it 'finds module shared tests' do
32
- @test_identifications.should include('TestSomething#test_truth')
33
- test = @tests[@test_identifications.index('TestSomething#test_truth')]
34
- test.file.should == (path_for_test_project('test/test_helper.rb')).to_s
35
- test.extras[:method_name].should == 'test_truth'
36
- end
37
- end
38
- end
39
- end
data/spec/rspec_spec.rb DELETED
@@ -1,35 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'test_parser/rspec'
4
-
5
- describe TestParser::RSpec do
6
- describe '.find_test_suite!' do
7
- context 'within test_project' do
8
-
9
- before(:all) do
10
- @test_suite = TestParser::RSpec.find_tests!(path_for_test_project)
11
- @test_names = @test_suite.map(&:identification)
12
- end
13
-
14
- it 'finds all the test_suite' do
15
- @test_suite.should have(2).test_suite
16
- end
17
-
18
- it 'finds nested test_suite' do
19
- @test_names.should include('Life/with death sentence/should(be_alive)')
20
- test = @test_suite[@test_names.index('Life/with death sentence/should(be_alive)')]
21
- test.file.should == (path_for_test_project('spec/example_spec.rb')).to_s
22
- test.extras[:line_number].should == 11
23
- end
24
-
25
- it 'finds not nested test_suite' do
26
- @test_names.should include('Life/actually lives')
27
- test = @test_suite[@test_names.index('Life/actually lives')]
28
-
29
- test.file.should == (path_for_test_project('spec/example_spec.rb')).to_s
30
- test.extras[:line_number].should == 5
31
- end
32
-
33
- end
34
- end
35
- end