Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

extra-careful about referencing the jQuery object

  • Loading branch information...
commit 82ecbcf11b2e107a7c495163357940e4815ad0e0 1 parent fd03031
Nick Kallen authored
View
148 lib/screw.behaviors.js
@@ -1,82 +1,84 @@
-$(Screw).bind('load', function() {
- $('.status').fn({
- display: function() {
- $(this).text(
- $('.passed').length + $('.failed').length + ' test(s), ' + $('.failed').length + ' failure(s)'
- );
- }
- });
+(function($) {
+ $(Screw).bind('load', function() {
+ $('.status').fn({
+ display: function() {
+ $(this).text(
+ $('.passed').length + $('.failed').length + ' test(s), ' + $('.failed').length + ' failure(s)'
+ );
+ }
+ });
- $('.describe').fn({
- parent: function() {
- return $(this).parent('.describes').parent('.describe');
- },
- run_befores: function() {
- $(this).fn('parent').fn('run_befores');
- $(this).children('.befores').children('.before').fn('run');
- },
- run_afters: function() {
- $(this).fn('parent').fn('run_afters');
- $(this).children('.afters').children('.after').fn('run');
- },
- enqueue: function() {
- $(this).children('.its').children('.it').fn('enqueue');
- $(this).children('.describes').children('.describe').fn('enqueue');
- },
- selector: function() {
- return $(this).fn('parent').fn('selector')
- + ' > .describes > .describe:eq(' + $(this).parent('.describes').children('.describe').index(this) + ')';
- }
- });
+ $('.describe').fn({
+ parent: function() {
+ return $(this).parent('.describes').parent('.describe');
+ },
+ run_befores: function() {
+ $(this).fn('parent').fn('run_befores');
+ $(this).children('.befores').children('.before').fn('run');
+ },
+ run_afters: function() {
+ $(this).fn('parent').fn('run_afters');
+ $(this).children('.afters').children('.after').fn('run');
+ },
+ enqueue: function() {
+ $(this).children('.its').children('.it').fn('enqueue');
+ $(this).children('.describes').children('.describe').fn('enqueue');
+ },
+ selector: function() {
+ return $(this).fn('parent').fn('selector')
+ + ' > .describes > .describe:eq(' + $(this).parent('.describes').children('.describe').index(this) + ')';
+ }
+ });
- $('body > .describe').fn({
- selector: function() { return 'body > .describe' }
- });
+ $('body > .describe').fn({
+ selector: function() { return 'body > .describe' }
+ });
- $('.it').fn({
- parent: function() {
- return $(this).parent('.its').parent('.describe');
- },
- run: function() {
- try {
+ $('.it').fn({
+ parent: function() {
+ return $(this).parent('.its').parent('.describe');
+ },
+ run: function() {
try {
- $(this).fn('parent').fn('run_befores');
- $(this).data('screwunit.run')();
- } finally {
- $(this).fn('parent').fn('run_afters');
+ try {
+ $(this).fn('parent').fn('run_befores');
+ $(this).data('screwunit.run')();
+ } finally {
+ $(this).fn('parent').fn('run_afters');
+ }
+ $(this).trigger('passed');
+ } catch(e) {
+ $(this).trigger('failed', [e]);
}
- $(this).trigger('passed');
- } catch(e) {
- $(this).trigger('failed', [e]);
+ },
+ enqueue: function() {
+ var self = $(this).trigger('enqueued');
+ $(Screw)
+ .queue(function() {
+ self.fn('run');
+ setTimeout(function() { $(Screw).dequeue() }, 0);
+ });
+ },
+ selector: function() {
+ return $(this).fn('parent').fn('selector')
+ + ' > .its > .it:eq(' + $(this).parent('.its').children('.it').index(this) + ')';
}
- },
- enqueue: function() {
- var self = $(this).trigger('enqueued');
- $(Screw)
- .queue(function() {
- self.fn('run');
- setTimeout(function() { $(Screw).dequeue() }, 0);
- });
- },
- selector: function() {
- return $(this).fn('parent').fn('selector')
- + ' > .its > .it:eq(' + $(this).parent('.its').children('.it').index(this) + ')';
- }
- });
+ });
- $('.before').fn({
- run: function() { $(this).data('screwunit.run')() }
- });
+ $('.before').fn({
+ run: function() { $(this).data('screwunit.run')() }
+ });
- $('.after').fn({
- run: function() { $(this).data('screwunit.run')() }
- });
+ $('.after').fn({
+ run: function() { $(this).data('screwunit.run')() }
+ });
- $(Screw).trigger('before');
- var to_run = unescape(location.search.slice(1)) || 'body > .describe > .describes > .describe';
- $(to_run)
- .focus()
- .eq(0).trigger('scroll').end()
- .fn('enqueue');
- $(Screw).queue(function() { $(Screw).trigger('after') });
-});
+ $(Screw).trigger('before');
+ var to_run = unescape(location.search.slice(1)) || 'body > .describe > .describes > .describe';
+ $(to_run)
+ .focus()
+ .eq(0).trigger('scroll').end()
+ .fn('enqueue');
+ $(Screw).queue(function() { $(Screw).trigger('after') });
+ })
+})(jQuery);
View
133 lib/screw.builder.js
@@ -1,73 +1,76 @@
-var Screw = {
- Unit: function(fn) {
- var contents = fn.toString().match(/^[^\{]*{((.*\n*)*)}/m)[1];
- var fn = new Function("matchers", "specifications",
- "with (specifications) { with (matchers) { " + contents + " } }"
- );
+var Screw = (function($) {
+ var screw = {
+ Unit: function(fn) {
+ var contents = fn.toString().match(/^[^\{]*{((.*\n*)*)}/m)[1];
+ var fn = new Function("matchers", "specifications",
+ "with (specifications) { with (matchers) { " + contents + " } }"
+ );
- $(Screw).queue(function() {
- Screw.Specifications.context.push($('body > .describe'));
- fn.call(this, Screw.Matchers, Screw.Specifications);
- Screw.Specifications.context.pop();
- $(this).dequeue();
- });
- },
- Specifications: {
- context: [],
- describe: function(name, fn) {
- var describe = $('<li class="describe">');
- $('<h1>').text(name).appendTo(describe);
- describe
- .append('<ol class="befores">')
- .append('<ul class="its">')
- .append('<ul class="describes">')
- .append('<ol class="afters">')
+ $(Screw).queue(function() {
+ Screw.Specifications.context.push($('body > .describe'));
+ fn.call(this, Screw.Matchers, Screw.Specifications);
+ Screw.Specifications.context.pop();
+ $(this).dequeue();
+ });
+ },
+ Specifications: {
+ context: [],
+ describe: function(name, fn) {
+ var describe = $('<li class="describe">');
+ $('<h1>').text(name).appendTo(describe);
+ describe
+ .append('<ol class="befores">')
+ .append('<ul class="its">')
+ .append('<ul class="describes">')
+ .append('<ol class="afters">')
- this.context.push(describe);
- fn.call();
- this.context.pop();
+ this.context.push(describe);
+ fn.call();
+ this.context.pop();
- this.context[this.context.length-1]
- .children('.describes')
- .append(describe);
- },
- it: function(name, fn) {
- var it = $('<li class="it">');
- $('<h2>').text(name).appendTo(it);
- it.data('screwunit.run', fn);
+ this.context[this.context.length-1]
+ .children('.describes')
+ .append(describe);
+ },
+ it: function(name, fn) {
+ var it = $('<li class="it">');
+ $('<h2>').text(name).appendTo(it);
+ it.data('screwunit.run', fn);
- this.context[this.context.length-1]
- .children('.its')
- .append(it);
- },
- before: function(fn) {
- var before = $('<li class="before">')
- .data('screwunit.run', fn);
+ this.context[this.context.length-1]
+ .children('.its')
+ .append(it);
+ },
+ before: function(fn) {
+ var before = $('<li class="before">')
+ .data('screwunit.run', fn);
- this.context[this.context.length-1]
- .children('.befores')
- .append(before);
- },
- after: function(fn) {
- var after = $('<li class="after">')
- .data('screwunit.run', fn);
+ this.context[this.context.length-1]
+ .children('.befores')
+ .append(before);
+ },
+ after: function(fn) {
+ var after = $('<li class="after">')
+ .data('screwunit.run', fn);
- this.context[this.context.length-1]
- .children('.afters')
- .append(after);
+ this.context[this.context.length-1]
+ .children('.afters')
+ .append(after);
+ }
}
- }
-};
+ };
+
+ $(screw).queue(function() { $(screw).trigger('loading') });
+ $(function() {
+ $('<div class="describe">')
+ .append('<h3 class="status">')
+ .append('<ol class="befores">')
+ .append('<ul class="describes">')
+ .append('<ol class="afters">')
+ .appendTo('body');
-$(Screw).queue(function() { $(Screw).trigger('loading') });
-$(function() {
- $('<div class="describe">')
- .append('<h3 class="status">')
- .append('<ol class="befores">')
- .append('<ul class="describes">')
- .append('<ol class="afters">')
- .appendTo('body');
-
- $(Screw).dequeue();
- $(Screw).trigger('load');
-});
+ $(screw).dequeue();
+ $(screw).trigger('load');
+ });
+ return screw;
+})(jQuery);
View
74 lib/screw.events.js
@@ -1,37 +1,39 @@
-$(Screw)
- .bind('load', function() {
- $('.describe, .it')
- .click(function() {
- document.location = location.href.split('?')[0] + '?' + $(this).fn('selector');
- return false;
- })
- .focus(function() {
- return $(this).addClass('focused');
- })
- .bind('scroll', function() {
- document.body.scrollTop = $(this).offset().top;
- });
+(function($) {
+ $(Screw)
+ .bind('load', function() {
+ $('.describe, .it')
+ .click(function() {
+ document.location = location.href.split('?')[0] + '?' + $(this).fn('selector');
+ return false;
+ })
+ .focus(function() {
+ return $(this).addClass('focused');
+ })
+ .bind('scroll', function() {
+ document.body.scrollTop = $(this).offset().top;
+ });
- $('.it')
- .bind('enqueued', function() {
- $(this).addClass('enqueued');
- })
- .bind('running', function() {
- $(this).addClass('running');
- })
- .bind('passed', function() {
- $(this).addClass('passed');
- })
- .bind('failed', function(e, reason) {
- $(this).addClass('failed');
- $('<p class="error">')
- .text(reason.toString())
- .appendTo($(this));
- })
- })
- .bind('before', function() {
- $('.status').text('Running...');
- })
- .bind('after', function() {
- $('.status').fn('display')
- })
+ $('.it')
+ .bind('enqueued', function() {
+ $(this).addClass('enqueued');
+ })
+ .bind('running', function() {
+ $(this).addClass('running');
+ })
+ .bind('passed', function() {
+ $(this).addClass('passed');
+ })
+ .bind('failed', function(e, reason) {
+ $(this).addClass('failed');
+ $('<p class="error">')
+ .text(reason.toString())
+ .appendTo($(this));
+ })
+ })
+ .bind('before', function() {
+ $('.status').text('Running...');
+ })
+ .bind('after', function() {
+ $('.status').fn('display')
+ })
+})(jQuery);
View
114 lib/screw.matchers.js
@@ -1,64 +1,68 @@
-Screw.Matchers = {
- expect: function(actual) {
- return {
- to: function(matcher, expected, not) {
- var matched = matcher.match(expected, actual);
- if (not ? matched : !matched) {
- throw(matcher.failure_message(expected, actual, not));
+Screw.Matchers = (function($) {
+ var matchers = {
+ expect: function(actual) {
+ return {
+ to: function(matcher, expected, not) {
+ var matched = matcher.match(expected, actual);
+ if (not ? matched : !matched) {
+ throw(matcher.failure_message(expected, actual, not));
+ }
+ },
+ to_not: function(matcher, expected) {
+ this.to(matcher, expected, true);
}
- },
- to_not: function(matcher, expected) {
- this.to(matcher, expected, true);
}
- }
- },
- equal: {
- match: function(expected, actual) {
- if (Screw.Matchers.equal.by_type[expected.constructor])
- return Screw.Matchers.equal.by_type[expected.constructor](expected, actual);
- else
- return expected == actual;
},
- failure_message: function(expected, actual, not) {
- return 'expected "' + $.print(actual) + (not ? '" to not equal "' : '" to equal "') + $.print(expected) + '"';
- }
- },
- match: {
- match: function(expected, actual) {
- if (expected.constructor == String) {
- return actual.indexOf(expected) > -1;
- } else {
- return expected.exec(actual);
+ equal: {
+ match: function(expected, actual) {
+ if (Screw.Matchers.equal.by_type[expected.constructor])
+ return Screw.Matchers.equal.by_type[expected.constructor](expected, actual);
+ else
+ return expected == actual;
+ },
+ failure_message: function(expected, actual, not) {
+ return 'expected "' + $.print(actual) + (not ? '" to not equal "' : '" to equal "') + $.print(expected) + '"';
}
},
- failure_message: function(expected, actual, not) {
- return 'expected "' + $.print(actual) + (not ? '" to not match "' : '" to match "') + $.print(expected) + '"'
- }
- },
- be_empty: {
- match: function(expected, actual) {
- if (actual.length == undefined) {
- throw(actual.toString() + " does not respond to length");
+ match: {
+ match: function(expected, actual) {
+ if (expected.constructor == String) {
+ return actual.indexOf(expected) > -1;
+ } else {
+ return expected.exec(actual);
+ }
+ },
+ failure_message: function(expected, actual, not) {
+ return 'expected "' + $.print(actual) + (not ? '" to not match "' : '" to match "') + $.print(expected) + '"'
}
- return actual.length == 0;
},
- failure_message: function(expected, actual, not) {
- return 'expected "' + $.print(actual) + (not ? '" to not be empty' : '" to be empty');
+ be_empty: {
+ match: function(expected, actual) {
+ if (actual.length == undefined) {
+ throw(actual.toString() + " does not respond to length");
+ }
+ return actual.length == 0;
+ },
+ failure_message: function(expected, actual, not) {
+ return 'expected "' + $.print(actual) + (not ? '" to not be empty' : '" to be empty');
+ }
}
- }
-};
-
-Screw.Matchers.equal.by_type = {};
-Screw.Matchers.equal.by_type[Array] = function(expected, actual) {
- for (var i = 0; i < actual.length; i++)
- if (!Screw.Matchers.equal.match(expected[i], actual[i])) return false;
- return actual.length == expected.length;
-};
+ };
+
+ matchers.equal.by_type = {};
+ matchers.equal.by_type[Array] = function(expected, actual) {
+ for (var i = 0; i < actual.length; i++)
+ if (!Screw.Matchers.equal.match(expected[i], actual[i])) return false;
+ return actual.length == expected.length;
+ };
-Screw.Matchers.equal.by_type[Object] = function(expected, actual) {
- for (var key in expected)
- if (!Screw.Matchers.equal.match(expected[key], actual[key])) return false;
- for (var key in actual)
- if (!Screw.Matchers.equal.match(actual[key], expected[key])) return false;
- return true;
-};
+ matchers.equal.by_type[Object] = function(expected, actual) {
+ for (var key in expected)
+ if (!Screw.Matchers.equal.match(expected[key], actual[key])) return false;
+ for (var key in actual)
+ if (!Screw.Matchers.equal.match(actual[key], expected[key])) return false;
+ return true;
+ };
+
+ return matchers;
+})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.