Skip to content

Commit 7e5c614

Browse files
committed
flesh out Player tests
1 parent e468c17 commit 7e5c614

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

test/elo.rb

+61-4
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,67 @@
7474
end
7575

7676
describe Elo::Player do
77-
it "must be initialized with an Elo object" do
78-
expect { Elo::Player.new }.must_raise ArgumentError
79-
expect { Elo::Player.new(1234) }.must_raise ArgumentError
80-
expect(Elo::Player.new(Elo.new)).must_be_kind_of Elo::Player
77+
it "has sensible defaults" do
78+
expect(Elo::Player.new).must_be_kind_of Elo::Player
79+
end
80+
81+
it "may be initialized with an Elo object" do
82+
expect(Elo::Player.new(elo: Elo.new)).must_be_kind_of Elo::Player
83+
end
84+
85+
it "is Comparable, based on Elo rating" do
86+
a = Elo::Player.new
87+
b = Elo::Player.new
88+
89+
expect(a <=> b).must_equal 0
90+
a.rating = 2 * b.rating
91+
expect(a <=> b).must_equal 1
92+
b.rating = a.rating + 1
93+
expect(a <=> b).must_equal(-1)
94+
95+
a.rating = b.rating
96+
expect(a == b).must_equal true
97+
expect(a == a).must_equal true
98+
expect(a != b).must_equal false
99+
100+
a.rating = b.rating + 1
101+
expect(a > b).must_equal true
102+
end
103+
104+
it "can initialize a pool of players" do
105+
pool = Elo::Player.init_pool(99)
106+
expect(pool).must_be_kind_of Array
107+
pool.each { |player| player.rating = rand(2000) }
108+
sorted = pool.sort # sorts on rating because of <=> and Comparable
109+
expect(sorted.first.rating).must_be :<, sorted.last.rating
110+
end
111+
112+
it "updates both self and opponent with a match outcome" do
113+
a = Elo::Player.new
114+
b = Elo::Player.new
115+
116+
# 5 wins for _a_
117+
5.times { a.update(b, 1) }
118+
119+
expect(a > b).must_equal true
120+
121+
a_rating, b_rating = a.rating, b.rating
122+
123+
# a draw, _a_'s rating goes down, _b_'s goes up
124+
a.update(b, 0.5)
125+
expect(a.rating).must_be :<, a_rating
126+
expect(b.rating).must_be :>, b_rating
127+
end
128+
129+
it "can perform skill-based match simulations" do
130+
a = Elo::Player.new(skill: 0.95)
131+
b = Elo::Player.new(skill: 0.3)
132+
133+
expect(a == b).must_equal true # ratings are the same
134+
135+
# 99 matches
136+
99.times { a.simulate(b) }
137+
expect(a > b).must_equal true # a's rating should increase
81138
end
82139
end
83140
end

0 commit comments

Comments
 (0)