Class: StepMaster::Matcher
- Inherits:
-
Object
- Object
- StepMaster::Matcher
- Defined in:
- lib/step_master/matcher.rb
Overview
steps that start with a given string (taking into account captures and imbedded regexps).
The is_match? method lets you check if a string exactly matches to a step definition
Constant Summary collapse
- STEP_REGEX =
/^\s*(Given|Then|When)\s*\(?\s*\/\^?(.*)\/(\w*)\s*\)?\s*(?:do|\{)\s*(\|[^\|]+\|)?/.freeze
- ARG_NAMES_REGEX =
/\|(.*)\|/
- ARG_TEXT_REGEX =
/\(.*?[^\\]\)[\?\+\*]?/
- NON_CAPTURE_REGEX =
/^\(\?\:/
- CHUNK_REGEX =
/\S+|\s\??/
Instance Attribute Summary collapse
-
#match_table ⇒ Object
readonly
Returns the value of attribute match_table.
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Insert a Ruby Step definition into the Match Table.
- #add(value, options = {}) ⇒ Object
-
#complete(string, options = {}) ⇒ Object
Returns all possible outcomes of a string.
-
#initialize(match_table = Possible.new) ⇒ Matcher
constructor
A new instance of Matcher.
-
#is_match?(string) ⇒ Boolean
Returns true if the string matches exactly to a step definition.
- #terminals(string) ⇒ Object
- #where_is?(string) ⇒ Boolean
Constructor Details
Instance Attribute Details
#match_table ⇒ Object (readonly)
Returns the value of attribute match_table.
29 30 31 |
# File 'lib/step_master/matcher.rb', line 29 def match_table @match_table end |
Instance Method Details
#<<(value) ⇒ Object
Insert a Ruby Step definition into the Match Table
Examples
matcher << "Given /^this is a step$/ do"
40 41 42 |
# File 'lib/step_master/matcher.rb', line 40 def <<(value) add(value, :format => :rb) end |
#add(value, options = {}) ⇒ Object
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 |
# File 'lib/step_master/matcher.rb', line 44 def add(value, = {}) raise "#{value.inspect} is not a step" unless value =~ STEP_REGEX full_line = $& step_type = $1 regex = $2.chomp("$") = $3 args = $4 arg_names = (args =~ ARG_NAMES_REGEX) ? $1.split(/\s*,\s*/) : [] arg_regexs = extract_captures(regex) arg_objects = arg_regexs.collect do |x| is_non_capture = (x =~ NON_CAPTURE_REGEX) != nil StepVariable.new(x, , (is_non_capture) ? nil : arg_names.shift) end if arg_regexs.length > 0 regex.split(Regexp.union(arg_regexs.collect { |x| Regexp.new(Regexp.escape(x)) })).collect { |x| x.scan(CHUNK_REGEX).collect { |i| StepItem.new(i, ) } }.+(Array.new(5)).zip(arg_objects).flatten.compact.unshift(StepItem.new(" ", )).unshift(StepItem.new(step_type, )) else regex.scan(CHUNK_REGEX).unshift(" ").unshift(step_type).collect { |i| StepItem.new(i, ) } end.inject(@match_table) { |parent, i| parent[i] ||= Possible.new }.terminal!(value, ) end |
#complete(string, options = {}) ⇒ Object
Returns all possible outcomes of a string.
Parameters
-
string
- The string to try to auto complete -
options
- :easy => true will replace captures with the variable names in the step definition
Examples
matcher.complete("Given this")
matcher.complete("Given this", :easy => true)
82 83 84 |
# File 'lib/step_master/matcher.rb', line 82 def complete(string, = {}) possible_strings find_possible(string), end |
#is_match?(string) ⇒ Boolean
Returns true if the string matches exactly to a step definition
Examples
matcher << "Given /^this$/ do"
matcher.is_match?("Given this") #=> true
matcher.is_match?("Given that") #=> false
93 94 95 |
# File 'lib/step_master/matcher.rb', line 93 def is_match?(string) find_possible(string).any?{ |x| x.terminal? } end |
#terminals(string) ⇒ Object
101 102 103 |
# File 'lib/step_master/matcher.rb', line 101 def terminals(string) find_possible(string).select{ |x| x.terminal? } end |
#where_is?(string) ⇒ Boolean
97 98 99 |
# File 'lib/step_master/matcher.rb', line 97 def where_is?(string) find_possible(string).select{ |x| x.terminal? }.collect { |x| x.file || x.result } end |