def roll(dice_spec: str) -> Tuple[int, Sequence[int]]: tracer = execution_context.get_opencensus_tracer() try: with tracer.span('initial_parse'): tracer.add_attribute_to_current_span("dice_spec", dice_spec) tree = get_parser().parse(dice_spec) except LarkError as e: raise RecognitionError( "Sorry, I couldn't understand your request") from e logging.debug("Initial parse tree:\n%s", pprint(tree)) try: tree = NumberTransformer().transform(tree) with tracer.span('dnd_knowledge'): tree = DnD5eKnowledge().transform(tree) tree = SimplifyTransformer().transform(tree) with tracer.span('crit_transform'): tree = CritTransformer().transform(tree) logging.debug("DnD transformed parse tree:\n%s", pprint(tree)) with tracer.span('final_eval'): transformer = EvalDice() tree = transformer.transform(tree) tree = SimplifyTransformer().transform(tree) except VisitError as e: # Get our nice exception out of lark's wrapper raise e.orig_exc return (tree.children[0], transformer.dice_results)
def test_roll_n_negative_count(self): initial_tree = Tree("roll_n", [-1, 3]) with self.assertRaises(UnfulfillableRequestError): try: EvalDice().transform(initial_tree) except VisitError as e: raise e.orig_exc
def test_roll_n_zero_sides(self): initial_tree = Tree("roll_n", [2, 0]) with self.assertRaises(UnfulfillableRequestError): try: EvalDice().transform(initial_tree) except VisitError as e: raise e.orig_exc
def test_roll_n(self, mock_randint): mock_randint.side_effect = range(1, 3) initial_tree = Tree("roll_n", [2, 3]) final_tree = EvalDice().transform(initial_tree) self.assertSimpleTreeEqual(final_tree, 3) mock_randint.assert_has_calls([mock.call(1, 3)] * 2)
def test_min(self): initial_tree = Tree("min", [2, 3]) final_tree = EvalDice().transform(initial_tree) self.assertSimpleTreeEqual(final_tree, 2)
def test_sub(self): initial_tree = Tree("sub", [2, 3]) final_tree = EvalDice().transform(initial_tree) self.assertSimpleTreeEqual(final_tree, -1)