mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-25 06:44:59 +00:00
test: add unit tests for AutoCompleter.get_command_completions method
This commit is contained in:
parent
2139de76fb
commit
d5b8a15410
1 changed files with 53 additions and 1 deletions
|
@ -3,7 +3,7 @@ import unittest
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from prompt_toolkit.completion import CompleteEvent
|
from prompt_toolkit.completion import CompleteEvent, Completion
|
||||||
from prompt_toolkit.document import Document
|
from prompt_toolkit.document import Document
|
||||||
|
|
||||||
from aider.dump import dump # noqa: F401
|
from aider.dump import dump # noqa: F401
|
||||||
|
@ -17,6 +17,58 @@ class TestInputOutput(unittest.TestCase):
|
||||||
io = InputOutput()
|
io = InputOutput()
|
||||||
self.assertFalse(io.pretty)
|
self.assertFalse(io.pretty)
|
||||||
|
|
||||||
|
def test_autocompleter_get_command_completions(self):
|
||||||
|
# Step 3: Mock the commands object
|
||||||
|
commands = MagicMock()
|
||||||
|
commands.get_commands.return_value = ['/help', '/add', '/drop']
|
||||||
|
commands.matching_commands.side_effect = lambda inp: (
|
||||||
|
[cmd for cmd in commands.get_commands() if cmd.startswith(inp.strip().split()[0])],
|
||||||
|
inp.strip().split()[0],
|
||||||
|
' '.join(inp.strip().split()[1:]),
|
||||||
|
)
|
||||||
|
commands.get_raw_completions.return_value = None
|
||||||
|
commands.get_completions.side_effect = lambda cmd: ['file1.txt', 'file2.txt'] if cmd == '/add' else None
|
||||||
|
|
||||||
|
# Step 4: Create an instance of AutoCompleter
|
||||||
|
root = ''
|
||||||
|
rel_fnames = []
|
||||||
|
addable_rel_fnames = []
|
||||||
|
autocompleter = AutoCompleter(
|
||||||
|
root=root,
|
||||||
|
rel_fnames=rel_fnames,
|
||||||
|
addable_rel_fnames=addable_rel_fnames,
|
||||||
|
commands=commands,
|
||||||
|
encoding='utf-8',
|
||||||
|
)
|
||||||
|
|
||||||
|
# Step 5: Set up test cases
|
||||||
|
test_cases = [
|
||||||
|
# Input text, Expected completion texts
|
||||||
|
('/', ['/help', '/add', '/drop']),
|
||||||
|
('/a', ['/add']),
|
||||||
|
('/add ', ['file1.txt', 'file2.txt']),
|
||||||
|
]
|
||||||
|
|
||||||
|
# Step 6: Iterate through test cases
|
||||||
|
for text, expected_completions in test_cases:
|
||||||
|
document = Document(text=text)
|
||||||
|
complete_event = CompleteEvent()
|
||||||
|
words = text.strip().split()
|
||||||
|
|
||||||
|
# Call get_command_completions
|
||||||
|
completions = list(autocompleter.get_command_completions(
|
||||||
|
document,
|
||||||
|
complete_event,
|
||||||
|
text,
|
||||||
|
words,
|
||||||
|
))
|
||||||
|
|
||||||
|
# Extract completion texts
|
||||||
|
completion_texts = [comp.text for comp in completions]
|
||||||
|
|
||||||
|
# Assert that the completions match expected results
|
||||||
|
self.assertEqual(completion_texts, expected_completions)
|
||||||
|
|
||||||
def test_autocompleter_with_non_existent_file(self):
|
def test_autocompleter_with_non_existent_file(self):
|
||||||
root = ""
|
root = ""
|
||||||
rel_fnames = ["non_existent_file.txt"]
|
rel_fnames = ["non_existent_file.txt"]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue