Test expression contexts in which dict key completion occurs
(self)
| 1392 | self.assertIn("0b10110", matches) |
| 1393 | |
| 1394 | def test_dict_key_completion_contexts(self): |
| 1395 | """Test expression contexts in which dict key completion occurs""" |
| 1396 | ip = get_ipython() |
| 1397 | complete = ip.Completer.complete |
| 1398 | d = {"abc": None} |
| 1399 | ip.user_ns["d"] = d |
| 1400 | |
| 1401 | class C: |
| 1402 | data = d |
| 1403 | |
| 1404 | ip.user_ns["C"] = C |
| 1405 | ip.user_ns["get"] = lambda: d |
| 1406 | ip.user_ns["nested"] = {"x": d} |
| 1407 | |
| 1408 | def assert_no_completion(**kwargs): |
| 1409 | _, matches = complete(**kwargs) |
| 1410 | self.assertNotIn("abc", matches) |
| 1411 | self.assertNotIn("abc'", matches) |
| 1412 | self.assertNotIn("abc']", matches) |
| 1413 | self.assertNotIn("'abc'", matches) |
| 1414 | self.assertNotIn("'abc']", matches) |
| 1415 | |
| 1416 | def assert_completion(**kwargs): |
| 1417 | _, matches = complete(**kwargs) |
| 1418 | self.assertIn("'abc'", matches) |
| 1419 | self.assertNotIn("'abc']", matches) |
| 1420 | |
| 1421 | # no completion after string closed, even if reopened |
| 1422 | assert_no_completion(line_buffer="d['a'") |
| 1423 | assert_no_completion(line_buffer='d["a"') |
| 1424 | assert_no_completion(line_buffer="d['a' + ") |
| 1425 | assert_no_completion(line_buffer="d['a' + '") |
| 1426 | |
| 1427 | # completion in non-trivial expressions |
| 1428 | assert_completion(line_buffer="+ d[") |
| 1429 | assert_completion(line_buffer="(d[") |
| 1430 | assert_completion(line_buffer="C.data[") |
| 1431 | |
| 1432 | # nested dict completion |
| 1433 | assert_completion(line_buffer="nested['x'][") |
| 1434 | |
| 1435 | with evaluation_policy("minimal"): |
| 1436 | with pytest.raises(AssertionError): |
| 1437 | assert_completion(line_buffer="nested['x'][") |
| 1438 | |
| 1439 | # greedy flag |
| 1440 | def assert_completion(**kwargs): |
| 1441 | _, matches = complete(**kwargs) |
| 1442 | self.assertIn("get()['abc']", matches) |
| 1443 | |
| 1444 | assert_no_completion(line_buffer="get()[") |
| 1445 | with greedy_completion(): |
| 1446 | assert_completion(line_buffer="get()[") |
| 1447 | assert_completion(line_buffer="get()['") |
| 1448 | assert_completion(line_buffer="get()['a") |
| 1449 | assert_completion(line_buffer="get()['ab") |
| 1450 | assert_completion(line_buffer="get()['abc") |
| 1451 |
nothing calls this directly
no test coverage detected