def _f(self, p=p): self.depth = depth out = speccer.spec(depth, getattr(self, p), output=False) # raise other exceptions out if isinstance(out, speccer.UnrelatedException): raise out.reason self.assertIsInstance(out, speccer.clauses.Success)
#!/usr/bin/env python3 from speccer import spec, forall from typing import List def is_sorted(xs): return xs == list(sorted(xs)) def prop_all_lists_are_sorted(): return forall(List[int], is_sorted) if __name__ == '__main__': spec(4, prop_all_lists_are_sorted) ''' Sample Output: ....F ================================================================================ Failure After 4 call(s) (0 did not meet implication) To depth 4 In property `prop_all_lists_are_sorted` prop_all_lists_are_sorted.FORALL(List[int]) -> counterexample: xs=[1, 0] FAIL '''
return list(sorted(xs)) == xs def prop_sorted(): '''A sorted list of length 2 exists ''' return speccer.exists(List[int], lambda xs: speccer.assertThat(is_sorted, xs) and speccer.assertEqual(len(xs), 2)) def prop_nested(): return speccer.forall(List[int], lambda xs: speccer.forall(int, lambda y: y in xs)) if __name__ == '__main__': speccer.spec(3, prop_nested) ''' Sample Output: >> spec(3, prop_sorted) ...* ---------------------------------------- Found witness after 4 call(s) In Property `prop_sorted` ---------------------------------------- Found Witness: prop_sorted::EXISTS(List[int]) -> xs=[0, 0] Reason:
def run_spec(depth, p): sio = io.StringIO() spec(depth, p, outfile=sio) return sio.getvalue()
def prop_sorted(): '''A sorted list of length 2 exists ''' return speccer.exists( List[int], lambda xs: speccer.assertThat(is_sorted, xs) and speccer.assertEqual( len(xs), 2)) def prop_nested(): return speccer.forall(List[int], lambda xs: speccer.forall(int, lambda y: y in xs)) if __name__ == '__main__': speccer.spec(3, prop_nested) ''' Sample Output: >> spec(3, prop_sorted) ...* ---------------------------------------- Found witness after 4 call(s) In Property `prop_sorted` ---------------------------------------- Found Witness: prop_sorted::EXISTS(List[int]) -> xs=[0, 0] Reason: > prop_sorted::EXISTS(List[int]), assert is_sorted([0, 0])