2016-09-09 22:38:32 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Type;
|
2016-09-09 22:38:32 +02:00
|
|
|
|
|
|
|
class TypeParseTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-12-14 18:55:23 +01:00
|
|
|
/** @var \PhpParser\Parser */
|
2016-11-02 07:29:00 +01:00
|
|
|
protected static $parser;
|
2016-09-09 22:38:32 +02:00
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-09-09 22:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function getAtomic($string)
|
|
|
|
{
|
|
|
|
return array_values(Type::parseString($string)->types)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIntOrString()
|
|
|
|
{
|
|
|
|
$this->assertEquals('int|string', (string) Type::parseString('int|string'));
|
2016-09-22 06:31:07 +02:00
|
|
|
}
|
2016-09-09 22:38:32 +02:00
|
|
|
|
2016-09-22 06:31:07 +02:00
|
|
|
public function testArray()
|
|
|
|
{
|
2016-11-13 17:24:25 +01:00
|
|
|
$this->assertEquals('array<int, int>', (string) Type::parseString('array<int, int>'));
|
|
|
|
$this->assertEquals('array<int, string>', (string) Type::parseString('array<int, string>'));
|
|
|
|
$this->assertEquals('array<int, static>', (string) Type::parseString('array<int, static>'));
|
|
|
|
$this->assertEquals('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
|
|
|
|
2016-09-22 06:31:07 +02:00
|
|
|
public function testGeneric()
|
|
|
|
{
|
2016-09-09 22:38:32 +02:00
|
|
|
$this->assertEquals('B<int>', (string) Type::parseString('B<int>'));
|
|
|
|
}
|
|
|
|
|
2016-10-30 02:57:03 +02:00
|
|
|
public function testPhpDocStyle()
|
|
|
|
{
|
2016-11-13 17:24:25 +01:00
|
|
|
$this->assertEquals('array<mixed, A>', (string) Type::parseString('A[]'));
|
|
|
|
$this->assertEquals('array<mixed, A|B>', (string) Type::parseString('(A|B)[]'));
|
|
|
|
$this->assertEquals('array<mixed, array<mixed, A>>', (string) Type::parseString('A[][]'));
|
|
|
|
$this->assertEquals('array<mixed, array<mixed, A|B>>', (string) Type::parseString('(A|B)[][]'));
|
2016-11-20 08:52:34 +01:00
|
|
|
$this->assertEquals('array<mixed, A|B>', (string) Type::parseString('A[]|B[]'));
|
|
|
|
$this->assertEquals('array<mixed, A|B>|C', (string) Type::parseString('A[]|B[]|C'));
|
2016-10-30 02:57:03 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 06:31:07 +02:00
|
|
|
public function testObjectLike()
|
|
|
|
{
|
2016-11-13 17:24:25 +01:00
|
|
|
$this->assertEquals('array{a:int, b:string}', (string) Type::parseString('array{a:int, b:string}'));
|
2016-11-02 07:29:00 +01:00
|
|
|
$this->assertEquals(
|
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}')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
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}')
|
|
|
|
);
|
2016-09-22 06:31:07 +02:00
|
|
|
}
|
2016-09-09 22:38:32 +02:00
|
|
|
}
|