Skip to content

Instantly share code, notes, and snippets.

@TheBigSadowski
Last active December 24, 2015 15:08
Show Gist options
  • Save TheBigSadowski/6817316 to your computer and use it in GitHub Desktop.
Save TheBigSadowski/6817316 to your computer and use it in GitHub Desktop.
TDD Pig Latin Building out a pig latin translator step by step using test driven development.

#TDD Pig Latin

A translator from English to Pig Latin, written javascript.

Running the tests

Tests run from the command line using node. If you don't have node, you can get it [here](http://nodejs.org/ here).

Run the tests:

node test
function ay(str) {
return str + 'ay';
}
function translateWord(str) {
if (/^[aeiou]/i.test(str)) {
return ay(str);
}
if (/^[A-Z]/.test(str)) {
return ay(str[1].toUpperCase() + str.substring(2) + str[0].toLowerCase());
}
return ay(str.substring(1) + str[0]);
}
exports.translate = function (str) {
return str.replace(/\b\w+\b/g, translateWord);
};
var assert = require('assert');
var pigLatin = require('./piglatin.js');
function assertCorrectTranslation(input, expected) {
try {
var pig = pigLatin.translate(input);
assert.equal(pig, expected, '"'+input+'" should translate to "'+expected+'", but was "'+pig+'"');
console.log('PASSED');
} catch (er) {
console.log('FAIL!!! '+er.message);
}
}
assertCorrectTranslation('hello', 'ellohay');
assertCorrectTranslation('hello world', 'ellohay orldway');
assertCorrectTranslation('Hello World', 'Ellohay Orldway');
assertCorrectTranslation('Hello, World!!', 'Ellohay, Orldway!!');
assertCorrectTranslation('I eat apples', 'Iay eatay applesay');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment