|
74 | 74 | end
|
75 | 75 |
|
76 | 76 | 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 |
81 | 138 | end
|
82 | 139 | end
|
83 | 140 | end
|
0 commit comments