-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from plasma-umass/coverup-tests
Added.
- Loading branch information
Showing
36 changed files
with
1,655 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# file src/chatdbg/pdb_util/paths.py:5-16 | ||
# lines [5, 7, 8, 9, 11, 12, 13, 14, 16] | ||
# branches ['8->9', '8->11', '11->12', '11->16', '12->11', '12->13', '13->11', '13->14'] | ||
|
||
import os | ||
import sys | ||
import pytest | ||
from chatdbg.pdb_util.paths import is_library_file | ||
|
||
def test_is_library_file_stdlib(monkeypatch): | ||
std_lib_path = os.path.dirname(os.__file__) | ||
file_path = os.path.join(std_lib_path, "os.py") | ||
assert is_library_file(file_path) == True | ||
|
||
def test_is_library_file_site_packages(monkeypatch): | ||
site_packages_path = None | ||
for path in sys.path: | ||
if "site-packages" in path: | ||
site_packages_path = path | ||
break | ||
if site_packages_path is None: | ||
pytest.skip("No site-packages directory found in sys.path") | ||
file_path = os.path.join(site_packages_path, "example_package", "example_module.py") | ||
monkeypatch.setattr(sys, 'path', sys.path + [site_packages_path]) | ||
assert is_library_file(file_path) == True | ||
|
||
def test_is_library_file_dist_packages(monkeypatch): | ||
dist_packages_path = None | ||
for path in sys.path: | ||
if "dist-packages" in path: | ||
dist_packages_path = path | ||
break | ||
if dist_packages_path is None: | ||
pytest.skip("No dist-packages directory found in sys.path") | ||
file_path = os.path.join(dist_packages_path, "example_package", "example_module.py") | ||
monkeypatch.setattr(sys, 'path', sys.path + [dist_packages_path]) | ||
assert is_library_file(file_path) == True | ||
|
||
def test_is_library_file_user_file(tmp_path): | ||
user_file = tmp_path / "user_file.py" | ||
user_file.touch() | ||
assert is_library_file(str(user_file)) == False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# file src/chatdbg/pdb_util/paths.py:19-33 | ||
# lines [19, 20, 21, 23, 24, 27, 29, 30, 31, 33] | ||
# branches ['30->31', '30->33'] | ||
|
||
import os | ||
import sys | ||
from unittest.mock import patch | ||
import pytest | ||
|
||
# Assuming the function `is_library_file` is defined somewhere in the module | ||
from chatdbg.pdb_util.paths import is_library_file, main | ||
|
||
@pytest.fixture | ||
def clean_sys_path(monkeypatch): | ||
# Backup original sys.path | ||
original_sys_path = sys.path.copy() | ||
yield | ||
# Restore original sys.path after the test | ||
monkeypatch.setattr(sys, 'path', original_sys_path) | ||
|
||
def test_main_with_library_file(clean_sys_path, monkeypatch, capsys): | ||
# Mock sys.path to contain known library paths | ||
monkeypatch.setattr(sys, 'path', ['/usr/lib/python3.8', '/usr/local/lib/python3.8/site-packages']) | ||
# Mock os.__file__ to a known location | ||
monkeypatch.setattr(os, '__file__', '/usr/lib/python3.8/os.py') | ||
# Mock is_library_file to return True | ||
monkeypatch.setattr('chatdbg.pdb_util.paths.is_library_file', lambda x: True) | ||
|
||
main() | ||
captured = capsys.readouterr() | ||
|
||
assert "*** user path: /usr/lib/python3.8 ***" in captured.out | ||
assert "/usr/local/lib/python3.8/site-packages" in captured.out | ||
assert "/path/to/your/file.py is likely a library file." in captured.out | ||
|
||
def test_main_with_user_written_file(clean_sys_path, monkeypatch, capsys): | ||
# Mock sys.path to contain no library paths | ||
monkeypatch.setattr(sys, 'path', ['/home/user/projects']) | ||
# Mock os.__file__ to a known location | ||
monkeypatch.setattr(os, '__file__', '/usr/lib/python3.8/os.py') | ||
# Mock is_library_file to return False | ||
monkeypatch.setattr('chatdbg.pdb_util.paths.is_library_file', lambda x: False) | ||
|
||
main() | ||
captured = capsys.readouterr() | ||
|
||
assert "*** user path: /home/user/projects ***" in captured.out | ||
assert "/path/to/your/file.py is likely a user-written file." in captured.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# file src/chatdbg/native_util/code.py:4-13 | ||
# lines [4, 5, 6, 7, 8, 9, 10, 11, 12, 13] | ||
# branches ['6->7', '6->8'] | ||
|
||
import pytest | ||
from unittest.mock import MagicMock | ||
|
||
# Assuming llm_utils is a module that needs to be mocked | ||
import llm_utils | ||
|
||
# Mocking llm_utils.read_lines and llm_utils.number_group_of_lines | ||
@pytest.fixture | ||
def mock_llm_utils(monkeypatch): | ||
mock_read_lines = MagicMock(return_value=(["line1", "line2", "line3"], 1)) | ||
mock_number_group_of_lines = MagicMock(return_value="numbered lines") | ||
monkeypatch.setattr(llm_utils, 'read_lines', mock_read_lines) | ||
monkeypatch.setattr(llm_utils, 'number_group_of_lines', mock_number_group_of_lines) | ||
return mock_read_lines, mock_number_group_of_lines | ||
|
||
def test_code_usage_message(): | ||
from chatdbg.native_util.code import code | ||
result = code("incorrect_usage") | ||
assert result == "usage: code <filename>:<lineno>" | ||
|
||
def test_code_file_not_found(mock_llm_utils): | ||
from chatdbg.native_util.code import code | ||
mock_read_lines, _ = mock_llm_utils | ||
mock_read_lines.side_effect = FileNotFoundError | ||
result = code("nonexistent.py:10") | ||
assert result == "file 'nonexistent.py' not found." | ||
|
||
def test_code_success(mock_llm_utils): | ||
from chatdbg.native_util.code import code | ||
mock_read_lines, mock_number_group_of_lines = mock_llm_utils | ||
result = code("existent.py:10") | ||
assert result == "numbered lines" | ||
mock_read_lines.assert_called_once_with("existent.py", 3, 13) | ||
mock_number_group_of_lines.assert_called_once_with(["line1", "line2", "line3"], 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# file src/chatdbg/native_util/stacks.py:51-62 | ||
# lines [51, 52, 53, 55, 56, 58, 59, 61, 62] | ||
# branches [] | ||
|
||
import pytest | ||
from chatdbg.native_util.stacks import _SkippedFramesEntry | ||
|
||
def test_skipped_frames_entry_count(): | ||
entry = _SkippedFramesEntry(5) | ||
assert entry.count() == 5 | ||
|
||
def test_skipped_frames_entry_str_singular(): | ||
entry = _SkippedFramesEntry(1) | ||
assert str(entry) == "[1 skipped frame...]" | ||
|
||
def test_skipped_frames_entry_str_plural(): | ||
entry = _SkippedFramesEntry(2) | ||
assert str(entry) == "[2 skipped frames...]" | ||
|
||
def test_skipped_frames_entry_repr(): | ||
entry = _SkippedFramesEntry(3) | ||
assert repr(entry) == "_SkippedFramesEntry(3)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# file src/chatdbg/util/trim.py:68-128 | ||
# lines [68, 69, 70, 71, 72, 85, 87, 88, 90, 91, 93, 97, 101, 102, 103, 104, 108, 109, 110, 112, 114, 117, 120, 124, 125, 126, 128] | ||
# branches ['90->91', '90->93', '101->102', '101->108', '103->101', '103->104', '108->109', '108->124', '110->112', '110->113', '113->117', '113->120'] | ||
|
||
import pytest | ||
from unittest.mock import MagicMock, patch | ||
from chatdbg.util.trim import trim_messages | ||
|
||
# Mocking the external dependencies and global variables | ||
litellm = MagicMock() | ||
litellm.model_cost = { | ||
"test_model": {"max_tokens": 100}, | ||
"another_model": {"max_tokens": 200}, | ||
} | ||
litellm.token_counter = lambda model, messages: sum(len(m['content']) for m in messages) | ||
chunkify = lambda messages, model: [([m], False) for m in messages] | ||
sum_messages = lambda messages, model: sum(len(m['content']) for m in messages) | ||
sum_kept_chunks = lambda chunks, model: sum(sum_messages(m, model) for m, kept in chunks if kept) | ||
sum_all_chunks = lambda chunks, model: sum(sum_messages(m, model) for m, kept in chunks) | ||
|
||
# Test to cover the branch where token count is less than max_tokens | ||
def test_trim_messages_no_trimming_required(): | ||
messages = [ | ||
{"role": "user", "content": "Hello"}, | ||
{"role": "system", "content": "System message"}, | ||
{"role": "user", "content": "How are you?"}, | ||
] | ||
model = "test_model" | ||
with patch('chatdbg.util.trim.litellm', litellm), \ | ||
patch('chatdbg.util.trim.chunkify', chunkify), \ | ||
patch('chatdbg.util.trim.sum_messages', sum_messages), \ | ||
patch('chatdbg.util.trim.sum_kept_chunks', sum_kept_chunks), \ | ||
patch('chatdbg.util.trim.sum_all_chunks', sum_all_chunks): | ||
trimmed_messages = trim_messages(messages, model) | ||
assert trimmed_messages == messages, "No messages should be trimmed if under max_tokens" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# file src/chatdbg/util/text.py:5-11 | ||
# lines [5, 7, 8, 9, 10, 11] | ||
# branches ['7->8', '7->9', '9->10', '9->11'] | ||
|
||
import pytest | ||
from chatdbg.util.text import make_arrow | ||
|
||
def test_make_arrow_with_pad_greater_than_two(): | ||
# Test with pad greater than 2 | ||
arrow = make_arrow(5) | ||
assert arrow == '---> ', "Arrow with pad greater than 2 should have dashes and a '> '" | ||
|
||
def test_make_arrow_with_pad_equal_two(): | ||
# Test with pad equal to 2 | ||
arrow = make_arrow(2) | ||
assert arrow == '> ', "Arrow with pad equal to 2 should be '> '" | ||
|
||
def test_make_arrow_with_pad_equal_one(): | ||
# Test with pad equal to 1 | ||
arrow = make_arrow(1) | ||
assert arrow == '>', "Arrow with pad equal to 1 should be '>'" | ||
|
||
def test_make_arrow_with_pad_less_than_one(): | ||
# Test with pad less than 1 | ||
arrow = make_arrow(0) | ||
assert arrow == '', "Arrow with pad less than 1 should be an empty string" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# file src/chatdbg/native_util/clangd_lsp_integration.py:9-16 | ||
# lines [9, 10, 11, 12, 14, 15, 16] | ||
# branches ['11->12', '11->14'] | ||
|
||
import json | ||
import pytest | ||
|
||
# Assuming the module name is `clangd_lsp_integration` and it contains the `_to_lsp_request` function. | ||
from chatdbg.native_util import clangd_lsp_integration | ||
|
||
def test_to_lsp_request_with_params(): | ||
request_id = 1 | ||
method = "testMethod" | ||
params = {"param1": "value1", "param2": "value2"} | ||
|
||
result = clangd_lsp_integration._to_lsp_request(request_id, method, params) | ||
expected_content = json.dumps({ | ||
"jsonrpc": "2.0", | ||
"id": request_id, | ||
"method": method, | ||
"params": params | ||
}) | ||
expected_header = f"Content-Length: {len(expected_content)}\r\n\r\n" | ||
expected_result = expected_header + expected_content | ||
|
||
assert result == expected_result | ||
|
||
def test_to_lsp_request_without_params(): | ||
request_id = 2 | ||
method = "testMethod" | ||
params = None | ||
|
||
result = clangd_lsp_integration._to_lsp_request(request_id, method, params) | ||
expected_content = json.dumps({ | ||
"jsonrpc": "2.0", | ||
"id": request_id, | ||
"method": method | ||
}) | ||
expected_header = f"Content-Length: {len(expected_content)}\r\n\r\n" | ||
expected_result = expected_header + expected_content | ||
|
||
assert result == expected_result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# file src/chatdbg/native_util/clangd_lsp_integration.py:20-27 | ||
# lines [20, 21, 22, 23, 25, 26, 27] | ||
# branches ['22->23', '22->25'] | ||
|
||
import json | ||
import pytest | ||
|
||
# Assuming the file structure is as follows: | ||
# chatdbg/ | ||
# └── native_util/ | ||
# └── clangd_lsp_integration.py | ||
|
||
from chatdbg.native_util.clangd_lsp_integration import _to_lsp_notification | ||
|
||
def test_to_lsp_notification_with_params(): | ||
method = "textDocument/didOpen" | ||
params = {"textDocument": {"uri": "file://path/to/file.py", "languageId": "python", "version": 1, "text": "print('Hello, world!')"}} | ||
|
||
result = _to_lsp_notification(method, params) | ||
|
||
content = json.dumps({"jsonrpc": "2.0", "method": method, "params": params}) | ||
expected_header = f"Content-Length: {len(content)}\r\n\r\n" | ||
expected_result = expected_header + content | ||
|
||
assert result == expected_result | ||
|
||
def test_to_lsp_notification_without_params(): | ||
method = "workspace/didChangeConfiguration" | ||
params = None | ||
|
||
result = _to_lsp_notification(method, params) | ||
|
||
content = json.dumps({"jsonrpc": "2.0", "method": method}) | ||
expected_header = f"Content-Length: {len(content)}\r\n\r\n" | ||
expected_result = expected_header + content | ||
|
||
assert result == expected_result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# file src/chatdbg/util/printer.py:9-20 | ||
# lines [9, 10, 11, 12, 13, 14, 15, 17, 20] | ||
# branches [] | ||
|
||
import os | ||
import pytest | ||
from chatdbg.util.printer import ChatDBGPrinter | ||
from unittest.mock import Mock | ||
|
||
# Mocking os.get_terminal_size to raise an OSError | ||
def test_get_terminal_size_failure(monkeypatch): | ||
def mock_get_terminal_size(): | ||
raise OSError("get_terminal_size failed") | ||
|
||
monkeypatch.setattr(os, "get_terminal_size", mock_get_terminal_size) | ||
|
||
out = Mock() | ||
debugger_prompt = "debug>" | ||
chat_prefix = "chat>" | ||
width = 80 | ||
|
||
printer = ChatDBGPrinter(out, debugger_prompt, chat_prefix, width) | ||
|
||
assert printer._width == width | ||
assert printer._debugger_prompt == debugger_prompt | ||
assert printer._chat_prefix == chat_prefix | ||
assert printer._at_start is True | ||
|
||
# Mocking os.get_terminal_size to return a smaller width than provided | ||
def test_get_terminal_size_smaller_width(monkeypatch): | ||
def mock_get_terminal_size(): | ||
columns = 50 | ||
return os.terminal_size((columns, 24)) | ||
|
||
monkeypatch.setattr(os, "get_terminal_size", mock_get_terminal_size) | ||
|
||
out = Mock() | ||
debugger_prompt = "debug>" | ||
chat_prefix = "chat>" | ||
width = 80 | ||
|
||
printer = ChatDBGPrinter(out, debugger_prompt, chat_prefix, width) | ||
|
||
expected_width = 50 - len(chat_prefix) | ||
assert printer._width == expected_width | ||
assert printer._debugger_prompt == debugger_prompt | ||
assert printer._chat_prefix == chat_prefix | ||
assert printer._at_start is True |
Oops, something went wrong.