コード例 #1
0
ファイル: searchstrategy.py プロジェクト: public/hypothesis
 def __init__(self, elements, descriptor=None):
     SearchStrategy.__init__(self)
     self.elements = tuple(elements)
     if descriptor is None:
         descriptor = descriptors.SampledFrom(self.elements)
     self.descriptor = descriptor
     self.parameter = params.NonEmptySubset(self.elements)
コード例 #2
0
ファイル: searchstrategy.py プロジェクト: public/hypothesis
 def __init__(self, start, end):
     SearchStrategy.__init__(self)
     self.start = start
     self.end = end
     if start > end:
         raise ValueError('Invalid range [%d, %d]' % (start, end))
     self.parameter = params.NonEmptySubset(tuple(range(start, end + 1)),
                                            activation_chance=min(
                                                0.5,
                                                3.0 / (end - start + 1)))
コード例 #3
0
ファイル: searchstrategy.py プロジェクト: public/hypothesis
 def __init__(self, main_strategy, examples):
     assert examples
     assert all(main_strategy.could_have_produced(e) for e in examples)
     self.examples = tuple(examples)
     self.main_strategy = main_strategy
     self.descriptor = main_strategy.descriptor
     self.parameter = params.CompositeParameter(
         examples=params.NonEmptySubset(examples),
         example_probability=params.UniformFloatParameter(0.0, 0.5),
         main=main_strategy.parameter)
     self.has_immutable_data = main_strategy.has_immutable_data
     if hasattr(main_strategy, 'element_strategy'):
         self.element_strategy = main_strategy.element_strategy
コード例 #4
0
ファイル: searchstrategy.py プロジェクト: public/hypothesis
 def __init__(self, strategies):
     SearchStrategy.__init__(self)
     flattened_strategies = []
     for s in strategies:
         if isinstance(s, OneOfStrategy):
             flattened_strategies += s.element_strategies
         else:
             flattened_strategies.append(s)
     strategies = tuple(flattened_strategies)
     if len(strategies) <= 1:
         raise ValueError('Need at least 2 strategies to choose amongst')
     descriptor = descriptors.one_of(
         _unique(s.descriptor for s in strategies))
     self.descriptor = descriptor
     self.element_strategies = list(strategies)
     n = len(self.element_strategies)
     self.parameter = params.CompositeParameter(
         enabled_children=params.NonEmptySubset(range(n)),
         child_parameters=params.CompositeParameter(
             e.parameter for e in self.element_strategies))
     self.has_immutable_data = all(s.has_immutable_data
                                   for s in self.element_strategies)
コード例 #5
0
ファイル: __init__.py プロジェクト: public/hypothesis
class DatetimeStrategy(SearchStrategy):
    descriptor = datetime
    parameter = params.CompositeParameter(
        p_hour=params.UniformFloatParameter(0, 1),
        p_minute=params.UniformFloatParameter(0, 1),
        p_second=params.UniformFloatParameter(0, 1),
        month=params.NonEmptySubset(list(range(1, 13))),
        naive_chance=params.UniformFloatParameter(0, 0.5),
        utc_chance=params.UniformFloatParameter(0, 1),
        timezones=params.NonEmptySubset(
            list(map(pytz.timezone, pytz.all_timezones))))

    def produce(self, random, pv):
        year = random.randint(MINYEAR, MAXYEAR)
        month = random.choice(pv.month)
        base = datetime(
            year=year,
            month=month,
            day=draw_day_for_month(random, year, month),
            hour=maybe_zero_or(random, pv.p_hour, random.randint(0, 23)),
            minute=maybe_zero_or(random, pv.p_minute, random.randint(0, 59)),
            second=maybe_zero_or(random, pv.p_second, random.randint(0, 59)),
            microsecond=random.randint(0, 1000000 - 1),
        )
        if random.random() <= pv.naive_chance:
            return base
        if random.random() <= pv.utc_chance:
            return pytz.UTC.localize(base)
        return random.choice(pv.timezones).localize(base)

    def simplify(self, value):
        if not value.tzinfo:
            yield pytz.UTC.localize(value)
        elif value.tzinfo != pytz.UTC:
            yield pytz.UTC.normalize(value.astimezone(pytz.UTC))
        s = {value}
        s.add(value.replace(microsecond=0))
        s.add(value.replace(second=0))
        s.add(value.replace(minute=0))
        s.add(value.replace(hour=0))
        s.add(value.replace(day=1))
        s.add(value.replace(month=1))
        s.remove(value)
        for t in s:
            yield t
        year = value.year
        if year == 2000:
            return
        yield value.replace(year=2000)
        # We swallow a bunch of value errors here.
        # These can happen if the original value was february 29 on a
        # leap year and the current year is not a leap year.
        # Note that 2000 was a leap year which is why we didn't need one above.
        mid = (year + 2000) // 2
        if mid != 2000 and mid != year:
            try:
                yield value.replace(year=mid)
            except ValueError:
                pass
        years = hrange(year, 2000, -1 if year > 2000 else 1)
        for year in years:
            if year == mid:
                continue
            try:
                yield value.replace(year)
            except ValueError:
                pass
コード例 #6
0
def test_non_empty_subset_errors_on_empty_set():
    with pytest.raises(ValueError) as e:
        params.NonEmptySubset([])
    assert 'at least one' in e.value.args[0]
コード例 #7
0
def test_non_empty_subset_on_one_element_does_not_call_random():
    x = params.NonEmptySubset([1])
    x.draw('this is bogus')