mirror of
https://github.com/tiennm99/serena.git
synced 2026-10-14 12:17:02 +00:00
Add tests relating to overloaded symbols
* Test whether retrieval returns all overloads * Test whether tools operating on well-defined symbols raise an error if the symbol is non-unique
This commit is contained in:
1 parent
bb79bf98d2
commit
a38311984a
2 files changed
+73
-1
No files matched your search
@@ -10,4 +10,12 @@ public class Model {
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getName(int maxChars) {
|
||||
if (name.length() <= maxChars) {
|
||||
return name;
|
||||
} else {
|
||||
return name.substring(0, maxChars) + "...";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import pytest
|
||||
from serena.agent import SerenaAgent
|
||||
from serena.config.serena_config import ProjectConfig, RegisteredProject, SerenaConfig
|
||||
from serena.project import Project
|
||||
from serena.tools import FindReferencingSymbolsTool, FindSymbolTool
|
||||
from serena.tools import FindReferencingSymbolsTool, FindSymbolTool, ReplaceSymbolBodyTool
|
||||
from solidlsp.ls_config import Language
|
||||
from test.conftest import get_repo_path, language_tests_enabled
|
||||
from test.solidlsp import clojure as clj
|
||||
@@ -64,6 +64,10 @@ def serena_agent(request: pytest.FixtureRequest, serena_config) -> Iterator[Sere
|
||||
project_name = f"test_repo_{language}"
|
||||
|
||||
agent = SerenaAgent(project=project_name, serena_config=serena_config)
|
||||
|
||||
# wait for agent to be ready
|
||||
agent.execute_task(lambda: None)
|
||||
|
||||
yield agent
|
||||
# explicitly delete to free resources
|
||||
agent.shutdown(timeout=5)
|
||||
@@ -288,3 +292,63 @@ class TestSerenaAgent:
|
||||
|
||||
symbols = json.loads(result)
|
||||
assert not symbols, f"Expected to find no symbols for {name_path}. Symbols found: {symbols}"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"serena_agent,name_path,num_expected",
|
||||
[
|
||||
pytest.param(
|
||||
Language.JAVA,
|
||||
"Model/getName",
|
||||
2,
|
||||
id="overloaded_java_method",
|
||||
marks=pytest.mark.java,
|
||||
),
|
||||
],
|
||||
indirect=["serena_agent"],
|
||||
)
|
||||
def test_find_symbol_overloaded_function(self, serena_agent: SerenaAgent, name_path: str, num_expected: int):
|
||||
"""
|
||||
Tests whether the FindSymbolTool can find all overloads of a function/method
|
||||
(provided that the overload id remains unspecified in the name path)
|
||||
"""
|
||||
agent = serena_agent
|
||||
|
||||
find_symbol_tool = agent.get_tool(FindSymbolTool)
|
||||
result = find_symbol_tool.apply_ex(
|
||||
name_path_pattern=name_path,
|
||||
depth=0,
|
||||
substring_matching=False,
|
||||
)
|
||||
|
||||
symbols = json.loads(result)
|
||||
assert (
|
||||
len(symbols) == num_expected
|
||||
), f"Expected to find {num_expected} symbols for overloaded function {name_path}. Symbols found: {symbols}"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"serena_agent,name_path,relative_path",
|
||||
[
|
||||
pytest.param(
|
||||
Language.JAVA,
|
||||
"Model/getName",
|
||||
os.path.join("src", "main", "java", "test_repo", "Model.java"),
|
||||
id="overloaded_java_method",
|
||||
marks=pytest.mark.java,
|
||||
),
|
||||
],
|
||||
indirect=["serena_agent"],
|
||||
)
|
||||
def test_non_unique_symbol_reference_error(self, serena_agent: SerenaAgent, name_path: str, relative_path: str):
|
||||
"""
|
||||
Tests whether the tools operating on a well-defined symbol raises an error when the symbol reference is non-unique.
|
||||
We exemplarily test a retrieval tool (FindReferencingSymbolsTool) and an editing tool (ReplaceSymbolBodyTool).
|
||||
"""
|
||||
match_text = "multiple"
|
||||
|
||||
find_refs_tool = serena_agent.get_tool(FindReferencingSymbolsTool)
|
||||
with pytest.raises(ValueError, match=match_text):
|
||||
find_refs_tool.apply(name_path=name_path, relative_path=relative_path)
|
||||
|
||||
replace_symbol_body_tool = serena_agent.get_tool(ReplaceSymbolBodyTool)
|
||||
with pytest.raises(ValueError, match=match_text):
|
||||
replace_symbol_body_tool.apply(name_path=name_path, relative_path=relative_path, body="")
|
||||
Reference in new issue
Block a user