def test_reference_to_nested_sub_recipe(self) -> None: nested_sr = SubRecipe(Ingredient(SVS("eggs")), (SVS("foo"), )) step = Step(SVS("scramble"), (nested_sr, )) ref = Reference(nested_sr) with pytest.raises(ReferenceToInvalidSubRecipeError): Recipe((step, ref))
def test_string_compilation(self) -> None: # Just a sanity check assert compile(["spam {3 eggs}"]) == [ Recipe((SubRecipe( Ingredient(SVS(("spam ", 3, " eggs"))), (SVS(["spam ", 3, " eggs"]), ), False, ), )) ]
def test_processed_ingredient_with_implied_output_name( self, syntax: str) -> None: assert compile([syntax]) == [ Recipe((SubRecipe( Step(SVS("fry"), (Ingredient(SVS("spam")), )), (SVS("spam"), ), False, ), )), ]
def test_reference_has_no_inferred_name(self) -> None: sub_recipe = SubRecipe(Ingredient(SVS("spam")), (SVS("spam"), ), False) assert compile(["spam\nspam\nspam"]) == [ Recipe(( sub_recipe, Reference(sub_recipe, 0), Reference(sub_recipe, 0), )), ]
def test_substitute(self) -> None: a = Ingredient(SVS("a")) b = Ingredient(SVS("b")) c = Ingredient(SVS("c")) orig = SubRecipe(a, (SVS("foo"), )) assert orig.substitute(a, b) == SubRecipe(b, (SVS("foo"), )) assert orig.substitute(orig, b) == b assert orig.substitute(c, b) == orig
def test_compilation_of_steps(self) -> None: assert compile(["fry(slice(spam), eggs)"]) == [ Recipe((Step( SVS("fry"), ( Step(SVS("slice"), (Ingredient(SVS("spam")), )), Ingredient(SVS("eggs")), ), ), )), ]
def test_render_sub_recipe_outputs() -> None: assert render_sub_recipe_outputs( SubRecipe(Ingredient(SVS("spam")), (SVS("foo"), SVS("bar"), SVS("baz"))), "sub-recipe-", ) == ('<ul class="rg-sub-recipe-output-list">\n' ' <li id="sub-recipe-foo">foo</li>\n' ' <li id="sub-recipe-bar">bar</li>\n' ' <li id="sub-recipe-baz">baz</li>\n' "</ul>")
def test_subrecipe_outputs(self) -> None: assert render_cell( Cell(SubRecipe(Ingredient(SVS("spam")), (SVS("foo"), SVS("bar"))))) == ( '<td class="rg-sub-recipe-outputs">\n' ' <ul class="rg-sub-recipe-output-list">\n' ' <li id="sub-recipe-foo">foo</li>\n' ' <li id="sub-recipe-bar">bar</li>\n' " </ul>\n" "</td>")
def test_name_validation(self) -> None: sr = SubRecipe(Ingredient(SVS("spam")), (SVS("foo"), SVS("bar"))) # Should work Reference(sr) Reference(sr, 0) Reference(sr, 1) # Unknown name with pytest.raises(OutputIndexError): Reference(sr, 2)
def test_scale(self) -> None: sr_2 = SubRecipe(Ingredient(SVS("spam"), Quantity(2)), (SVS([2, " blocks of spam"]), )) sr_6 = SubRecipe(Ingredient(SVS("spam"), Quantity(6)), (SVS([6, " blocks of spam"]), )) assert Reference(sr_2, 0).scale(3) == Reference(sr_6, 0) assert Reference(sr_2, 0, Quantity(100, "g")).scale(3) == Reference( sr_6, 0, Quantity(300, "g"), )
def test_inlining_single_references_name_explicitly_required(self) -> None: assert compile(["meat := spam, sliced\nfry(meat, eggs)"]) == [ Recipe((Step( SVS("fry"), ( SubRecipe( Step(SVS("sliced"), (Ingredient(SVS("spam")), )), (SVS("meat"), ), ), Ingredient(SVS("eggs")), ), ), )) ]
def test_single_output_hidden_subrecipe(self) -> None: assert render_recipe_tree( SubRecipe(Ingredient(SVS("spam")), (SVS("spam"), ), show_output_names=False), "qux-", ) == ('<table class="rg-table" id="qux-spam">' "<tr>" '<td class="rg-ingredient ' "rg-border-left-sub-recipe " "rg-border-right-sub-recipe " "rg-border-top-sub-recipe " 'rg-border-bottom-sub-recipe">spam</td>' "</tr>" "</table>")
def test_inlining_single_references(self, quantity_spec: str) -> None: assert compile( [f"meat = 10g spam, sliced\nfry({quantity_spec} meat, eggs)"]) == [ Recipe((Step( SVS("fry"), ( Step( SVS("sliced"), (Ingredient(SVS("spam"), Quantity(10, "g")), ), ), Ingredient(SVS("eggs")), ), ), )) ]
def test_scale(self) -> None: sr_2 = SubRecipe(Ingredient(SVS("spam"), Quantity(2)), (SVS("spam"), )) ref_sr_2 = Reference(sr_2) sr_6 = SubRecipe(Ingredient(SVS("spam"), Quantity(6)), (SVS("spam"), )) ref_sr_6 = Reference(sr_6) first_rec_2 = Recipe((sr_2, )) second_rec_2 = Recipe((ref_sr_2, ), follows=first_rec_2) first_rec_6 = Recipe((sr_6, )) second_rec_6 = Recipe((ref_sr_6, ), follows=first_rec_6) assert first_rec_2.scale(3) == first_rec_6 assert second_rec_2.scale(3) == second_rec_6
def test_single_output_sub_recipe_hidden() -> None: ingredient = Ingredient(SVS("spam")) step = Step(SVS("fry"), (ingredient, )) sub_recipe = SubRecipe(step, (SVS("out"), ), show_output_names=False) assert recipe_tree_to_table(sub_recipe) == set_border_around_table( Table.from_dict( cast( Mapping[Tuple[int, int], Cell[RecipeTreeNode]], { (0, 0): Cell(ingredient), (0, 1): Cell(step) }, )), BorderType.sub_recipe, )
def __init__(self, match: Match[str]) -> None: self.string = SVS( [ # Fraction case ( (int(submatch["integer"]) if submatch["integer"] is not None else 0) + Fraction( int(submatch["numerator"]), int(submatch["denominator"]), ) ) if submatch["numerator"] is not None else # Decimal case ( int(float(submatch["decimal"])) if "." not in submatch["decimal"] else float(submatch["decimal"]) ) if submatch["decimal"] is not None else # Escaped char case submatch["escaped_char"] if submatch["escaped_char"] is not None else submatch["char"] for submatch in self.any_part_pattern.finditer(match["source"]) ] )
def test_inlines_within_inlines(self) -> None: recipe = Recipe((Step( SVS("boil"), ( SubRecipe( Step( SVS("fry"), (Ingredient(SVS("spam"), Quantity(100, "g")), ), ), (SVS("fried spam"), ), ), Ingredient(SVS("water")), ), ), )) assert compile([ "100g spam\nfried spam := fry(spam)\nboil(fried spam, water)" ]) == [recipe]
def test_child_assertion_checked(self) -> None: ingredient = Ingredient(SVS("spam")) singleton_sub_recipe = SubRecipe(Ingredient(SVS("eggs")), (SVS("foo"), )) multiple_sub_recipe = SubRecipe(Ingredient(SVS("foo")), (SVS("bar"), SVS("baz"))) # Should work SubRecipe(ingredient, (SVS("foo"), )) SubRecipe(singleton_sub_recipe, (SVS("foo"), )) # Cannot have child with multiple outputs with pytest.raises(MultiOutputSubRecipeUsedAsNonRootNodeError): SubRecipe(multiple_sub_recipe, (SVS("foo"), ))
def test_dont_inline_partial_uses_of_a_subrecipe(self) -> None: sub_recipe = SubRecipe( Ingredient(SVS("spam"), Quantity(100, "g")), (SVS("spam"), ), False, ) assert compile(["100g spam\nfry(50g spam, eggs)"]) == [ Recipe(( sub_recipe, Step( SVS("fry"), ( Reference(sub_recipe, 0, Quantity(50, "g")), Ingredient(SVS("eggs")), ), ), )) ]
def test_dont_inline_multi_output_subrecipes(self) -> None: sub_recipe = SubRecipe( Ingredient(SVS("spam")), (SVS("meat"), SVS("tin")), True, ) assert compile(["meat, tin = spam\nfry(meat, eggs)"]) == [ Recipe(( sub_recipe, Step( SVS("fry"), ( Reference(sub_recipe, 0), Ingredient(SVS("eggs")), ), ), )) ]
def test_valid_references(self) -> None: sr = SubRecipe(Ingredient(SVS("eggs")), (SVS("foo"), )) ref1 = Reference(sr) # Shouldn't fail rec1 = Recipe((sr, ref1)) # Also shouldn't fail (since marked as follows) ref2 = Reference(sr) rec2 = Recipe((ref2, ), follows=rec1) # Chained references ref3 = Reference(sr) Recipe((ref3, ), follows=rec2) # Should fail: not referenced ref4 = Reference(sr) with pytest.raises(ReferenceToInvalidSubRecipeError): Recipe((ref4, ))
def test_dont_inline_definitions_from_earlier_blocks(self) -> None: sub_recipe = SubRecipe( Ingredient(SVS("spam"), Quantity(100, "g")), (SVS("spam"), ), False, ) recipe0 = Recipe((sub_recipe, )) recipe1 = Recipe( (Step( SVS("fry"), ( Reference(sub_recipe, 0, Quantity(50, "g")), Ingredient(SVS("eggs")), ), ), ), follows=recipe0, ) assert compile(["100g spam", "fry(50g spam, eggs)"]) == [recipe0, recipe1]
def test_ingredient_compilation(self) -> None: assert compile( ["500g spam\n2 eggs\n1 kg foo\n1 can of dog food\nheat"]) == [ Recipe(( # With unit SubRecipe( Ingredient(SVS("spam"), Quantity(500.0, "g")), (SVS("spam"), ), False, ), # No unit SubRecipe( Ingredient(SVS("eggs"), Quantity(2.0)), (SVS("eggs"), ), False, ), # With spacing between number and unit SubRecipe( Ingredient(SVS("foo"), Quantity(1.0, "kg", value_unit_spacing=" ")), (SVS("foo"), ), False, ), # With spacing between number and unit SubRecipe( Ingredient( SVS("dog food"), Quantity( 1, "can", value_unit_spacing=" ", preposition=" of", ), ), (SVS("dog food"), ), False, ), # No quantity SubRecipe(Ingredient(SVS("heat")), (SVS("heat"), ), False), )) ]
def test_non_subrecipe(self) -> None: assert render_recipe_tree(Ingredient( SVS("spam"))) == ('<table class="rg-table">' "<tr>" '<td class="rg-ingredient ' "rg-border-left-sub-recipe " "rg-border-right-sub-recipe " "rg-border-top-sub-recipe " 'rg-border-bottom-sub-recipe">spam</td>' "</tr>" "</table>")
def test_recipes_split_across_blocks(self) -> None: compiled = compile_markdown( dedent(""" A recipe in two parts. Part one: sauce = boil down(tomatoes, water) Part two: pour over(pasta, sauce) """).strip()) sr = SubRecipe( Step( SVS("boil down"), ( Ingredient(SVS("tomatoes")), Ingredient(SVS("water")), ), ), (SVS("sauce"), ), ) r1 = Recipe((sr, )) r2 = Recipe( (Step( SVS("pour over"), ( Ingredient(SVS("pasta")), Reference(sr), ), ), ), follows=r1, ) assert compiled.recipes == [[r1, r2]]
def test_render_table(id: Optional[str], exp_attrs: str) -> None: assert render_table( Table.from_dict({ (0, 0): Cell(Ingredient(SVS("spam"))), (1, 0): Cell(Ingredient(SVS("eggs"))), (0, 1): Cell( Step(SVS("fry"), (Ingredient(SVS("spam")), Ingredient(SVS("eggs")))), rows=2, ), }), id=id, ) == (f'<table class="rg-table"{exp_attrs}>\n' " <tr>\n" ' <td class="rg-ingredient">spam</td>\n' ' <td class="rg-step" rowspan="2">fry</td>\n' " </tr>\n" ' <tr><td class="rg-ingredient">eggs</td></tr>\n' "</table>")
def test_step() -> None: input_0 = Ingredient(SVS("input 0")) input_1 = Ingredient(SVS("input 1")) input_2_ingredient = Ingredient(SVS("input 2")) input_2 = Step(SVS("chopped"), (input_2_ingredient, )) step = Step(SVS("combine"), (input_0, input_1, input_2)) assert recipe_tree_to_table(step) == set_border_around_table( Table.from_dict( cast( Mapping[Tuple[int, int], Cell[RecipeTreeNode]], { (0, 0): Cell(input_0, columns=2), (1, 0): Cell(input_1, columns=2), (2, 0): Cell(input_2_ingredient), (2, 1): Cell(input_2), (0, 2): Cell(step, rows=3), }, )), BorderType.sub_recipe, )
def test_recipe_code_blocks_and_scaled_rendering(self, source: str) -> None: compiled = compile_markdown(dedent(source).strip()) assert compiled.title == "A recipe" assert compiled.servings == 2 assert compiled.recipes == [[ Recipe((Step( SVS("fry"), ( Ingredient(SVS("spam"), Quantity(100, "g")), Ingredient(SVS("eggs"), Quantity(2)), ), ), )), ]] assert (compiled.render(2) == dedent(""" <header><h1 class="rg-title-scalable">A recipe <span class="rg-serving-count">for <span class="rg-scaled-value">4</span></span></h1><p>Rescaled from <span class="rg-original-servings">2 servings</span>.</p></header> <div class="rg-recipe-block"> <table class="rg-table"> <tr> <td class="rg-ingredient rg-border-left-sub-recipe rg-border-top-sub-recipe"> <span class="rg-quantity-with-conversions rg-scaled-value" tabindex="0"> 200g<ul class="rg-quantity-conversions"> <li><sup>1</sup>⁄<sub>5</sub>kg</li> <li>0.441lb</li> <li>7.05oz</li> </ul> </span> spam </td> <td class="rg-step rg-border-right-sub-recipe rg-border-top-sub-recipe rg-border-bottom-sub-recipe" rowspan="2">fry</td> </tr> <tr><td class="rg-ingredient rg-border-left-sub-recipe rg-border-bottom-sub-recipe"><span class="rg-quantity-unitless rg-scaled-value">4</span> eggs</td></tr> </table> </div><p>Ta-da!</p> """ # noqa: E501 ).lstrip())
def test_edges(self) -> None: assert render_cell( Cell( Ingredient(SVS("spam")), border_top=BorderType.sub_recipe, border_left=BorderType.none, border_bottom=BorderType.sub_recipe, border_right=BorderType.none, ), ) == ('<td class="' "rg-ingredient " "rg-border-left-none " "rg-border-right-none " "rg-border-top-sub-recipe " "rg-border-bottom-sub-recipe" '">spam</td>')
def test_substitute(self) -> None: a = Ingredient(SVS("a")) b = Ingredient(SVS("b")) c = Ingredient(SVS("c")) d = Ingredient(SVS("d")) orig = Step(SVS("stir"), (a, b)) assert orig.substitute(a, c) == Step(SVS("stir"), (c, b)) assert orig.substitute(orig, c) == c assert orig.substitute(d, c) == orig