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.

hooks.rb 2.5 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # frozen_string_literal: true
  2. require 'pathname'
  3. require 'cucumber/core/test/location'
  4. require 'cucumber/core/test/around_hook'
  5. module Cucumber
  6. # Hooks quack enough like `Cucumber::Core::Ast` source nodes that we can use them as
  7. # source for test steps
  8. module Hooks
  9. class << self
  10. def before_hook(id, location, &block)
  11. build_hook_step(id, location, block, BeforeHook, Core::Test::UnskippableAction)
  12. end
  13. def after_hook(id, location, &block)
  14. build_hook_step(id, location, block, AfterHook, Core::Test::UnskippableAction)
  15. end
  16. def after_step_hook(id, test_step, location, &block)
  17. raise ArgumentError if test_step.hook?
  18. build_hook_step(id, location, block, AfterStepHook, Core::Test::Action)
  19. end
  20. def around_hook(&block)
  21. Core::Test::AroundHook.new(&block)
  22. end
  23. private
  24. def build_hook_step(id, location, block, hook_type, action_type)
  25. action = action_type.new(location, &block)
  26. hook = hook_type.new(action.location)
  27. Core::Test::HookStep.new(id, hook.text, location, action)
  28. end
  29. end
  30. class AfterHook
  31. attr_reader :location
  32. def initialize(location)
  33. @location = location
  34. end
  35. def text
  36. 'After hook'
  37. end
  38. def to_s
  39. "#{text} at #{location}"
  40. end
  41. def match_locations?(queried_locations)
  42. queried_locations.any? { |other_location| other_location.match?(location) }
  43. end
  44. def describe_to(visitor, *args)
  45. visitor.after_hook(self, *args)
  46. end
  47. end
  48. class BeforeHook
  49. attr_reader :location
  50. def initialize(location)
  51. @location = location
  52. end
  53. def text
  54. 'Before hook'
  55. end
  56. def to_s
  57. "#{text} at #{location}"
  58. end
  59. def match_locations?(queried_locations)
  60. queried_locations.any? { |other_location| other_location.match?(location) }
  61. end
  62. def describe_to(visitor, *args)
  63. visitor.before_hook(self, *args)
  64. end
  65. end
  66. class AfterStepHook
  67. attr_reader :location
  68. def initialize(location)
  69. @location = location
  70. end
  71. def text
  72. 'AfterStep hook'
  73. end
  74. def to_s
  75. "#{text} at #{location}"
  76. end
  77. def match_locations?(queried_locations)
  78. queried_locations.any? { |other_location| other_location.match?(location) }
  79. end
  80. def describe_to(visitor, *args)
  81. visitor.after_step_hook(self, *args)
  82. end
  83. end
  84. end
  85. end

No Description

Contributors (1)