def test_verifier_builder_contains_pred_list(self): aA = jp.PathPredicate('a', jp.STR_EQ('A')) bB = jp.PathPredicate('b', jp.STR_EQ('B')) builder = jc.ValueObservationVerifierBuilder('TestContainsGroup') builder.contains_path_pred('a', jp.STR_EQ('A')) builder.contains_path_pred('b', jp.STR_EQ('B')) verifier = builder.build() count_aA = jp.CardinalityPredicate(aA, 1, None) count_bB = jp.CardinalityPredicate(bB, 1, None) self.assertEqual([count_aA, count_bB], verifier.constraints)
def test_cardinality_bounds_2_indirect(self): predicate = jp.CardinalityPredicate(_AorB, min=lambda x: x['min'], max=lambda x: x['max']) for min in range(0, 3): for max in range(0, 3): if min > max: continue context = ExecutionContext(min=min, max=max) expect_ok = min <= 2 and max >= 2 source = _CAB result = predicate(context, source) all_results = [ jp.SequencedPredicateResult( False, _AorB, [jp.PathValueResult(_CAB, '', PathValue('[0]', 'C'), valid=False, pred=_eq_A), jp.PathValueResult(_CAB, '', PathValue('[0]', 'C'), valid=False, pred=_eq_B)]), jp.SequencedPredicateResult( True, _AorB, [jp.PathValueResult(_CAB, '', PathValue('[1]', 'A'), valid=True, pred=_eq_A)]), jp.SequencedPredicateResult( True, _AorB, [jp.PathValueResult(_CAB, '', PathValue('[2]', 'B'), valid=False, pred=_eq_A), jp.PathValueResult(_CAB, '', PathValue('[2]', 'B'), valid=True, pred=_eq_B)])] builder = jp.PathPredicateResultBuilder( pred=jp.PathPredicate('', _AorB), source=_CAB) builder.add_result_candidate(PathValue('[0]', 'C'), all_results[0]) builder.add_result_candidate(PathValue('[1]', 'A'), all_results[1]) builder.add_result_candidate(PathValue('[2]', 'B'), all_results[2]) expect_path_result = builder.build(True) if expect_ok: self.assertEqual( jp.ConfirmedCardinalityResult(predicate, expect_path_result), result) elif max == 0: self.assertEqual( jp.UnexpectedValueCardinalityResult( predicate, expect_path_result), result) else: self.assertEqual( jp.FailedCardinalityRangeResult( predicate, expect_path_result), result)
def test_cardinality_bounds_1(self): for min in range(0, 3): for max in range(0, 3): if min > max: continue predicate = jp.CardinalityPredicate(_AorX, min=min, max=max) expect_ok = min <= 1 and max >= 1 source = _CAB result = predicate(source) all_results = [ jp.CompositePredicateResult(False, _AorX, [ jp.PathValueResult(_CAB, '', PathValue('[0]', 'C'), valid=False, pred=_eq_A), jp.PathValueResult(_CAB, '', PathValue('[0]', 'C'), valid=False, pred=_eq_X) ]), jp.CompositePredicateResult(True, _AorX, [ jp.PathValueResult(_CAB, '', PathValue('[1]', 'A'), valid=True, pred=_eq_A) ]), jp.CompositePredicateResult(False, _AorX, [ jp.PathValueResult(_CAB, '', PathValue('[2]', 'B'), valid=False, pred=_eq_A), jp.PathValueResult(_CAB, '', PathValue('[2]', 'B'), valid=False, pred=_eq_X) ]) ] builder = jp.PathPredicateResultBuilder(pred=jp.PathPredicate( '', _AorX), source=_CAB) builder.add_result_candidate(PathValue('[0]', 'C'), all_results[0]) builder.add_result_candidate(PathValue('[1]', 'A'), all_results[1]) builder.add_result_candidate(PathValue('[2]', 'B'), all_results[2]) expect_path_result = builder.build(True) if expect_ok: self.assertEqual( jp.ConfirmedCardinalityResult(predicate, expect_path_result), result) elif max == 0: self.assertEqual( jp.UnexpectedValueCardinalityResult( predicate, expect_path_result), result) else: self.assertEqual( jp.FailedCardinalityRangeResult( predicate, expect_path_result), result)