def parse_recipe(f): consumed_comments = False title = f.readline().rstrip() validate_title(title) if f.readline() != "\n": raise syntax_errors.MissingEmptyLineError(2) lineno = 2 parsed_ingredients = parsed_cooking_time = parsed_oven_temperature = False # set some default values for recipe elements ingredients = Ingredients() cooking_time = oven_temperature = serves = undefined while True: cur_par, cur_line = update_current_par_and_line(f) lineno += 1 if cur_line == "Ingredients.": headline, ingredient_list = cur_par.split("\n", 1) ingredients, lineno = parse_ingredient_list(ingredient_list, lineno) parsed_ingredients = True elif is_cooking_time(cur_line): cooking_time = parse_cooking_time(cur_line, lineno) parsed_cooking_time = True elif is_oven_temperature(cur_line): oven_temperature = parse_oven_temperature(cur_line, lineno) parsed_oven_temperature = True elif cur_line == "Method.": if parsed_ingredients: lineno += 1 if parsed_cooking_time: lineno += 1 if parsed_oven_temperature: lineno += 1 parsed_instructions, lineno = parse_method(cur_par, lineno) serves, lineno = parse_serves_if_possible(f, lineno) break else: if consumed_comments: raise syntax_errors.ChefSyntaxError("missing syntax element: method", lineno) else: lineno += cur_par.count("\n") consumed_comments = True # make sure the whole file content is exhausted rest = f.read() assert rest == "" return Recipe(ingredients, cooking_time, oven_temperature, parsed_instructions, serves)
def test_dotless(self): with pytest.raises(syntax_errors.MissingTrailingFullStopError) as e: validate_title('A title without a dot\n') assert e.value.lineno == 1
def test_missing(self): with pytest.raises(syntax_errors.MissingTitleError): validate_title('')