Permalink
Browse files

global before and afters

different quoting rules for error messages
  • Loading branch information...
1 parent 07b7a59 commit fd03031c32de4d167d8889c229fe69821b85577c Nick Kallen committed May 4, 2008
Showing with 54 additions and 87 deletions.
  1. +2 −3 lib/jquery.print.js
  2. +7 −3 lib/screw.behaviors.js
  3. +8 −67 lib/screw.builder.js
  4. +3 −1 lib/screw.events.js
  5. +3 −3 lib/screw.matchers.js
  6. +22 −2 spec/behaviors_spec.js
  7. +8 −8 spec/matchers_spec.js
  8. +1 −0 spec/suite.html
View
@@ -1,10 +1,9 @@
(function($) {
- $.fn.print = function() {
- var obj = this.get();
+ $.print = function(obj) {
if (obj instanceof Function) {
return obj.toString().match(/^([^\{]*) {/)[1];
} else if(obj instanceof Array) {
- return "[" + obj.toString() + "]";
+ return "[" + $.print(obj.toString()) + "]";
} else if(obj instanceof HTMLElement) {
return "<" + obj.tagName + " " + (obj.className != "" ? "class='" + obj.className + "'" : "") +
(obj.id != "" ? "id='" + obj.id + "'" : "") + ">";
@@ -24,11 +24,14 @@ $(Screw).bind('load', function() {
$(this).children('.describes').children('.describe').fn('enqueue');
},
selector: function() {
- var parent_selector = $(this).fn('parent').fn('selector');
- return (parent_selector ? parent_selector : 'body')
+ return $(this).fn('parent').fn('selector')
+ ' > .describes > .describe:eq(' + $(this).parent('.describes').children('.describe').index(this) + ')';
}
});
+
+ $('body > .describe').fn({
+ selector: function() { return 'body > .describe' }
+ });
$('.it').fn({
parent: function() {
@@ -70,9 +73,10 @@ $(Screw).bind('load', function() {
});
$(Screw).trigger('before');
- var to_run = unescape(location.search.slice(1)) || 'body > .describes > .describe';
+ 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') });
});
View
@@ -6,7 +6,7 @@ var Screw = {
);
$(Screw).queue(function() {
- Screw.Specifications.context.push($('body'));
+ Screw.Specifications.context.push($('body > .describe'));
fn.call(this, Screw.Matchers, Screw.Specifications);
Screw.Specifications.context.pop();
$(this).dequeue();
@@ -56,77 +56,18 @@ var Screw = {
.children('.afters')
.append(after);
}
- },
- 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);
- }
- }
- },
- equal: {
- match: function(expected, actual) {
- if (Screw.Matchers.equal.by_type[expected.constructor.name])
- return Screw.Matchers.equal.by_type[expected.constructor.name](expected, actual);
- else
- return expected == actual;
- },
- failure_message: function(expected, actual, not) {
- return 'expected ' + $([actual]).print() + (not ? ' to not equal ' : ' to equal ') + $([expected]).print();
- },
- 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;
- },
- 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;
- }
- }
- },
- 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 ' + $([actual]).print() + (not ? ' to not match ' : ' to match ') + $([expected]).print();
- }
- },
- 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 ' + $([actual]).print() + (not ? ' to not be empty' : ' to be empty');
- }
- }
}
};
$(Screw).queue(function() { $(Screw).trigger('loading') });
$(function() {
- $('body')
- .append($('<h3 class="status">'))
- .append($('<ul class="describes">'))
+ $('<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');
});
View
@@ -6,8 +6,10 @@ $(Screw)
return false;
})
.focus(function() {
- $('body').get(0).scrollTop = $(this).offset().top;
return $(this).addClass('focused');
+ })
+ .bind('scroll', function() {
+ document.body.scrollTop = $(this).offset().top;
});
$('.it')
@@ -20,7 +20,7 @@ Screw.Matchers = {
return expected == actual;
},
failure_message: function(expected, actual, not) {
- return 'expected ' + $([actual]).print() + (not ? ' to not equal ' : ' to equal ') + $([expected]).print();
+ return 'expected "' + $.print(actual) + (not ? '" to not equal "' : '" to equal "') + $.print(expected) + '"';
}
},
match: {
@@ -32,7 +32,7 @@ Screw.Matchers = {
}
},
failure_message: function(expected, actual, not) {
- return 'expected ' + $([actual]).print() + (not ? ' to not match ' : ' to match ') + $([expected]).print();
+ return 'expected "' + $.print(actual) + (not ? '" to not match "' : '" to match "') + $.print(expected) + '"'
}
},
be_empty: {
@@ -43,7 +43,7 @@ Screw.Matchers = {
return actual.length == 0;
},
failure_message: function(expected, actual, not) {
- return 'expected ' + $([actual]).print() + (not ? ' to not be empty' : ' to be empty');
+ return 'expected "' + $.print(actual) + (not ? '" to not be empty' : '" to be empty');
}
}
};
@@ -1,7 +1,27 @@
Screw.Unit(function() {
- describe('behaviors', function() {
+ var global_before_invoked = false, global_after_invoked = false;
+ before(function() { global_before_invoked = true });
+ after(function() { global_after_invoked = true });
+
+ describe('Behaviors', function() {
describe('#run', function() {
- describe("A describe with a before and after block", function() {
+ describe("a simple [describe]", function() {
+ it("invokes the global [before] before an [it]", function() {
+ expect(global_before_invoked).to(equal, true);
+ global_before_invoked = false;
+ });
+
+ it("invokes the global [before] before each [it]", function() {
+ expect(global_before_invoked).to(equal, true);
+ global_after_invoked = false;
+ });
+
+ it("invokes the global [after] after an [it]", function() {
+ expect(global_after_invoked).to(equal, true);
+ });
+ });
+
+ describe("a [describe] with a [before] and [after] block", function() {
var before_invoked = false, after_invoked = false;
before(function() { before_invoked = true });
after(function() { after_invoked = true });
View
@@ -31,13 +31,13 @@ Screw.Unit(function() {
});
describe(".failure_message", function() {
- it("prints 'expected [expected] to (not) be equal [actual]'", function() {
+ it('prints "expected "expected" to (not) be equal "actual""', function() {
var message = null;
try { expect(1).to(equal, 2) } catch(e) { message = e }
- expect(message).to(equal, "expected [1] to equal [2]");
+ expect(message).to(equal, 'expected "1" to equal "2"');
try { expect(1).to_not(equal, 1) } catch(e) { message = e }
- expect(message).to(equal, "expected [1] to not equal [1]");
+ expect(message).to(equal, 'expected "1" to not equal "1"');
});
});
});
@@ -58,13 +58,13 @@ Screw.Unit(function() {
});
describe(".failure_message", function() {
- it("prints 'expected [actual] to (not) match [expected]", function() {
+ it('prints "expected "actual" to (not) match "expected"', function() {
var message = null;
try { expect("hello").to(match, "schmello") } catch(e) { message = e }
- expect(message).to(equal, "expected [hello] to match [schmello]");
+ expect(message).to(equal, 'expected "hello" to match "schmello"');
try { expect("hello").to_not(match, "ello") } catch(e) { message = e }
- expect(message).to(equal, "expected [hello] to not match [ello]");
+ expect(message).to(equal, 'expected "hello" to not match "ello"');
});
});
});
@@ -79,10 +79,10 @@ Screw.Unit(function() {
it("prints 'expected [actual] to (not) be empty", function() {
var message = null;
try { expect([1]).to(be_empty) } catch(e) { message = e }
- expect(message).to(equal, "expected [1] to be empty");
+ expect(message).to(equal, 'expected "[1]" to be empty');
try { expect([]).to_not(be_empty) } catch(e) { message = e }
- expect(message).to(equal, "expected [] to not be empty");
+ expect(message).to(equal, 'expected "[]" to not be empty');
});
});
});
View
@@ -7,6 +7,7 @@
<script src="../lib/screw.matchers.js"></script>
<script src="../lib/screw.events.js"></script>
<script src="../lib/screw.behaviors.js"></script>
+ <script src="spec_helper.js"></script>
<script src="behaviors_spec.js"></script>
<script src="matchers_spec.js"></script>

0 comments on commit fd03031

Please sign in to comment.