From 0cf16f90ce015bb5adb79ff4a74a64c175ba1461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= <honza.kuchar@grifart.cz> Date: Mon, 1 May 2017 14:50:49 +0200 Subject: [PATCH] tests: added primitive types test --- src/functions.php | 2 -- tests/fn.assertSignature.primitives.phpt | 32 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/fn.assertSignature.primitives.phpt diff --git a/src/functions.php b/src/functions.php index 533a41c..e668304 100644 --- a/src/functions.php +++ b/src/functions.php @@ -3,9 +3,7 @@ * Used to load functions. */ -// todo: TESTS: better assertion messages + throw FunctionSignatureAssertionError + catch in fn + pass it to assert -> compatible with PHP assert config // todo: covariance and contra-variance -// todo: tests for primitive types namespace Grifart\AssertFunction; diff --git a/tests/fn.assertSignature.primitives.phpt b/tests/fn.assertSignature.primitives.phpt new file mode 100644 index 0000000..bdc2566 --- /dev/null +++ b/tests/fn.assertSignature.primitives.phpt @@ -0,0 +1,32 @@ +<?php declare(strict_types=1); +namespace MyTestNamespace; +require __DIR__ . '/bootstrap.php'; +require __DIR__ . '/testClasses.php'; +use function Grifart\AssertFunction\{assertSignature, nullable, params}; +use Grifart\AssertFunction\FunctionSignatureAssertionError; +use Tester\Assert; +$place = 'tests/' . basename(__FILE__) . ':' . (__LINE__ +2) . ' '; + +$fn = function(string $p1, ?int $p2): void {}; + +assertSignature($fn, params('string', '?int'), 'void'); +assertSignature($fn, params('string', nullable('int')), 'void'); +assertSignature($fn, params('string', nullable('int')), NULL); // no-return type expectations + +// parameter nullability +Assert::exception( + function () use ($fn) { + assertSignature($fn, params(nullable('string'), 'int'), NULL); // no-return type expectations + }, + FunctionSignatureAssertionError::class, + $place . 'Parameter #1 ($p1) is expected to be nullable. But it is required.' +); + +// return-type mismatch +Assert::exception( + function () use ($fn) { + assertSignature($fn, params('string', '?int'), 'int'); + }, + FunctionSignatureAssertionError::class, + $place . "Expected return type of type 'int', but given function declares 'void'." +); -- GitLab