Module: Maxitest::Testrbl

Defined in:
lib/maxitest/vendor/testrbl.rb

Constant Summary collapse

PATTERNS =
[
  /^(\s+)(should|test|it)(\s+|\s*\(\s*)['"](.*)['"](\s*\))?\s+do\s*(?:#.*)?$/,
  /^(\s+)(context|describe)(\s+|\s*\(\s*)['"]?(.*?)['"]?(\s*\))?\s+do\s*(?:#.*)?$/,
  /^(\s+)def(\s+)(test_)([a-z_\d]+)\s*(?:#.*)?$/
]
OPTION_WITH_ARGUMENT =
["-I", "-r", "-n", "--name", "-e", "--exclude", "-s", "--seed"]
INTERPOLATION =
/\\\#\\\{.*?\\\}/

Class Method Summary collapse

Class Method Details

.filter_duplicate_final(patterns) ⇒ Object

only keep 1 pattern that stops matching via $



88
89
90
91
# File 'lib/maxitest/vendor/testrbl.rb', line 88

def filter_duplicate_final(patterns)
  found_final = 0
  patterns.reject { |p| p.end_with?("$") and (found_final += 1) > 1 }
end

.line_pattern_option(file, line) ⇒ Object

overwritten by maxitest to just return line



66
67
68
# File 'lib/maxitest/vendor/testrbl.rb', line 66

def line_pattern_option(file, line)
  [file, "-n", "/#{pattern_from_file(File.readlines(file), line)}/"]
end

.pattern_from_file(lines, line) ⇒ Object

usable via external tools like zeus



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/maxitest/vendor/testrbl.rb', line 71

def pattern_from_file(lines, line)
  possible_lines = lines[0..(line.to_i-1)].reverse

  found = possible_lines.map { |line| test_pattern_from_line(line) || block_start_from_line(line) }.compact

  # pattern and the groups it is nested under (like describe - describe - it)
  last_spaces = " " * 100
  patterns = found.select do |spaces, name|
    last_spaces = spaces if spaces.size < last_spaces.size
  end.map(&:last).compact

  return filter_duplicate_final(patterns).reverse.join(".*") if found.size > 0

  raise "no test found before line #{line}"
end

.run_from_cli(argv) ⇒ Object



20
21
22
23
24
25
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
58
59
60
61
62
63
# File 'lib/maxitest/vendor/testrbl.rb', line 20

def run_from_cli(argv)
  if argv == ["-v"] || argv == ["--version"]
    puts Maxitest::VERSION
    exit 0
  end

  if argv == ["-h"] || argv == ["--help"]
    puts <<~EOF
      test given file, folder or file:line
      Usage: mtest foo_test.rb

      -v to run without bracktrace cleaner
      --changed to run all all tests in current `git diff` or `git show`
    EOF
    exit 0
  end

  files, options = partition_argv(argv)
  files.concat(changed_files) if options.delete("--changed")
  files = ["test"] if files.empty?
  files = files.map { |f| localize(f) }
  load_options, options = partition_options(options)

  if files.size == 1 and files.first =~ /^(\S+):(\d+)$/
    file = $1
    line = $2
    run(ruby + load_options + line_pattern_option(file, line) + options)
  else
    if files.size == 1 and File.file?(files.first)
      run(ruby + load_options + files + options)
    elsif options.none? { |arg| arg =~ /^-n/ }
      seed = if seed = options.index("--seed")
        ["--"] + options.slice!(seed, 2)
      else
        []
      end
      files = files.map { |f| File.directory?(f) ? all_test_files_in(f) : f }.flatten
      run(ruby + load_options + files.map { |f| "-r#{f}" } + options + ["-e", ""] + seed)
    else # pass though
      # no bundle exec: projects with mini and unit-test do not run well via bundle exec testrb
      run ["testrb"] + argv
    end
  end
end