forked from cucumber/cucumber-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull snippet rendering out of RbLanguage.
- Loading branch information
Roel van Dijk
committed
Mar 27, 2013
1 parent
88580ae
commit 1a668f1
Showing
4 changed files
with
260 additions
and
101 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
module Cucumber | ||
module RbSupport | ||
module Snippet | ||
|
||
ARGUMENT_PATTERNS = ['"(.*?)"', '(\d+)'] | ||
|
||
class BaseSnippet | ||
|
||
attr_accessor :code_keyword, :pattern, :multiline_argument_class | ||
|
||
def render | ||
replace_and_count_capturing_groups! | ||
render_snippet | ||
end | ||
|
||
private | ||
|
||
def replace_and_count_capturing_groups! | ||
self.pattern = ::Regexp.escape(pattern).gsub('\ ', ' ').gsub('/', '\/') | ||
|
||
arg_count = 0 | ||
|
||
ARGUMENT_PATTERNS.each do |pattern| | ||
self.pattern = self.pattern.gsub(::Regexp.new(pattern), pattern) | ||
arg_count += self.pattern.scan(pattern).length | ||
end | ||
|
||
@number_of_arguments = arg_count | ||
end | ||
|
||
def render_snippet | ||
"#{code_keyword}#{typed_pattern} #{do_block}" | ||
end | ||
|
||
def do_block | ||
do_block = "" | ||
do_block << "do#{arguments}\n" | ||
do_block << multiline_comment if multiline_argument_class? | ||
do_block << " pending # express the regexp above with the code you wish you had\n" | ||
do_block << "end" | ||
do_block | ||
end | ||
|
||
def arguments | ||
block_args = (0...@number_of_arguments).map {|n| "arg#{n+1}"} | ||
|
||
if multiline_argument_class | ||
block_args << multiline_argument_class.default_arg_name | ||
end | ||
|
||
block_args.empty? ? "" : " |#{block_args.join(", ")}|" | ||
end | ||
|
||
def multiline_comment | ||
" # #{multiline_argument_class.default_arg_name} is a #{multiline_argument_class.to_s}\n" | ||
end | ||
|
||
def multiline_argument_class? | ||
multiline_argument_class == Ast::Table | ||
end | ||
|
||
end | ||
|
||
class Regexp < BaseSnippet | ||
def typed_pattern | ||
"(/^#{pattern}$/)" | ||
end | ||
end | ||
|
||
class Legacy < BaseSnippet | ||
def typed_pattern | ||
" /^#{pattern}$/" | ||
end | ||
end | ||
|
||
class Percent < BaseSnippet | ||
def typed_pattern | ||
" %r{^#{pattern}$}" | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains 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
Oops, something went wrong.