Created
December 12, 2012 19:55
-
-
Save chatrjr/4271007 to your computer and use it in GitHub Desktop.
Luhn formula multi-step testing example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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