Class: TestLauncher::Frameworks::Mochajs::Searcher

Inherits:
Base::Searcher show all
Defined in:
lib/test_launcher/frameworks/mochajs.rb

Instance Attribute Summary

Attributes inherited from Base::Searcher

#raw_searcher

Instance Method Summary collapse

Methods inherited from Base::Searcher

#examples, #grep, #initialize, #test_files

Constructor Details

This class inherits a constructor from TestLauncher::Frameworks::Base::Searcher

Instance Method Details

#by_line(file_pattern, line_number) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/test_launcher/frameworks/mochajs.rb', line 26

def (file_pattern, line_number)
  files = test_files(file_pattern)
  return [] unless files.any?
  raise multiple_files_error if files.size > 1
  #
  file = files.first
  grep_results = raw_searcher.grep(example_name_regex, file_pattern: file)

  if grep_results.empty?
    # the file exists, but doesn't appear to contain any tests...
    # we'll try to run it anyway
    return [file: file]
  end

  best_result =
    grep_results
      .select {|r| line_number >= r[:line_number]}
      .min_by {|r| line_number - r[:line_number]}

  if best_result
    [{
      file: best_result[:file],
      example_name: best_result[:line].match(/(it|describe)\(["'](.*)['"],/)[2],
      line_number: best_result[:line_number]
    }]
  else
    # line number outside of example. Run whole file
    [{
      file: grep_results.first[:file]
    }]
  end
end