Skip to content

Commit

Permalink
extra-careful about referencing the jQuery object
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Kallen committed May 4, 2008
1 parent fd03031 commit 82ecbcf
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 229 deletions.
148 changes: 75 additions & 73 deletions 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);
133 changes: 68 additions & 65 deletions 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);
74 changes: 38 additions & 36 deletions 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);

0 comments on commit 82ecbcf

Please sign in to comment.