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.

hook.rb 2.3 kB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # frozen_string_literal: true
  2. require 'cucumber/glue/invoke_in_world'
  3. module Cucumber
  4. module Glue
  5. # TODO: Kill pointless wrapper for Before, After and AfterStep hooks with fire
  6. class Hook
  7. attr_reader :id, :tag_expressions, :location, :name
  8. def initialize(id, registry, tag_expressions, proc, name: nil)
  9. @id = id
  10. @registry = registry
  11. @name = name
  12. @tag_expressions = sanitize_tag_expressions(tag_expressions)
  13. @proc = proc
  14. @location = Cucumber::Core::Test::Location.from_source_location(*@proc.source_location)
  15. fail_for_old_style_tag_expressions(@tag_expressions)
  16. end
  17. def invoke(pseudo_method, arguments, &block)
  18. check_arity = false
  19. InvokeInWorld.cucumber_instance_exec_in(
  20. @registry.current_world,
  21. check_arity,
  22. pseudo_method,
  23. *[arguments, block].flatten.compact,
  24. &@proc
  25. )
  26. end
  27. def to_envelope
  28. Cucumber::Messages::Envelope.new(
  29. hook: Cucumber::Messages::Hook.new(
  30. id: id,
  31. name: name,
  32. tag_expression: tag_expressions.empty? ? nil : tag_expressions.join(' '),
  33. source_reference: Cucumber::Messages::SourceReference.new(
  34. uri: location.file,
  35. location: Cucumber::Messages::Location.new(
  36. line: location.lines.first
  37. )
  38. )
  39. )
  40. )
  41. end
  42. private
  43. def sanitize_tag_expressions(tag_expressions)
  44. # TODO: remove when '~@no-clobber' has been changed to 'not @no-clobber' in aruba
  45. tag_expressions.map { |tag_expression| tag_expression == '~@no-clobber' ? 'not @no-clobber' : tag_expression }
  46. end
  47. def fail_for_old_style_tag_expressions(tag_expressions)
  48. tag_expressions.each do |tag_expression|
  49. if tag_expression.include?('~')
  50. raise("Found tagged hook with '#{tag_expression}'." \
  51. "'~@tag' is no longer supported, use 'not @tag' instead.")
  52. end
  53. next unless tag_expression.include?(',')
  54. warn("Found tagged hook with '#{tag_expression}'." \
  55. "'@tag1,@tag2' is no longer supported, use '@tag or @tag2' instead.")
  56. end
  57. end
  58. end
  59. end
  60. end

No Description

Contributors (1)