Top Level Namespace
Defined Under Namespace
Classes: BuiltinFunctions
Instance Method Summary collapse
- #dereference_value(value, scope) ⇒ Object
- #get_property(owner, name) ⇒ Object
- #parse_feature(feature) ⇒ Object
- #parse_spec(path) ⇒ Object
-
#parse_specs(path) ⇒ Object
Helpers.
- #set_property(owner, name, value) ⇒ Object
- #test_feature(feature, scope, exit_first = false) ⇒ Object
- #test_spec(spec, exit_first = false) ⇒ Object
- #test_specs(specs, exit_first = false) ⇒ Object
Instance Method Details
#dereference_value(value, scope) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/cli.rb', line 380 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
400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/cli.rb', line 400 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] elsif owner.is_a?(Class) return owner.method(name) end return owner.public_method(name) end |
#parse_feature(feature) ⇒ Object
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 191 192 193 194 195 |
# File 'lib/cli.rb', line 110 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 103 104 105 106 107 |
# 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("module UserScope\n" + documents[1]['rb'] + "\nend") for name in UserScope.constants scope["$#{name}"] = UserScope.const_get(name) end default = Class.new methods = Class.new.extend(UserScope) for name in methods.public_methods if !default.respond_to?(name) scope["$#{name}"] = methods.public_method(name) end 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
415 416 417 418 419 420 421 422 423 424 |
# File 'lib/cli.rb', line 415 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, exit_first = false) ⇒ Object
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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/cli.rb', line 247 def test_feature(feature, scope, exit_first=false) # Comment if !!feature['comment'] = "\n # #{feature['comment']}\n".bold puts() return true end # Skip if !!feature['skip'] = " #{Emoji.find_by_alias('heavy_minus_sign').raw} ".yellow += feature['text'] puts() return true end # Dereference 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 set_property(owner, names[-1], result) end # Compare if feature['result'] != nil success = result == feature['result'] else success = result != 'ERROR' end if success = " #{Emoji.find_by_alias('heavy_check_mark').raw} ".green += feature['text'] puts() else begin result_text = JSON.generate(result) rescue Exception result_text = result.to_s end = " #{Emoji.find_by_alias('x').raw} ".red += "#{feature['text']}\n" if exception += "Exception: #{exception}".red.bold else += "Assertion: #{result_text} != #{JSON.generate(feature['result'])}".red.bold end puts() if exit_first puts('---') puts('Scope (current execution scope):') puts("[#{scope.keys.join(', ')}]") if exception puts('---') raise exception else exit(1) end end end return success end |
#test_spec(spec, exit_first = false) ⇒ Object
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 |
# File 'lib/cli.rb', line 216 def test_spec(spec, exit_first=false) # Message = Emoji.find_by_alias('heavy_minus_sign').raw * 3 + "\n\n" puts() # Test spec passed = 0 for feature in spec['features'] result = test_feature(feature, spec['scope'], exit_first) if result passed += 1 end end success = (passed == spec['stats']['features']) # Message color = 'green' = ("\n " + Emoji.find_by_alias('heavy_check_mark').raw + ' ').green.bold if !success color = 'red' = ("\n " + Emoji.find_by_alias('x').raw + ' ').red.bold end += "#{spec['package']}: #{passed - spec['stats']['comments'] - spec['stats']['skipped']}/#{spec['stats']['tests'] - spec['stats']['skipped']}\n".colorize(color).bold puts() return success end |
#test_specs(specs, exit_first = false) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/cli.rb', line 198 def test_specs(specs, exit_first=false) # Message = "\n # Ruby\n".bold puts() # Test specs success = true for spec in specs spec_success = test_spec(spec, exit_first) success = success && spec_success end return success end |