Class: TestCaseGenerator::DSLContext

Inherits:
Object
  • Object
show all
Defined in:
lib/test_case_generator/dsl_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSLContext

Returns a new instance of DSLContext.



10
11
12
13
14
15
16
17
# File 'lib/test_case_generator/dsl_context.rb', line 10

def initialize
  @patterns = []
  @before = []
  @after = []
  @children = []
  @labels = []
  @class_name = nil
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/test_case_generator/dsl_context.rb', line 6

def children
  @children
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



8
9
10
# File 'lib/test_case_generator/dsl_context.rb', line 8

def class_name
  @class_name
end

#labelsObject (readonly)

Returns the value of attribute labels.



7
8
9
# File 'lib/test_case_generator/dsl_context.rb', line 7

def labels
  @labels
end

Instance Method Details

#<<(events) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/test_case_generator/dsl_context.rb', line 19

def <<(events)
  if events.is_a?(String) || events.is_a?(Symbol)
    @patterns << [events]
    @labels << events unless @labels.include? events
  else
    @patterns << events
    events.each do |label|
      @labels << label unless @labels.include? label
    end
  end
end

#add_async_events(src_items, options = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/test_case_generator/dsl_context.rb', line 151

def add_async_events(src_items, options={})
  out_items = []

  src_items.each do |pattern1|
    idx_from = options[:from].nil? ? nil : pattern1.find_index{|item| item==options[:from]}
    if idx_from.nil?
      out_items << pattern1
      next
    end

    pattern2 = pattern1[idx_from + 1 ... pattern1.size]
    idx_to = options[:to].nil? ? nil : pattern2.find_index{|item| item==options[:to]}

    tmp_items = idx_to.nil? ? [pattern2] : [pattern2[0 ... idx_to]]
    Utils.para! tmp_items, options[:items]

    out_items.concat tmp_items.map{ |ptn| pattern1[0 .. idx_from] + ptn + (idx_to.nil? ? [] : pattern2[idx_to ... pattern2.size]) }
  end

  out_items.uniq
end

#add_async_events!(src_items, options = {}) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/test_case_generator/dsl_context.rb', line 173

def add_async_events!(src_items, options={})
  tmp_items = add_async_events(src_items, options)

  src_items.clear
  src_items.concat tmp_items
  src_items
end

#add_patterns(patterns) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/test_case_generator/dsl_context.rb', line 181

def add_patterns(patterns)
  @patterns.concat patterns

  patterns.each do |pattern|
    pattern.each do |label|
      @labels << label unless @labels.include? label
    end
  end
end

#after {|@after| ... } ⇒ Object

Yields:



39
40
41
# File 'lib/test_case_generator/dsl_context.rb', line 39

def after
  yield @after
end

#before {|@before| ... } ⇒ Object

Yields:



35
36
37
# File 'lib/test_case_generator/dsl_context.rb', line 35

def before
  yield @before
end

#concat(&block) ⇒ Object Also known as: seq



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
# File 'lib/test_case_generator/dsl_context.rb', line 57

def concat(&block)
  child_context = DSLContext.new
  child_context.instance_eval &block

  first = true
  tmp = []
  child_context.children.each do |ctx|
    tmp2 = []
    ctx.raw_each do |ptn|
      if first
        tmp2 << ptn
      else
        tmp.each do |x|
          tmp2 << x + ptn
        end
      end
    end

    tmp = tmp2
    first = false
  end

  tmp.each do |x|
    @patterns << x
    x.each do |label|
      @labels << label unless @labels.include? label
    end
  end
end

#def_labels {|@labels| ... } ⇒ Object

Yields:



31
32
33
# File 'lib/test_case_generator/dsl_context.rb', line 31

def def_labels
  yield @labels
end

#def_state_machine(options = {}, &block) ⇒ Object



133
134
135
136
137
# File 'lib/test_case_generator/dsl_context.rb', line 133

def def_state_machine(options={}, &block)
  ctx = StateMachineContext.new(options)
  ctx.instance_eval &block if block_given?
  ctx
end

#eachObject



199
200
201
202
203
204
205
206
207
208
# File 'lib/test_case_generator/dsl_context.rb', line 199

def each
  raw_each do |raw_ptn|
    yield raw_ptn.map { |p|
      # p.to_s.split('_').inject([]) { |buffer, e|
      #   buffer << (buffer.empty? ? e : e.capitalize)
      # }.join
      TestCaseGenerator::Utils.make_method_name p
    }
  end
end

#parallel(&block) ⇒ Object Also known as: para



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
# File 'lib/test_case_generator/dsl_context.rb', line 88

def parallel(&block)
  child_context = DSLContext.new
  child_context.instance_eval &block

  first = true
  tmp = []
  child_context.children.each do |ctx|
    tmp2 = []
    ctx.raw_each do |ptn|
      if first
        tmp2 << ptn
      else
        tmp.each do |x|
          (0..x.length + ptn.length - 1).to_a.combination(x.length) do |index_arr|
            x_index = 0
            ptn_index = 0
            tmp2 << (0..x.length + ptn.length - 1).map do |i|
              if index_arr.include?(i)
                ret = x[x_index]
                x_index += 1
              else
                ret = ptn[ptn_index]
                ptn_index += 1
              end

              ret
            end
          end
        end
      end
    end

    tmp = tmp2
    first = false
  end

  tmp.each do |x|
    @patterns << x
    x.each do |label|
      @labels << label unless @labels.include? label
    end
  end
end

#pattern {|child_context| ... } ⇒ Object Also known as: choice

Yields:

  • (child_context)


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/test_case_generator/dsl_context.rb', line 43

def pattern
  child_context = DSLContext.new
  yield child_context
  @children << child_context

  child_context.raw_each do |ptn|
    @patterns << ptn
    ptn.each do |label|
      @labels << label unless @labels.include? label
    end
  end
end

#raw_eachObject



195
196
197
# File 'lib/test_case_generator/dsl_context.rb', line 195

def raw_each
  @patterns.each { |ptn| yield @before + ptn + @after }
end

#set_class_name(class_name) ⇒ Object



191
192
193
# File 'lib/test_case_generator/dsl_context.rb', line 191

def set_class_name(class_name)
  @class_name = class_name
end

#state_machine(options = {}, &block) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/test_case_generator/dsl_context.rb', line 139

def state_machine(options={}, &block)
  ctx = def_state_machine options, &block

  ctx.items.each do |x|
    @patterns << x
    x.each do |label|
      @labels << label unless @labels.include? label
    end
  end
  # @patterns.concat ctx.items
end