Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for Java and fixes to support Java #28

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ fun Array<Method>.findMethod(method: TestMethod, customErrorMessage: String? = n
it.name == method.name
}
}
val returnTypeJava = (method.returnTypeJava?.let { listOf(it) } ?: listOfNotNull(method.returnType.type, *method.returnType.possibleBounds.toTypedArray())).map { it.lowercase() }
val returnTypeJava = (listOf(method.returnTypeJava)).map { it.lowercase() }
nbirillo marked this conversation as resolved.
Show resolved Hide resolved
val filteredByType =
filteredByName.filterByCondition(customErrorMessage ?: "The method ${method.name} should have the return type ${method.returnType.getTypePrettyString()}") {
filteredByName.filterByCondition(customErrorMessage ?: "The method ${method.name} should have the return type ${method.returnType?.getTypePrettyString()}") {
it.returnType.name.getShortName().lowercase() in returnTypeJava
}
val filteredByArgumentsCount =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,36 @@ import kotlin.reflect.jvm.kotlinFunction
*/
data class TestMethod(
val name: String,
val returnType: TestKotlinType,
val returnTypeJava: String,
val returnType: TestKotlinType? = null,
val arguments: List<TestVariable> = emptyList(),
val returnTypeJava: String? = null,
val visibility: Visibility = Visibility.PUBLIC,
val hasGeneratedPartInName: Boolean = false,
) {
constructor(
name: String,
returnTypeJava: String,
arguments: List<TestVariable>,
visibility: Visibility
) : this(
name = name,
returnTypeJava = returnTypeJava,
returnType = null,
arguments = arguments,
visibility = visibility,
hasGeneratedPartInName = false
)

private fun getTypePrettyString() = returnType?.getTypePrettyString() ?: returnTypeJava

fun prettyString(withToDo: Boolean = true): String {
val args = arguments.joinToString(", ") { it.paramPrettyString() }
val body = if (withToDo) {
"TODO(\"Not implemented yet\")"
} else {
"// Some code"
}
return "${visibility.key} fun $name($args): ${returnType.getTypePrettyString()} = $body"
return "${visibility.key} fun $name($args): ${getTypePrettyString()} = $body"
}

private fun TestVariable.paramPrettyString() = "$name: $javaType"
Expand All @@ -48,6 +64,6 @@ data class TestMethod(
this.visibility.key,
"\"The visibility of the method $name must be ${this.visibility.key}\""
)
kotlinFunction.returnType.checkType(returnType, returnTypeJava ?: returnType.type, "the function $name")
kotlinFunction.returnType.checkType(returnType, returnTypeJava, "the function $name")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ data class TestVariable(
val isConst: Boolean = false,
// TODO: add nullability?
) {
constructor(
name: String,
javaType: String,
value: String?,
visibility: Visibility?,
isFinal: Boolean,
isInPrimaryConstructor: Boolean,
isStatic: Boolean,
isConst: Boolean
) : this(
name = name,
javaType = javaType,
value = value,
kotlinType = null,
visibility = visibility,
mutability = if (isFinal) VariableMutability.VAL else VariableMutability.VAR,
isInPrimaryConstructor = isInPrimaryConstructor,
isStatic = isStatic,
isConst = isConst
)

constructor(name: String, javaType: String) : this(name, javaType, null, null, null, null, false, false, false)

private fun getTypePrettyString() = kotlinType?.getTypePrettyString() ?: javaType

fun prettyString(): String {
Expand Down Expand Up @@ -69,10 +92,7 @@ data class TestVariable(
}
}
field.kotlinProperty?.returnType?.checkType(
kotlinType,
javaType,
"the field $name",
false
kotlinType, javaType, "the field $name", false
)
}
}
Expand All @@ -86,8 +106,7 @@ fun TestVariable.isVariableExist(fileContent: String): Boolean {
val defWithType = variableDefTemplateWithType()
if (!(baseDef in fileContent || defWithType in fileContent)) {
error(
"The code should contains a definition of the ${this.name} variable! " +
"Please, add <$baseDef> or <$defWithType> code in your solution."
"The code should contains a definition of the ${this.name} variable! " + "Please, add <$baseDef> or <$defWithType> code in your solution."
)
}
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.jetbrains.academy.test.system.core;

import org.jetbrains.academy.test.system.core.models.Visibility;
import org.jetbrains.academy.test.system.core.models.classes.ClassType;
import org.jetbrains.academy.test.system.core.models.classes.TestClass;
import org.jetbrains.academy.test.system.core.models.method.TestMethod;
import org.jetbrains.academy.test.system.core.models.variable.TestVariable;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.List;

import static java.util.Collections.emptyList;

public class TestJavaClass {
private static TestClass javaClassTestClass;

@BeforeAll
static void beforeAll() {
javaClassTestClass = new TestClass(
"JavaClass",
"org.jetbrains.academy.test.system.core.testData.java",
Visibility.PUBLIC,
ClassType.CLASS,
List.of(
new TestVariable(
"PUBLIC_CONSTANT",
"int",
"1",
Visibility.PUBLIC,
true,
false,
false,
true
),
new TestVariable(
"PRIVATE_CONSTANT",
"int",
"2",
Visibility.PRIVATE,
true,
false,
false,
true
),
new TestVariable(
"publicStaticVar",
"int",
"3",
Visibility.PUBLIC,
false,
false,
true,
false
),
new TestVariable(
"privateStaticVar",
"int",
"4",
Visibility.PRIVATE,
false,
false,
true,
false
),
new TestVariable(
"publicVar",
"String",
"publicVar",
Visibility.PUBLIC,
false,
false,
false,
false
),
new TestVariable(
"privateVar",
"String",
"privateVar",
Visibility.PRIVATE,
false,
false,
false,
false
),
new TestVariable(
"customClass",
"CustomJavaClass",
null,
Visibility.PUBLIC,
false,
false,
false,
false
),
new TestVariable(
"primaryConstructorVar",
"String",
null,
Visibility.PUBLIC,
false,
true,
false,
false
)
),
List.of(
new TestMethod(
"publicMethod",
"void",
emptyList(),
Visibility.PUBLIC
),
new TestMethod(
"privateMethod",
"void",
emptyList(),
Visibility.PRIVATE
),
new TestMethod(
"calculateSum",
"double",
List.of(
new TestVariable("a", "double"),
new TestVariable("b", "double")
),
Visibility.PUBLIC
),
new TestMethod(
"getString",
"String",
List.of(new TestVariable("string", "String")),
Visibility.PRIVATE
),
new TestMethod(
"processList",
"List",
List.of(new TestVariable("list", "List")),
Visibility.PUBLIC
)
),
false,
emptyList(),
emptyList()
);
}

@Test
public void javaClassTest() {
Class<?> clazz = javaClassTestClass.checkBaseDefinition();
javaClassTestClass.checkFieldsDefinition(clazz, true);
javaClassTestClass.checkDeclaredMethods(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.jetbrains.academy.test.system.core.testData.java;

public class CustomJavaClass {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.jetbrains.academy.test.system.core.testData.java;

import java.util.List;

public class JavaClass {

public static final int PUBLIC_CONSTANT = 1;
private static final int PRIVATE_CONSTANT = 2;

public static int publicStaticVar = 3;
private static int privateStaticVar = 4;

public String publicVar = "publicVar";
private String privateVar = "privateVar";

public CustomJavaClass customClass = null;

public String primaryConstructorVar;

public JavaClass(String primaryVar) {
this.primaryConstructorVar = primaryVar;
}

public void publicMethod() {
System.out.println("Public instance method called!");
}

private void privateMethod() {
System.out.println("Private instance method called!");
}

public double calculateSum(double a, double b) {
return a + b;
}

private String getString(String string) {
return string;
}

public List<String> processList(List<String> list) {
for (String item : list) {
System.out.println("Processing item: " + item);
}
return List.of("item1", "item2", "item3");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jetbrains.academy.test.system.core

import org.jetbrains.academy.test.system.core.testData.java.javaClassTestClass
import org.junit.jupiter.api.Test

class JavaClassTests {

@Test
fun javaClassTest() {
val clazz = javaClassTestClass.checkBaseDefinition()
javaClassTestClass.checkFieldsDefinition(clazz)
javaClassTestClass.checkDeclaredMethods(clazz)
}
}
Loading
Loading