| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import attrs |
| 12 |
|
| 13 |
from hypothesis.vendor import pretty |
| 14 |
|
| 15 |
|
| 16 |
class ReprDetector: |
| 17 |
def _repr_pretty_(self, p, cycle): |
| 18 |
"""Exercise the IPython callback interface.""" |
| 19 |
p.text("GOOD") |
| 20 |
|
| 21 |
def __repr__(self): |
| 22 |
return "BAD" |
| 23 |
|
| 24 |
|
| 25 |
@attrs.define |
| 26 |
class SomeAttrsClass: |
| 27 |
x: ReprDetector |
| 28 |
|
| 29 |
|
| 30 |
def test_pretty_prints_attrs_classes(): |
| 31 |
assert pretty.pretty(SomeAttrsClass(ReprDetector())) == "SomeAttrsClass(x=GOOD)" |
| 32 |
|
| 33 |
|
| 34 |
@attrs.define |
| 35 |
class SomeAttrsClassWithCustomPretty: |
| 36 |
def _repr_pretty_(self, p, cycle): |
| 37 |
"""Exercise the IPython callback interface.""" |
| 38 |
p.text("I am a banana") |
| 39 |
|
| 40 |
|
| 41 |
def test_custom_pretty_print_method_overrides_field_printing(): |
| 42 |
assert pretty.pretty(SomeAttrsClassWithCustomPretty()) == "I am a banana" |
| 43 |
|
| 44 |
|
| 45 |
@attrs.define |
| 46 |
class SomeAttrsClassWithLotsOfFields: |
| 47 |
a: int |
| 48 |
b: int |
| 49 |
c: int |
| 50 |
d: int |
| 51 |
e: int |
| 52 |
f: int |
| 53 |
g: int |
| 54 |
h: int |
| 55 |
i: int |
| 56 |
j: int |
| 57 |
k: int |
| 58 |
l: int |
| 59 |
m: int |
| 60 |
n: int |
| 61 |
o: int |
| 62 |
p: int |
| 63 |
q: int |
| 64 |
r: int |
| 65 |
s: int |
| 66 |
|
| 67 |
|
| 68 |
def test_will_line_break_between_fields(): |
| 69 |
obj = SomeAttrsClassWithLotsOfFields( |
| 70 |
**{ |
| 71 |
at.name: 12345678900000000000000001 |
| 72 |
for at in SomeAttrsClassWithLotsOfFields.__attrs_attrs__ |
| 73 |
} |
| 74 |
) |
| 75 |
assert "\n" in pretty.pretty(obj) |
| 76 |
|
| 77 |
|
| 78 |
@attrs.define |
| 79 |
class SomeDataClassWithNoFields: ... |
| 80 |
|
| 81 |
|
| 82 |
def test_prints_empty_dataclass_correctly(): |
| 83 |
assert pretty.pretty(SomeDataClassWithNoFields()) == "SomeDataClassWithNoFields()" |
| 84 |
|
| 85 |
|
| 86 |
@attrs.define |
| 87 |
class AttrsClassWithNoInitField: |
| 88 |
x: int |
| 89 |
y: int = attrs.field(init=False) |
| 90 |
|
| 91 |
|
| 92 |
def test_does_not_include_no_init_fields_in_attrs_printing(): |
| 93 |
record = AttrsClassWithNoInitField(x=1) |
| 94 |
assert pretty.pretty(record) == "AttrsClassWithNoInitField(x=1)" |
| 95 |
record.y = 1 |
| 96 |
assert pretty.pretty(record) == "AttrsClassWithNoInitField(x=1)" |
| 97 |
|
| 98 |
|
| 99 |
class Namespace: |
| 100 |
@attrs.define |
| 101 |
class A: |
| 102 |
x: int |
| 103 |
|
| 104 |
|
| 105 |
def test_includes_namespace_classes_in_pretty(): |
| 106 |
obj = Namespace.A(x=1) |
| 107 |
assert pretty.pretty(obj) == "Namespace.A(x=1)" |