2016-09-09 22:38:32 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Type;
|
2016-09-09 22:38:32 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class TypeParseTest extends TestCase
|
2016-09-09 22:38:32 +02:00
|
|
|
{
|
2017-12-19 04:07:52 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-09 22:38:32 +02:00
|
|
|
public function testIntOrString()
|
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('int|string', (string) Type::parseString('int|string'));
|
2016-09-22 06:31:07 +02:00
|
|
|
}
|
2016-09-09 22:38:32 +02:00
|
|
|
|
2017-11-20 06:32:40 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNullable()
|
|
|
|
{
|
|
|
|
$this->assertSame('null|string', (string) Type::parseString('?string'));
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-22 06:31:07 +02:00
|
|
|
public function testArray()
|
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('array<int, int>', (string) Type::parseString('array<int, int>'));
|
|
|
|
$this->assertSame('array<int, string>', (string) Type::parseString('array<int, string>'));
|
|
|
|
$this->assertSame('array<int, static>', (string) Type::parseString('array<int, static>'));
|
|
|
|
$this->assertSame('array<int|string, string>', (string) Type::parseString('array<int|string, string>'));
|
2016-09-22 06:31:07 +02:00
|
|
|
}
|
2016-09-09 22:38:32 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-22 06:31:07 +02:00
|
|
|
public function testGeneric()
|
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('B<int>', (string) Type::parseString('B<int>'));
|
2016-09-09 22:38:32 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 18:57:04 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIntersection()
|
|
|
|
{
|
|
|
|
$this->assertSame('I1&I2', (string) Type::parseString('I1&I2'));
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-30 02:57:03 +02:00
|
|
|
public function testPhpDocStyle()
|
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('array<mixed, A>', (string) Type::parseString('A[]'));
|
|
|
|
$this->assertSame('array<mixed, A|B>', (string) Type::parseString('(A|B)[]'));
|
|
|
|
$this->assertSame('array<mixed, array<mixed, A>>', (string) Type::parseString('A[][]'));
|
|
|
|
$this->assertSame('array<mixed, array<mixed, A|B>>', (string) Type::parseString('(A|B)[][]'));
|
|
|
|
$this->assertSame('array<mixed, A|B>', (string) Type::parseString('A[]|B[]'));
|
|
|
|
$this->assertSame('array<mixed, A|B>|C', (string) Type::parseString('A[]|B[]|C'));
|
2016-10-30 02:57:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 22:06:06 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPsalmOnlyAtomic()
|
|
|
|
{
|
|
|
|
$this->assertSame('class-string', (string) Type::parseString('class-string'));
|
|
|
|
}
|
|
|
|
|
2017-10-12 20:02:06 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\TypeParseTreeException
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvalidType()
|
|
|
|
{
|
|
|
|
Type::parseString('array(A)');
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-22 06:31:07 +02:00
|
|
|
public function testObjectLike()
|
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('array{a:int, b:string}', (string) Type::parseString('array{a:int, b:string}'));
|
|
|
|
$this->assertSame(
|
2016-11-13 17:24:25 +01:00
|
|
|
'array{a:int|string, b:string}',
|
2016-11-02 07:29:00 +01:00
|
|
|
(string) Type::parseString('array{a:int|string, b:string}')
|
|
|
|
);
|
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame(
|
2016-11-13 17:24:25 +01:00
|
|
|
'array{a:array<int, string|int>, b:string}',
|
2016-11-02 07:29:00 +01:00
|
|
|
(string) Type::parseString('array{a:array<int, string|int>, b:string}')
|
|
|
|
);
|
2017-12-19 04:07:52 +01:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'array{0:stdClass|null}',
|
|
|
|
(string)Type::parseString('array{stdClass|null}')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'array{0:array<mixed, mixed>}',
|
|
|
|
(string)Type::parseString('array{array}')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'array{0:array<int, string>}',
|
|
|
|
(string)Type::parseString('array{array<int, string>}')
|
|
|
|
);
|
2018-03-17 04:37:10 +01:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
'array{a:int, b?:int}',
|
|
|
|
(string)Type::parseString('array{a:int, b?:int}')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testObjectLikeOptional()
|
|
|
|
{
|
|
|
|
$this->assertSame(
|
|
|
|
'array{a:int, b?:int}',
|
|
|
|
(string)Type::parseString('array{a:int, b?:int}')
|
|
|
|
);
|
2016-09-22 06:31:07 +02:00
|
|
|
}
|
2016-09-09 22:38:32 +02:00
|
|
|
}
|