def detect_ingredient_state(measure, measure_type=None, lineno=None): """If there is no measure given, the ingredient is neither dry nor liquid. If there is any valid measure_type given, the ingredient is dry and needs no further analysis. If the given measure is "g", "kg", "pinch" or "pinches", it is dry. If it is "l", "ml", "dash" or "dashes", it is "liquid". If it is "cup(s)", "teaspoon(s)" or "tablespoon(s)", it is not known if the ingredient is dry or liquid. This function returns a tuple in the form (is_dry, is_liquid). """ if measure is None: is_dry, is_liquid = False, False elif measure_type is not None: validate_measure_type(measure, measure_type, lineno) is_dry, is_liquid = True, False elif re.match(DRY_MEASURE_PATTERN, measure) is not None: is_dry, is_liquid = True, False elif re.match(LIQUID_MEASURE_PATTERN, measure) is not None: is_dry, is_liquid = False, True elif re.match(DRY_OR_LIQUID_MEASURE_PATTERN, measure) is not None: is_dry, is_liquid = unknown, unknown else: raise ValueError("invalid measure: %r" % measure) return is_dry, is_liquid
def test_non_matching_measure_type(self, measure): with pytest.raises(syntax_errors.NonMatchingMeasureTypeError): validate_measure_type(measure, 'heaped') with pytest.raises(syntax_errors.NonMatchingMeasureTypeError): validate_measure_type(measure, 'level')
def test_invalid_measure_type_value(self): empty_measure = '' with pytest.raises(syntax_errors.InvalidMeasureTypeValue): validate_measure_type(empty_measure, 'foo')