Skip to content

Commit

Permalink
Merge pull request #28 from jetbrains-academy/core-module-for-java
Browse files Browse the repository at this point in the history
Added tests for Java and fixes to support Java
  • Loading branch information
nbirillo authored Dec 20, 2023
2 parents 590152d + 7bd4c84 commit 24a48f7
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "org.jetbrains.academy.test.system"
version = "2.1.1"
version = "2.1.2"

allprojects {
apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ data class TestMethod(
val visibility: Visibility = Visibility.PUBLIC,
val hasGeneratedPartInName: Boolean = false,
) {
constructor(
name: String,
returnTypeJava: String,
arguments: List<TestVariable>,
visibility: Visibility
) : this(
name = name,
returnType = TestKotlinType(returnTypeJava),
arguments = arguments,
returnTypeJava = returnTypeJava,
visibility = visibility,
hasGeneratedPartInName = false
)

fun prettyString(withToDo: Boolean = true): String {
val args = arguments.joinToString(", ") { it.paramPrettyString() }
val body = if (withToDo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ 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
) : this(
name = name,
javaType = javaType,
value = value,
kotlinType = null,
visibility = visibility,
mutability = if (isFinal) VariableMutability.VAL else VariableMutability.VAR,
isInPrimaryConstructor = isInPrimaryConstructor,
isStatic = isStatic,
isConst = isStatic && isFinal
)

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 +91,7 @@ data class TestVariable(
}
}
field.kotlinProperty?.returnType?.checkType(
kotlinType,
javaType,
"the field $name",
false
kotlinType, javaType, "the field $name", false
)
}
}
Expand All @@ -86,8 +105,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,146 @@
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,
true
),
new TestVariable(
"PRIVATE_CONSTANT",
"int",
"2",
Visibility.PRIVATE,
true,
false,
true
),
new TestVariable(
"publicStaticVar",
"int",
"3",
Visibility.PUBLIC,
false,
false,
true
),
new TestVariable(
"privateStaticVar",
"int",
"4",
Visibility.PRIVATE,
false,
false,
true
),
new TestVariable(
"publicVar",
"String",
"publicVar",
Visibility.PUBLIC,
false,
false,
false
),
new TestVariable(
"privateVar",
"String",
"privateVar",
Visibility.PRIVATE,
false,
false,
false
),
new TestVariable(
"customClass",
"CustomJavaClass",
null,
Visibility.PUBLIC,
false,
false,
false
),
new TestVariable(
"primaryConstructorVar",
"String",
null,
Visibility.PUBLIC,
false,
true,
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

0 comments on commit 24a48f7

Please sign in to comment.