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) return str unless str.is_a?(String) if str.include?("'") "\"#{str}\"" else "'#{str}'" end end
def stamp_example(example, tags)
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