Skip to content

Instantly share code, notes, and snippets.

@dinigo
Last active December 18, 2017 17:35
Show Gist options
  • Save dinigo/f63484c603537cddb0ef66ad47f7c619 to your computer and use it in GitHub Desktop.
Save dinigo/f63484c603537cddb0ef66ad47f7c619 to your computer and use it in GitHub Desktop.
Basic Assert in javascript ES5 (syntax) for Appscript

Example:

Assert.true(true,'This is truthful');
Assert.true(false,'This is truthful');
Assert.false(false,'This is a lie');
Assert.false(true,'This is a lie');
Assert.equals(typeof 'hola', 'string', 'This variable contains a message');
Assert.equals(typeof 3, 'string', 'This variable contains a message');

Result:

๐Ÿ ๏ผ This is truthful
๐ŸŽ ๏ผ This is truthful
    condition is "false"
๐Ÿ ๏ผ This is a lie
๐ŸŽ ๏ผ This is a lie
    condition is "false"
๐Ÿ ๏ผ This variable contains a message
๐ŸŽ ๏ผ This variable contains a message
    provided "number" not equals expected "string"
/**
* Basic class
*/
function Assert() {}
/**
* Compose the string to see if the test succeded or failed
*
* @return {string|Error} Message with the corresponding
*/
Assert.basicCheck = (condition,message)=>(condition ? '๐Ÿ' : '๐ŸŽ') + ' ๏ผ ' + message;
/**
* Print the message in the correct way
*/
Assert.print = function(condition, message, error) {
var res = this.basicCheck(condition, message);
console.log(res + (condition? '' : '\n ' + error));
}
/**
* Checks if the condition is true, prints and returns true if so
*
* @param {boolean} condition to be tested
* @param {string} message describe the assert
* @return {boolean} wheter the assert succeded or failed
*/
Assert.true = function(condition, message) {
var error = 'condition doesn\'t match';
this.print(condition, message, error)
return condition;
}
/**
* Checks if the condition is false, prints and returns true if so
*
* @param {boolean} condition to be tested
* @param {string} message describe the assert
* @return {boolean} wheter the assert succeded or failed
*/
Assert.false = function(condition, message) {
return this.true(!condition, message);
}
/**
* Assert function to implement a couple of tests
*
* @param {Object} first object to be tested
* @param {Object} second object to be tested
* @return {boolean} wheter the assert succeded or failed
*/
Assert.equals = function(first, second, message) {
var condition = first == second;
var error = 'provided "' + first + '" not equals expected "' + second + '"';
this.print(condition, message, error);
return condition;
}
Assert.true(true, 'This is truthful');
Assert.true(false, 'This is truthful');
Assert.false(false, 'This is a lie');
Assert.false(true, 'This is a lie');
Assert.equals(typeof 'hola', 'string', 'This variable contains a message');
Assert.equals(typeof 3, 'string', 'This variable contains a message');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment