module TestProf::RSpecStamp

Mark RSpec examples with provided tags

Constants

EXAMPLE_RXP

Public Class Methods

apply_tags(code, lines, tags) click to toggle source

Accepts source code (as array of lines), line numbers (of example to apply tags) and an array of tags.

# File lib/test_prof/rspec_stamp.rb, line 105
def apply_tags(code, lines, tags)
  failed = 0

  lines.each do |line|
    unless stamp_example(code[line - 1], tags)
      failed += 1
      log :warn, "Failed to stamp: #{code[line - 1]}"
    end
  end
  failed
end
config() click to toggle source
# File lib/test_prof/rspec_stamp.rb, line 94
def config
  @config ||= Configuration.new
end
configure() { |config| ... } click to toggle source
# File lib/test_prof/rspec_stamp.rb, line 98
def configure
  yield config
end

Private Class Methods

quote(str) click to toggle source

rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/PerceivedComplexity

# File lib/test_prof/rspec_stamp.rb, line 172
def quote(str)
  return str unless str.is_a?(String)
  if str.include?("'")
    "\"#{str}\""
  else
    "'#{str}'"
  end
end
stamp_example(example, tags) click to toggle source

rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity

# File lib/test_prof/rspec_stamp.rb, line 121
def stamp_example(example, tags)
  matches = example.match(EXAMPLE_RXP)
  return false unless matches

  code = matches[2]
  block = matches[3]

  parsed = Parser.parse(code)
  return false unless parsed

  desc = parsed.desc_const || quote(parsed.desc || "works")

  tags.each do |t|
    if t.is_a?(Hash)
      t.each_key do |k|
        parsed.remove_tag(k)
        parsed.add_htag(k, t[k])
      end
    else
      parsed.remove_tag(t)
      parsed.add_tag(t)
    end
  end

  need_parens = block == "{"

  tags_str = parsed.tags.map { |t| t.is_a?(Symbol) ? ":#{t}" : t }.join(", ") unless
    parsed.tags.nil? || parsed.tags.empty?

  unless parsed.htags.nil? || parsed.htags.empty?
    htags_str = parsed.htags.map do |(k, v)|
      vstr = v.is_a?(Symbol) ? ":#{v}" : quote(v)

      "#{k}: #{vstr}"
    end
  end

  replacement = "\\1#{parsed.fname}#{need_parens ? "(" : " "}"\
                "#{[desc, tags_str, htags_str].compact.join(", ")}"\
                "#{need_parens ? ") " : " "}\\3"

  if config.dry_run?
    log :info, "Patched: #{example.sub(EXAMPLE_RXP, replacement)}"
  else
    example.sub!(EXAMPLE_RXP, replacement)
  end
  true
end