Module: Bryton::Lite::Tests

Defined in:
lib/bryton/lite.rb

Overview

Utilities for use in the test script.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.hshHash (readonly)

Returns:

  • (Hash)


247
248
249
# File 'lib/bryton/lite.rb', line 247

def hsh
  @hsh
end

Class Method Details

.[](k) ⇒ Object

Get the element with the given key.



259
260
261
# File 'lib/bryton/lite.rb', line 259

def self.[](k)
	return @hsh[k]
end

.[]=(k, v) ⇒ Object

Set the element with the given key and value.



264
265
266
# File 'lib/bryton/lite.rb', line 264

def self.[]=(k, v)
	return @hsh[k] = v
end

.add_to_nest(child) ⇒ Object

Created the “nested” array and adds the given results to that array



345
346
347
348
# File 'lib/bryton/lite.rb', line 345

def self.add_to_nest(child)
	@hsh['nested'] ||= []
	@hsh['nested'].push child
end

.allow_success?Boolean

Test if try_succeed can set success. You probably don’t need to call this method directly.

Returns:

  • (Boolean)


296
297
298
# File 'lib/bryton/lite.rb', line 296

def self.allow_success?
	return allow_success_recurse(@hsh)
end

.allow_success_recurse(test_hsh) ⇒ Object

Tests each nested result. If any of the nested results are set to failure, return false. Otherwise returns true.



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/bryton/lite.rb', line 302

def self.allow_success_recurse(test_hsh)
	# if any errors
	if test_hsh['errors'] and test_hsh['errors'].any?
		return false
	end
	
	# recurse into nested elements
	if test_hsh['nested']
		test_hsh['nested'].each do |child|
			if not allow_success_recurse(child)
				return false
			end
		end
	end
	
	# at this point it should be successful
	return true
end

.assert(bool, id = nil, level = 0, &block) ⇒ Object

Asserts a condition as true. Fails if condition is not true



364
365
366
367
368
# File 'lib/bryton/lite.rb', line 364

def self.assert(bool, id=nil, level=0, &block)
	if not bool
		fail id, 1, &block
	end
end

.assert_equal(expected, actual, id = nil, level = 0, &block) ⇒ Object

Asserts that two objects are equal according to ==. Fails if they are not.



378
379
380
381
382
# File 'lib/bryton/lite.rb', line 378

def self.assert_equal(expected, actual, id=nil, level=0, &block)
	if not (expected == actual)
		fail id, 1, &block
	end
end

.doneObject

done



351
352
353
354
355
356
# File 'lib/bryton/lite.rb', line 351

def self.done
	try_succeed()
	reorder_results()
	STDOUT.puts JSON.generate(@hsh)
	exit
end

.errorsObject

Returns the errors array, creating it if necessary.



322
323
324
325
# File 'lib/bryton/lite.rb', line 322

def self.errors
	@hsh['errors'] ||= []
	return @hsh['errors']
end

.fail(id = nil, level = 0, &block) ⇒ Object

Mark the test as failed.



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/bryton/lite.rb', line 392

def self.fail(id=nil, level=0, &block)
	loc = caller_locations[level]
	
	# create error object
	error = {}
	error['line'] = loc.lineno
	id and error['id'] = id
	
	# add to errors
	@hsh['errors'] ||= []
	@hsh['errors'].push error
	
	# if block
	if block_given?
		yield error
	end
end

.nestObject

Creates (if necessary) an array with the key “nested”. Creates a results hash that is nested in that array. In the do block, Bryton::Lite::Tests.hsh is set to that child results hash.



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/bryton/lite.rb', line 330

def self.nest()
	@hsh['nested'] ||= []
	child = {}
	@hsh['nested'].push child
	hold_hsh = @hsh
	@hsh = child
	
	begin
		yield
	ensure
		@hsh = hold_hsh
	end
end

.refute(bool, id = nil, level = 0, &block) ⇒ Object

Asserts a condition as false. Fails if condition is true.



371
372
373
374
375
# File 'lib/bryton/lite.rb', line 371

def self.refute(bool, id=nil, level=0, &block)
	if bool
		fail id, 1, &block
	end
end

.refute_equal(expected, actual, id = nil, level = 0, &block) ⇒ Object

Asserts that two objects are not equal according to ==. Fails if they are.



385
386
387
388
389
# File 'lib/bryton/lite.rb', line 385

def self.refute_equal(expected, actual, id=nil, level=0, &block)
	if (expected == actual)
		fail id, 1, &block
	end
end

.reorder_resultsObject

reorder_results This method is for improving the readability of a result by putting the “success” element first and “nested” last.



413
414
415
# File 'lib/bryton/lite.rb', line 413

def self.reorder_results
	@hsh = reorder_results_recurse(@hsh)
end

.reorder_results_recurse(old_hsh) ⇒ Object

reorder_results_recurse This method rcurses through test results, putting “success” first and “nested” last.



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/bryton/lite.rb', line 420

def self.reorder_results_recurse(old_hsh)
	new_hsh = {}
	nested = old_hsh.delete('nested')
	
	# add success if it exists
	if old_hsh.has_key?('success')
		new_hsh['success'] = old_hsh.delete('success')
	end
	
	# add every other element
	new_hsh = new_hsh.merge(old_hsh)
	
	# add nested last
	if nested
		new_hsh['nested'] = nested
		
		# recurse through nested results
		new_hsh['nested'].map! do |child|
			reorder_results_recurse child
		end
	end
	
	# return reformatted hash
	return new_hsh
end

.resetObject

reset



251
252
253
# File 'lib/bryton/lite.rb', line 251

def self.reset()
	@hsh = {}
end

.succeedObject

Set to success. Raises an exception if any errors exist. You probably want to use try_succeed() instead.



270
271
272
273
274
275
276
# File 'lib/bryton/lite.rb', line 270

def self.succeed
	if not allow_success?
		raise 'cannot-set-to-success'
	end
	
	return @hsh['success'] = true
end

.success?Boolean

Returns true if [‘success’] is true, false otherwise. Always returns true or false.

Returns:

  • (Boolean)


281
282
283
# File 'lib/bryton/lite.rb', line 281

def self.success?
	return @hsh['success'] ? true : false
end

.to_jsonObject

to_json



359
360
361
# File 'lib/bryton/lite.rb', line 359

def self.to_json
	return JSON.generate(@hsh)
end

.try_succeedBoolean

Try to set to success, but will not do so if there are errors or if the test is already set as fail. Does not raise an exception if the success cannot be set to true.

Returns:

  • (Boolean)


289
290
291
# File 'lib/bryton/lite.rb', line 289

def self.try_succeed
	return @hsh['success'] = allow_success?()
end