Skip to content

Commit 4b20c8c

Browse files
committed
Add test for simple class method
1 parent 7c67319 commit 4b20c8c

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

utbot-python/src/test/java/org/utbot/python/framework/external/PythonUtBotJavaApiTest.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void setUp() {
2828
public void tearDown() {
2929
Cleaner.INSTANCE.doCleaning();
3030
}
31-
private File loadExampleCode(String name) {
31+
private File loadResource(String name) {
3232
URL resource = getClass().getClassLoader().getResource(name);
3333
if (resource == null) {
3434
throw new IllegalArgumentException("file not found!");
@@ -43,7 +43,7 @@ private File loadExampleCode(String name) {
4343

4444
@Test
4545
public void testSimpleFunction() {
46-
File fileWithCode = loadExampleCode("example_code/arithmetic.py");
46+
File fileWithCode = loadResource("example_code/arithmetic.py");
4747
String pythonRunRoot = fileWithCode.getParentFile().getAbsolutePath();
4848
String moduleFilename = fileWithCode.getAbsolutePath();
4949
PythonObjectName testMethodName = new PythonObjectName("arithmetic", "calculate_function_value");
@@ -66,7 +66,7 @@ public void testSimpleFunction() {
6666

6767
@Test
6868
public void testSimpleFunctionTestCase() {
69-
File fileWithCode = loadExampleCode("example_code/arithmetic.py");
69+
File fileWithCode = loadResource("example_code/arithmetic.py");
7070
String pythonRunRoot = fileWithCode.getParentFile().getAbsolutePath();
7171
String moduleFilename = fileWithCode.getAbsolutePath();
7272
PythonObjectName testMethodName = new PythonObjectName("arithmetic", "calculate_function_value");
@@ -86,4 +86,29 @@ public void testSimpleFunctionTestCase() {
8686
Assertions.assertTrue(testCase.size() > 0);
8787
Assertions.assertTrue(testCase.get(0).component2().size() > 0);
8888
}
89+
90+
@Test
91+
public void testSimpleClassMethod() {
92+
File fileWithCode = loadResource("example_code/inner_dir/inner_module.py");
93+
File projectRoot = loadResource("example_code/");
94+
String pythonRunRoot = projectRoot.getAbsolutePath();
95+
String moduleFilename = fileWithCode.getAbsolutePath();
96+
PythonObjectName containingClassName = new PythonObjectName("inner_dir.inner_module", "InnerClass");
97+
PythonObjectName testMethodName = new PythonObjectName("inner_dir.inner_module", "f");
98+
PythonTestMethodInfo methodInfo = new PythonTestMethodInfo(testMethodName, moduleFilename, containingClassName);
99+
ArrayList<PythonTestMethodInfo> testMethods = new ArrayList<>(1);
100+
testMethods.add(methodInfo);
101+
ArrayList<String> directoriesForSysPath = new ArrayList<>();
102+
directoriesForSysPath.add(pythonRunRoot);
103+
String testCode = PythonUtBotJavaApi.generate(
104+
testMethods,
105+
pythonPath,
106+
pythonRunRoot,
107+
directoriesForSysPath,
108+
10_000,
109+
1_000,
110+
Unittest.INSTANCE
111+
);
112+
Assertions.assertTrue(testCode.length() > 0);
113+
}
89114
}

utbot-python/src/test/resources/example_code/__init__.py

Whitespace-only changes.

utbot-python/src/test/resources/example_code/inner_dir/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class InnerClass:
2+
x: int
3+
4+
def __init__(self, x: int):
5+
self.x = x
6+
7+
def f(self, y: int):
8+
return y**2 + self.x*y + 1

0 commit comments

Comments
 (0)