class RSpec::Core::MemoizedHelpers::ClassMethods::WithContext

Attributes

and_block[RW]
and_name[RW]
block[RW]
description[RW]
focused[RW]
hint[RW]
myobject[RW]
name[RW]
values[RW]

Public Class Methods

new(name, values, block, myobject) click to toggle source
# File lib/rspecz/with.rb, line 8
def initialize(name, values, block, myobject)
  @name, @values, @block, @myobject = name, values, block, myobject
end

Public Instance Methods

__get_description(text, method_name) click to toggle source

TODO: This method is temporary method. Need to implement proper logic in future .

# File lib/rspecz/with.rb, line 103
def __get_description(text, method_name)
  begin
    text = text[text.index("#{method_name}")..-1]
    bracket_start = text.index('{') || text.length
    do_start = text.index('do') || text.length

    start_word = bracket_start < do_start ? '{' : 'do'
    end_word = start_word == '{' ? '}' : 'end'

    start_index = [bracket_start, do_start].min + start_word.length
    inline_bracket_start = start_index
    next_end = 1
    start_word_count = 0

    loop do
      next_end = text[inline_bracket_start..-1].index(end_word) || text.length
      start_word_count += text[inline_bracket_start..inline_bracket_start+next_end].scan(start_word).length
      start_word_count -= 1
      # binding.pry
      break if start_word_count < 0
      inline_bracket_start += next_end + 1
    end

    text[start_index...inline_bracket_start+next_end].split("\n").last.strip
  rescue => e
    p 'Warning: rspecz with __get_description failed...'
    'different'
  end
end
_super() click to toggle source
# File lib/rspecz/with.rb, line 54
def _super
  @super.call()
end
and(name, &block) click to toggle source
# File lib/rspecz/with.rb, line 25
def and(name, &block)
  raise RuntimeError.new('You cannot set and two times! "and" method can use only one time in single with method.') unless @and_block.nil?
  @and_name = name
  @and_block = block
  self
end
desc(description) click to toggle source
# File lib/rspecz/with.rb, line 12
def desc(description)
  @description = description
  self
end
so(&block) click to toggle source
Calls superclass method
# File lib/rspecz/with.rb, line 32
def so(&block)
  root_context = @myobject.send(:root_context)
  so_count = root_context.metadata[RSpecZ::METADATA_SO_COUNT]
  root_context.metadata[RSpecZ::METADATA_SO_COUNT] = so_count == nil ? 1 : so_count + 1;

  continue_object = self
  continue_object_block = @block
  # TODO: create description from block.source
  if @values.length > 0
    raise RuntimeError.new("Syntax error you cannot set description by 'desc' method when you have multiple values set.") if @values.length > 1 && @description
    @values.each do |value|
      context_text = @hint ? "when #{@name} is #{@hint}(#{value})" : "when #{@name} is #{value}"

      spec_without_and = lambda do
        let(continue_object.name) { value }
        instance_exec(value, &block)
      end

      spec = @and_block.nil? ? spec_without_and : lambda do
        context "and #{continue_object.and_name} is #{continue_object.__get_description(continue_object.and_block.source, 'and')}" do
          let(continue_object.and_name) do
            @super = lambda { super() } if defined? super
            def _super
              @super.call()
            end
            instance_eval(&continue_object.and_block)
          end
          instance_exec(&spec_without_and)
        end
      end

      if @focused
        @myobject.fcontext(context_text, &spec)
      else
        @myobject.context(context_text, &spec)
      end
    end
  else
    context_text = @name ? "when #{@name} is #{__get_description(@block.source, 'with')}" : "prepare #{__get_description(@block.source, 'with')}"

    spec_without_and = lambda do
      if continue_object.name
        let(continue_object.name) do
          @super = lambda { super() } if defined? super
          def _super
            @super.call()
          end
          instance_eval(&continue_object_block)
        end
      else
        before { instance_eval(&continue_object_block) }
      end
      instance_exec(&block)
    end

    spec = @and_block.nil? ? spec_without_and : lambda do
      context "and #{continue_object.and_name} is #{continue_object.__get_description(continue_object.and_block.source, 'and')}" do
        let(continue_object.and_name) { instance_eval(&continue_object.and_block) }
        instance_exec(&spec_without_and)
      end
    end

    if @focused
      @myobject.fcontext(@description || context_text, &spec)
    else
      @myobject.context(@description || context_text, &spec)
    end
  end
end