From ad884e358383200846eef6db76c97f67283fdaea Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Tue, 18 Jan 2011 21:38:19 +0100 Subject: [PATCH] Add assertion API to jQuery. Signed-off-by: Florent Bruneau --- htdocs/javascript/core.js | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/htdocs/javascript/core.js b/htdocs/javascript/core.js index d9f38f3..bf0e5d1 100644 --- a/htdocs/javascript/core.js +++ b/htdocs/javascript/core.js @@ -18,9 +18,27 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ***************************************************************************/ +// {{{ Assertion + +// }}} // {{{ jQuery object extension (function($) { + function assert(condition, text) { + if ($.isFunction(condition)) { + condition = condition(); + } + if (condition) { + return this; + } + if (!text) { + throw "Assertion failed"; + } else { + throw "Assertion failed: " + text; + } + } + + /* Add new functions to jQuery namesapce */ $.extend({ /* The goal of the following functions is to provide an AJAX API that @@ -113,7 +131,9 @@ } $('body').appendTo(form); form.submit(); - } + }, + + assert: assert }); /* Add new functions to jQuery objects */ @@ -176,6 +196,26 @@ +'width='+w+',height='+h); return false; }); + }, + + assert: assert, + + assertLength: function(len) { + return this.assert(function() { + return $(this).length == len; + }); + }, + + assertId: function(id) { + return this.assert(function() { + return $(this).attr('id') == id; + }); + }, + + assertClass: function(clazz) { + return this.assert(function() { + return $(this).hasClass(clazz); + }); } }); })(jQuery); -- 2.1.4