-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathutil_test.py
99 lines (71 loc) · 2.64 KB
/
util_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright (c) 2019 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import random as pyrandom
import shlex
from hypothesis import assume, given
from hypothesis.strategies import floats, integers, iterables, lists, text
from bayesmark import util as bobm_util
from util import _hashable
def some_mock_f(x):
"""Some arbitrary deterministic test function.
"""
random_stream = pyrandom.Random(hash(x))
y = random_stream.gauss(0, 1)
return y
@given(_hashable(), lists(_hashable()))
def test_in_or_none(x, L):
val = bobm_util.in_or_none(x, L)
assert isinstance(val, bool)
assert val == (x in L)
assert val == (x in set(L))
@given(_hashable())
def test_in_or_none_on_none(x):
val = bobm_util.in_or_none(x, None)
assert isinstance(val, bool)
assert val
@given(lists(_hashable()))
def test_in_or_none_self(L):
for xx in L:
val = bobm_util.in_or_none(xx, L)
assert isinstance(val, bool)
assert val
@given(lists(_hashable()))
def test_all_unique(L):
bobm_util.all_unique(L)
@given(lists(integers(), unique=True) | lists(text(), unique=True) | lists(floats(), unique=True))
def test_strict_sorted(L):
bobm_util.strict_sorted(L)
@given(integers(-5, 1000))
def test_range_str(stop):
list(bobm_util.range_str(stop))
@given(text(), lists(text()))
def test_str_join_safe(delim, str_vec):
assume(not any(delim in ss for ss in str_vec))
bobm_util.str_join_safe(delim, str_vec, append=False)
@given(text(), lists(text()), lists(text()))
def test_str_join_safe_append(delim, str_vec0, str_vec):
assume(not any(delim in ss for ss in str_vec0))
assume(not any(delim in ss for ss in str_vec))
start = bobm_util.str_join_safe(delim, str_vec0, append=False)
bobm_util.str_join_safe(delim, [start] + str_vec, append=True)
@given(lists(text()))
def test_shell_join(argv):
cmd = bobm_util.shell_join(argv, delim=" ")
assert shlex.split(cmd) == list(argv)
@given(text(), text(min_size=1))
def test_chomp(str_val, ext):
bobm_util.chomp(str_val + ext, ext)
@given(iterables(_hashable()))
def test_preimage_func(x):
bobm_util.preimage_func(some_mock_f, x)