Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fizx committed Dec 29, 2009
1 parent a64045f commit e01c765
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
17 changes: 15 additions & 2 deletions jquery.parsley.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,22 @@ StringNodeList.prototype.toString = function() {
*/
StringNodeList.prototype.normalizeSpace = function() {
jQuery.each(this, function() {
this.string = this.string.replace(/\s+/g, ' ').replace(/^\s+/m, '').replace(/\s+jQuery/m, '');
this.string = this.string.replace(/\s+/g, ' ').replace(/^\s+/m, '').replace(/\s+$/m, '');
});
return this;
}

/**
* Creates an array of strings that mirrors this StringNodeList.
*/
StringNodeList.prototype.toSimple = function() {
var array = [];
jQuery.each(this, function() {
array.push(this.string);
});
return array;
}

/**
* The core function is used to convert jQuery objects to
* StringNodeLists. i.e.:
Expand All @@ -63,6 +74,8 @@ StringNodeList.prototype.normalizeSpace = function() {
* });
*
* The following shortcut arguments are also available:
* - extract()
* => function(node) { return jQuery(node).text(); }
* - extract("@foo")
* => function(node) { return jQuery(node).attr("foo"); }
* - extract(/regex/)
Expand All @@ -85,7 +98,7 @@ jQuery.fn.extract = function(func) {
}

// extract("@attribute")
if (func instanceof String && func[0] == "@") {
if (typeof(func) == "string" && func[0] == "@") {
var attr = func.substring(1);
func = function(node) {
return jQuery(node).attr(attr);
Expand Down
62 changes: 30 additions & 32 deletions tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,41 @@
<script>

$(document).ready(function(){
test("extraction", function() {
equals($(".date").extract(/\d+/).toString(), "[<12(18)>, <01(21)>]");
test("extracting regex", function() {
same($(".date").extract(/\d+/).toSimple(), ["12", "01"]);
});

test("normalization", function() {
equals($(".message").extract().normalizeText().toString(), "[<Merry christmas!(26)>, <Happy new year!(29)>]");
test("extracting attribute", function() {
same($(".message").extract("@id").toSimple(), ["b", "a"]);
});

test("collation", function() {
var parselet = {
page: location.href,
messages: [{

}]
}

test("extracting function", function() {
same($(".date").extract(function(node){
var text = $(node).text();
var arr = text.split("/");
var max = 0;
$.each(arr,function(){
var i = parseInt(this);
if(i > max) max = i;
});
return max;
}).toSimple(), [25, 10]);
});


// module("Module A");
//
// test("first test within module", function() {
// ok( true, "all pass" );
// });
//
// test("second test within module", function() {
// ok( true, "all pass" );
// });
//
// module("Module B");
//
// test("some other test", function() {
// expect(2);
// equals( true, false, "failing test" );
// equals( true, true, "passing test" );
// });

test("trailing space normalization", function() {
same($(".message").extract().toSimple(), ["Merry christmas! ", "Happy new year!"]);
same($(".message").extract().normalizeSpace().toSimple(), ["Merry christmas!", "Happy new year!"]);
});

// test("collation", function() {
// var parselet = {
// page: location.href,
// messages: [{
//
// }]
// }
//
// });
});
</script>

Expand All @@ -62,7 +60,7 @@ <h1>Test dom</h1>
<ul>
<li>
<span class="date">12/25/09</span>
<span class="message" id="b">Merry christmas! </span>
<span class="message" id="b">Merry christmas! </span>
</li>
<li>
<span class="date">01/01/10</span>
Expand Down

0 comments on commit e01c765

Please sign in to comment.