Skip to content

Commit

Permalink
Start on sum.
Browse files Browse the repository at this point in the history
  • Loading branch information
darrencauthon committed Dec 28, 2011
1 parent 318494f commit b35c2d1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
4 changes: 3 additions & 1 deletion mathstuff.js
@@ -1,5 +1,7 @@
(function( $ ) {
$.fn.sum = function() {
return 0
if (this.length == 0) return 0;
var value = this.val();
return isNaN(value) ? 0 : value;
};
})( jQuery );
34 changes: 31 additions & 3 deletions mathstuff.tests.html
Expand Up @@ -10,9 +10,33 @@
<script>
$(document).ready(function(){

test("sum returns zero when passed nothing", function() {
test("sum returns zero when given nothing", function() {
var value = $('input[name=nothing]').sum();
equal( value, 0, "should return zero" );
equal(value, 0, "should return zero" );
});

test("sum returns 1 when given a single-item selector with value of ", function() {
$('input[name=one]').val(1);
var value = $('input[name=one]').sum();
equal(value, 1, "should return 1");
});

test("sum returns 2 when given a single-item selector with value of 2", function() {
$('input[name=one]').val(2);
var value = $('input[name=one]').sum();
equal(value, 2, "should return 2");
});

test("sum returns 0 when given a single-item selector with value of NaN", function() {
$('input[name=one]').val(NaN);
var value = $('input[name=one]').sum();
equal(value, 0, "should return 0");
});

test("sum returns 0 when given a single-item selector with non-numeric value", function() {
$('input[name=one]').val("abc");
var value = $('input[name=one]').sum();
equal(value, 0, "should return 0");
});

});
Expand All @@ -25,6 +49,10 @@ <h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
<div id="qunit-fixture">
<form>
<input name="one" />
</form>
</div>
</body>
</html>

0 comments on commit b35c2d1

Please sign in to comment.