Created
July 8, 2012 22:14
-
-
Save tcz/3073145 to your computer and use it in GitHub Desktop.
ROT13 Javascript BDD example with Jasmine
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
function rot13( text ) | |
{ | |
var results = []; | |
var charcode; | |
for ( var i = 0; i < text.length; ++i ) | |
{ | |
charcode = text.charCodeAt( i ); | |
if ( charcode < 65 || charcode > 90 ) | |
{ | |
throw new Error( 'Gooby pls' ); | |
} | |
charcode = 65 + ( charcode + 13 - 65 ) % 26; | |
results.push( String.fromCharCode( charcode ) ); | |
} | |
return results.join(''); | |
} |
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("ROT13 encoding", function() | |
{ | |
it( "should encode normal text", function() | |
{ | |
expect( rot13( "TEST" ) ).toEqual( "GRFG" ); | |
} ); | |
it( "should fail for special characters", function() | |
{ | |
expect( function() | |
{ | |
return rot13( "$*=" ); | |
} ).toThrow(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment