Skip to content

Instantly share code, notes, and snippets.

@obenjiro
Last active August 29, 2015 14:02
Show Gist options
  • Save obenjiro/a99f028bf2ccf065744a to your computer and use it in GitHub Desktop.
Save obenjiro/a99f028bf2ccf065744a to your computer and use it in GitHub Desktop.
Assert that good for debugging
function Test(a, b){ // noprotect
assert(typeOf({a:a}, 'string'), function(m){ throw m });
assert(typeOf({b:b}, 'string'), function(m){ throw m });
var result = a + b;
result = null;
assert(notNullOrUndefined({result:result}), function(m){ throw m });
assert(equal({result:result}, 'ac'), function(m){ throw m });
return result;
}
function assert(isValid, errorCallback) {
var check = isValid();
if (check.message) {
if (!check.isValid) {
errorCallback(check.message);
}
} else {
errorCallback();
}
}
function typeOf(val, type) {
return function (){
var firstProp = getFirstPropName(val);
var firstVal = val[firstProp];
return {
isValid: typeof firstVal === type,
message: '[' + firstProp + '] must be a type of "' + type + '", current type is "' + (typeof firstVal) + '"'
};
};
}
function equal(val, valToComare) {
return function(){
var firstProp = getFirstPropName(val);
var firstVal = val[firstProp];
return {
isValid: firstVal === valToComare,
message: '[' + firstProp + '] must be equal to "' + valToComare + '", current value is "' + firstVal + '"'
};
};
};
function notNullOrUndefined(val) {
return function(){
var firstProp = getFirstPropName(val);
var firstVal = val[firstProp];
return {
isValid: firstVal != null,
message: '[' + firstProp + '] must not be Null or Undefined, current value is "' + firstVal + '"'
};
};
}
function getFirstPropName(obj){
for (var key in obj){
if (obj.hasOwnProperty(key)) {
return key;
}
}
return null;
}
console.log(Test('a', 'b'));
Test = (a, b) -> # noprotect
assert typeOf(a:a, "string"), (m) -> throw m
assert typeOf(b:b, "string"), (m) -> throw m
result = a + b
result = null
assert notNullOrUndefined(result:result), (m) -> throw m
assert equal(result:result), (m) -> throw m
result
assert = (isValid, errorCallback) ->
check = isValid()
if check.message
errorCallback check.message unless check.isValid
else
errorCallback()
return
typeOf = (val, type) ->
->
firstProp = getFirstPropName(val)
firstVal = val[firstProp]
isValid: typeof firstVal is type
message: "[" + firstProp + "] must be a type of \"" + type + "\", current type is \"" + (typeof firstVal) + "\""
equal = (val, valToComare) ->
->
firstProp = getFirstPropName(val)
firstVal = val[firstProp]
isValid: firstVal == valToComare
message: '[' + firstProp + '] must be equal to "' + valToComare + '", current value is "' + firstVal + '"'
notNullOrUndefined = (val) ->
->
firstProp = getFirstPropName(val)
firstVal = val[firstProp]
isValid: firstVal?
message: "[" + firstProp + "] must not be Null or Undefined, current value is \"" + firstVal + "\""
getFirstPropName = (obj) ->
for key of obj
return key if obj.hasOwnProperty(key)
null
console.log Test("a", "b")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment