Top Level Namespace

Defined Under Namespace

Classes: BuiltinFunctions

Instance Method Summary collapse

Instance Method Details

#dereference_value(value, scope) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/cli.rb', line 361

def dereference_value(value, scope)
  if value.is_a?(Hash) && value.length == 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



381
382
383
384
385
386
387
388
389
390
391
# File 'lib/cli.rb', line 381

def get_property(owner, name)
  if owner.is_a?(Method)
    owner = owner.call()
  end
  if owner.class == Hash
    return owner[name]
  elsif owner.class == Array
    return owner[name.to_i]
  end
  return owner.method(name)
end

#parse_feature(feature) ⇒ Object



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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cli.rb', line 105

def parse_feature(feature)

  # General
  if feature.is_a?(String)
    match = /^(?:\((.*)\))?(\w.*)$/.match(feature)
    skip, comment = match[1], match[2]
    if !!skip
      skip = !skip.split(':').include?('rb')
    end
    return {'assign' => nil, 'comment' => comment, 'skip' => skip}
  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
    skip = !skip.split(':').include?('rb')
  end
  if !assign && !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} == #{result == 'ERROR' ? result : 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(path) ⇒ Object



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

def parse_spec(path)

  # Package
  documents = []
  contents = File.read(path)
  YAML.load_stream(contents) do |document|
    documents.push(document)
  end
  feature = parse_feature(documents[0][0])
  if feature['skip']
    return nil
  end
  package = feature['comment']

  # Features
  skip = false
  features = []
  for feature in documents[0]
    feature = parse_feature(feature)
    features.push(feature)
    if feature['comment']
      skip = feature['skip']
    end
    feature['skip'] = skip || feature['skip']
  end

  # Scope
  scope = {}
  scope['$import'] = BuiltinFunctions.new().public_method(:builtin_import)
  if documents.length > 1 && documents[1]['rb']
    eval(documents[1]['rb'])
    hook_scope = Functions.new()
    for name in hook_scope.public_methods
      # TODO: filter ruby builtin methods
      scope["$#{name}"] = hook_scope.public_method(name)
    end
  end

  # Stats
  stats = {'features' => 0, 'comments' => 0, 'skipped' => 0, 'tests' => 0}
  for feature in features
    stats['features'] += 1
    if feature['comment']
      stats['comments'] += 1
    else
      stats['tests'] += 1
      if feature['skip']
        stats['skipped'] += 1
      end
    end
  end

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

end

#parse_specs(path) ⇒ Object

Helpers



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

def parse_specs(path)

  # Paths
  paths = []
  if path
    if File.file?(path)
      paths = [path]
    elsif File.directory?(path)
      paths = Dir.glob("#{path}/*.yml")
    end
  end
  if !path
    if paths.empty?
      paths = Dir.glob('packspec.yml')
    end
    if paths.empty?
      paths = Dir.glob("packspec/*.yml")
    end
  end

  # Specs
  specs = []
  for path in paths
    spec = parse_spec(path)
    if spec
      specs.push(spec)
    end
  end

  return specs

end

#set_property(owner, name, value) ⇒ Object



394
395
396
397
398
399
400
401
402
403
# File 'lib/cli.rb', line 394

def set_property(owner, name, value)
  if owner.class == Hash
    owner[name] = value
    return
  elsif owner.class == Array
    owner[name.to_i] = value
    return
  end
  return owner.const_set(name, value)
end

#test_feature(feature, scope) ⇒ Object



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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/cli.rb', line 242

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

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

  # Execute
  exception = nil
  result = feature['result']
  if !!feature['property']
    begin
      property = scope
      for name in feature['property'].split('.')
        property = get_property(property, name)
      end
      if !!feature['call']
        args = feature['args'].dup
        if !feature['kwargs'].empty?
          args.push(Hash[feature['kwargs'].map{|k, v| [k.to_sym, v]}])
        end
        if property.respond_to?('new')
          result = property.new(*args)
        else
          result = property.call(*args)
        end
      else
        result = property
        if result.is_a?(Method)
          result = result.call()
        end
      end
    rescue Exception => exc
      exception = exc
      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
  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']}\n"
    if exception
      message += "Exception: #{exception}".red.bold
    else
      message += "Assertion: #{result_text} != #{JSON.generate(feature['result'])}".red.bold
    end
    puts(message)
  end

  return success

end

#test_spec(spec) ⇒ Object



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

def test_spec(spec)

  # Message
  message = Emoji.find_by_alias('heavy_minus_sign').raw * 3 + "\n\n"
  puts(message)

  # Test spec
  passed = 0
  for feature in spec['features']
    result = test_feature(feature, spec['scope'])
    if result
      passed += 1
    end
  end
  success = (passed == spec['stats']['features'])

  # Message
  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 - spec['stats']['comments'] - spec['stats']['skipped']}/#{spec['stats']['tests'] - spec['stats']['skipped']}\n".colorize(color).bold
  puts(message)

  return success

end

#test_specs(specs) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cli.rb', line 193

def test_specs(specs)

  # Message
  message = "\n #  Ruby\n".bold
  puts(message)

  # Test specs
  success = true
  for spec in specs
    spec_success = test_spec(spec)
    success = success && spec_success
  end

  return success

end