Sha256: 7fb3f26c38f6a8b5594292950ab722a05cb2315f12ec8ca7ade6b5253d5c81bc

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Opal
  module Spec
    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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opal-spec-0.2.8 lib/assets/javascripts/opal/spec/example.rb
opal-spec-0.2.7 lib/assets/javascripts/opal/spec/example.rb