我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用hypothesis.example()。
def pattern_to_statements(pattern): if isinstance(pattern, template): return lists(just(pattern), min_size=1, max_size=1) rule, value = pattern if rule == 'sequence': return tuples(*map(pattern_to_statements, value)).map(unpack_list).map(list) elif rule == 'alternates': return one_of(*map(pattern_to_statements, value)) elif rule == 'zeroOrMore': return lists(pattern_to_statements(value)).map(unpack_list).map(list) elif rule == 'oneOrMore': return lists(pattern_to_statements(value), min_size=1).map(unpack_list).map(list) elif rule == 'optional': return lists(pattern_to_statements(value), min_size=0, max_size=1).map(unpack_list).map(list) else: raise Exception("impossible!", rule) # this replicates the current scorm pattern, a realistic example of medium # complexity. Note it has repeated elements, just not in ambiguous ways.
def test_cached_property(): class A: call_count = 0 @cached_property def example(self): """Docstring Test""" A.call_count += 1 return 42 a = A() b = A() assert A.call_count == 0 assert a.example == 42 assert A.call_count == 1 assert a.example == 42 assert A.call_count == 1 assert b.example == 42 assert A.call_count == 2 assert b.example == 42 assert A.call_count == 2 assert A.example.__doc__ == "Docstring Test"
def test_slot_cached_property(): class A: __slots__ = ('cache', ) call_count = 0 @slot_cached_property('cache') def example(self): """Docstring Test""" A.call_count += 1 return 42 a = A() b = A() assert A.call_count == 0 assert a.example == 42 assert A.call_count == 1 assert a.example == 42 assert A.call_count == 1 assert b.example == 42 assert A.call_count == 2 assert b.example == 42 assert A.call_count == 2 assert A.example.__doc__ == "Docstring Test"
def test_examples(self): """ Discover the example settings modules. """ # Limit the search to the test root to avoid having to mask out third-party modules, # such as hypothesis._settings. with mock.patch('sys.path', [TEST_ROOT]): self.assertEqual( list(utils.discover_candidate_settings()), [(TEST_ROOT, [ 'test_examples.error_settings', 'test_examples.likely_settings', 'test_examples.no_likely_settings', 'test_examples.no_settings', ])])
def test_errors(self): """ If a cert renewal fails within the panic interval, the panic callback is invoked; otherwise the error is logged normally. """ now = datetime(2000, 1, 1, 0, 0, 0) certs = { u'a' * 100: _generate_cert( u'example.com', not_valid_before=now - timedelta(seconds=1), not_valid_after=now + timedelta(days=31)), } panics = [] with AcmeFixture(now=now, certs=certs, panic=lambda *a: panics.append(a)) as fixture: fixture.service.startService() self.assertThat( fixture.service.when_certs_valid(), succeeded(Is(None))) self.assertThat(fixture.responder.challenges, HasLength(0)) fixture.clock.advance(36 * 60 * 60) self.assertThat(flush_logged_errors(), HasLength(1)) self.assertThat(panics, Equals([])) self.assertThat(fixture.responder.challenges, HasLength(0)) fixture.clock.advance(15 * 24 * 60 * 60) self.assertThat( panics, MatchesListwise([ MatchesListwise([IsInstance(Failure), Equals(u'a' * 100)]), ])) self.assertThat(fixture.responder.challenges, HasLength(0))
def test_timer_errors(self): """ If the timed check fails (for example, because registration fails), the error should be caught and logged. """ with AcmeFixture(client=FailingClient()) as fixture: fixture.service.startService() self.assertThat( fixture.service._check_certs(), succeeded(Always())) self.assertThat(flush_logged_errors(), HasLength(2))
def test_stop_responding_already_stopped(self, token): """ Calling ``stop_responding`` when we are not responding for a server name does nothing. """ challenge = self._challenge_factory(token=token) response = challenge.response(RSA_KEY_512) responder = self._responder_factory() d = maybeDeferred( responder.stop_responding, u'example.com', challenge, response) self._do_one_thing() self.assertThat(d, succeeded(Always()))
def test_start_responding(self, token): """ Calling ``start_responding`` makes an appropriate resource available. """ challenge = challenges.HTTP01(token=token) response = challenge.response(RSA_KEY_512) responder = HTTP01Responder() challenge_resource = Resource() challenge_resource.putChild(b'acme-challenge', responder.resource) root = Resource() root.putChild(b'.well-known', challenge_resource) client = StubTreq(root) encoded_token = challenge.encode('token') challenge_url = URL(host=u'example.com', path=[ u'.well-known', u'acme-challenge', encoded_token]).asText() self.assertThat(client.get(challenge_url), succeeded(MatchesStructure(code=Equals(404)))) responder.start_responding(u'example.com', challenge, response) self.assertThat(client.get(challenge_url), succeeded(MatchesAll( MatchesStructure( code=Equals(200), headers=AfterPreprocessing( methodcaller('getRawHeaders', b'content-type'), Equals([b'text/plain']))), AfterPreprocessing(methodcaller('content'), succeeded( Equals(response.key_authorization.encode('utf-8')))) ))) # Starting twice before stopping doesn't break things responder.start_responding(u'example.com', challenge, response) self.assertThat(client.get(challenge_url), succeeded(MatchesStructure(code=Equals(200)))) responder.stop_responding(u'example.com', challenge, response) self.assertThat(client.get(challenge_url), succeeded(MatchesStructure(code=Equals(404))))
def _responder_factory(self, zone_name=u'example.com'): responder = LibcloudDNSResponder.create( reactor=SynchronousReactorThreads(), driver_name='dummy', username='ignored', password='ignored', zone_name=zone_name, settle_delay=0.0) if zone_name is not None: responder._driver.create_zone(zone_name) responder._thread_pool, self._perform = createMemoryWorker() return responder
def test_equivalent_state_example(): # Construct the DFA in this example: # https://www.tutorialspoint.com/automata_theory/dfa_minimization.htm alphabet = '01' a, b, c, d, e, f = states = 'abcdef' dfa = DFA(a, False, alphabet=alphabet) # type: DFA[six.text_type] dfa.add_state(b, False) dfa.add_state(c, True) dfa.add_state(d, True) dfa.add_state(e, True) dfa.add_state(f, False) assert set(dfa.find_invalid_nodes()) == set(states) dfa.add_transition(a, b, '0') dfa.add_transition(a, c, '1') assert set(dfa.find_invalid_nodes()) == set(states) - {a} dfa.add_transition(b, a, '0') dfa.add_transition(b, d, '1') assert set(dfa.find_invalid_nodes()) == set(states) - {a, b} dfa.add_transition(c, e, '0') dfa.add_transition(c, f, '1') assert set(dfa.find_invalid_nodes()) == set(states) - {a, b, c} dfa.add_transition(d, e, '0') dfa.add_transition(d, f, '1') assert set(dfa.find_invalid_nodes()) == set(states) - {a, b, c, d} dfa.add_transition(e, e, '0') dfa.add_transition(e, f, '1') assert set(dfa.find_invalid_nodes()) == set(states) - {a, b, c, d, e} dfa.add_transition(f, f, '0') dfa.add_transition(f, f, '1') assert not dfa.find_invalid_nodes() expected = {(a, b), (c, d), (c, e), (d, e)} expected |= {(x, x) for x in states} expected |= {(p, q) for (q, p) in expected} equivalent = get_equivalent_states(dfa) assert equivalent == expected new_dfa = minimize_dfa(dfa) assert not new_dfa.find_invalid_nodes() expected_dfa = DFA('ab', False, alphabet=alphabet) # type: DFA[six.text_type] expected_dfa.add_state('cde', True) expected_dfa.add_state('f', False) expected_dfa.add_transition('ab', 'ab', '0') expected_dfa.add_transition('ab', 'cde', '1') expected_dfa.add_transition('cde', 'cde', '0') expected_dfa.add_transition('cde', 'f', '1') expected_dfa.add_transition('f', 'f', '0') expected_dfa.add_transition('f', 'f', '1') assert not expected_dfa.construct_isomorphism(dfa) assert expected_dfa.construct_isomorphism(new_dfa)
def test_time_marches_on(self): """ Any certs that have exceeded the panic or reissue intervals will be reissued at the next check. """ now = datetime(2000, 1, 1, 0, 0, 0) certs = { u'example.com': _generate_cert( u'example.com', not_valid_before=now - timedelta(seconds=1), not_valid_after=now + timedelta(days=31)), u'example.org': _generate_cert( u'example.org', not_valid_before=now - timedelta(seconds=1), not_valid_after=now + timedelta(days=32)), } with AcmeFixture(now=now, certs=certs) as fixture: fixture.service.startService() self.assertThat( fixture.service.when_certs_valid(), succeeded(Is(None))) self.assertThat( fixture.cert_store.as_dict(), succeeded(Equals(certs))) fixture.clock.advance(36 * 60 * 60) self.assertThat( fixture.cert_store.as_dict(), succeeded( MatchesDict({ u'example.com': Not(Equals(certs[u'example.com'])), u'example.org': Equals(certs[u'example.org']), }))) self.assertThat(fixture.responder.challenges, HasLength(0)) fixture.clock.advance(36 * 60 * 60) self.assertThat( fixture.cert_store.as_dict(), succeeded( MatchesDict({ u'example.com': Not(Equals(certs[u'example.com'])), u'example.org': Not(Equals(certs[u'example.org'])), }))) self.assertThat(fixture.responder.challenges, HasLength(0))