From: Florent Bruneau Date: Tue, 18 Jan 2011 20:38:19 +0000 (+0100) Subject: Add assertion API to jQuery. X-Git-Tag: core/1.1.2~5 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=ad884e358383200846eef6db76c97f67283fdaea;p=platal.git Add assertion API to jQuery. Signed-off-by: Florent Bruneau --- 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);