Created
June 17, 2013 03:08
-
-
Save lewang/5794420 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| gem "ruby_parser", "~> 2.0" | |
| require 'ruby_parser' | |
| class Step | |
| attr_reader :file, :line, :regexp | |
| def initialize(regexp, file, line) | |
| @file, @line = file, line | |
| self.regexp = regexp | |
| end | |
| def regexp=(value) | |
| @regexp = | |
| case value | |
| when String | |
| pieces, regexp = [], value.dup | |
| regexp.gsub!(/\$\w+/) { |match| pieces << match; "TOKEN" } | |
| regexp = Regexp.escape(regexp) | |
| regexp.gsub!(/TOKEN/) { |match| "(.*)" } | |
| Regexp.new("^#{regexp}$") | |
| when Regexp | |
| value | |
| else | |
| STDERR.puts "Warning: invalid parameter to Given/When/Then on #{file}:#{line}. Expected Regexp or String, got #{value.class} #{value.inspect}" | |
| Regexp.new(/^INVALID PARAM$/) | |
| end | |
| end | |
| def match?(text) | |
| @regexp.match(text) | |
| end | |
| end | |
| class StepParser | |
| def self.parser | |
| @parser ||= RubyParser.new | |
| end | |
| attr_accessor :steps, :file | |
| def initialize(file) | |
| @file = file | |
| @steps = [] | |
| extract_steps(self.class.parser.parse(File.read(file))) | |
| end | |
| def extract_steps(sexp) | |
| return unless sexp.is_a?(Sexp) | |
| case sexp.first | |
| when :block | |
| sexp[1..-1].each do |child_sexp| | |
| extract_steps(child_sexp) | |
| end | |
| when :iter | |
| child_sexp = sexp[1] | |
| return unless child_sexp[0] == :call && [:When, :Then, :Given, :And].include?(child_sexp[2]) | |
| regexp = child_sexp[3][1] && child_sexp[3][1][1] | |
| @steps << Step.new(regexp, file, child_sexp.line) | |
| else | |
| sexp.each do |child_sexp| | |
| extract_steps(child_sexp) | |
| end | |
| end | |
| end | |
| end | |
| git_root = `git rev-parse --show-toplevel`.chomp | |
| feature_dir = File.expand_path("features", git_root) | |
| files = Dir["#{feature_dir}/**/*steps.rb"] | |
| steps = [] | |
| files.each do |file| | |
| steps.concat(StepParser.new(file).steps) | |
| end | |
| steps.each do |step| | |
| puts "#{File.expand_path(step.file, Dir.pwd)}:#{step.line}:#{step.regexp.inspect}" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment