1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-04 02:27:59 +01:00
psalm/tests/TypeReconciliationTest.php

153 lines
4.3 KiB
PHP
Raw Normal View History

2016-06-17 22:05:28 +02:00
<?php
2016-07-26 00:37:44 +02:00
namespace Psalm\Tests;
2016-06-17 22:05:28 +02:00
2016-07-26 00:37:44 +02:00
use Psalm\Type;
use Psalm\Checker\TypeChecker;
2016-06-17 22:05:28 +02:00
use PHPUnit_Framework_TestCase;
class TypeReconciliationTest extends PHPUnit_Framework_TestCase
{
public function testNotNull()
{
$this->assertEquals(
2016-07-12 06:53:36 +02:00
'MyObject',
(string) TypeChecker::reconcileTypes('!null', Type::parseString('MyObject'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
2016-07-12 06:53:36 +02:00
'MyObject',
(string) TypeChecker::reconcileTypes('!null', Type::parseString('MyObject|null'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
2016-07-12 06:53:36 +02:00
'MyObject|false',
(string) TypeChecker::reconcileTypes('!null', Type::parseString('MyObject|false'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
'mixed',
(string) TypeChecker::reconcileTypes('!null', Type::parseString('mixed'))
);
}
public function testNotEmpty()
{
$this->assertEquals(
2016-07-12 06:53:36 +02:00
'MyObject',
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('MyObject'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
2016-07-12 06:53:36 +02:00
'MyObject',
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('MyObject|null'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
2016-07-12 06:53:36 +02:00
'MyObject',
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('MyObject|false'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
'mixed',
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('mixed'))
);
2016-07-26 00:31:03 +02:00
// @todo in the future this should also work
/*
2016-06-17 22:05:28 +02:00
$this->assertEquals(
2016-07-12 06:53:36 +02:00
'MyObject|true',
(string) TypeChecker::reconcileTypes('!empty', Type::parseString('MyObject|bool'))
2016-06-17 22:05:28 +02:00
);
2016-07-26 00:31:03 +02:00
*/
2016-06-17 22:05:28 +02:00
}
public function testNull()
{
$this->assertEquals(
'null',
2016-07-12 06:53:36 +02:00
(string) TypeChecker::reconcileTypes('null', Type::parseString('MyObject|null'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
'null',
2016-07-12 06:53:36 +02:00
(string) TypeChecker::reconcileTypes('null', Type::parseString('MyObject'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
'null',
2016-07-12 06:53:36 +02:00
(string) TypeChecker::reconcileTypes('null', Type::parseString('MyObject|false'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
'null',
(string) TypeChecker::reconcileTypes('null', Type::parseString('mixed'))
);
}
public function testEmpty()
{
$this->assertEquals(
'null',
2016-07-12 06:53:36 +02:00
(string) TypeChecker::reconcileTypes('empty', Type::parseString('MyObject'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
'false',
2016-07-12 06:53:36 +02:00
(string) TypeChecker::reconcileTypes('empty', Type::parseString('MyObject|false'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
'false',
2016-07-12 06:53:36 +02:00
(string) TypeChecker::reconcileTypes('empty', Type::parseString('MyObject|bool'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
'mixed',
(string) TypeChecker::reconcileTypes('empty', Type::parseString('mixed'))
);
$reconciled = TypeChecker::reconcileTypes('empty', Type::parseString('bool'));
$this->assertEquals('false', (string) $reconciled);
2016-07-26 00:37:44 +02:00
$this->assertInstanceOf('Psalm\Type\Atomic', $reconciled->types['false']);
2016-06-17 22:05:28 +02:00
}
2016-07-12 06:53:36 +02:00
public function testNotMyObject()
2016-06-17 22:05:28 +02:00
{
$this->assertEquals(
'bool',
2016-07-12 06:53:36 +02:00
(string) TypeChecker::reconcileTypes('!MyObject', Type::parseString('MyObject|bool'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
'null',
2016-07-12 06:53:36 +02:00
(string) TypeChecker::reconcileTypes('!MyObject', Type::parseString('MyObject|null'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
2016-07-12 06:53:36 +02:00
'MyObjectB',
(string) TypeChecker::reconcileTypes('!MyObjectA', Type::parseString('MyObjectA|MyObjectB'))
2016-06-17 22:05:28 +02:00
);
}
2016-07-12 06:53:36 +02:00
public function testMyObject()
{
2016-06-17 22:05:28 +02:00
$this->assertEquals(
2016-07-12 06:53:36 +02:00
'MyObject',
(string) TypeChecker::reconcileTypes('MyObject', Type::parseString('MyObject|bool'))
2016-06-17 22:05:28 +02:00
);
$this->assertEquals(
2016-07-12 06:53:36 +02:00
'MyObjectA',
(string) TypeChecker::reconcileTypes('MyObjectA', Type::parseString('MyObjectA|MyObjectB'))
2016-06-17 22:05:28 +02:00
);
}
public function testAllMixed()
{
2016-06-17 22:05:28 +02:00
$this->assertEquals(
'mixed',
(string) TypeChecker::reconcileTypes('mixed', Type::parseString('mixed'))
);
}
}