[kahlan:api/core/Checker] tests + fixed Checker (array single type vs. array complex type)

This commit is contained in:
xdrm-brackets 2017-09-20 16:52:42 +02:00
parent 3c61e63af1
commit 4eb873a182
2 changed files with 29 additions and 10 deletions

View File

@ -48,16 +48,12 @@
/* [2] Si de type ARRAY(type_elements) /* [2] Si de type ARRAY(type_elements)
=========================================================*/ =========================================================*/
if( preg_match('/^array(?:<(.+)>)?$/', $type, $match) ){ if( preg_match('/^array(?:<(.+)>)$/', $type, $match) ){
// Si c'est pas un tableau on retourne une erreur // Si c'est pas un tableau on retourne une erreur
if( !is_array($value) ) if( !is_array($value) )
return false; return false;
// Si c'est un tableau et que 'array' recu
if( $type == 'array' )
return true;
$elements_type = $match[1]; $elements_type = $match[1];
// On verifie le type pour chaque element // On verifie le type pour chaque element

View File

@ -775,7 +775,7 @@
/* (17) Array /* (17) Array
---------------------------------------------------------*/ ---------------------------------------------------------*/
context('* array', function(){ context('* array<subtype>', function(){
it('pass when empty array', function(){ it('pass when empty array', function(){
@ -783,7 +783,7 @@
expect($arr)->toBeA('array'); expect($arr)->toBeA('array');
expect($arr)->toBeEmpty(); expect($arr)->toBeEmpty();
expect(Checker::run('array', $arr))->toBeTruthy(); expect(Checker::run('array<mixed>', $arr))->toBeTruthy();
}); });
@ -800,6 +800,17 @@
}); });
it('fail when empty array (single type \'array\')', function(){
$arr = [];
expect($arr)->toBeA('array');
expect($arr)->toBeEmpty();
expect(Checker::run('array', $arr))->toBeFalsy();
});
it('pass when recursive empty arrays', function(){ it('pass when recursive empty arrays', function(){
$arr = [[], []]; $arr = [[], []];
@ -810,9 +821,22 @@
expect($arr[0])->toBeEmpty(); expect($arr[0])->toBeEmpty();
expect($arr[1])->toBeEmpty(); expect($arr[1])->toBeEmpty();
expect(Checker::run('array', $arr))->toBeTruthy();
expect(Checker::run('array<mixed>', $arr))->toBeTruthy(); expect(Checker::run('array<mixed>', $arr))->toBeTruthy();
expect(Checker::run('array<array>', $arr))->toBeTruthy(); expect(Checker::run('array<array<mixed>>', $arr))->toBeTruthy();
});
it('fail when recursive empty arrays (array<array>)', function(){
$arr = [[], []];
expect($arr)->toBeA('array');
expect($arr[0])->toBeA('array');
expect($arr[1])->toBeA('array');
expect($arr[0])->toBeEmpty();
expect($arr[1])->toBeEmpty();
expect(Checker::run('array<array>', $arr))->toBeFalsy();
}); });
@ -824,7 +848,6 @@
expect(Checker::run('id', $id))->toBeTruthy(); expect(Checker::run('id', $id))->toBeTruthy();
expect($arr)->toBeA('array'); expect($arr)->toBeA('array');
expect(Checker::run('array', $arr))->toBeTruthy();
expect(Checker::run('array<mixed>', $arr))->toBeTruthy(); expect(Checker::run('array<mixed>', $arr))->toBeTruthy();
expect(Checker::run('array<id>', $arr))->toBeTruthy(); expect(Checker::run('array<id>', $arr))->toBeTruthy();