Skip to content
This repository
tree: 0592acad14
Fetching contributors…

Cannot retrieve contributors at this time

file 484 lines (442 sloc) 16.929 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
module RSpec
  module Core
    module Hooks
      include MetadataHashBuilder::WithConfigWarning

      module HookExtension
        attr_reader :options

        def with(options)
          @options = options
          self
        end

        def options_apply?(example_or_group)
          example_or_group.all_apply?(options)
        end
      end

      module BeforeHookExtension
        include HookExtension

        def run(example)
          example.instance_eval(&self)
        end

        def display_name
          "before hook"
        end
      end

      module AfterHookExtension
        include HookExtension

        def run(example)
          example.instance_eval_with_rescue("in an after hook", &self)
        end

        def display_name
          "after hook"
        end
      end

      module AroundHookExtension
        include HookExtension

        def display_name
          "around hook"
        end
      end

      class HookCollection < Array
        def for(example_or_group)
          self.class.new(select {|hook| hook.options_apply?(example_or_group)}).
            with(example_or_group)
        end

        def with(example)
          @example = example
          self
        end

        def run
          each {|h| h.run(@example) } unless empty?
        end
      end

      class GroupHookCollection < Array
        def for(group)
          @group = group
          self
        end

        def run
          shift.run(@group) until empty?
        end
      end

      class AroundHookCollection < Array
        def for(example, initial_procsy=nil)
          self.class.new(select {|hook| hook.options_apply?(example)}).
            with(example, initial_procsy)
        end

        def with(example, initial_procsy)
          @example = example
          @initial_procsy = initial_procsy
          self
        end

        def run
          inject(@initial_procsy) do |procsy, around_hook|
            Example.procsy(procsy.metadata) do
              @example.instance_eval_with_args(procsy, &around_hook)
            end
          end.call
        end
      end

      module RegistersGlobals
        def register_globals host, globals
          [:before, :after, :around].each do |position|
            process host, globals, position, :each
            next if position == :around # no around(:all) hooks
            process host, globals, position, :all
          end
        end

        private
        def process host, globals, position, scope
          globals[position][scope].each do |hook|
            unless host.ancestors.any? { |a| a.hooks[position][scope].include? hook }
              self[position][scope] << hook if scope == :each || hook.options_apply?(host)
            end
          end
        end
      end

      # @private
      def hooks
        @hooks ||= {
          :around => { :each => AroundHookCollection.new },
          :before => { :each => [], :all => [], :suite => HookCollection.new },
          :after => { :each => [], :all => [], :suite => HookCollection.new }
        }.extend(RegistersGlobals)
      end

      # @api public
      # @overload before(&block)
      # @overload before(scope, &block)
      # @overload before(scope, conditions, &block)
      # @overload before(conditions, &block)
      #
      # @param [Symbol] scope `:each`, `:all`, or `:suite` (defaults to `:each`)
      # @param [Hash] conditions
      # constrains this hook to examples matching these conditions e.g.
      # `before(:each, :ui => true) { ... }` will only run with examples or
      # groups declared with `:ui => true`.
      #
      # @see #after
      # @see #around
      # @see ExampleGroup
      # @see SharedContext
      # @see SharedExampleGroup
      # @see Configuration
      #
      # Declare a block of code to be run before each example (using `:each`)
      # or once before any example (using `:all`). These are usually declared
      # directly in the {ExampleGroup} to which they apply, but they can also
      # be shared across multiple groups.
      #
      # You can also use `before(:suite)` to run a block of code before any
      # example groups are run. This should be declared in {RSpec.configure}
      #
      # Instance variables declared in `before(:each)` or `before(:all)` are
      # accessible within each example.
      #
      # ### Order
      #
      # `before` hooks are stored in three scopes, which are run in order:
      # `:suite`, `:all`, and `:each`. They can also be declared in several
      # different places: `RSpec.configure`, a parent group, the current group.
      # They are run in the following order:
      #
      # before(:suite) # declared in RSpec.configure
      # before(:all) # declared in RSpec.configure
      # before(:all) # declared in a parent group
      # before(:all) # declared in the current group
      # before(:each) # declared in RSpec.configure
      # before(:each) # declared in a parent group
      # before(:each) # declared in the current group
      #
      # If more than one `before` is declared within any one scope, they are run
      # in the order in which they are declared.
      #
      # ### Conditions
      #
      # When you add a conditions hash to `before(:each)` or `before(:all)`,
      # RSpec will only apply that hook to groups or examples that match the
      # conditions. e.g.
      #
      # RSpec.configure do |config|
      # config.before(:each, :authorized => true) do
      # log_in_as :authorized_user
      # end
      # end
      #
      # describe Something, :authorized => true do
      # # the before hook will run in before each example in this group
      # end
      #
      # describe SomethingElse do
      # it "does something", :authorized => true do
      # # the before hook will run before this example
      # end
      #
      # it "does something else" do
      # # the hook will not run before this example
      # end
      # end
      #
      # ### Warning: `before(:suite, :with => :conditions)`
      #
      # The conditions hash is used to match against specific examples. Since
      # `before(:suite)` is not run in relation to any specific example or
      # group, conditions passed along with `:suite` are effectively ignored.
      #
      # ### Exceptions
      #
      # When an exception is raised in a `before` block, RSpec skips any
      # subsequent `before` blocks and the example, but runs all of the
      # `after(:each)` and `after(:all)` hooks.
      #
      # ### Warning: implicit before blocks
      #
      # `before` hooks can also be declared in shared contexts which get
      # included implicitly either by you or by extension libraries. Since
      # RSpec runs these in the order in which they are declared within each
      # scope, load order matters, and can lead to confusing results when one
      # before block depends on state that is prepared in another before block
      # that gets run later.
      #
      # ### Warning: `before(:all)`
      #
      # It is very tempting to use `before(:all)` to speed things up, but we
      # recommend that you avoid this as there are a number of gotchas, as well
      # as things that simply don't work.
      #
      # #### context
      #
      # `before(:all)` is run in an example that is generated to provide group
      # context for the block.
      #
      # #### instance variables
      #
      # Instance variables declared in `before(:all)` are shared across all the
      # examples in the group. This means that each example can change the
      # state of a shared object, resulting in an ordering dependency that can
      # make it difficult to reason about failures.
      #
      # ### other frameworks
      #
      # Mock object frameworks and database transaction managers (like
      # ActiveRecord) are typically designed around the idea of setting up
      # before an example, running that one example, and then tearing down.
      # This means that mocks and stubs can (sometimes) be declared in
      # `before(:all)`, but get torn down before the first real example is ever
      # run.
      #
      # You _can_ create database-backed model objects in a `before(:all)` in
      # rspec-rails, but it will not be wrapped in a transaction for you, so
      # you are on your own to clean up in an `after(:all)` block.
      #
      # @example before(:each) declared in an {ExampleGroup}
      #
      # describe Thing do
      # before(:each) do
      # @thing = Thing.new
      # end
      #
      # it "does something" do
      # # here you can access @thing
      # end
      # end
      #
      # @example before(:all) declared in an {ExampleGroup}
      #
      # describe Parser do
      # before(:all) do
      # File.open(file_to_parse, 'w') do |f|
      # f.write <<-CONTENT
      # stuff in the file
      # CONTENT
      # end
      # end
      #
      # it "parses the file" do
      # Parser.parse(file_to_parse)
      # end
      #
      # after(:all) do
      # File.delete(file_to_parse)
      # end
      # end
      def before(*args, &block)
        register_hook :append, :before, *args, &block
      end

      alias_method :append_before, :before

      # Adds `block` to the front of the list of `before` blocks in the same
      # scope (`:each`, `:all`, or `:suite`).
      #
      # See #before for scoping semantics.
      def prepend_before(*args, &block)
        register_hook :prepend, :before, *args, &block
      end

      # @api public
      # @overload after(&block)
      # @overload after(scope, &block)
      # @overload after(scope, conditions, &block)
      # @overload after(conditions, &block)
      #
      # @param [Symbol] scope `:each`, `:all`, or `:suite` (defaults to `:each`)
      # @param [Hash] conditions
      # constrains this hook to examples matching these conditions e.g.
      # `after(:each, :ui => true) { ... }` will only run with examples or
      # groups declared with `:ui => true`.
      #
      # @see #before
      # @see #around
      # @see ExampleGroup
      # @see SharedContext
      # @see SharedExampleGroup
      # @see Configuration
      #
      # Declare a block of code to be run after each example (using `:each`) or
      # once after all examples (using `:all`). See
      # [#before](Hooks#before-instance_method) for more information about
      # ordering.
      #
      # ### Exceptions
      #
      # `after` hooks are guaranteed to run even when there are exceptions in
      # `before` hooks or examples. When an exception is raised in an after
      # block, the exception is captured for later reporting, and subsequent
      # `after` blocks are run.
      #
      # ### Order
      #
      # `after` hooks are stored in three scopes, which are run in order:
      # `:each`, `:all`, and `:suite`. They can also be declared in several
      # different places: `RSpec.configure`, a parent group, the current group.
      # They are run in the following order:
      #
      # after(:each) # declared in the current group
      # after(:each) # declared in a parent group
      # after(:each) # declared in RSpec.configure
      # after(:all) # declared in the current group
      # after(:all) # declared in a parent group
      # after(:all) # declared in RSpec.configure
      #
      # This is the reverse of the order in which `before` hooks are run.
      # Similarly, if more than one `after` is declared within any one scope,
      # they are run in reverse order of that in which they are declared.
      def after(*args, &block)
        register_hook :prepend, :after, *args, &block
      end

      alias_method :prepend_after, :after

      # Adds `block` to the back of the list of `after` blocks in the same
      # scope (`:each`, `:all`, or `:suite`).
      #
      # See #after for scoping semantics.
      def append_after(*args, &block)
        register_hook :append, :after, *args, &block
      end

      # @api public
      # @overload around(&block)
      # @overload around(scope, &block)
      # @overload around(scope, conditions, &block)
      # @overload around(conditions, &block)
      #
      # @param [Symbol] scope `:each` (defaults to `:each`)
      # present for syntax parity with `before` and `after`, but `:each` is
      # the only supported value.
      #
      # @param [Hash] conditions
      # constrains this hook to examples matching these conditions e.g.
      # `around(:each, :ui => true) { ... }` will only run with examples or
      # groups declared with `:ui => true`.
      #
      # @yield [Example] the example to run
      #
      # @note the syntax of `around` is similar to that of `before` and `after`
      # but the semantics are quite different. `before` and `after` hooks are
      # run in the context of of the examples with which they are associated,
      # whereas `around` hooks are actually responsible for running the
      # examples. Consequently, `around` hooks do not have direct access to
      # resources that are made available within the examples and their
      # associated `before` and `after` hooks.
      #
      # @note `:each` is the only supported scope.
      #
      # Declare a block of code, parts of which will be run before and parts
      # after the example. It is your responsibility to run the example:
      #
      # around(:each) do |ex|
      # # do some stuff before
      # ex.run
      # # do some stuff after
      # end
      #
      # The yielded example aliases `run` with `call`, which lets you treat it
      # like a `Proc`. This is especially handy when working with libaries
      # that manage their own setup and teardown using a block or proc syntax,
      # e.g.
      #
      # around(:each) {|ex| Database.transaction(&ex)}
      # around(:each) {|ex| FakeFS(&ex)}
      #
      def around(*args, &block)
        register_hook :prepend, :around, *args, &block
      end

      # @private
      #
      # Runs all of the blocks stored with the hook in the context of the
      # example. If no example is provided, just calls the hook directly.
      def run_hook(hook, scope, example_or_group=ExampleGroup.new, initial_procsy=nil)
        find_hook(hook, scope, example_or_group, initial_procsy).run
      end

      # @private
      def around_each_hooks_for(example, initial_procsy=nil)
        AroundHookCollection.new(ancestors.map {|a| a.hooks[:around][:each]}.flatten).for(example, initial_procsy)
      end

    private

      SCOPES = [:each, :all, :suite]

      EXTENSIONS = {
        :before => BeforeHookExtension,
        :after => AfterHookExtension,
        :around => AroundHookExtension
      }

      def before_all_hooks_for(group)
        GroupHookCollection.new(hooks[:before][:all]).for(group)
      end

      def after_all_hooks_for(group)
        GroupHookCollection.new(hooks[:after][:all]).for(group)
      end

      def before_each_hooks_for(example)
        HookCollection.new(ancestors.reverse.map {|a| a.hooks[:before][:each]}.flatten).for(example)
      end

      def after_each_hooks_for(example)
        HookCollection.new(ancestors.map {|a| a.hooks[:after][:each]}.flatten).for(example)
      end

      def register_hook prepend_or_append, hook, *args, &block
        scope, options = scope_and_options_from(*args)
        hooks[hook][scope].send(prepend_or_append == :prepend ? :unshift : :push, block.extend(EXTENSIONS[hook]).with(options))
      end

      def find_hook(hook, scope, example_or_group, initial_procsy)
        case [hook, scope]
        when [:before, :all]
          before_all_hooks_for(example_or_group)
        when [:after, :all]
          after_all_hooks_for(example_or_group)
        when [:around, :each]
          around_each_hooks_for(example_or_group, initial_procsy)
        when [:before, :each]
          before_each_hooks_for(example_or_group)
        when [:after, :each]
          after_each_hooks_for(example_or_group)
        when [:before, :suite], [:after, :suite]
          hooks[hook][:suite].with(example_or_group)
        end
      end

      def scope_and_options_from(*args)
        return extract_scope_from(args), build_metadata_hash_from(args)
      end

      def extract_scope_from(args)
        if SCOPES.include?(args.first)
          args.shift
        elsif args.any? { |a| a.is_a?(Symbol) }
          raise ArgumentError.new("You must explicitly give a scope (:each, :all, or :suite) when using symbols as metadata for a hook.")
        else
          :each
        end
      end
    end
  end
end
Something went wrong with that request. Please try again.