|
12 | 12 |
|
13 | 13 |
|
14 | 14 | def frame(funcname, *, filename="", lineno=None): |
15 | | - location = ( |
16 | | - None if lineno is None else SimpleNamespace(lineno=lineno) |
17 | | - ) |
| 15 | + location = SimpleNamespace(lineno=lineno) if lineno is not None else None |
18 | 16 | return SimpleNamespace( |
19 | 17 | funcname=funcname, |
20 | 18 | filename=filename, |
21 | 19 | location=location, |
22 | 20 | ) |
23 | 21 |
|
24 | 22 |
|
| 23 | +def frames(*names): |
| 24 | + return [frame(name) for name in names] |
| 25 | + |
| 26 | + |
25 | 27 | class ClassifierTests(unittest.TestCase): |
26 | | - def test_recognized_impossible_patterns(self): |
27 | | - lines = snippets.FLAT_ALTERNATING_LINES |
| 28 | + def test_classifiers(self): |
| 29 | + flat_lines = snippets.FLAT_ALTERNATING_LINES |
| 30 | + short_line = min(snippets.SHARED_LEAF_SHORT_LINES) |
28 | 31 | cases = [ |
29 | 32 | ( |
30 | 33 | snippets.classify_flat, |
31 | 34 | [ |
32 | | - frame("hot_a", lineno=lines["hot_a"]), |
33 | | - frame("hot_b", lineno=lines["hot_b"]), |
| 35 | + frame("leaf_a", lineno=flat_lines["leaf_a"]), |
| 36 | + frame("hot_a", lineno=flat_lines["hot_a"]), |
| 37 | + ], |
| 38 | + [ |
| 39 | + frame("hot_a", lineno=flat_lines["hot_a"]), |
| 40 | + frame("hot_b", lineno=flat_lines["hot_b"]), |
34 | 41 | ], |
35 | 42 | ), |
36 | | - (snippets.classify_nested, [frame("a_leaf")]), |
37 | | - (snippets.classify_shared, [frame("shared_leaf")]), |
38 | 43 | ( |
39 | | - snippets.classify_gen, |
40 | | - [frame("agen"), frame("drv_b")], |
| 44 | + snippets.classify_nested, |
| 45 | + frames("burn_a", "a_leaf", "a_parent"), |
| 46 | + frames("a_parent", "a_leaf", "burn_a"), |
| 47 | + ), |
| 48 | + ( |
| 49 | + snippets.classify_shared, |
| 50 | + [ |
| 51 | + frame("shared_leaf", lineno=short_line), |
| 52 | + frame("a_wrapper"), |
| 53 | + ], |
| 54 | + [ |
| 55 | + frame("shared_leaf", lineno=short_line), |
| 56 | + frame("b_wrapper"), |
| 57 | + ], |
41 | 58 | ), |
42 | 59 | ( |
43 | 60 | snippets.classify_recursion, |
44 | | - [frame("a"), frame("b")], |
| 61 | + frames("a", "a"), |
| 62 | + frames("a", "b"), |
45 | 63 | ), |
46 | 64 | ( |
47 | 65 | snippets.classify_async_running_task, |
48 | | - (None, "hot", None, [frame("leaf_rare")]), |
| 66 | + (None, "hot", None, frames("leaf_hot")), |
| 67 | + (None, "hot", None, frames("leaf_rare")), |
49 | 68 | ), |
50 | 69 | ( |
51 | 70 | snippets.classify_code_object_reuse, |
| 71 | + [frame("func_a", filename="A_file.py")], |
52 | 72 | [frame("func_a", filename="B_file.py")], |
53 | 73 | ), |
54 | 74 | ( |
55 | 75 | snippets.classify_oversized_chunk, |
| 76 | + [frame("big_a", filename="a.py")], |
56 | 77 | [frame("big_b", filename="a.py")], |
57 | 78 | ), |
58 | 79 | ] |
59 | | - for classifier, sample in cases: |
| 80 | + for classifier, unrecognized, recognized in cases: |
60 | 81 | with self.subTest(classifier=classifier.__name__): |
61 | | - self.assertTrue(classifier(sample)) |
| 82 | + self.assertFalse(classifier(unrecognized)) |
| 83 | + self.assertTrue(classifier(recognized)) |
62 | 84 |
|
63 | | - def test_unrecognized_gen_patterns(self): |
64 | | - for names in [("agen",), ("agen", "agen", "drv_a")]: |
| 85 | + def test_classify_gen(self): |
| 86 | + cases = [ |
| 87 | + (("agen", "drv_a"), False), |
| 88 | + (("agen", "drv_b"), True), |
| 89 | + (("bgen", "drv_a"), True), |
| 90 | + (("agen",), False), |
| 91 | + (("agen", "agen", "drv_a"), False), |
| 92 | + ] |
| 93 | + for names, impossible in cases: |
65 | 94 | with self.subTest(names=names): |
66 | | - self.assertFalse( |
67 | | - snippets.classify_gen([frame(name) for name in names]) |
| 95 | + self.assertEqual( |
| 96 | + snippets.classify_gen(frames(*names)), impossible |
68 | 97 | ) |
69 | 98 |
|
70 | 99 |
|
|
0 commit comments