Skip to content

Instantly share code, notes, and snippets.

@chatrjr
Created December 12, 2012 19:55
Show Gist options
  • Select an option

  • Save chatrjr/4271007 to your computer and use it in GitHub Desktop.

Select an option

Save chatrjr/4271007 to your computer and use it in GitHub Desktop.
Luhn formula multi-step testing example
describe('Luhn Formula Example', function () {
// Final test at the top
it('should validate IDs according to the Luhn formula', function () {
expect(validateID('120488395')).to.be.ok();
});
// 1. Convert a string of numbers to an integer
it('should check that number characters can be read as digits', function () {
function charConversion (char) {
var result;
result = parseInt(char, 10);
return result;
}
expect(charConversion('8')).to.be(8);
});
// 2. Take the parsed number and double every digit
// 3. Add up individual digits if doubled >= 10
it('should check that doubled numbers greater than 10 are broken down to single digits', function () {
function digitCheck (num) {
var sum,
doubled;
doubled = num * 2;
if (doubled >= 10) {
sum = 1 + doubled % 10;
} else {
sum = doubled;
}
return sum;
}
expect(digitCheck(8)).to.be(7);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment