Created
June 6, 2011 22:54
-
-
Save nazgob/1011296 to your computer and use it in GitHub Desktop.
codebraker js tdd kata / powered by Jessie / thanks @marcinbunsch!
This file contains 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
var Game = function() {} | |
Game.prototype.start = function(secret) { | |
this.secret = secret; | |
return "Hello, Enter guess:"; | |
}; | |
Game.prototype.guess = function(guess) { | |
if(guess === this.secret) { | |
return "Correct!"; | |
} | |
var guess_arr = String(guess).split(''); | |
var secret_arr = String(this.secret).split(''); | |
var answer_arr = []; | |
for(var i = 0; i < guess_arr.length; i++) { | |
if(guess_arr[i] === secret_arr[i]) { | |
answer_arr.push('+'); | |
delete guess_arr[i]; | |
} | |
} | |
for(var i = 0; i < guess_arr.length; i++) { | |
for(var j = 0; j < secret_arr.length; j++) { | |
if(guess_arr[i] === secret_arr[j]) { | |
answer_arr.push('-'); | |
} | |
} | |
} | |
return answer_arr.join(''); | |
}; | |
exports.Game = Game |
This file contains 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
var Game = require('./../lib/codebreaker').Game; | |
describe('Codebreaker', function() { | |
describe('Game', function() { | |
var game = new Game(); | |
describe('#start', function() { | |
it("sends a welcome message", function() { | |
var message = game.start(); | |
expect(message).not.toBeUndefined(); | |
expect(message).toContain('Hello'); | |
}); | |
it("prompts for first guess", function() { | |
var message = game.start(); | |
expect(message).not.toBeUndefined(); | |
expect(message).toContain('Enter guess'); | |
}); | |
it("should accept secret code", function() { | |
game.start(1234); | |
expect(game.secret).not.toBeUndefined(); | |
expect(game.secret).toEqual(1234); | |
}); | |
}); | |
describe('#guess', function() { | |
beforeEach(function() { | |
game.start(1234); | |
}); | |
describe("with a full match", function() { | |
it("sends a mark with 'Correct!'", function() { | |
var result = game.guess(1234); | |
expect(result).toEqual('Correct!'); | |
}); | |
}); | |
describe("with no matches", function() { | |
it("sends a mark with ''", function() { | |
var result = game.guess(5678); | |
expect(result).toEqual(''); | |
}); | |
}); | |
describe("with 1 number match", function() { | |
it("sends a mark with '-'", function() { | |
var result = game.guess(5618); | |
expect(result).toEqual('-'); | |
}); | |
}); | |
describe("with 1 exact match", function() { | |
it("sends a mark with '+'", function() { | |
var result = game.guess(1678); | |
expect(result).toEqual('+'); | |
}); | |
}); | |
describe("with 2 number matches", function() { | |
it("sends a mark with '--'", function() { | |
var result = game.guess(6512); | |
expect(result).toEqual('--'); | |
}); | |
}); | |
describe("with 1 number match and 1 exact match -in that order", function() { | |
it("sends a mark with '+-'", function() { | |
var result = game.guess(1592); | |
expect(result).toEqual('+-'); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment