Permalink
Browse files

Use chai for assertions.

  • Loading branch information...
1 parent 981d687 commit 9eaf0bedffd921dd005d56d2dc32c429e4b6320b @drewolson committed Oct 7, 2012
View
@@ -12,8 +12,9 @@
"optimist": "0.3.4"
},
"devDependencies": {
- "mocha": "1.5.0",
- "portfinder": "0.2.1"
+ "chai": "*",
+ "mocha": "*",
+ "portfinder": "*"
},
"scripts": {
"test": "mocha --recursive --compilers 'coffee:coffee-script'"
@@ -1,27 +1,27 @@
-assert = require "assert"
+expect = require("chai").expect
CommandResponse = require "../../lib/gnodego/command_response"
describe "CommandResponse", ->
describe "isSuccess", ->
it "checks first character for success", ->
response = new CommandResponse "=1 3.8"
- assert.ok response.isSuccess()
+ expect(response.isSuccess()).to.be.true
it "deals with failures", ->
response = new CommandResponse "?1 error message"
- assert.ok not response.isSuccess()
+ expect(response.isSuccess()).to.be.false
describe "commandId", ->
it "returns the id of the command", ->
response = new CommandResponse "=1 data"
- assert.equal "1", response.commandId()
+ expect(response.commandId()).to.equal "1"
describe "data", ->
it "displays data on the first line", ->
response = new CommandResponse "=1 3.8"
- assert.equal "3.8", response.data()
+ expect(response.data()).to.equal "3.8"
describe "errorMessage", ->
it "returns the error message from the response", ->
response = new CommandResponse "?1 error message"
- assert.equal "error message", response.errorMessage()
+ expect(response.errorMessage()).to.equal "error message"
@@ -1,4 +1,4 @@
-assert = require "assert"
+expect = require("chai").expect
GameEngine = require "../../lib/gnodego/game_engine"
describe "GameEngine", ->
@@ -8,7 +8,7 @@ describe "GameEngine", ->
engine.start()
engine.performCommand "protocol_version", (err, data) ->
- assert.equal "2", data
+ expect(data).to.equal "2"
done()
describe "performCommands", ->
@@ -17,21 +17,21 @@ describe "GameEngine", ->
engine.start()
engine.performCommands ["protocol_version"], (err, data) ->
- assert.equal "2", data
+ expect(data).to.equal "2"
done()
it "plays a game of go", (done) ->
engine = new GameEngine
engine.start()
engine.performCommands ["boardsize 9", "play black c3", "play white c6", "move_history"], (err, data) ->
- assert.equal "white C6\nblack C3", data
+ expect(data).to.equal "white C6\nblack C3"
done()
it "stops performing commands if one errors", (done) ->
engine = new GameEngine
engine.start()
engine.performCommands ["boardsize 9", "play black", "showboard"], (err, data) ->
- assert.equal "invalid color or coordinate", err
+ expect(err).to.equal "invalid color or coordinate"
done()
@@ -1,4 +1,4 @@
-assert = require "assert"
+expect = require("chai").expect
net = require "net"
helper = require "../test_helper"
@@ -10,11 +10,11 @@ describe "GameServer", ->
helper.withServer (server, port) ->
socket = new net.Socket
socket.once "data", (data) ->
- assert.equal "Please enter your name: ", data.toString()
+ expect(data.toString()).to.equal "Please enter your name: "
socket.write "Drew\n"
socket.once "data", (data) ->
- assert.equal "Thanks Drew, we're waiting to match you with the next player.\n\n", data.toString()
+ expect(data.toString()).to.equal "Thanks Drew, we're waiting to match you with the next player.\n\n"
done()
socket.connect port
@@ -24,13 +24,13 @@ describe "GameServer", ->
helper.withServer (server, port) ->
socket = new net.Socket
socket.once "data", (data) ->
- assert.equal "Please enter your name: ", data.toString()
+ expect(data.toString()).to.equal "Please enter your name: "
socket.write "Drew\n"
socket.once "data", (data) ->
player = server.unmatchedPlayers[0]
player.on "disconnect", ->
- assert.equal 0, server.unmatchedPlayers.length
+ expect(server.unmatchedPlayers).to.be.empty
done()
socket.end()
@@ -42,12 +42,12 @@ describe "GameServer", ->
helper.withServer (server, port) ->
socket = new net.Socket
socket.once "data", (data) ->
- assert.equal "Please enter your name: ", data.toString()
+ expect(data.toString()).to.equal "Please enter your name: "
socket.write "Drew\n"
socket.once "data", (data) ->
player = server.findUnmatchedPlayer "Drew"
- assert.equal "Drew", player.name
+ expect(player.name).to.equal "Drew"
done()
socket.connect port
@@ -58,16 +58,16 @@ describe "GameServer", ->
socket = new net.Socket
socket.once "data", (data) ->
- assert.equal "Please enter your name: ", data.toString()
+ expect(data.toString()).to.equal "Please enter your name: "
socket.write "Drew\n"
socket.once "data", (data) ->
- assert.ok /Thanks Drew, you've been matched with William\./.test(data.toString())
- assert.equal server.unmatchedPlayers.length, 0
+ expect(data.toString()).to.match /Thanks Drew, you've been matched with William\./
+ expect(server.unmatchedPlayers).to.be.empty
game = server.games[0]
- assert.equal game.players.white.name, "Drew"
+ expect(game.players.white.name).to.equal "Drew"
done()
socket.connect port
@@ -96,8 +96,8 @@ describe "GameServer", ->
1 . . . . . . . . . 1 BLACK (X) has captured 0 stones
A B C D E F G H J"
actualBoardState = william.messages[1].replace(/\r\n|\r|\n|\s$|^\s/gm, '')
- assert.equal welcomeBroadcast.trim(), william.messages[0]
- assert.equal boardState, actualBoardState
+ expect(welcomeBroadcast.trim()).to.equal william.messages[0]
+ expect(actualBoardState).to.equal actualBoardState
done()
socket.connect port
@@ -1,4 +1,4 @@
-assert = require "assert"
+expect = require("chai").expect
Game = require "../../lib/gnodego/game"
Player = require "../../lib/gnodego/player"
@@ -25,18 +25,18 @@ describe "Game", ->
g.start (e, r) ->
g.play "A4", (e, r) -> g.play "A5", (e, r) ->
- g.play "B4", (e, r) -> g.play "B5", (e, r) ->
- g.play "C4", (e, r) -> g.play "C5", (e, r) ->
- g.play "D4", (e, r) -> g.play "D5", (e, r) ->
- g.play "E4", (e, r) -> g.play "E5", (e, r) ->
- g.play "F4", (e, r) -> g.play "F5", (e, r) ->
- g.play "G4", (e, r) -> g.play "G5", (e, re) ->
- g.play "H4", (e, r) -> g.play "H5", (e, r) ->
- g.play "J4", (e, r) -> g.play "J5", (e, r) ->
- g.play "pass", (e, r) -> g.play "pass", (e, r) ->
- assert.ok "W+9.0" in g.players['black'].messages
- assert.ok "W+9.0" in g.players['white'].messages
- done()
+ g.play "B4", (e, r) -> g.play "B5", (e, r) ->
+ g.play "C4", (e, r) -> g.play "C5", (e, r) ->
+ g.play "D4", (e, r) -> g.play "D5", (e, r) ->
+ g.play "E4", (e, r) -> g.play "E5", (e, r) ->
+ g.play "F4", (e, r) -> g.play "F5", (e, r) ->
+ g.play "G4", (e, r) -> g.play "G5", (e, re) ->
+ g.play "H4", (e, r) -> g.play "H5", (e, r) ->
+ g.play "J4", (e, r) -> g.play "J5", (e, r) ->
+ g.play "pass", (e, r) -> g.play "pass", (e, r) ->
+ expect(g.players["black"].messages).to.include "W+9.0"
+ expect(g.players["white"].messages).to.include "W+9.0"
+ done()
describe "playerDisconnect", ->
it "notifies and disconnects the opponent of the disconnected player", ->
@@ -50,17 +50,17 @@ describe "Game", ->
game.playerDisconnect()
- assert.ok william.disconnected
- assert.ok "\nYour opponent disconnected, you win!" in william.messages
+ expect(william.disconnected).to.be.true
+ expect(william.messages).to.include "\nYour opponent disconnected, you win!"
describe "start", ->
it "sets the board size to default", ->
@game.start (err, response) =>
- assert.ok "boardsize 9" in @engine.commands
+ expect(@engine.commands).to.include "boardsize 9"
it "shows the board", ->
@game.start (err, response) =>
- assert.ok "showboard" in @engine.commands
+ expect(@engine.commands).to.include "showboard"
it "sets the board size based on options", ->
game = new Game
@@ -70,106 +70,105 @@ describe "Game", ->
boardSize: 19
game.start (err, response) =>
- assert.ok "boardsize 19" in @engine.commands
+ expect(@engine.commands).to.include "boardsize 19"
it "sets a listener on the black player play event", ->
@game.start (err, response) =>
- assert.deepEqual ['play', @game.play], @drew.listeners[1]
+ expect(@drew.listeners[1]).to.deep.equal ['play', @game.play]
it "sets a listener on both players' disconnect events", ->
@game.start (err, response) =>
- assert.deepEqual ['disconnect', @game.playerDisconnect], @drew.listeners[0]
- assert.deepEqual ['disconnect', @game.playerDisconnect], @drew.listeners[0]
+ expect(@drew.listeners[0]).to.deep.equal ['disconnect', @game.playerDisconnect]
+ expect(@william.listeners[0]).to.deep.equal ['disconnect', @game.playerDisconnect]
describe "activePlayer", ->
it "starts as black", ->
- assert.equal @game.activePlayer(), @drew
+ expect(@game.activePlayer()).to.equal @drew
describe "inactivePlayer", ->
it "starts as white", ->
- assert.equal @game.inactivePlayer(), @william
+ expect(@game.inactivePlayer()).to.equal @william
describe "listenForPlay", ->
it "sets a listener on the new active player", ->
@game.listenForPlay()
- assert.deepEqual ['play', @game.play], @drew.listeners[0]
+ expect(@drew.listeners[0]).to.deep.equal ['play', @game.play]
it "tells the active player it is their move", ->
@game.listenForPlay()
- assert.ok "Please select a move: " in @drew.messages
+ expect(@drew.messages).to.include "Please select a move: "
it "tells the inactive player to wait for their opponent", ->
@game.listenForPlay()
- assert.ok "Your opponent is selecting a move." in @william.messages
+ expect(@william.messages).to.include "Your opponent is selecting a move."
it "does not tell the inactive player to wait for their opponent if so instructed", ->
@game.listenForPlay({informOpponent: false})
- assert.ok "Your opponent is selecting a move." not in @william.messages
+ expect(@drew.messages).to.not.include "Your opponent is selecting a move."
describe "play", ->
it "sends a move to the engine", (done) ->
@game.play "C6", (response) =>
- assert.ok "play black C6" in @engine.commands
+ expect(@engine.commands).to.include "play black C6"
done()
it "lets the opponenet know that you moved", (done) ->
@game.play "C6", (response) =>
- assert.ok "Your opponent's move: C6" in @william.messages
+ expect(@william.messages).to.include "Your opponent's move: C6"
done()
context "player passes", ->
it "notes that the player passed", (done) ->
@game.play "pass", (response) =>
- assert.ok @game.lastPlayerPassed
+ expect(@game.lastPlayerPassed).to.be.true
done()
it "lets the opponent know the player passed", (done) ->
@game.play "pass", (response) =>
- assert.ok "Your opponent's move: pass" in @william.messages
+ expect(@william.messages).to.include "Your opponent's move: pass"
done()
it "shows the score if both players pass", (done) ->
@game.play "pass", (response) =>
@game.play "pass", (response) =>
- assert.ok "Waiting for final score calculation..." in @drew.messages
- assert.ok "Waiting for final score calculation..." in @william.messages
+ expect(@drew.messages).to.include "Waiting for final score calculation..."
+ expect(@william.messages).to.include "Waiting for final score calculation..."
- assert.ok "final_score" in @engine.commands
+ expect(@engine.commands).to.include "final_score"
- assert.ok "Thanks for playing." in @drew.messages
- assert.ok "Thanks for playing." in @william.messages
+ expect(@drew.messages).to.include "Thanks for playing."
+ expect(@william.messages).to.include "Thanks for playing."
done()
it "disconnects the players if both players pass", ->
@game.play "pass", (response) =>
@game.play "pass", (response) =>
- assert.ok @drew.disconnected
- assert.ok @william.disconnected
-
+ expect(@drew.disconnected).to.be.true
+ expect(@william.disconnected).to.be.true
context "engine accepts command", ->
it "toggles the active player", (done) ->
@game.play "C6", (response) =>
- assert.equal @game.activePlayer(), @william
- assert.equal @game.inactivePlayer(), @drew
+ expect(@game.activePlayer()).to.equal @william
+ expect(@game.inactivePlayer()).to.equal @drew
done()
it "shows the board", (done) ->
@game.play "C6", (response) =>
- assert.ok "showboard" in @engine.commands
- assert.ok "success" in @drew.messages
- assert.ok "success" in @william.messages
+ expect(@engine.commands).to.include "showboard"
+ expect(@drew.messages).to.include "success"
+ expect(@william.messages).to.include "success"
done()
it "resets the lastPlayerPassedState", ->
@game.play "pass", (response) =>
@game.play "C6", (response) =>
- assert.ok not @game.lastPlayerPassed
+ expect(@game.lastPlayerPassed).to.be.false
it "sets a listener on the new active player", ->
@game.play "C6", (response) =>
- assert.deepEqual ['play', @game.play], @william.listeners[0]
+ expect(@william.listeners[0]).to.deep.equal ['play', @game.play]
context "engine rejects command", ->
beforeEach ->
@@ -181,15 +180,15 @@ describe "Game", ->
it "does not toggle the active player", (done) ->
@game.play "C6", (response) =>
- assert.equal @game.activePlayer(), @drew
- assert.equal @game.inactivePlayer(), @william
+ expect(@game.activePlayer()).to.equal @drew
+ expect(@game.inactivePlayer()).to.equal @william
done()
it "returns the error to the player", (done) ->
@game.play "C6", (response) =>
- assert.ok response in @game.activePlayer().messages
+ expect(@game.activePlayer().messages).to.include response
done()
it "relistens to the active player", ->
@game.play "C6", (response) =>
- assert.deepEqual ["play", @game.play], @drew.listeners[0]
+ expect(@drew.listeners[0]).to.deep.equal ["play", @game.play]
Oops, something went wrong.

0 comments on commit 9eaf0be

Please sign in to comment.