module TestProf::RSpecStamp
def apply_tags(code, lines, tags)
line numbers (of example to apply tags)
Accepts source code (as array of lines),
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
def config
def config @config ||= Configuration.new end
def configure
def configure yield config end
def quote(str)
def quote(str) if str.include?("'") "\"#{str}\"" else "'#{str}'" end end
def stamp_example(example, tags)
rubocop: disable Metrics/CyclomaticComplexity
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 parsed.desc ||= 'works' tags.each do |t| if t.is_a?(Hash) t.keys.each 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? unless parsed.htags.nil? 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 ? '(' : ' '}"\ "#{[quote(parsed.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