def test_error_if_unrecognized_range_keyword(self): """ The keyword must be 'first', 'allbutfirst', 'allbutlast' or 'last'. """ expression = "my_var(some 3)" with self.assertRaises(ValueError): substitute_quantifiers(expression)
def test_error_if_index_is_str(self): """ Indices must be int. """ expression = "my_var(last ten)" with self.assertRaises(ValueError): substitute_quantifiers(expression)
def test_error_if_missing_index(self): """ The end/starting index may not be missing. Also not in the case of 'first' or 'last'. """ expression = "my_var(last)" with self.assertRaises(ValueError): substitute_quantifiers(expression)
def test_error_if_float(self): """ Floating point indices are sementically undefined and are hence not allowed. """ expression = "my_var(last 3.5)" with self.assertRaises(ValueError): substitute_quantifiers(expression)
def test_substitute_multiple(self): """ Multiple quantifiers should all be substituted. """ expression = "sleep(last 3) + meh(allbutlast2) + fish(last1)" expected = "sleep[-3:] + meh[:-2] + fish[-1:]" result = substitute_quantifiers(expression) self.assertEqual(expected, result)
def test_substitute_extra_whitespaces(self): """ Extra whitespaces within the brackets are allowed as long as they leave the keyword intact. """ expression = "sleep( allbutfirst 3 )" expected = "sleep[3:]" result = substitute_quantifiers(expression) self.assertEqual(expected, result)
def test_preserve_mean(self): """ The 'mean' keyword also uses brackets. They must be retained! """ expression = "mean(something(allbutfirst2))" expected = "mean(something[2:])" result = substitute_quantifiers(expression) self.assertEqual(expected, result)
def test_substitute_without_space(self): """ A whitespace between the quentifier keyword and the index is not enforced. """ expression = "sleep(first3)- 10" expected = "sleep[:3]- 10" result = substitute_quantifiers(expression) self.assertEqual(expected, result)
def read_rules_from_file(filename: str) -> List[Rule]: output = [] rule_lines = extract_lines_with_prefix_from_file(filename, "RULE") for rule_line in rule_lines: cut_rule = cut_rule_expression(rule_line) trig_vars = extract_vars(cut_rule[0]) trig_expr = substitute_vars(cut_rule[0], trig_vars) trig_expr = substitute_quantifiers(trig_expr) trigger = TriggerExpression(trig_expr, trig_vars) mess_vars = extract_vars(cut_rule[1]) mess_expr = substitute_vars(cut_rule[1], mess_vars) mess_expr = substitute_quantifiers(mess_expr) messager = MessageExpression(mess_expr, mess_vars) eval_vars = extract_vars(cut_rule[2]) eval_expr = substitute_vars(cut_rule[2], eval_vars) eval_expr = substitute_quantifiers(eval_expr) evaluator = EvaluationExpression(eval_expr, eval_vars) rule = Rule(trigger, messager, evaluator) output.append(rule) return output
def test_substitute_last(self): expression = "sleep(last 3) -~- 10 :)" expected = "sleep[-3:] -~- 10 :)" result = substitute_quantifiers(expression) self.assertEqual(expected, result)
def test_substitute_first(self): expression = "sleep(first 3)- 10" expected = "sleep[:3]- 10" result = substitute_quantifiers(expression) self.assertEqual(expected, result)
def test_substitute_allbutlast(self): expression = "hello world how(allbutlast 3) oh?" expected = "hello world how[:-3] oh?" result = substitute_quantifiers(expression) self.assertEqual(expected, result)