class TestGetVarExpression(TestCase): def setUp(self): self._probability_distribution_factory = ProbabilityDistributionFactory( ) self._probability_distribution = self._probability_distribution_factory.create( { -5: 1, 1: 2, 4: 1 }) self._test_name = "test_name" self._mock_state = create_autospec(IProbabilityDistributionState, spec_set=True) self._mock_state.get_constant.return_value = 2 self._mock_state.get_var.return_value = self._probability_distribution self._test_assignment_expression = GetVarExpression( self._mock_state, self._test_name) self._mock_parser_gen = create_autospec(rply.ParserGenerator) def test_get_var_add_production_function(self): GetVarExpression.add_production_function( self._mock_parser_gen, self._probability_distribution_factory) self._mock_parser_gen.production.assert_called_once_with( """expression : NAME""") def test_get_var_roll(self): for _ in range(100): self.assertEqual(2, self._test_assignment_expression.roll()) self._mock_state.get_constant.assert_called_with(self._test_name) def test_get_var_max(self): self.assertEqual(2, self._test_assignment_expression.max()) self._mock_state.get_constant.assert_called_with(self._test_name) def test_get_var_min(self): self.assertEqual(2, self._test_assignment_expression.min()) self._mock_state.get_constant.assert_called_with(self._test_name) def test_var_assignment_str(self): self.assertEqual(f"{self._test_name}", str(self._test_assignment_expression)) def test_var_estimated_cost(self): self.assertEqual(2, self._test_assignment_expression.estimated_cost()) def test_var_get_probability_distribution(self): self.assertEqual( self._probability_distribution, self._test_assignment_expression.get_probability_distribution(), ) self._mock_state.get_var.assert_called_with(self._test_name) def test_dice_get_contained_variables(self): self.assertSetEqual( {f"{self._test_name}"}, self._test_assignment_expression.get_contained_variables(), )
class TestParenthesisEnclosedExpression(TestCase): def setUp(self): self._probability_distribution_factory = ProbabilityDistributionFactory() self._mock_syntax = create_autospec(IDiceExpression) self._mock_syntax.roll.return_value = 10 self._mock_syntax.max.return_value = 8 self._mock_syntax.min.return_value = 6 self._mock_syntax.__str__.return_value = "7d4" self._mock_syntax.estimated_cost.return_value = 987654321 self._mock_syntax.get_probability_distribution.return_value = self._probability_distribution_factory.create( {-2: 1, 4: 1} ) self._mock_syntax.get_contained_variables.return_value = {"mock"} self._test_parentheses_enclosed_expression = ParenthesisEnclosedExpression(self._mock_syntax) self._mock_parser_gen = create_autospec(rply.ParserGenerator) def test_add_add_production_function(self): ParenthesisEnclosedExpression.add_production_function( self._mock_parser_gen, self._probability_distribution_factory ) self._mock_parser_gen.production.assert_called_once_with( """expression : OPEN_PARENTHESIS expression CLOSE_PARENTHESIS""" ) def test_parenthesis_roll(self): for _ in range(100): self.assertEqual(10, self._test_parentheses_enclosed_expression.roll()) def test_parenthesis_max(self): self.assertEqual(8, self._test_parentheses_enclosed_expression.max()) def test_parenthesis_min(self): self.assertEqual(6, self._test_parentheses_enclosed_expression.min()) def test_parenthesis_str(self): self.assertEqual("(7d4)", str(self._test_parentheses_enclosed_expression)) def test_parenthesis_estimated_cost(self): self.assertEqual(987654321, self._test_parentheses_enclosed_expression.estimated_cost()) def test_parenthesis_get_probability_distribution(self): self.assertEqual( {-2: 1, 4: 1}, self._test_parentheses_enclosed_expression.get_probability_distribution().get_result_map(), ) def test_parenthesis_get_contained_variables(self): self.assertSetEqual( {"mock"}, self._test_parentheses_enclosed_expression.get_contained_variables(), )
class TestBinaryOperatorGreaterThanExpression(TestCase): def setUp(self): self._probability_distribution_factory = ProbabilityDistributionFactory( ) self._mock_syntax = [ create_autospec(IDiceExpression) for _ in range(2) ] self._mock_syntax[0].roll.return_value = 9 self._mock_syntax[0].max.return_value = 8 self._mock_syntax[0].min.return_value = 6 self._mock_syntax[0].__str__.return_value = "7" self._mock_syntax[0].estimated_cost.return_value = 9 self._mock_syntax[ 0].get_probability_distribution.return_value = self._probability_distribution_factory.create( { -3: 1, 0: 2, 4: 1 }) self._mock_syntax[0].get_contained_variables.return_value = { "mock one" } self._mock_syntax[1].roll.return_value = 4 self._mock_syntax[1].max.return_value = 6 self._mock_syntax[1].min.return_value = 8 self._mock_syntax[1].__str__.return_value = "2d8" self._mock_syntax[1].estimated_cost.return_value = 7 self._mock_syntax[ 1].get_probability_distribution.return_value = self._probability_distribution_factory.create( { -3: 2, 0: 1, 1: 2, 4: 1 }) self._mock_syntax[1].get_contained_variables.return_value = { "mock two" } self._test_binary_operator = BinaryOperatorExpression( ">", self._mock_syntax[0], self._mock_syntax[1]) self._mock_parser_gen = create_autospec(rply.ParserGenerator) def test_binary_operator_add_production_function(self): BinaryOperatorExpression.add_production_function( self._mock_parser_gen, self._probability_distribution_factory) self._mock_parser_gen.production.assert_called_once_with( """expression : expression BINARY_OPERATOR expression""") def test_gt_roll_true(self): for _ in range(100): self.assertEqual(1, self._test_binary_operator.roll()) def test_gt_roll_false(self): self._mock_syntax[0].roll.return_value = 2 for _ in range(100): self.assertEqual(0, self._test_binary_operator.roll()) def test_gt_max_true(self): self.assertEqual(1, self._test_binary_operator.max()) def test_gt_max_false(self): self._mock_syntax[ 0].get_probability_distribution.return_value = self._probability_distribution_factory.create( {5: 1}) self._mock_syntax[ 1].get_probability_distribution.return_value = self._probability_distribution_factory.create( {5: 1}) self.assertEqual(0, self._test_binary_operator.max()) def test_gt_min_true(self): self._mock_syntax[ 0].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 5: 1, 6: 1 }) self._mock_syntax[ 1].get_probability_distribution.return_value = self._probability_distribution_factory.create( {4: 1}) self.assertEqual(1, self._test_binary_operator.min()) def test_gt_min_false(self): self.assertEqual(0, self._test_binary_operator.min()) def test_gt_str(self): self.assertEqual("7 > 2d8", str(self._test_binary_operator)) def test_gt_estimated_cost(self): self.assertEqual(16, self._test_binary_operator.estimated_cost()) def test_gt_get_probability_distribution(self): self.assertEqual( { 0: 15, 1: 9 }, self._test_binary_operator.get_probability_distribution(). get_result_map(), ) def test_gt_get_contained_variables(self): self.assertSetEqual( {"mock one", "mock two"}, self._test_binary_operator.get_contained_variables(), )
class TestAddExpression(TestCase): def setUp(self): self._probability_distribution_factory = ProbabilityDistributionFactory() self._mock_syntax = [create_autospec(IDiceExpression) for _ in range(2)] self._mock_syntax[0].roll.return_value = 10 self._mock_syntax[0].max.return_value = 8 self._mock_syntax[0].min.return_value = 6 self._mock_syntax[0].__str__.return_value = "7" self._mock_syntax[0].estimated_cost.return_value = 7 self._mock_syntax[0].get_probability_distribution.return_value = self._probability_distribution_factory.create( {-2: 1, 4: 1} ) self._mock_syntax[0].get_contained_variables.return_value = {"mock one"} self._mock_syntax[1].roll.return_value = 4 self._mock_syntax[1].max.return_value = 6 self._mock_syntax[1].min.return_value = 8 self._mock_syntax[1].__str__.return_value = "2" self._mock_syntax[1].estimated_cost.return_value = 2 self._mock_syntax[1].get_probability_distribution.return_value = self._probability_distribution_factory.create( {8: 1, -3: 2} ) self._mock_syntax[1].get_contained_variables.return_value = {"mock two"} self._test_add = AddExpression(self._mock_syntax[0], self._mock_syntax[1]) self._mock_parser_gen = create_autospec(rply.ParserGenerator) def test_add_add_production_function(self): AddExpression.add_production_function(self._mock_parser_gen, self._probability_distribution_factory) self._mock_parser_gen.production.assert_called_once_with("""expression : expression ADD expression""") def test_add_roll(self): for _ in range(100): self.assertEqual(14, self._test_add.roll()) def test_add_max(self): self.assertEqual(12, self._test_add.max()) def test_add_min(self): self.assertEqual(-5, self._test_add.min()) def test_add_str(self): self.assertEqual("7 + 2", str(self._test_add)) def test_add_estimated_cost(self): self.assertEqual(9, self._test_add.estimated_cost()) def test_add_get_probability_distribution(self): self._mock_syntax[0].get_probability_distribution.return_value = self._probability_distribution_factory.create( {10: 1, -12: 2, 0: 1} ) self._mock_syntax[1].get_probability_distribution.return_value = self._probability_distribution_factory.create( {2: 1, 3: 2} ) self.assertEqual( {12: 1, -10: 2, 2: 1, 13: 2, -9: 4, 3: 2}, self._test_add.get_probability_distribution().get_result_map(), ) def test_add_get_contained_variables(self): self.assertSetEqual({"mock one", "mock two"}, self._test_add.get_contained_variables())
class TestMultiplyExpression(TestCase): def setUp(self): self._probability_distribution_factory = ProbabilityDistributionFactory( ) self._mock_syntax = [ create_autospec(IDiceExpression) for _ in range(2) ] self._mock_syntax[0].roll.return_value = 10 self._mock_syntax[0].max.return_value = 8 self._mock_syntax[0].min.return_value = 2 self._mock_syntax[0].__str__.return_value = "7" self._mock_syntax[0].estimated_cost.return_value = 9 self._mock_syntax[ 0].get_probability_distribution.return_value = self._probability_distribution_factory.create( { -2: 1, 4: 1 }) self._mock_syntax[0].get_contained_variables.return_value = { "mock one" } self._mock_syntax[1].roll.return_value = 4 self._mock_syntax[1].max.return_value = 6 self._mock_syntax[1].min.return_value = 8 self._mock_syntax[1].__str__.return_value = "2" self._mock_syntax[1].estimated_cost.return_value = 7 self._mock_syntax[ 1].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 8: 1, -3: 2 }) self._mock_syntax[1].get_contained_variables.return_value = { "mock two" } self._test_multiply = MultiplyExpression(self._mock_syntax[0], self._mock_syntax[1]) self._mock_parser_gen = create_autospec(rply.ParserGenerator) def test_multiply_add_production_function(self): MultiplyExpression.add_production_function( self._mock_parser_gen, self._probability_distribution_factory) self._mock_parser_gen.production.assert_called_once_with( """expression : expression MULTIPLY expression""") def test_multiply_roll(self): for _ in range(100): self.assertEqual(40, self._test_multiply.roll()) def test_multiply_max(self): self.assertEqual(32, self._test_multiply.max()) def test_multiply_min(self): self.assertEqual(-16, self._test_multiply.min()) def test_multiply_str(self): self.assertEqual("7 * 2", str(self._test_multiply)) def test_multiply_estimated_cost(self): self.assertEqual(16, self._test_multiply.estimated_cost()) def test_multiply_get_probability_distribution(self): self._mock_syntax[ 0].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 10: 1, -12: 2, 0: 1 }) self._mock_syntax[ 1].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 2: 1, 3: 2 }) self.assertEqual( { -36: 4, -24: 2, 0: 3, 20: 1, 30: 2 }, self._test_multiply.get_probability_distribution().get_result_map( ), ) def test_multiply_get_contained_variables(self): self.assertSetEqual({"mock one", "mock two"}, self._test_multiply.get_contained_variables())
class TestVarAssignmentExpression(TestCase): def setUp(self): self._probability_distribution_factory = ProbabilityDistributionFactory( ) self._mock_syntax = create_autospec(IDiceExpression) self._mock_syntax.roll.return_value = 2 self._mock_syntax.max.return_value = 8 self._mock_syntax.min.return_value = 6 self._mock_syntax.__str__.return_value = "7d3" self._mock_syntax.estimated_cost.return_value = 45 self._mock_syntax.get_probability_distribution.return_value = self._probability_distribution_factory.create( { -5: 1, 1: 2, 4: 1 }) self._mock_syntax.get_contained_variables.return_value = {"mock"} self._test_name = "test_name" self._mock_state = create_autospec(IProbabilityDistributionState, spec_set=True) self._test_assignment_expression = VarAssignmentExpression( self._mock_state, self._test_name, self._mock_syntax) self._mock_parser_gen = create_autospec(rply.ParserGenerator) def test_var_assignment_add_production_function(self): VarAssignmentExpression.add_production_function( self._mock_parser_gen, self._probability_distribution_factory) self._mock_parser_gen.production.assert_called_once_with( """expression : VAR NAME ASSIGNMENT expression""") def test_var_assignment_roll(self): for _ in range(100): self.assertEqual(2, self._test_assignment_expression.roll()) self._mock_state.set_constant.assert_called_with(self._test_name, 2) def test_var_assignment_max(self): self.assertEqual(8, self._test_assignment_expression.max()) self._mock_state.set_constant.assert_called_with(self._test_name, 8) def test_var_assignment_min(self): self.assertEqual(6, self._test_assignment_expression.min()) self._mock_state.set_constant.assert_called_with(self._test_name, 6) def test_var_assignment_str(self): self.assertEqual(f"VAR {self._test_name} = 7d3", str(self._test_assignment_expression)) def test_var_assignment_estimated_cost(self): self.assertEqual(45 + 2, self._test_assignment_expression.estimated_cost()) def test_var_get_probability_distribution(self): mock_probability_distribution = create_autospec( IProbabilityDistribution) self._mock_syntax.get_probability_distribution.return_value = mock_probability_distribution self.assertEqual( mock_probability_distribution, self._test_assignment_expression.get_probability_distribution(), ) self._mock_state.set_var.assert_called_with( self._test_name, mock_probability_distribution) def test_get_contained_variables(self): self.assertEqual( set(), self._test_assignment_expression.get_contained_variables())
class TestProbabilityState(TestCase): def setUp(self) -> None: self._probability_distribution_factory = ProbabilityDistributionFactory( ) self._probability_outcome_factory = ProbabilityOutcomeFactory() self._constraint_factory = ConstraintFactory() self._test_name = "test_name" _1_n5 = self._probability_outcome_factory.create_empty(-5) _1_n5.constraint_set.add_constraint( self._constraint_factory.var_value_constraint("test_name_1", {-5})) _1_1 = self._probability_outcome_factory.create_empty(1) _1_1.constraint_set.add_constraint( self._constraint_factory.var_value_constraint("test_name_1", {1})) _1_4 = self._probability_outcome_factory.create_empty(4) _1_4.constraint_set.add_constraint( self._constraint_factory.var_value_constraint("test_name_1", {4})) _2_n5 = self._probability_outcome_factory.create_empty(-5) _2_n5.constraint_set.add_constraint( self._constraint_factory.var_value_constraint("test_name_2", {-5})) _2_1 = self._probability_outcome_factory.create_empty(1) _2_1.constraint_set.add_constraint( self._constraint_factory.var_value_constraint("test_name_2", {1})) _2_4 = self._probability_outcome_factory.create_empty(4) _2_4.constraint_set.add_constraint( self._constraint_factory.var_value_constraint("test_name_2", {4})) self._test_distribution = self._probability_distribution_factory.create( { -5: 1, 1: 2, 4: 1 }) self._return_one_distribution = self._probability_distribution_factory.create( { _1_n5: 1, _1_1: 2, _1_4: 1 }) self._return_two_distribution = self._probability_distribution_factory.create( { _2_n5: 1, _2_1: 2, _2_4: 1 }) self._test_int = 1 self._test_probability_state = ProbabilityDistributionState( self._probability_distribution_factory, self._constraint_factory) def test_probability_state_has_constant_false(self): self._test_probability_state.set_constant("a", self._test_int) self.assertFalse( self._test_probability_state.has_constant(self._test_name)) def test_probability_state_has_constant_true(self): self._test_probability_state.set_constant(self._test_name, self._test_int) self.assertTrue( self._test_probability_state.has_constant(self._test_name)) def test_probability_state_has_var_false(self): self._test_probability_state.set_var("a", self._test_distribution) self.assertFalse(self._test_probability_state.has_var(self._test_name)) def test_probability_state_has_var_true(self): self._test_probability_state.set_var(self._test_name, self._test_distribution) self.assertTrue(self._test_probability_state.has_var(self._test_name)) def test_probability_state_get_constant_false(self): self._test_probability_state.set_constant("a", self._test_int) with self.assertRaises(KeyError): self._test_probability_state.get_constant(self._test_name) def test_probability_state_get_constant_true(self): self._test_probability_state.set_constant(self._test_name, self._test_int) self.assertEqual( self._test_int, self._test_probability_state.get_constant(self._test_name)) def test_probability_state_get_var_false(self): self._test_probability_state.set_var("a", self._test_distribution) with self.assertRaises(KeyError): self._test_probability_state.get_var(self._test_name) def test_probability_state_get_var_dict(self): self._test_probability_state.set_var(self._test_name, self._test_distribution) self.assertIn( self._test_name, self._test_probability_state.get_var_dict(), ) self.assertEqual( str(self._return_one_distribution), str(self._test_probability_state.get_var_dict()[self._test_name])) def test_probability_state_get_var_dict_second_time(self): self._test_probability_state.set_var(self._test_name, self._test_distribution) self._test_probability_state.set_var(self._test_name, self._test_distribution) self.assertIn( self._test_name, self._test_probability_state.get_var_dict(), ) self.assertEqual( str(self._return_two_distribution), str(self._test_probability_state.get_var_dict()[self._test_name])) def test_probability_state_get_constant_dict(self): self._test_probability_state.set_constant(self._test_name, self._test_int) self.assertEqual( {self._test_name: self._test_int}, self._test_probability_state.get_constant_dict(), )
class TestAbsExpression(TestCase): def setUp(self): self._probability_distribution_factory = ProbabilityDistributionFactory( ) self._mock_syntax = create_autospec(IDiceExpression) self._mock_syntax.roll.return_value = 2 self._mock_syntax.max.return_value = 8 self._mock_syntax.min.return_value = 6 self._mock_syntax.__str__.return_value = "7d3" self._mock_syntax.estimated_cost.return_value = 21 self._mock_syntax.get_probability_distribution.return_value = self._probability_distribution_factory.create( { -5: 1, 1: 2, 4: 1 }) self._mock_syntax.get_contained_variables.return_value = {"mock"} self._test_abs_operator = AbsExpression(self._mock_syntax) self._mock_parser_gen = create_autospec(rply.ParserGenerator) def test_abs_add_production_function(self): AbsExpression.add_production_function( self._mock_parser_gen, self._probability_distribution_factory) self._mock_parser_gen.production.assert_called_once_with( """expression : ABS OPEN_PARENTHESIS expression CLOSE_PARENTHESIS""" ) def test_abs_roll_pos(self): for _ in range(100): self.assertEqual(2, self._test_abs_operator.roll()) def test_abs_roll_neg(self): self._mock_syntax.roll.return_value = -4 for _ in range(100): self.assertEqual(4, self._test_abs_operator.roll()) def test_abs_max(self): self.assertEqual(5, self._test_abs_operator.max()) def test_abs_min(self): self.assertEqual(1, self._test_abs_operator.min()) def test_abs_str(self): self.assertEqual("ABS(7d3)", str(self._test_abs_operator)) def test_abs_estimated_cost(self): self.assertEqual(21, self._test_abs_operator.estimated_cost()) def test_abs_get_probability_distribution(self): self.assertEqual( { 1: 2, 4: 1, 5: 1 }, self._test_abs_operator.get_probability_distribution(). get_result_map(), ) def test_abs_get_contained_variables(self): self.assertSetEqual({"mock"}, self._test_abs_operator.get_contained_variables())
class TestBinaryOperatorNotExpression(TestCase): def setUp(self): self._probability_distribution_factory = ProbabilityDistributionFactory( ) self._mock_syntax = create_autospec(IDiceExpression) self._mock_syntax.roll.return_value = 0 self._mock_syntax.max.return_value = 8 self._mock_syntax.min.return_value = 6 self._mock_syntax.__str__.return_value = "7d3" self._mock_syntax.estimated_cost.return_value = 9 self._mock_syntax.get_probability_distribution.return_value = self._probability_distribution_factory.create( { -3: 1, 0: 2, 4: 1 }) self._mock_syntax.get_contained_variables.return_value = {"mock"} self._test_binary_operator = NotExpression(self._mock_syntax) self._mock_parser_gen = create_autospec(rply.ParserGenerator) def test_binary_operator_add_production_function(self): NotExpression.add_production_function( self._mock_parser_gen, self._probability_distribution_factory) self._mock_parser_gen.production.assert_called_once_with( """expression : NOT expression""") def test_ne_roll_true(self): for _ in range(100): self.assertEqual(1, self._test_binary_operator.roll()) def test_ne_roll_false(self): self._mock_syntax.roll.return_value = 4 for _ in range(100): self.assertEqual(0, self._test_binary_operator.roll()) def test_ne_max_true(self): self._mock_syntax.max.return_value = 4 self.assertEqual(1, self._test_binary_operator.max()) def test_ne_max_false(self): self._mock_syntax.get_probability_distribution.return_value = self._probability_distribution_factory.create( { 4: 1, 54: 1 }) self.assertEqual(0, self._test_binary_operator.max()) def test_ne_min_true(self): self._mock_syntax.get_probability_distribution.return_value = self._probability_distribution_factory.create( {0: 1}) self.assertEqual(1, self._test_binary_operator.min()) def test_ne_min_false(self): self.assertEqual(0, self._test_binary_operator.min()) def test_ne_str(self): self.assertEqual("!7d3", str(self._test_binary_operator)) def test_ne_estimated_cost(self): self.assertEqual(9, self._test_binary_operator.estimated_cost()) def test_ne_get_probability_distribution(self): self.assertEqual( { 0: 1, 1: 3 }, self._test_binary_operator.get_probability_distribution(). get_result_map(), ) def test_ne_get_contained_variables(self): self.assertSetEqual( {"mock"}, self._test_binary_operator.get_contained_variables(), )
class TestSubtractExpression(TestCase): def setUp(self): self._probability_distribution_factory = ProbabilityDistributionFactory( ) self._mock_syntax = [ create_autospec(IDiceExpression) for _ in range(2) ] self._mock_syntax[0].roll.return_value = 10 self._mock_syntax[0].max.return_value = 8 self._mock_syntax[0].min.return_value = 6 self._mock_syntax[0].__str__.return_value = "7" self._mock_syntax[0].estimated_cost.return_value = 9 self._mock_syntax[ 0].get_probability_distribution.return_value = self._probability_distribution_factory.create( { -2: 1, 4: 1 }) self._mock_syntax[0].get_contained_variables.return_value = { "mock one" } self._mock_syntax[1].roll.return_value = 4 self._mock_syntax[1].max.return_value = 6 self._mock_syntax[1].min.return_value = 8 self._mock_syntax[1].__str__.return_value = "2" self._mock_syntax[1].estimated_cost.return_value = 7 self._mock_syntax[ 1].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 8: 1, -3: 2 }) self._mock_syntax[1].get_contained_variables.return_value = { "mock two" } self._test_subtract = SubtractExpression(self._mock_syntax[0], self._mock_syntax[1]) self._mock_parser_gen = create_autospec(rply.ParserGenerator) def test_subtract_add_production_function(self): SubtractExpression.add_production_function( self._mock_parser_gen, self._probability_distribution_factory) self._mock_parser_gen.production.assert_called_once_with( """expression : expression SUBTRACT expression""") def test_subtract_roll(self): for _ in range(100): self.assertEqual(6, self._test_subtract.roll()) def test_subtract_max(self): self.assertEqual(7, self._test_subtract.max()) def test_subtract_min(self): self.assertEqual(-10, self._test_subtract.min()) def test_subtract_str(self): self.assertEqual("7 - 2", str(self._test_subtract)) def test_subtract_estimated_cost(self): self.assertEqual(16, self._test_subtract.estimated_cost()) def test_subtract_get_probability_distribution(self): self._mock_syntax[ 0].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 10: 1, -12: 2, 0: 1 }) self._mock_syntax[ 1].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 2: 1, 3: 2 }) self.assertEqual( { 8: 1, -14: 2, -2: 1, 7: 2, -15: 4, -3: 2 }, self._test_subtract.get_probability_distribution().get_result_map( ), ) def test_subtract_get_contained_variables(self): self.assertSetEqual({"mock one", "mock two"}, self._test_subtract.get_contained_variables())
class TestIntegerDivisionExpression(TestCase): def setUp(self): self._probability_distribution_factory = ProbabilityDistributionFactory( ) self._mock_syntax = [ create_autospec(IDiceExpression) for _ in range(4) ] self._mock_syntax[0].roll.return_value = 10 self._mock_syntax[0].max.return_value = 8 self._mock_syntax[0].min.return_value = 201 self._mock_syntax[0].__str__.return_value = "7" self._mock_syntax[0].estimated_cost.return_value = 7 self._mock_syntax[ 0].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 201: 1, -10: 1 }) self._mock_syntax[0].get_contained_variables.return_value = { "mock one" } self._mock_syntax[1].roll.return_value = 3 self._mock_syntax[1].max.return_value = 8 self._mock_syntax[1].min.return_value = 2 self._mock_syntax[1].__str__.return_value = "2" self._mock_syntax[1].estimated_cost.return_value = 9 self._mock_syntax[ 1].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 2: 1, -2: 1 }) self._mock_syntax[1].get_contained_variables.return_value = { "mock two" } self._mock_syntax[ 2].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 10: 1, 12: 2 }) self._mock_syntax[ 3].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 2: 1, 3: 2, 0: 10 }) self._test_integer_division = IntegerDivisionExpression( self._mock_syntax[0], self._mock_syntax[1]) self._mock_parser_gen = create_autospec(rply.ParserGenerator) def test_integer_division_add_production_function(self): IntegerDivisionExpression.add_production_function( self._mock_parser_gen, self._probability_distribution_factory) self._mock_parser_gen.production.assert_called_once_with( """expression : expression INTEGER_DIVISION expression""") def test_integer_division_roll(self): for _ in range(100): self.assertEqual(3, self._test_integer_division.roll()) def test_integer_division_roll_raise_on_zero(self): self._mock_syntax[1].roll.return_value = 0 self.assertRaises(ZeroDivisionError, self._test_integer_division.roll) def test_integer_division_max(self): self.assertEqual(100, self._test_integer_division.max()) def test_integer_division_max_raise_on_zero(self): self._test_integer_division = IntegerDivisionExpression( self._mock_syntax[2], self._mock_syntax[3]) self.assertRaises(ZeroDivisionError, self._test_integer_division.max) def test_integer_division_min(self): self.assertEqual(-101, self._test_integer_division.min()) def test_integer_division_min_raise_on_zero(self): self._test_integer_division = IntegerDivisionExpression( self._mock_syntax[2], self._mock_syntax[3]) self.assertRaises(ZeroDivisionError, self._test_integer_division.min) def test_integer_division_str(self): self.assertEqual("7 // 2", str(self._test_integer_division)) def test_integer_division_estimated_cost(self): self.assertEqual(16, self._test_integer_division.estimated_cost()) def test_integer_division_get_probability_distribution(self): self._mock_syntax[ 0].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 10: 1, 12: 2, 0: 1 }) self._mock_syntax[ 1].get_probability_distribution.return_value = self._probability_distribution_factory.create( { 2: 1, 3: 2 }) self.assertEqual( { 5: 1, 6: 2, 3: 2, 4: 4, 0: 3 }, self._test_integer_division.get_probability_distribution(). get_result_map(), ) def test_integer_division_get_probability_distribution_raise_on_zero(self): self._test_integer_division = IntegerDivisionExpression( self._mock_syntax[2], self._mock_syntax[3]) self.assertRaises( ZeroDivisionError, self._test_integer_division.get_probability_distribution) def test_integer_division_get_contained_variables(self): self.assertSetEqual( {"mock one", "mock two"}, self._test_integer_division.get_contained_variables(), )
class TestAddExpression(TestCase): def setUp(self): self._probability_distribution_factory = ProbabilityDistributionFactory() self._mock_syntax = [create_autospec(IDiceExpression) for _ in range(2)] self._mock_syntax[0].roll.return_value = 10 self._mock_syntax[0].max.return_value = 8 self._mock_syntax[0].min.return_value = 6 self._mock_syntax[0].__str__.return_value = "7d7" self._mock_syntax[0].estimated_cost.return_value = 9 self._mock_syntax[0].get_probability_distribution.return_value = self._probability_distribution_factory.create( {-2: 1, 4: 1} ) self._mock_syntax[0].get_contained_variables.return_value = {"mock one"} self._mock_syntax[1].roll.return_value = 4 self._mock_syntax[1].max.return_value = 6 self._mock_syntax[1].min.return_value = 8 self._mock_syntax[1].__str__.return_value = "2d2" self._mock_syntax[1].estimated_cost.return_value = 7 self._mock_syntax[1].get_probability_distribution.return_value = self._probability_distribution_factory.create( {8: 1, -3: 2} ) self._mock_syntax[1].get_contained_variables.return_value = {"mock two"} self._test_minmax = MinMaxExpression("MAX", self._mock_syntax[0], self._mock_syntax[1]) self._mock_parser_gen = create_autospec(rply.ParserGenerator) def test_minmax_add_production_function(self): MinMaxExpression.add_production_function(self._mock_parser_gen, self._probability_distribution_factory) self._mock_parser_gen.production.assert_called_once_with( """expression : MINMAX OPEN_PARENTHESIS expression COMMA expression CLOSE_PARENTHESIS""" ) def test_max_roll(self): for _ in range(100): self.assertEqual(10, self._test_minmax.roll()) def test_min_roll(self): self._test_minmax = MinMaxExpression("MIN", self._mock_syntax[0], self._mock_syntax[1]) for _ in range(100): self.assertEqual(4, self._test_minmax.roll()) def test_max_max(self): self.assertEqual(8, self._test_minmax.max()) def test_min_max(self): self._test_minmax = MinMaxExpression("MIN", self._mock_syntax[0], self._mock_syntax[1]) self.assertEqual(6, self._test_minmax.max()) def test_max_min(self): self.assertEqual(8, self._test_minmax.max()) def test_min_min(self): self._test_minmax = MinMaxExpression("MIN", self._mock_syntax[0], self._mock_syntax[1]) self.assertEqual(6, self._test_minmax.min()) def test_max_str(self): self.assertEqual("MAX(7d7, 2d2)", str(self._test_minmax)) def test_min_str(self): self._test_minmax = MinMaxExpression("MIN", self._mock_syntax[0], self._mock_syntax[1]) self.assertEqual("MIN(7d7, 2d2)", str(self._test_minmax)) def test_min_max_estimated_cost(self): self.assertEqual(16, self._test_minmax.estimated_cost()) def test_max_get_probability_distribution(self): self._mock_syntax[0].get_probability_distribution.return_value = self._probability_distribution_factory.create( {10: 1, -12: 2, 0: 1} ) self._mock_syntax[1].get_probability_distribution.return_value = self._probability_distribution_factory.create( {2: 1, 3: 2} ) self.assertEqual( {2: 3, 3: 6, 10: 3}, self._test_minmax.get_probability_distribution().get_result_map(), ) def test_min_get_probability_distribution(self): self._test_minmax = MinMaxExpression("MIN", self._mock_syntax[0], self._mock_syntax[1]) self._mock_syntax[0].get_probability_distribution.return_value = self._probability_distribution_factory.create( {10: 1, -12: 2, 0: 1} ) self._mock_syntax[1].get_probability_distribution.return_value = self._probability_distribution_factory.create( {2: 1, 3: 2} ) self.assertEqual( {-12: 6, 0: 3, 2: 1, 3: 2}, self._test_minmax.get_probability_distribution().get_result_map(), ) def test_max_get_probability_distribution_second_example(self): self._mock_syntax[0].get_probability_distribution.return_value = self._probability_distribution_factory.create( {-3: 1, -2: 1, -1: 1, 0: 1, 5: 1, 6: 1} ) self._mock_syntax[1].get_probability_distribution.return_value = self._probability_distribution_factory.create( {0: 1} ) self.assertEqual( {0: 4, 5: 1, 6: 1}, self._test_minmax.get_probability_distribution().get_result_map(), ) def test_min_get_probability_distribution_second_example(self): self._test_minmax = MinMaxExpression("MIN", self._mock_syntax[0], self._mock_syntax[1]) self._mock_syntax[0].get_probability_distribution.return_value = self._probability_distribution_factory.create( {-1: 1, 1: 1} ) self._mock_syntax[1].get_probability_distribution.return_value = self._probability_distribution_factory.create( {0: 1} ) self.assertEqual( {0: 1, -1: 1}, self._test_minmax.get_probability_distribution().get_result_map(), ) def test_max_get_probability_distribution_third_example(self): self._mock_syntax[0].get_probability_distribution.return_value = self._probability_distribution_factory.create( {-1: 1, 1: 1} ) self._mock_syntax[1].get_probability_distribution.return_value = self._probability_distribution_factory.create( {0: 1} ) self.assertEqual( {0: 1, 1: 1}, self._test_minmax.get_probability_distribution().get_result_map(), ) def test_min_get_probability_distribution_third_example(self): self._test_minmax = MinMaxExpression("MIN", self._mock_syntax[0], self._mock_syntax[1]) self._mock_syntax[0].get_probability_distribution.return_value = self._probability_distribution_factory.create( {-3: 1, -2: 1, -1: 1, 0: 1, 5: 1, 6: 1} ) self._mock_syntax[1].get_probability_distribution.return_value = self._probability_distribution_factory.create( {0: 1} ) self.assertEqual( {-3: 1, -2: 1, -1: 1, 0: 3}, self._test_minmax.get_probability_distribution().get_result_map(), ) def test_min_get_contained_variables(self): self.assertSetEqual({"mock one", "mock two"}, self._test_minmax.get_contained_variables())