Skip to content

Commit fd58670

Browse files
committed
flesh out Player tests
1 parent e468c17 commit fd58670

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

test/elo.rb

+60-4
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,66 @@
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+
99+
a.rating = b.rating + 1
100+
expect(a > b).must_equal true
101+
end
102+
103+
it "can initialize a pool of players" do
104+
pool = Elo::Player.init_pool(99)
105+
expect(pool).must_be_kind_of Array
106+
pool.each { |player| player.rating = rand(2000) }
107+
sorted = pool.sort # sorts on rating because of <=> and Comparable
108+
expect(sorted.first.rating).must_be :<, sorted.last.rating
109+
end
110+
111+
it "updates both self and opponent with a match outcome" do
112+
a = Elo::Player.new
113+
b = Elo::Player.new
114+
115+
# 5 wins for _a_
116+
5.times { a.update(b, 1) }
117+
118+
expect(a > b).must_equal true
119+
120+
a_rating, b_rating = a.rating, b.rating
121+
122+
# a draw, _a_'s rating goes down, _b_'s goes up
123+
a.update(b, 0.5)
124+
expect(a.rating).must_be :<, a_rating
125+
expect(b.rating).must_be :>, b_rating
126+
end
127+
128+
it "can perform skill-based match simulations" do
129+
a = Elo::Player.new(skill: 0.95)
130+
b = Elo::Player.new(skill: 0.3)
131+
132+
expect(a == b).must_equal true # ratings are the same
133+
134+
# 99 matches
135+
99.times { a.simulate(b) }
136+
expect(a > b).must_equal true # a's rating should increase
81137
end
82138
end
83139
end

0 commit comments

Comments
 (0)