require 'games_dice' require 'helpers' # This spec demonstrates that documentation from the README.md works as intended describe GamesDice do describe '#create' do it "converts a string such as '3d6+6' into a GamesDice::Dice object" do d = GamesDice.create '3d6+6' d.is_a?( GamesDice::Dice ).should be_true end it "takes a parameter 'dice_description', which is a string such as '3d6' or '2d4-1'" do d = GamesDice.create '3d6' d.is_a?( GamesDice::Dice ).should be_true d = GamesDice.create '2d4-1' d.is_a?( GamesDice::Dice ).should be_true end it "takes an optional parameter 'prng', which if provided it should be an object that has a method 'rand( integer )'" do prng = TestPRNG.new d = GamesDice.create '3d6', prng d.is_a?( GamesDice::Dice ).should be_true end end # describe '#create' end # describe GamesDice describe GamesDice::Dice do before :each do srand(67809) end let(:dice) { GamesDice.create '3d6'} describe "#roll" do it "simulates rolling the dice as they were described in the constructor" do expected_results = [11,15,11,12,12,9] expected_results.each do |expected| dice.roll.should == expected end end end describe "#result" do it "returns the value from the last call to roll" do expected_results = [11,15,11,12,12,9] expected_results.each do |expected| dice.roll dice.result.should == expected end end it "will be nil if no roll has been made yet" do dice.result.should be_nil end end describe "#max" do it "returns the maximum possible value from a roll of the dice" do dice.max.should == 18 end end describe "#min" do it "returns the minimum possible value from a roll of the dice" do dice.min.should == 3 end end describe "#minmax" do it "returns an array [ dice.min, dice.max ]" do dice.minmax.should == [3,18] end end describe "#probabilities" do it "calculates probability distribution for the dice" do pd = dice.probabilities pd.is_a?( GamesDice::Probabilities ).should be_true pd.p_eql( 3).should be_within(1e-10).of 1.0/216 pd.p_eql( 11 ).should be_within(1e-10).of 27.0/216 end end end # describe GamesDice::Dice describe GamesDice::Probabilities do let(:probs) { GamesDice.create('3d6').probabilities } describe "#to_h" do it "returns a hash representation of the probability distribution" do h = probs.to_h h.should be_valid_distribution h[3].should be_within(1e-10).of 1.0/216 h[11].should be_within(1e-10).of 27.0/216 end end describe "#max" do it "returns maximum result in the probability distribution" do probs.max.should == 18 end end describe "#min" do it "returns minimum result in the probability distribution" do probs.min.should == 3 end end describe "#p_eql( n )" do it "returns the probability of a result equal to the integer n" do probs.p_eql( 3 ).should be_within(1e-10).of 1.0/216 probs.p_eql( 2 ).should == 0.0 end end describe "#p_gt( n )" do it "returns the probability of a result greater than the integer n" do probs.p_gt( 17 ).should be_within(1e-10).of 1.0/216 probs.p_gt( 2 ).should == 1.0 end end describe "#p_ge( n )" do it "returns the probability of a result greater than the integer n" do probs.p_ge( 17 ).should be_within(1e-10).of 4.0/216 probs.p_ge( 3 ).should == 1.0 end end describe "#p_le( n )" do it "returns the probability of a result less than or equal to the integer n" do probs.p_le( 17 ).should be_within(1e-10).of 215.0/216 probs.p_le( 3 ).should be_within(1e-10).of 1.0/216 end end describe "#p_lt( n )" do it "returns the probability of a result less than the integer n" do probs.p_lt( 17 ).should be_within(1e-10).of 212.0/216 probs.p_lt( 3 ).should == 0.0 end end end # describe GamesDice::Probabilities describe 'String Dice Description' do before :each do srand(35241) end describe "'1d6'" do it "returns expected results from rolling" do d = GamesDice.create '1d6' (1..20).map { |n| d.roll }.should == [6, 3, 2, 3, 4, 6, 4, 2, 6, 3, 3, 5, 6, 6, 3, 6, 5, 2, 1, 4] end end describe "'2d6 + 1d4'" do it "returns expected results from rolling" do d = GamesDice.create '2d6 + 1d4' (1..5).map { |n| d.roll }.should == [11, 10, 12, 12, 14] end end describe "'1d100 + 1d20 - 5'" do it "returns expected results from rolling" do d = GamesDice.create '1d100 + 1d20 - 5' (1..5).map { |n| d.roll }.should == [75, 78, 24, 102, 32] end end describe "'1d10x'" do it "returns expected results from rolling" do d = GamesDice.create '1d10x' (1..20).map { |n| d.roll }.should == [2, 3, 4, 7, 6, 7, 4, 2, 6, 3, 7, 5, 6, 7, 6, 6, 5, 19, 4, 19] end end describe "'1d6r1'" do it "returns expected results from rolling" do d = GamesDice.create '1d6r1' (1..20).map { |n| d.roll }.should == [6, 3, 2, 3, 4, 6, 4, 2, 6, 3, 3, 5, 6, 6, 3, 6, 5, 2, 4, 2] end end describe "'5d10r:10,add.k2'" do it "returns expected results from rolling" do d = GamesDice.create '5d10r:10,add.k2' (1..5).map { |n| d.roll }.should == [13, 13, 14, 38, 15] end end describe "'3d10m6'" do it "returns expected results from rolling" do d = GamesDice.create '3d10m6' (1..6).map { |n| d.roll }.should == [0, 3, 1, 1, 3, 2] end end describe "'5d10k2'" do it "returns expected results from rolling" do d = GamesDice.create '5d10k2' (1..5).map { |n| d.roll }.should == [13, 13, 14, 19, 19] end end describe "'5d10x'" do it "returns expected results from rolling" do d = GamesDice.create '5d10x' (1..5).map { |n| d.roll }.should == [22, 22, 31, 53, 25] end end end