From 07c415d588ad914cb8953f058e8df99d4b925fae Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Fri, 23 Dec 2016 23:52:34 +0000 Subject: [PATCH] Add tests for upcoming functionality --- tests/BinaryOperationTest.php | 133 +++++++++++++++++++++++++++++++ tests/TypeReconciliationTest.php | 29 +++++++ 2 files changed, 162 insertions(+) create mode 100644 tests/BinaryOperationTest.php diff --git a/tests/BinaryOperationTest.php b/tests/BinaryOperationTest.php new file mode 100644 index 000000000..14cc4d845 --- /dev/null +++ b/tests/BinaryOperationTest.php @@ -0,0 +1,133 @@ +create(ParserFactory::PREFER_PHP7); + } + + public function setUp() + { + FileChecker::clearCache(); + $config = new TestConfig(); + } + + public function testRegularAddition() + { + $stmts = self::$parser->parse('check(true, true, $context); + } + + /** + * @expectedException \Psalm\Exception\CodeException + * @expectedExceptionMessage InvalidOperand + */ + public function testBadAddition() + { + $stmts = self::$parser->parse('check(true, true, $context); + } + + public function testDifferingNumericTypesAdditionInWeakMode() + { + $stmts = self::$parser->parse('check(true, true, $context); + } + + /** + * @expectedException \Psalm\Exception\CodeException + * @expectedExceptionMessage InvalidOperand + */ + public function testDifferingNumericTypesAdditionInStrictMode() + { + Config::getInstance()->strict_binary_operands = true; + + $stmts = self::$parser->parse('check(true, true, $context); + } + + public function testNumericAddition() + { + $stmts = self::$parser->parse('check(true, true, $context); + } + + public function testConcatenation() + { + $stmts = self::$parser->parse('check(true, true, $context); + } + + public function testConcatenationWithNumberInWeakMode() + { + $stmts = self::$parser->parse('check(true, true, $context); + } + + /** + * @expectedException \Psalm\Exception\CodeException + * @expectedExceptionMessage InvalidOperand + */ + public function testConcatenationWithNumberInStrictMode() + { + Config::getInstance()->strict_binary_operands = true; + + $stmts = self::$parser->parse('check(true, true, $context); + } +} diff --git a/tests/TypeReconciliationTest.php b/tests/TypeReconciliationTest.php index 8fe851e2f..212af56d9 100644 --- a/tests/TypeReconciliationTest.php +++ b/tests/TypeReconciliationTest.php @@ -182,6 +182,14 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase ); } + public function testNumeric() + { + $this->assertEquals( + 'string', + (string) TypeChecker::reconcileTypes('numeric', Type::parseString('string')) + ); + } + /** * @expectedException \Psalm\Exception\CodeException * @expectedExceptionMessage TypeDoesNotContainType @@ -339,4 +347,25 @@ class TypeReconciliationTest extends PHPUnit_Framework_TestCase $this->assertEquals('int', (string) $context->vars_in_scope['$b']); $this->assertEquals('string', (string) $context->vars_in_scope['$c']); } + + /** + * @expectedException \Psalm\Exception\CodeException + * @expectedExceptionMessage TypeDoesNotContainType + */ + public function testTypeTransformation() + { + $stmts = self::$parser->parse('check(true, true, $context); + } }