mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Summary: expecttest and test_expecttest are the implementation and tests for this functionality. I wired it up to the --accept flag, but there's also a new environment variable EXPECTTEST_ACCEPT which may be more convenient to trigger. Haven't tested if this works in fbcode. There may be a few expect tests which will benefit from inline treatment, but I just did one to show it works. Signed-off-by: Edward Z. Yang <ezyang@fb.com> Pull Request resolved: https://github.com/pytorch/pytorch/pull/12825 Reviewed By: teng-li Differential Revision: D10448630 Pulled By: ezyang fbshipit-source-id: 3d339f82e2d00891309620a60e13039fa1ed8b46
106 lines
2.8 KiB
Python
106 lines
2.8 KiB
Python
import expecttest
|
|
|
|
import unittest
|
|
import string
|
|
import textwrap
|
|
import doctest
|
|
|
|
import hypothesis
|
|
from hypothesis.strategies import text, integers, composite, sampled_from, booleans
|
|
|
|
|
|
@composite
|
|
def text_lineno(draw):
|
|
t = draw(text("a\n"))
|
|
lineno = draw(integers(min_value=1, max_value=t.count("\n") + 1))
|
|
return (t, lineno)
|
|
|
|
|
|
class TestExpectTest(expecttest.TestCase):
|
|
@hypothesis.given(text_lineno())
|
|
def test_nth_line_ref(self, t_lineno):
|
|
t, lineno = t_lineno
|
|
hypothesis.event("lineno = {}".format(lineno))
|
|
|
|
def nth_line_ref(src, lineno):
|
|
xs = src.split("\n")[:lineno]
|
|
xs[-1] = ''
|
|
return len("\n".join(xs))
|
|
self.assertEqual(expecttest.nth_line(t, lineno), nth_line_ref(t, lineno))
|
|
|
|
@hypothesis.given(text(string.printable), booleans(), sampled_from(['"', "'"]))
|
|
def test_replace_string_literal_roundtrip(self, t, raw, quote):
|
|
if raw:
|
|
hypothesis.assume(expecttest.ok_for_raw_triple_quoted_string(t, quote=quote))
|
|
prog = """\
|
|
r = {r}{quote}placeholder{quote}
|
|
r2 = {r}{quote}placeholder2{quote}
|
|
r3 = {r}{quote}placeholder3{quote}
|
|
""".format(r='r' if raw else '', quote=quote * 3)
|
|
new_prog = expecttest.replace_string_literal(textwrap.dedent(prog), 2, t)[0]
|
|
ns = {}
|
|
exec(new_prog, ns)
|
|
msg = "program was:\n{}".format(new_prog)
|
|
self.assertEqual(ns['r'], 'placeholder', msg=msg) # noqa: F821
|
|
self.assertEqual(ns['r2'], expecttest.normalize_nl(t), msg=msg) # noqa: F821
|
|
self.assertEqual(ns['r3'], 'placeholder3', msg=msg) # noqa: F821
|
|
|
|
def test_sample(self):
|
|
prog = r"""
|
|
single_single('''0''')
|
|
single_multi('''1''')
|
|
multi_single('''\
|
|
2
|
|
''')
|
|
multi_multi_less('''\
|
|
3
|
|
4
|
|
''')
|
|
multi_multi_same('''\
|
|
5
|
|
''')
|
|
multi_multi_more('''\
|
|
6
|
|
''')
|
|
"""
|
|
# NB: These are the end of the statements, not beginning
|
|
# TODO: Test other permutations of these edits
|
|
edits = [(2, "a"),
|
|
(3, "b\n"),
|
|
(6, "c"),
|
|
(10, "d\n"),
|
|
(13, "e\n"),
|
|
(16, "f\ng\n")]
|
|
history = expecttest.EditHistory()
|
|
fn = 'not_a_real_file.py'
|
|
for lineno, actual in edits:
|
|
lineno = history.adjust_lineno(fn, lineno)
|
|
prog, delta = expecttest.replace_string_literal(prog, lineno, actual)
|
|
history.record_edit(fn, lineno, delta)
|
|
self.assertExpectedInline(prog, r"""
|
|
single_single('''a''')
|
|
single_multi('''\
|
|
b
|
|
''')
|
|
multi_single('''c''')
|
|
multi_multi_less('''\
|
|
d
|
|
''')
|
|
multi_multi_same('''\
|
|
e
|
|
''')
|
|
multi_multi_more('''\
|
|
f
|
|
g
|
|
''')
|
|
""")
|
|
|
|
|
|
def load_tests(loader, tests, ignore):
|
|
tests.addTests(doctest.DocTestSuite(expecttest))
|
|
return tests
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|