예제 #1
0
    def example(self):
        """Provide an example of the sort of value that this strategy
        generates. This is biased to be slightly simpler than is typical for
        values from this strategy, for clarity purposes.

        This method shouldn't be taken too seriously. It's here for interactive
        exploration of the API, not for any sort of real testing.

        This method is part of the  public API.

        """
        random = Random()
        context = BuildContext(random)

        parts = []

        for _ in hrange(20):
            if len(parts) >= 3:
                break
            try:
                template = self.draw_and_produce(context)
                reified = self.reify(template)
                parts.append((template, reified))
            except UnsatisfiedAssumption:
                pass
        if not parts:
            raise NoExamples(
                'Could not find any valid examples in 20 tries'
            )

        return min(parts, key=lambda tr: self.size(tr[0]))[1]
예제 #2
0
    def example(self, random=None):
        """Provide an example of the sort of value that this strategy
        generates. This is biased to be slightly simpler than is typical for
        values from this strategy, for clarity purposes.

        This method shouldn't be taken too seriously. It's here for interactive
        exploration of the API, not for any sort of real testing.

        This method is part of the public API.

        """
        from hypothesis import find, settings
        try:
            return find(
                self,
                lambda x: True,
                random=random,
                settings=settings(
                    max_shrinks=0,
                    max_iterations=1000,
                    database=None
                )
            )
        except (NoSuchExample, Unsatisfiable):
            raise NoExamples(
                u'Could not find any valid examples in 100 tries'
            )
예제 #3
0
    def example(self, random=None):
        """Provide an example of the sort of value that this strategy
        generates. This is biased to be slightly simpler than is typical for
        values from this strategy, for clarity purposes.

        This method shouldn't be taken too seriously. It's here for interactive
        exploration of the API, not for any sort of real testing.

        This method is part of the public API.

        """
        context = _current_build_context.value
        if context is not None:
            if context.data is not None and context.data.depth > 0:
                note_deprecation(
                    'Using example() inside a strategy definition is a bad '
                    'idea. It will become an error in a future version of '
                    "Hypothesis, but it's unlikely that it's doing what you "
                    'intend even now. Instead consider using '
                    'hypothesis.strategies.builds() or '
                    '@hypothesis.strategies.composite to define your strategy.'
                    ' See '
                    'https://hypothesis.readthedocs.io/en/latest/data.html'
                    '#hypothesis.strategies.builds or '
                    'https://hypothesis.readthedocs.io/en/latest/data.html'
                    '#composite-strategies for more details.'
                )
            else:
                note_deprecation(
                    'Using example() inside a test function is a bad '
                    'idea. It will become an error in a future version of '
                    "Hypothesis, but it's unlikely that it's doing what you "
                    'intend even now. Instead consider using '
                    'hypothesis.strategies.data() to draw '
                    'more examples during testing. See '
                    'https://hypothesis.readthedocs.io/en/latest/data.html'
                    '#drawing-interactively-in-tests for more details.'
                )

        from hypothesis import find, settings, Verbosity
        try:
            return find(
                self,
                lambda x: True,
                random=random,
                settings=settings(
                    max_shrinks=0,
                    max_iterations=1000,
                    database=None,
                    verbosity=Verbosity.quiet,
                )
            )
        except (NoSuchExample, Unsatisfiable):
            raise NoExamples(
                u'Could not find any valid examples in 100 tries'
            )
예제 #4
0
def some_template(spec, random=None):
    if random is None:
        random = Random()
    strat = strategy(spec)
    for _ in hrange(100):
        element = strat.draw_and_produce(random)
        try:
            strat.reify(element)
            return element
        except UnsatisfiedAssumption:
            pass
    else:
        raise NoExamples('some_template called on strategy with no examples')
예제 #5
0
def some_template(spec, random=None):
    if random is None:
        random = Random()
    strat = spec
    for _ in hrange(100):
        try:
            element = strat.draw_and_produce(random)
        except BadTemplateDraw:
            continue
        try:
            with BuildContext():
                strat.reify(element)
            return element
        except UnsatisfiedAssumption:
            pass
    else:
        raise NoExamples(u'some_template called on strategy with no examples')
예제 #6
0
    def example(self, random=None):
        """Provide an example of the sort of value that this strategy
        generates. This is biased to be slightly simpler than is typical for
        values from this strategy, for clarity purposes.

        This method shouldn't be taken too seriously. It's here for interactive
        exploration of the API, not for any sort of real testing.

        This method is part of the public API.

        """
        random = random or Random()

        for _ in hrange(100):
            try:
                template = self.draw_and_produce(random)
                with BuildContext():
                    return self.reify(template)
            except (BadTemplateDraw, UnsatisfiedAssumption):
                pass
        raise NoExamples(u'Could not find any valid examples in 100 tries')
예제 #7
0
    def example(self, random=None):
        """Provide an example of the sort of value that this strategy
        generates. This is biased to be slightly simpler than is typical for
        values from this strategy, for clarity purposes.

        This method shouldn't be taken too seriously. It's here for interactive
        exploration of the API, not for any sort of real testing.

        This method is part of the public API.

        """
        context = _current_build_context.value
        if context is not None:
            if context.data is not None and context.data.depth > 0:
                note_deprecation(
                    'Using example() inside a strategy definition is a bad '
                    'idea. It will become an error in a future version of '
                    "Hypothesis, but it's unlikely that it's doing what you "
                    'intend even now. Instead consider using '
                    'hypothesis.strategies.builds() or '
                    '@hypothesis.strategies.composite to define your strategy.'
                    ' See '
                    'https://hypothesis.readthedocs.io/en/latest/data.html'
                    '#hypothesis.strategies.builds or '
                    'https://hypothesis.readthedocs.io/en/latest/data.html'
                    '#composite-strategies for more details.'
                )
            else:
                note_deprecation(
                    'Using example() inside a test function is a bad '
                    'idea. It will become an error in a future version of '
                    "Hypothesis, but it's unlikely that it's doing what you "
                    'intend even now. Instead consider using '
                    'hypothesis.strategies.data() to draw '
                    'more examples during testing. See '
                    'https://hypothesis.readthedocs.io/en/latest/data.html'
                    '#drawing-interactively-in-tests for more details.'
                )

        from hypothesis import find, settings, Verbosity

        # Conjecture will always try the zero example first. This would result
        # in us producing the same example each time, which is boring, so we
        # deliberately skip the first example it feeds us.
        first = []

        def condition(x):
            if first:
                return True
            else:
                first.append(x)
                return False
        try:
            return find(
                self,
                condition,
                random=random,
                settings=settings(
                    max_shrinks=0,
                    max_iterations=1000,
                    database=None,
                    verbosity=Verbosity.quiet,
                )
            )
        except (NoSuchExample, Unsatisfiable):
            # This can happen when a strategy has only one example. e.g.
            # st.just(x). In that case we wanted the first example after all.
            if first:
                return first[0]
            raise NoExamples(
                u'Could not find any valid examples in 100 tries'
            )
예제 #8
0
    def example(self, random=None):
        # type: (Random) -> Ex
        """Provide an example of the sort of value that this strategy
        generates. This is biased to be slightly simpler than is typical for
        values from this strategy, for clarity purposes.

        This method shouldn't be taken too seriously. It's here for interactive
        exploration of the API, not for any sort of real testing.

        This method is part of the public API.
        """
        context = _current_build_context.value
        if context is not None:
            if context.data is not None and context.data.depth > 0:
                raise HypothesisException(
                    "Using example() inside a strategy definition is a bad "
                    "idea. Instead consider using hypothesis.strategies.builds() "
                    "or @hypothesis.strategies.composite to define your strategy."
                    " See https://hypothesis.readthedocs.io/en/latest/data.html"
                    "#hypothesis.strategies.builds or "
                    "https://hypothesis.readthedocs.io/en/latest/data.html"
                    "#composite-strategies for more details."
                )
            else:
                raise HypothesisException(
                    "Using example() inside a test function is a bad "
                    "idea. Instead consider using hypothesis.strategies.data() "
                    "to draw more examples during testing. See "
                    "https://hypothesis.readthedocs.io/en/latest/data.html"
                    "#drawing-interactively-in-tests for more details."
                )

        from hypothesis import find, settings, Verbosity

        # Conjecture will always try the zero example first. This would result
        # in us producing the same example each time, which is boring, so we
        # deliberately skip the first example it feeds us.
        first = []  # type: list

        def condition(x):
            if first:
                return True
            else:
                first.append(x)
                return False

        try:
            return find(
                self,
                condition,
                random=random,
                settings=settings(
                    database=None,
                    verbosity=Verbosity.quiet,
                    phases=tuple(set(Phase) - {Phase.shrink}),
                ),
            )
        except (NoSuchExample, Unsatisfiable):
            # This can happen when a strategy has only one example. e.g.
            # st.just(x). In that case we wanted the first example after all.
            if first:
                return first[0]
            raise NoExamples(u"Could not find any valid examples in 100 tries")