Caffe2 - Python API
A deep learning, cross platform ML framework
test_expecttest.py
1 import expecttest
2 
3 import unittest
4 import string
5 import textwrap
6 import doctest
7 
8 import hypothesis
9 from hypothesis.strategies import text, integers, composite, sampled_from, booleans
10 
11 
12 @composite
13 def text_lineno(draw):
14  t = draw(text("a\n"))
15  lineno = draw(integers(min_value=1, max_value=t.count("\n") + 1))
16  return (t, lineno)
17 
18 
20  @hypothesis.given(text_lineno())
21  def test_nth_line_ref(self, t_lineno):
22  t, lineno = t_lineno
23  hypothesis.event("lineno = {}".format(lineno))
24 
25  def nth_line_ref(src, lineno):
26  xs = src.split("\n")[:lineno]
27  xs[-1] = ''
28  return len("\n".join(xs))
29  self.assertEqual(expecttest.nth_line(t, lineno), nth_line_ref(t, lineno))
30 
31  @hypothesis.given(text(string.printable), booleans(), sampled_from(['"', "'"]))
32  def test_replace_string_literal_roundtrip(self, t, raw, quote):
33  if raw:
34  hypothesis.assume(expecttest.ok_for_raw_triple_quoted_string(t, quote=quote))
35  prog = """\
36  r = {r}{quote}placeholder{quote}
37  r2 = {r}{quote}placeholder2{quote}
38  r3 = {r}{quote}placeholder3{quote}
39  """.format(r='r' if raw else '', quote=quote * 3)
40  new_prog = expecttest.replace_string_literal(textwrap.dedent(prog), 2, t)[0]
41  ns = {}
42  exec(new_prog, ns)
43  msg = "program was:\n{}".format(new_prog)
44  self.assertEqual(ns['r'], 'placeholder', msg=msg) # noqa: F821
45  self.assertEqual(ns['r2'], expecttest.normalize_nl(t), msg=msg) # noqa: F821
46  self.assertEqual(ns['r3'], 'placeholder3', msg=msg) # noqa: F821
47 
48  def test_sample(self):
49  prog = r"""
50 single_single('''0''')
51 single_multi('''1''')
52 multi_single('''\
53 2
54 ''')
55 multi_multi_less('''\
56 3
57 4
58 ''')
59 multi_multi_same('''\
60 5
61 ''')
62 multi_multi_more('''\
63 6
64 ''')
65 """
66  # NB: These are the end of the statements, not beginning
67  # TODO: Test other permutations of these edits
68  edits = [(2, "a"),
69  (3, "b\n"),
70  (6, "c"),
71  (10, "d\n"),
72  (13, "e\n"),
73  (16, "f\ng\n")]
74  history = expecttest.EditHistory()
75  fn = 'not_a_real_file.py'
76  for lineno, actual in edits:
77  lineno = history.adjust_lineno(fn, lineno)
78  prog, delta = expecttest.replace_string_literal(prog, lineno, actual)
79  history.record_edit(fn, lineno, delta)
80  self.assertExpectedInline(prog, r"""
81 single_single('''a''')
82 single_multi('''\
83 b
84 ''')
85 multi_single('''c''')
86 multi_multi_less('''\
87 d
88 ''')
89 multi_multi_same('''\
90 e
91 ''')
92 multi_multi_more('''\
93 f
94 g
95 ''')
96 """)
97 
98 
99 def load_tests(loader, tests, ignore):
100  tests.addTests(doctest.DocTestSuite(expecttest))
101  return tests
102 
103 
104 if __name__ == '__main__':
105  unittest.main()
def replace_string_literal(src, lineno, new_string)
Definition: expecttest.py:99
def normalize_nl(t)
Definition: expecttest.py:42
def ok_for_raw_triple_quoted_string(s, quote)
Definition: expecttest.py:75
def assertExpectedInline(self, actual, expect, skip=0)
Definition: expecttest.py:163
def nth_line(src, lineno)
Definition: expecttest.py:11