Class: RSpec::Core::MemoizedHelpers::ClassMethods::WithContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rspecz/with.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, values, block, myobject) ⇒ WithContext

Returns a new instance of WithContext.



8
9
10
# File 'lib/rspecz/with.rb', line 8

def initialize(name, values, block, myobject)
  @name, @values, @block, @myobject = name, values, block, myobject
end

Instance Attribute Details

#and_blockObject

Returns the value of attribute and_block.



6
7
8
# File 'lib/rspecz/with.rb', line 6

def and_block
  @and_block
end

#and_nameObject

Returns the value of attribute and_name.



6
7
8
# File 'lib/rspecz/with.rb', line 6

def and_name
  @and_name
end

#blockObject

Returns the value of attribute block.



6
7
8
# File 'lib/rspecz/with.rb', line 6

def block
  @block
end

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/rspecz/with.rb', line 6

def description
  @description
end

#focusedObject

Returns the value of attribute focused.



6
7
8
# File 'lib/rspecz/with.rb', line 6

def focused
  @focused
end

#hint(text) ⇒ Object

Returns the value of attribute hint.



6
7
8
# File 'lib/rspecz/with.rb', line 6

def hint
  @hint
end

#myobjectObject

Returns the value of attribute myobject.



6
7
8
# File 'lib/rspecz/with.rb', line 6

def myobject
  @myobject
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/rspecz/with.rb', line 6

def name
  @name
end

#valuesObject

Returns the value of attribute values.



6
7
8
# File 'lib/rspecz/with.rb', line 6

def values
  @values
end

Instance Method Details

#__get_description(text, method_name) ⇒ Object

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



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
# 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

#and(name, &block) ⇒ Object

Raises:

  • (RuntimeError)


25
26
27
28
29
30
# 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) ⇒ Object



12
13
14
15
# File 'lib/rspecz/with.rb', line 12

def desc(description)
  @description = description
  self
end

#so(&block) ⇒ Object



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
# File 'lib/rspecz/with.rb', line 32

def so(&block)
  root_context = @myobject.send(:root_context)
  so_count = root_context.[RSpecZ::]
  root_context.[RSpecZ::] = 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