Skip to content

Commit

Permalink
flesh out Player tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rickhull committed Jun 10, 2024
1 parent e468c17 commit 1d395ad
Showing 1 changed file with 60 additions and 4 deletions.
64 changes: 60 additions & 4 deletions test/elo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,66 @@
end

describe Elo::Player do
it "must be initialized with an Elo object" do
expect { Elo::Player.new }.must_raise ArgumentError
expect { Elo::Player.new(1234) }.must_raise ArgumentError
expect(Elo::Player.new(Elo.new)).must_be_kind_of Elo::Player
it "has sensible defaults" do
expect(Elo::Player.new).must_be_kind_of Elo::Player
end

it "may be initialized with an Elo object" do
expect(Elo::Player.new(elo: Elo.new)).must_be_kind_of Elo::Player
end

it "is Comparable, based on Elo rating" do
a = Elo::Player.new
b = Elo::Player.new

expect(a <=> b).must_equal 0
a.rating = 2 * b.rating
expect(a <=> b).must_equal 1
b.rating = a.rating + 1
expect(a <=> b).must_equal(-1)

a.rating = b.rating
expect(a == b).must_equal true
expect(a == a).must_equal true

a.rating = b.rating + 1
expect(a > b).must_equal true
end

it "can initialize a pool of players" do
pool = Elo::Player.init_pool(99)
expect(pool).must_be_kind_of Array
pool.each { |player| player.rating = rand(2000) }
sorted = pool.sort # sorts on rating because of <=> and Comparable
expect(sorted.first.rating).must_be :<, sorted.last.rating
end

it "updates both self and opponent with a match outcome" do
a = Elo::Player.new
b = Elo::Player.new

# 5 wins for _a_
5.times { a.update(b, 1) }

expect(a > b).must_equal true

a_rating, b_rating = a.rating, b.rating

# a draw, _a_'s rating goes down, _b_'s goes up
a.update(b, 0.5)
expect(a.rating).must_be :<, a_rating
expect(b.rating).must_be :>, b_rating
end

it "can perform skill-based match simulations" do
a = Elo::Player.new(skill: 0.95)
b = Elo::Player.new(skill: 0.3)

expect(a == b).must_equal true # ratings are the same

# 99 matches
99.times { a.simulate(b) }
expect(a > b).must_equal true # a's rating should increase
end
end
end

0 comments on commit 1d395ad

Please sign in to comment.