Top Level Namespace

Extended by:
Test::Unit::Assertions

Instance Method Summary collapse

Instance Method Details

#dereference_feature(feature, scope) ⇒ Object



280
281
282
283
284
285
286
287
288
# File 'lib/cli.rb', line 280

def dereference_feature(feature, scope)
    #TODO: deepcopy feature
    if !!feature['call']
      feature['args'] = dereference_value(feature['args'], scope)
      feature['kwargs'] = dereference_value(feature['kwargs'], scope)
    end
    feature['result'] = dereference_value(feature['result'], scope)
    return feature
end

#dereference_value(value, scope) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/cli.rb', line 291

def dereference_value(value, scope)
  #TODO: deepcopy value
  if value.is_a?(Hash) && value.lengh == 1 && Array(value.each_value)[0] == nil
    result = scope
    for name in Array(value.each_key)[0].split('.')
      result = get_property(result, name)
    end
    value = result
  elsif value.is_a?(Hash)
    for key, item in value
      value[key] = dereference_value(item, scope)
    end
  elsif value.is_a?(Array)
    for item, index in value.each_with_index
      value[index] = dereference_value(item, scope)
    end
  end
  return value
end

#get_property(owner, name) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
# File 'lib/cli.rb', line 318

def get_property(owner, name)
  # TODO: review
  result = nil
  if owner.is_a?(Hash)
    result = owner[name]
  end
  if !result
    result = owner.method(name)
  end
  return result
end

#isoformat_value(value) ⇒ Object



312
313
314
315
# File 'lib/cli.rb', line 312

def isoformat_value(value)
  # TODO: implement
  return value
end

#parse_feature(feature) ⇒ Object



83
84
85
86
87
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cli.rb', line 83

def parse_feature(feature)
  if feature.is_a?(String)
    return {'comment' => feature}
  end
  left, right = Array(feature.each_pair)[0]

  # Left side
  call = false
  match = /^(?:(.*):)?(?:([^=]*)=)?([^=].*)?$/.match(left)
  skip, assign, property = match[1], match[2], match[3]
  if !!skip
    filters = skip.split(':')
    skip = (filters[0] == 'not') == (filters.include?('rb'))
  end
  if !assign and !property
    raise Exception.new('Non-valid feature')
  end
  if !!property
    call = true
    if property.end_with?('==')
      property = property[0..-3]
      call = false
    end
  end

  # Right side
  args = []
  kwargs = {}
  result = right
  if !!call
    result = nil
    for item in right
      if item.is_a?(Hash) && item.length == 1
        item_left, item_right = Array(item.each_pair)[0]
        if item_left == '=='
          result = item_right
          next
        end
        if item_left.end_with?('=')
          kwargs[item_left[0..-2]] = item_right
          next
        end
      end
      args.push(item)
    end
  end

  # Text repr
  text = property
  if !!assign
    text = "#{assign} = #{property || JSON.generate(result)}"
  end
  if !!call
    items = []
    for item in args
      items.push(JSON.generate(item))
    end
    for name, item in kwargs.each_pair
      items.push("#{name}=#{JSON.generate(item)}")
    end
    text = "#{text}(#{items.join(', ')})"
  end
  if !!result && !assign
    text = "#{text} == #{JSON.generate(result)}"
  end
  text = text.gsub(/{"([^{}]*?)": null}/, '\1')

  return {
    'comment' => nil,
    'skip' => skip,
    'call' => call,
    'assign' => assign,
    'property' => property,
    'args' => args,
    'kwargs' => kwargs,
    'result' => result,
    'text' => text,
  }

end

#parse_spec(spec) ⇒ Object



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
# File 'lib/cli.rb', line 38

def parse_spec(spec)

  # Package
  contents = YAML.load(spec)
  begin
    feature = parse_feature(contents[0])
    package = feature['result']
    assert_equal(feature['assign'], 'PACKAGE')
    assert_equal(feature['skip'], nil)
  rescue Exception
    return nil
  end

  # Features
  features = []
  for feature in contents
    feature = parse_feature(feature)
    features.push(feature)
  end

  # Scope
  scope = {}
  require(package)
  for item in ObjectSpace.each_object
    if package == String(item).downcase
      begin
        namespace = Kernel.const_get(item)
      rescue Exception
        next
      end
      for name in namespace.constants
        scope[String(name)] = namespace.const_get(name)
      end
    end
  end

  return {
    'package' => package,
    'features' => features,
    'scope' => scope,
  }

end

#parse_specs(path) ⇒ Object

Helpers



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cli.rb', line 12

def parse_specs(path)

  # Specs
  specmap = {}
  for path in Dir.glob("#{path}/**/*.yml")
    spec = parse_spec(File.read(path))
    if !spec
      next
    elsif !specmap.include?(spec['package'])
      specmap[spec['package']] = spec
    else
      specmap[spec['package']]['features'].merge!(spec['features'])
    end
  end

  # Hooks
  # TODO: implement

  # Result
  specs = Array(specmap.sort.to_h.each_value)

  return specs

end

#set_property(owner, name, value) ⇒ Object



331
332
333
334
335
336
337
338
# File 'lib/cli.rb', line 331

def set_property(owner, name, value)
  # TODO: review
  if owner.is_a?(Hash)
    owner[name] = value
    return
  end
  return owner.const_set(name, value)
end

#test_feature(feature, scope) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/cli.rb', line 201

def test_feature(feature, scope)

  # Comment
  if !!feature['comment']
    message = "\n # #{feature['comment']}\n".bold
    puts(message)
    return true
  end

  # Skip
  if !!feature['skip']
    message = " #{Emoji.find_by_alias('heavy_minus_sign').raw}  ".yellow
    message += feature['text']
    puts(message)
    return true
  end

  # Execute
  # TODO: dereference feature
  result = feature['result']
  if !!feature['property']
    begin
      property = scope
      for name in feature['property'].split('.')
        property = get_property(property, name)
      end
      if !!feature['call']
        # TODO: support kwargs
        if property.respond_to?('new')
          result = property.new(*feature['args'])
        else
          result = property.call(*feature['args'])
        end
      else
        result = property
      end
    rescue Exception
      result = 'ERROR'
    end
  end

  # Assign
  if !!feature['assign']
    owner = scope
    names = feature['assign'].split('.')
    for name in names[0..-2]
      owner = get_property(owner, name)
    end
    # TODO: ensure constants are immutable
    set_property(owner, names[-1], result)
  end

  # Compare
  # TODO: isoformat value
  if feature['result'] != nil
    success = result == feature['result']
  else
    success = result != 'ERROR'
  end
  if success
    message = " #{Emoji.find_by_alias('heavy_check_mark').raw}  ".green
    message += feature['text']
    puts(message)
  else
    begin
      result_text = JSON.generate(result)
    rescue Exception
      result_text = result.to_s
    end
    message = " #{Emoji.find_by_alias('x').raw}  ".red
    message += "#{feature['text']} # #{result_text}"
    puts(message)
  end

  return success

end

#test_spec(spec) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cli.rb', line 177

def test_spec(spec)
  passed = 0
  amount = spec['features'].length
  message = Emoji.find_by_alias('heavy_minus_sign').raw * 3 + "\n\n"
  puts(message)
  for feature in spec['features']
    result = test_feature(feature, spec['scope'])
    if result
      passed += 1
    end
  end
  success = (passed == amount)
  color = 'green'
  message = ("\n " + Emoji.find_by_alias('heavy_check_mark').raw + '  ').green.bold
  if !success
    color = 'red'
    message = ("\n " + Emoji.find_by_alias('x').raw + '  ').red.bold
  end
  message += "#{spec['package']}: #{passed}/#{amount}\n".colorize(color).bold
  puts(message)
  return success
end

#test_specs(specs) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/cli.rb', line 165

def test_specs(specs)
  success = true
  message = "\n #  Ruby\n".bold
  puts(message)
  for spec in specs
    spec_success = test_spec(spec)
    success = success && spec_success
  end
  return success
end