Sha256: 007b91b90daaf7304c4e99fd6611e85a03d862fd14dc2bda10bbc8d78195190a

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

module OpalSpec
  class Example
    attr_reader :description, :example_group, :exception
    attr_accessor :asynchronous

    def initialize(group, desc, block)
      @example_group = group
      @description   = desc
      @__block__     = block
    end

    def finish_running
      if @exception
        @example_group.example_failed self
      else
        @example_group.example_passed self
      end
    end

    def run
      begin
        @example_group.example_started self
        run_before_hooks
        instance_eval(&@__block__)
      rescue => e
        @exception = e
      ensure
        run_after_hooks unless @asynchronous
      end

      if @asynchronous
        # must wait ...
      else
        finish_running
      end
    end

    def run_after_hooks
      begin
        @example_group.after_hooks.each do |after|
        instance_eval &after
        end
      rescue => e
        @exception = e
      end
    end

    def run_before_hooks
      @example_group.before_hooks.each do |before|
        instance_eval &before
      end
    end

    def run_async(&block)
      begin
        block.call
      rescue => e
        @exception = e
      ensure
        run_after_hooks
      end

      finish_running
    end

    def set_timeout(duration, &block)
      %x{
        setTimeout(function() {
          #{ block.call };
        }, duration);
      }

      self
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
opal-spec-0.2.6 lib/opal-spec/example.rb
opal-spec-0.2.5 lib/opal-spec/example.rb
opal-spec-0.2.1 lib/opal-spec/example.rb