Skip to content

Commit

Permalink
docs: use tabbed code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Oct 15, 2023
1 parent 2e86c2e commit 77402e5
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 192 deletions.
121 changes: 100 additions & 21 deletions docs/cmp_snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,119 @@

A snapshot can be compared against any value with `<=` or `>=`.
This can be used to create a upper/lower bound for some result.
The snapshot value can be trimmed to the lowest/largest valid value.

Example:

<!-- inline-snapshot: update this -->
```python
def some_algo():
...
result = 42
iterations = 2
...
return result, iterations
=== "original code"
<!-- inline-snapshot: outcome-passed=1 outcome-errors=1 -->
```python
def gcd(x, y):
iterations = 0
if x > y:
small = y
else:
small = x
for i in range(1, small + 1):
iterations += 1
if (x % i == 0) and (y % i == 0):
gcd = i

return gcd, iterations

def test_something():
result, iterations = some_algo()

assert result == snapshot(42)
assert iterations <= snapshot(2)
```
def test_gcd():
result, iterations = gcd(12, 18)

assert result == snapshot()
assert iterations <= snapshot()
```

=== "--inline-snapshot=create"
<!-- inline-snapshot: create -->
```python
def gcd(x, y):
iterations = 0
if x > y:
small = y
else:
small = x
for i in range(1, small + 1):
iterations += 1
if (x % i == 0) and (y % i == 0):
gcd = i

return gcd, iterations


def test_gcd():
result, iterations = gcd(12, 18)

assert result == snapshot(6)
assert iterations <= snapshot(12)
```

=== "optimized code "
<!-- inline-snapshot: outcome-passed=1 -->
```python
def gcd(x, y):
# use Euclidean Algorithm
iterations = 0
while y:
iterations += 1
x, y = y, x % y
return abs(x), iterations


def test_gcd():
result, iterations = gcd(12, 18)

assert result == snapshot(6)
assert iterations <= snapshot(12)
```

=== "--inline-snapshot=trim"
<!-- inline-snapshot: trim -->
```python
def gcd(x, y):
# use Euclidean Algorithm
iterations = 0
while y:
iterations += 1
x, y = y, x % y
return abs(x), iterations


def test_gcd():
result, iterations = gcd(12, 18)

assert result == snapshot(6)
assert iterations <= snapshot(3)
```

!!! warning
This should not be used to check for any flaky values like the runtime of some code, because it will randomly break your tests.

The same snapshot value can also be used in multiple assertions.
The snapshot value in the following case would be `6`.

<!-- inline-snapshot: outcome-errors=1 outcome-passed=1 -->
```python
def test_something():
value = snapshot()
=== "original code"
<!-- inline-snapshot: outcome-errors=1 outcome-passed=1 -->
```python
def test_something():
value = snapshot()

assert 5 <= value
assert 6 <= value
```
=== "--inline-snapshot=create"
<!-- inline-snapshot: create -->
```python
def test_something():
value = snapshot(6)

assert 5 <= value
assert 6 <= value
```
assert 5 <= value
assert 6 <= value
```

## pytest options

Expand Down
18 changes: 13 additions & 5 deletions docs/eq_snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ The value gets recorded if the snapshot is undefined (`snapshot()`)

Example:

<!-- inline-snapshot: update this -->
```python
def test_something():
assert 2 + 2 == snapshot(4)
```
=== "original code"
<!-- inline-snapshot: outcome-passed=1 outcome-errors=1 -->
```python
def test_something():
assert 2 + 2 == snapshot()
```

=== "--inline-snapshot=create"
<!-- inline-snapshot: create -->
```python
def test_something():
assert 2 + 2 == snapshot(4)
```

## pytest options

Expand Down
36 changes: 19 additions & 17 deletions docs/getitem_snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ This sub-snapshots can be used like a normal snapshot.

Example:

```python
def test_something():
s = snapshot()

assert s["a"] == 4
assert s["b"] == 5
```

generates:

```python
def test_something():
s = snapshot({"a": 4, "b": 5})

assert s["a"] == 4
assert s["b"] == 5
```
=== "original code"
<!-- inline-snapshot: outcome-errors=1 outcome-passed=1 -->
```python
def test_something():
s = snapshot()

assert s["a"] == 4
assert s["b"] == 5
```

=== "--inline-snapshot=create"
<!-- inline-snapshot: create -->
```python
def test_something():
s = snapshot({"a": 4, "b": 5})

assert s["a"] == 4
assert s["b"] == 5
```

`s[key]` can be used with every normal snapshot operation including `s[key1][key2]`.

Expand Down
52 changes: 27 additions & 25 deletions docs/in_snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@ It is possible to check if an value is in a snapshot. The value of the generated

Example:

<!-- inline-snapshot: outcome-errors=1 outcome-passed=1 -->
```python
def test_something():
s = snapshot()

assert 5 in s
assert 5 in s
assert 8 in s

for v in ["a", "b"]:
assert v in s
```

<!-- inline-snapshot: create -->
```python
def test_something():
s = snapshot([5, 8, "a", "b"])

assert 5 in s
assert 5 in s
assert 8 in s

for v in ["a", "b"]:
assert v in s
```
=== "original code"
<!-- inline-snapshot: outcome-errors=1 outcome-passed=1 -->
```python
def test_something():
s = snapshot()

assert 5 in s
assert 5 in s
assert 8 in s

for v in ["a", "b"]:
assert v in s
```

=== "--inline-snapshot=create"
<!-- inline-snapshot: create -->
```python
def test_something():
s = snapshot([5, 8, "a", "b"])

assert 5 in s
assert 5 in s
assert 8 in s

for v in ["a", "b"]:
assert v in s
```

## pytest options

Expand Down
Loading

0 comments on commit 77402e5

Please sign in to comment.