summaryrefslogtreecommitdiff
path: root/spec/mastermind/game_spec.rb
blob: dd14dc85d8012d721fbde2ef4d7325fbeeabba80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# game_spec.rb
require File.join(File.dirname(__FILE__), "/../spec_helper")

module Mastermind
	describe Game do
		before(:each) do
			@messenger = mock("messenger").as_null_object
			@game = Game.new(@messenger)
		end
		context "starting up" do
			it "should send a welcome message" do
				@messenger.should_receive(:puts).with("Welcome to Mastermind!")
				@game.start(%w[r g y c])
			end
			it "should prompt for the first guess" do
				@messenger.should_receive(:puts).with("Enter guess:")
				@game.start(%w[r g y c])
			end
		end
		context "marking a guess" do
			context "with all 4 colors correct in the correct places" do
				it "should mark the guess with bbbb" do
					@game.start(%w[r g y c])
					@messenger.should_receive(:puts).with("bbbb")
					@game.guess(%w[r g y c])
				end
			end
			context "with all 4 colors correct and 2 in the correct places" do
				it "should mark the guess with bbww" do
					@game.start(%w[r g y c])
					@messenger.should_receive(:puts).with("bbww")
					@game.guess(%w[r g c y])
				end
			end
			context "with all 4 colors correct and 1 in the correct place" do
				it "should mark the guess with bwww" do
					@game.start(%w[r g y c])
					@messenger.should_receive(:puts).with("bwww")
					@game.guess(%w[y r g c])
				end
			end
		end
		context "with duplicates in the guess that match a peg in the code" do
			context "by color and position" do
				it "should add a single b to the mark" do
					@game.start(%w[r y g c])
					@messenger.should_receive(:puts).with("bbb")
					@game.guess(%w[r y g g])
				end
			end
		end
		context "with three colors correct in the correct places" do
			it "should mark the guess with bbb" do
				@game.start(%w[r g y c])
				@messenger.should_receive(:puts).with("bbb")
				@game.guess(%w[r g y w])
			end
		end
	end
end