require "test_helper" require "metal_adventures/dice" module MetalAdventures class DiceTest < Minitest::Test def test_parser_full d1 = Dice.parse("1/2 SC : super nom") assert d1 assert_equal "super nom", d1.description assert_equal 1, d1.n assert_equal 2, d1.difficulty assert_equal :SC, d1.modifiers.reduced end def test_parser_not_a_match refute Dice.parse("coucou 1/2 SC") refute Dice.parse("1x/2 SC") refute Dice.parse("1/2 SC #") refute Dice.parse("1/2 # : name") end def test_parser_parts d1 = Dice.parse("1/2 SC") assert d1 assert_equal "", d1.description assert_equal 1, d1.n assert_equal 2, d1.difficulty assert_equal :SC, d1.modifiers.reduced d2 = Dice.parse("1/2 : super nom") assert d2 assert_equal "super nom", d2.description assert_equal 1, d2.n assert_equal 2, d2.difficulty assert_nil d2.modifiers.reduced d3 = Dice.parse("1 SC : super nom") assert d3 assert_equal "super nom", d3.description assert_equal 1, d3.n assert_equal 1, d3.difficulty assert_equal :SC, d3.modifiers.reduced d4 = Dice.parse("1") assert d4 assert_equal "", d4.description assert_equal 1, d4.n assert_equal 1, d4.difficulty assert_nil d4.modifiers.reduced end def test_make_parser assert "!roll 1".match(Dice.make_parser(prefix: "!roll ")) assert "#roll 1".match(Dice.make_parser(prefix: "#roll ")) end def test_roll Kernel.stubs(:rand).returns(1, 2, 3, 4, 5, 6) d = Dice.new(n: 6) d.roll! assert_equal 3, d.result end def test_roll_with_rerolls Kernel.stubs(:rand).returns(1, 2, 3, 4, 5, 6) d = Dice.new(n: 3, modifiers: Dice::Modifiers.new(:SC)) d.roll! assert_equal 3, d.result end end end