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

[Kotlin] Package directive does not match file location & test cases do not see modifications in the file under test #296

Open
Frosendroska opened this issue Jul 19, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Frosendroska
Copy link
Contributor

Frosendroska commented Jul 19, 2024

Describe the bug

How it works for Java

The following java class

package org.tests;

<some imports>

public class GeneratedMethodPublicInvokesPrivateTest {

    @Test
    public void methodPublicInvokesPrivateTest() {
       <some code>
    }
}

is located in

/private/var/folders/pm/<>/T/testSparkResults/test_gen_result_<>/org/tests/GeneratedMethodPublicInvokesPrivateTest.java

And the IDEA see that the package name and the location (the suffix of the path) matches. So we can have the proper access to the package elements.

How it works for Kotlin

The following kotlin class

package org.example

<some imports>

class GeneratedConstructorPrimaryNameOnlyTest {

    @Test
    fun constructorPrimaryNameOnlyTest() {
        <some code>
    }
}

is located in

/private/var/folders/pm/<>/T/testSparkResults/test_gen_result_<>/org/example/GeneratedConstructorPrimaryNameOnlyTest.kt

But IDEA shows me the warning: Package directive does not match the file location. So we have the problem with access.

For example, we can not access, so the compilation for all tests of this class will fail.

internal class MyClass(private val name: String) {}
@Frosendroska Frosendroska added the bug Something isn't working label Jul 19, 2024
@Frosendroska Frosendroska added the Urgent Urgant PR label Jul 26, 2024
@Vladislav0Art
Copy link
Collaborator

What works

Currently, the described scenario works fine: the test case compiles and runs successfully. Yet, the warning persists, it does not affect the execution.

Screenshot 2024-10-08 at 14 05 10

What does not work

But there will be an error if I generate a test suite, then add any class/method/function in my file under test and try to use the newly created construct in any test case. I will get the following error:
Screenshot 2024-10-08 at 14 09 28

Steps to reproduce:

  1. Generate a test suite for a class CalcKotlin in the following file:
internal class A {
    val a = "123"
}

class CalcKotlin {
    val a = 10;

    fun add(a: Int, b: Int): Int {
        return a + b
    }
}
  1. Suppose the following test case is generated:
class GeneratedAddNegativeNumbersTest {
    private val calc = CalcKotlin()
    @Test
    fun addNegativeNumbersTest() {
        val result = calc.add(-5, -10)
        assertEquals(-15, result)
    }
}
  1. Now, add a creation of A and an assert for its value, it should execute successfully:
class GeneratedAddNegativeNumbersTest {
    private val calc = CalcKotlin()
    @Test
    fun addNegativeNumbersTest() {
        val result = calc.add(-5, -10)
        assertEquals(-15, result)

        val a = A()
        assertEquals("123", a.a)
    }
}
  1. Now, add the class B into the file under test:
internal class A {
    val a = "123"
}

class B { // added this class; notice that it is public!
    val b = "1"
}

class CalcKotlin {
    val a = 10;

    fun add(a: Int, b: Int): Int {
        return a + b
    }
}
  1. Now, instantiate this class in the test case:
class GeneratedAddNegativeNumbersTest {
    private val calc = CalcKotlin()
    @Test
    fun addNegativeNumbersTest() {
        val result = calc.add(-5, -10)
        assertEquals(-15, result)

        val a = A()
        assertEquals("123", a.a)

       val b = B()
       // optional:
       assertEquals("1", b.b)
    }
}
  1. Execute the test case and see the error as in the image above.

@Vladislav0Art Vladislav0Art changed the title [Kotlin] Package directive does not match file location [Kotlin] Package directive does not match file location & test cases do not see modifications in the file under test Oct 8, 2024
@Vladislav0Art Vladislav0Art added important and removed Urgent Urgant PR labels Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants