forked from michaelklishin/cucumber.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_step.rb
87 lines (76 loc) · 2.08 KB
/
find_step.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'rubygems'
gem "ruby_parser", "~> 2.0"
require 'ruby_parser'
require 'yaml'
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, keywords)
@file = file
@steps = []
@keywords = keywords
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 && @keywords.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
i18n_key = ARGV[0]
i18n_file = ARGV[1]
i18n_map = YAML::load(File.open(i18n_file))
i18n = i18n_map[i18n_key]
keywords = (i18n["when"] + i18n["then"] + i18n["given"] + i18n["and"]).gsub("*","").split("|").last(4).map {|k| k.to_sym }
input_text = ARGV[2].strip.gsub(/(#{keywords.join("|")}) */, "")
files = Dir["features/**/*steps.rb"]
steps = []
files.each do |file|
steps.concat(StepParser.new(file, keywords).steps)
end
steps.each do |step|
if step.match?(input_text)
puts "#{step.file}:#{step.line}"
end
end