Last active
December 1, 2020 13:31
-
-
Save caius/606b80252b176e353fe0893f8888dbbf 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
module GivenWhenThen | |
module ClassMethods | |
def def_Given(name, *args, &block) | |
_gwt_define(:Given, name, *args, &block) | |
end | |
def def_When(name, *args, &block) | |
_gwt_define(:When, name, *args, &block) | |
end | |
def def_Then(name, *args, &block) | |
_gwt_define(:Then, name, *args, &block) | |
end | |
private | |
def _gwt_define(prefix, name, *args, &block) | |
define_method("#{prefix} #{name}", *args, &block) | |
end | |
end | |
def self.included(klass) | |
klass.extend(ClassMethods) | |
end | |
def Given(name, *args) | |
_gwt_call(:Given, name, *args) | |
end | |
def When(name, *args) | |
_gwt_call(:When, name, *args) | |
end | |
def Then(name, *args) | |
_gwt_call(:Then, name, *args) | |
end | |
def And(name, *args) | |
_gwt_call(:And, name, *args) | |
end | |
private | |
def _gwt_call(prefix, name, *args) | |
if prefix == :And | |
prefix = @_gwt_last_prefix || :Given | |
end | |
public_send("#{prefix} #{name}", *args) | |
ensure | |
@_gwt_last_prefix = prefix | |
end | |
end | |
RSpec.configure do |c| | |
c.include GivenWhenThen, type: :feature | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool implementation. I have made a change so it puts the prefix and step name: