You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # frozen_string_literal: true
  2. require 'cucumber/cucumber_expressions/parameter_type'
  3. require 'cucumber/deprecate'
  4. module Cucumber
  5. module Glue
  6. # This module provides the methods the DSL you can use to define
  7. # steps, hooks, transforms etc.
  8. module Dsl
  9. class << self
  10. attr_writer :rb_language
  11. def alias_adverb(adverb)
  12. alias_method adverb, :register_rb_step_definition
  13. end
  14. def build_rb_world_factory(world_modules, namespaced_world_modules, proc)
  15. @rb_language.build_rb_world_factory(world_modules, namespaced_world_modules, proc)
  16. end
  17. def register_rb_hook(phase, tag_names, proc, name: nil)
  18. @rb_language.register_rb_hook(phase, tag_names, proc, name: name)
  19. end
  20. def define_parameter_type(parameter_type)
  21. @rb_language.define_parameter_type(parameter_type)
  22. end
  23. def register_rb_step_definition(regexp, proc_or_sym, options = {})
  24. @rb_language.register_rb_step_definition(regexp, proc_or_sym, options)
  25. end
  26. end
  27. # Registers any number of +world_modules+ (Ruby Modules) and/or a Proc.
  28. # The +proc+ will be executed once before each scenario to create an
  29. # Object that the scenario's steps will run within. Any +world_modules+
  30. # will be mixed into this Object (via Object#extend).
  31. #
  32. # By default the +world modules+ are added to a global namespace. It is
  33. # possible to create a namespaced World by using an hash, where the
  34. # symbols are the namespaces.
  35. #
  36. # This method is typically called from one or more Ruby scripts under
  37. # <tt>features/support</tt>. You can call this method as many times as you
  38. # like (to register more modules), but if you try to register more than
  39. # one Proc you will get an error.
  40. #
  41. # Cucumber will not yield anything to the +proc+. Examples:
  42. #
  43. # World do
  44. # MyClass.new
  45. # end
  46. #
  47. # World(MyModule)
  48. #
  49. # World(my_module: MyModule)
  50. #
  51. def World(*world_modules, **namespaced_world_modules, &proc)
  52. Dsl.build_rb_world_factory(world_modules, namespaced_world_modules, proc)
  53. end
  54. # Registers a proc that will run before each Scenario. You can register as many
  55. # as you want (typically from ruby scripts under <tt>support/hooks.rb</tt>).
  56. def Before(*tag_expressions, name: nil, &proc)
  57. Dsl.register_rb_hook('before', tag_expressions, proc, name: name)
  58. end
  59. # Registers a proc that will run after each Scenario. You can register as many
  60. # as you want (typically from ruby scripts under <tt>support/hooks.rb</tt>).
  61. def After(*tag_expressions, name: nil, &proc)
  62. Dsl.register_rb_hook('after', tag_expressions, proc, name: name)
  63. end
  64. # Registers a proc that will be wrapped around each scenario. The proc
  65. # should accept two arguments: two arguments: the scenario and a "block"
  66. # argument (but passed as a regular argument, since blocks cannot accept
  67. # blocks in 1.8), on which it should call the .call method. You can register
  68. # as many as you want (typically from ruby scripts under <tt>support/hooks.rb</tt>).
  69. def Around(*tag_expressions, name: nil, &proc)
  70. Dsl.register_rb_hook('around', tag_expressions, proc, name: name)
  71. end
  72. # Registers a proc that will run after each Step. You can register as
  73. # as you want (typically from ruby scripts under <tt>support/hooks.rb</tt>).
  74. def AfterStep(*tag_expressions, name: nil, &proc)
  75. Dsl.register_rb_hook('after_step', tag_expressions, proc, name: name)
  76. end
  77. def ParameterType(options)
  78. type = options[:type] || Object
  79. use_for_snippets = if_nil(options[:use_for_snippets], true)
  80. prefer_for_regexp_match = if_nil(options[:prefer_for_regexp_match], false)
  81. parameter_type = CucumberExpressions::ParameterType.new(
  82. options[:name],
  83. options[:regexp],
  84. type,
  85. options[:transformer],
  86. use_for_snippets,
  87. prefer_for_regexp_match
  88. )
  89. Dsl.define_parameter_type(parameter_type)
  90. end
  91. def if_nil(value, default)
  92. value.nil? ? default : value
  93. end
  94. # Registers a proc that will run after Cucumber is configured in order to install an external plugin.
  95. def InstallPlugin(name: nil, &proc)
  96. Dsl.register_rb_hook('install_plugin', [], proc, name: name)
  97. end
  98. # Registers a proc that will run before the execution of the scenarios.
  99. # Use it for your final set-ups
  100. def BeforeAll(name: nil, &proc)
  101. Dsl.register_rb_hook('before_all', [], proc, name: name)
  102. end
  103. # Registers a proc that will run after the execution of the scenarios.
  104. # Use it for your final clean-ups
  105. def AfterAll(name: nil, &proc)
  106. Dsl.register_rb_hook('after_all', [], proc, name: name)
  107. end
  108. # Registers a new Ruby StepDefinition. This method is aliased
  109. # to <tt>Given</tt>, <tt>When</tt> and <tt>Then</tt>, and
  110. # also to the i18n translations whenever a feature of a
  111. # new language is loaded.
  112. #
  113. # If provided, the +symbol+ is sent to the <tt>World</tt> object
  114. # as defined by #World. A new <tt>World</tt> object is created
  115. # for each scenario and is shared across step definitions within
  116. # that scenario. If the +options+ hash contains an <tt>:on</tt>
  117. # key, the value for this is assumed to be a proc. This proc
  118. # will be executed in the context of the <tt>World</tt> object
  119. # and then sent the +symbol+.
  120. #
  121. # If no +symbol+ if provided then the +&proc+ gets executed in
  122. # the context of the <tt>World</tt> object.
  123. def register_rb_step_definition(regexp, symbol = nil, options = {}, &proc)
  124. proc_or_sym = symbol || proc
  125. Dsl.register_rb_step_definition(regexp, proc_or_sym, options)
  126. end
  127. end
  128. end
  129. end
  130. # rubocop:disable Style/MixinUsage
  131. # This "should" always be present, because it allows users to write `Before` and `After`
  132. # See. https://github.com/cucumber/cucumber-ruby/pull/1566#discussion_r683235396
  133. extend(Cucumber::Glue::Dsl)
  134. # rubocop:enable Style/MixinUsage

No Description

Contributors (1)