|
1 | | -"""Tests for scripts in Tools/inspection.""" |
| 1 | +"""Tests for snippets in Tools/inspection.""" |
2 | 2 |
|
3 | 3 | import unittest |
4 | 4 | from types import SimpleNamespace |
|
11 | 11 | import snippets |
12 | 12 |
|
13 | 13 |
|
14 | | -def frames(*names): |
15 | | - return [SimpleNamespace(funcname=name) for name in names] |
| 14 | +def frame(funcname, *, filename="", lineno=None): |
| 15 | + return SimpleNamespace( |
| 16 | + funcname=funcname, |
| 17 | + filename=filename, |
| 18 | + location=SimpleNamespace(lineno=lineno), |
| 19 | + ) |
16 | 20 |
|
17 | 21 |
|
18 | 22 | class ClassifierTests(unittest.TestCase): |
19 | | - def test_classify_gen(self): |
| 23 | + def test_recognized_impossible_patterns(self): |
| 24 | + lines = snippets.FLAT_ALTERNATING_LINES |
20 | 25 | cases = [ |
21 | | - (("agen", "drv_a"), False), |
22 | | - (("agen", "drv_b"), True), |
23 | | - (("bgen", "drv_a"), True), |
24 | | - (("agen",), False), |
25 | | - (("agen", "agen", "drv_a"), False), |
| 26 | + ( |
| 27 | + snippets.classify_flat, |
| 28 | + [ |
| 29 | + frame("hot_a", lineno=lines["hot_a"]), |
| 30 | + frame("hot_b", lineno=lines["hot_b"]), |
| 31 | + ], |
| 32 | + ), |
| 33 | + (snippets.classify_nested, [frame("a_leaf")]), |
| 34 | + (snippets.classify_shared, [frame("shared_leaf")]), |
| 35 | + ( |
| 36 | + snippets.classify_gen, |
| 37 | + [frame("agen"), frame("drv_b")], |
| 38 | + ), |
| 39 | + ( |
| 40 | + snippets.classify_recursion, |
| 41 | + [frame("a"), frame("b")], |
| 42 | + ), |
| 43 | + ( |
| 44 | + snippets.classify_async_running_task, |
| 45 | + (None, "hot", None, [frame("leaf_rare")]), |
| 46 | + ), |
| 47 | + ( |
| 48 | + snippets.classify_code_object_reuse, |
| 49 | + [frame("func_a", filename="B_file.py")], |
| 50 | + ), |
| 51 | + ( |
| 52 | + snippets.classify_oversized_chunk, |
| 53 | + [frame("big_b", filename="a.py")], |
| 54 | + ), |
26 | 55 | ] |
27 | | - for names, impossible in cases: |
| 56 | + for classifier, sample in cases: |
| 57 | + with self.subTest(classifier=classifier.__name__): |
| 58 | + self.assertTrue(classifier(sample)) |
| 59 | + |
| 60 | + def test_unrecognized_gen_patterns(self): |
| 61 | + for names in [("agen",), ("agen", "agen", "drv_a")]: |
28 | 62 | with self.subTest(names=names): |
29 | | - self.assertEqual( |
30 | | - snippets.classify_gen(frames(*names)), impossible |
| 63 | + self.assertFalse( |
| 64 | + snippets.classify_gen([frame(name) for name in names]) |
31 | 65 | ) |
32 | 66 |
|
33 | 67 |
|
|
0 commit comments