def test_nest_by_oper_with_arrays(self): """Test and expression with arrays (combined expressions).""" nested_expr = ConditionalSimplifier.nest_by_oper( [['a', '&', 'b'], '&', ['b', '&', 'c']], '&') self.assertIsInstance(nested_expr, list) self.assertEqual([['a', '&', 'b'], '&', ['b', '&', 'c']], nested_expr)
def set_condition(self, expr): """Set the conditional expression for this prerequisite. Resets the cached state (self._all_satisfied). """ drop_these = [] self._all_satisfied = None if self.pre_initial_messages: for message in self.pre_initial_messages: drop_these.append(message) # Needed to drop pre warm-start dependence: for message in self.satisfied: if message in drop_these: continue if self.start_point: if message[1]: # Cycle point. if get_point(message[1]) < self.start_point <= self.point: # Drop if outside of relevant point range. drop_these.append(message) for message in drop_these: if message in self.satisfied: self.satisfied.pop(message) if '|' in expr: if drop_these: simpler = ConditionalSimplifier( expr, [self.MESSAGE_TEMPLATE % m for m in drop_these]) expr = simpler.get_cleaned() # Make a Python expression so we can eval() the logic. for message in self.satisfied: expr = expr.replace(self.MESSAGE_TEMPLATE % message, self.SATISFIED_TEMPLATE % message) self.conditional_expression = expr
def test_flatten_nested_expr_with_brackets(self): """Test expressions with brackets""" bracketed = ConditionalSimplifier.get_bracketed( ['(', 'a', '&', 'b', ')']) self.assertEqual(['(', ['a', '&', 'b'], ')'], bracketed)
def test_clean_expr(self): """Test clean expressions""" for expr, criterion, expected in self.get_clean_expr: self.assertEqual(expected, ConditionalSimplifier.clean_expr(expr, criterion))
def test_flatten_nested_expr_with_arrays(self): """Test flattened expressions with nested arrays""" flattened = ConditionalSimplifier.flatten_nested_expr([['a', '&', 'b'], '&', 'c']) self.assertEqual('((a & b) & c)', flattened)
def test_flatten_nested_expr(self): """Test flattened expressions""" flattened = ConditionalSimplifier.flatten_nested_expr(['a', '&', 'b']) self.assertEqual('(a & b)', flattened)
def test_nest_by_oper_not_matching_operator(self): """Test when the operation is simply not found. Same input returned.""" input_expr = ['a', 'xor', 'b', 'not', 'c'] nested_expr = ConditionalSimplifier.nest_by_oper(input_expr, 'mod') self.assertIsInstance(nested_expr, list) self.assertEqual(input_expr, nested_expr)
def test_nest_by_oper_simple(self): """Test the case where we have a simple expression.""" nested_expr = ConditionalSimplifier.nest_by_oper( ['a', '||', 'b', '||', 'c'], '||') self.assertIsInstance(nested_expr, list) self.assertEqual([['a', '||', 'b'], '||', 'c'], nested_expr)
def test_flatten_nested_expr_with_arrays(self): """Test flattened expressions with nested arrays""" flattened = ConditionalSimplifier.flatten_nested_expr( [['a', '&', 'b'], '&', 'c']) self.assertEqual('((a & b) & c)', flattened)
def test_nest_by_oper_simple(self): """Test the case where we have a simple expression.""" nested_expr = ConditionalSimplifier.nest_by_oper(['a', '||', 'b', '||', 'c'], '||') self.assertIsInstance(nested_expr, list) self.assertEqual([['a', '||', 'b'], '||', 'c'], nested_expr)