Skip to content

Commit a7c6c72

Browse files
committed
Adding tests
1 parent 70be529 commit a7c6c72

File tree

4 files changed

+310
-0
lines changed

4 files changed

+310
-0
lines changed

poetry.lock

Lines changed: 163 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prop_based_tests.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from hypothesis import example, given, strategies as st
2+
from hypothesis.strategies import text
3+
4+
def encode(input_string):
5+
if not input_string:
6+
return []
7+
8+
count = 1
9+
prev = ""
10+
lst = []
11+
12+
for character in input_string:
13+
if character != prev:
14+
if prev:
15+
entry = (prev, count)
16+
lst.append(entry)
17+
count = 1 # Missing reset operation
18+
prev = character
19+
else:
20+
count += 1
21+
entry = (character, count)
22+
lst.append(entry)
23+
return lst
24+
25+
26+
def decode(lst):
27+
q = ""
28+
for character, count in lst:
29+
q += character * count
30+
return q
31+
32+
33+
@given(st.integers(), st.integers())
34+
def test_ints_are_commutative(x, y):
35+
assert x + y == y + x
36+
37+
38+
@given(x=st.integers(), y=st.integers())
39+
def test_ints_cancel(x, y):
40+
assert (x + y) - y == x
41+
42+
43+
@given(st.lists(st.integers()))
44+
def test_reversing_twice_gives_same_list(xs):
45+
# This will generate lists of arbitrary length (usually between 0 and
46+
# 100 elements) whose elements are integers.
47+
ys = list(xs)
48+
ys.reverse()
49+
ys.reverse()
50+
assert xs == ys
51+
52+
53+
@given(st.tuples(st.booleans(), st.text()))
54+
def test_look_tuples_work_too(t):
55+
# A tuple is generated as the one you provided, with the corresponding
56+
# types in those positions.
57+
assert len(t) == 2
58+
assert isinstance(t[0], bool)
59+
assert isinstance(t[1], str)
60+
61+
@given(s=st.text())
62+
@example(s="")
63+
def test_decode_inverts_encode(s):
64+
assert decode(encode(s)) == s
65+
66+
if __name__ == "__main__":
67+
test_decode_inverts_encode()

pyproject.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[tool.poetry]
2+
name = "hypothesis-property-based-testing"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["James Bristow <jbris@users.noreply.github.com>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.10"
10+
hypothesis = "^6.98.15"
11+
pytest = "^8.0.2"
12+
13+
14+
[build-system]
15+
requires = ["poetry-core"]
16+
build-backend = "poetry.core.masonry.api"

tests/test_prop_based.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from hypothesis import example, given, strategies as st
2+
from hypothesis.strategies import text
3+
4+
def encode(input_string):
5+
if not input_string:
6+
return []
7+
8+
count = 1
9+
prev = ""
10+
lst = []
11+
12+
for character in input_string:
13+
if character != prev:
14+
if prev:
15+
entry = (prev, count)
16+
lst.append(entry)
17+
count = 1 # Missing reset operation
18+
prev = character
19+
else:
20+
count += 1
21+
entry = (character, count)
22+
lst.append(entry)
23+
return lst
24+
25+
26+
def decode(lst):
27+
q = ""
28+
for character, count in lst:
29+
q += character * count
30+
return q
31+
32+
33+
@given(st.integers(), st.integers())
34+
def test_ints_are_commutative(x, y):
35+
assert x + y == y + x
36+
37+
38+
@given(x=st.integers(), y=st.integers())
39+
def test_ints_cancel(x, y):
40+
assert (x + y) - y == x
41+
42+
43+
@given(st.lists(st.integers()))
44+
def test_reversing_twice_gives_same_list(xs):
45+
# This will generate lists of arbitrary length (usually between 0 and
46+
# 100 elements) whose elements are integers.
47+
ys = list(xs)
48+
ys.reverse()
49+
ys.reverse()
50+
assert xs == ys
51+
52+
53+
@given(st.tuples(st.booleans(), st.text()))
54+
def test_look_tuples_work_too(t):
55+
# A tuple is generated as the one you provided, with the corresponding
56+
# types in those positions.
57+
assert len(t) == 2
58+
assert isinstance(t[0], bool)
59+
assert isinstance(t[1], str)
60+
61+
@given(s=st.text())
62+
@example(s="")
63+
def test_decode_inverts_encode(s):
64+
assert decode(encode(s)) == s

0 commit comments

Comments
 (0)