Skip to content

Support Kotlin null safety #1257

Open
Open
@volivan239

Description

@volivan239

Description

We have to properly handle Kotlin nullable and not-nullable types to produce correct tests:

  1. We shouldn't generate models in which value of not-nullable type is null
  2. We should put ?. and !! in generated files where it is needed.
  3. We should properly handle possibly-null values returned by java code.

There were some attempts to support this, but this is only partially supported currently.

To Reproduce

There are many cases where behavior is incorrect. For example, launch action on the following class:

class NullabilityIssuesExample(var x: Int) {
    fun copyWithX(new_x: Int?): NullabilityIssuesExample? {
        if (new_x == null)
            return null
        return NullabilityIssuesExample(x + new_x)
    }
}

Expected behavior

Correct tests are generated.

Actual behavior

Among others, the following test is generated:

fun testCopyWithX2() {
    val nullabilityIssuesExample = NullabilityIssuesExample(-255)
    val new_x = -255
    
    val actual = nullabilityIssuesExample.copyWithX(new_x)
    
    val expected = NullabilityIssuesExample(0)
    expected.x = -510
    val expectedX = expected.x
    val actualX = actual.x
    assertEquals(expectedX, actualX)   
}

Additional context

This is an old big issue, which may be solved step by step.

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorcomp-symbolic-engineIssue is related to the symbolic execution enginectg-bugIssue is a buglang-kotlinIssue is related to Kotlin language support

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions