Skip to content

Commit

Permalink
new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed Feb 17, 2025
1 parent 25dc928 commit 2972b37
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/tests/checkers/null_safety_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,89 @@ testVariadic(new A(), null);
}
test.RunAndMatch()
}

// TODO: After realisation Control Flow Graph (CFG) и Data Flow Graph (DFG) this test must fail
func TestIfNullCheckSafe(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
declare(strict_types=1);
class A {
public string $value = 'Safe';
}
/**
* Function expecting a non-null instance of A.
*/
function test(A $a): void {
echo $a->value;
}
$v = null;
if ($v == null) {
// Correctly assign a new instance when $v is null.
$v = new A();
}
test($v); // Should be safe.
`)

test.Expect = []string{
`not null safety call in function test signature of param`,
}
test.RunAndMatch()
}

func TestIfNullCheckUnsafe(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
declare(strict_types=1);
class A {
public string $value = 'Unsafe';
}
/**
* Function expecting a non-null instance of A.
*/
function test(A $a): void {
echo $a->value;
}
$v = null;
if ($v == null) {
// No assignment is done here – $v remains null.
}
test($v); // Should trigger a null safety error.
`)
test.Expect = []string{
"not null safety call in function test signature of param",
}
test.RunAndMatch()
}

func TestIfNotNullCheck(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
declare(strict_types=1);
class A {
public string $value = 'NotNull';
}
/**
* Function expecting a non-null instance of A.
*/
function test(A $a): void {
echo $a->value;
}
$v = new A();
if ($v !== null) {
// $v is known to be non-null.
test($v); // Should be safe.
}
`)

test.Expect = []string{}
test.RunAndMatch()
}

0 comments on commit 2972b37

Please sign in to comment.