Skip to content

Commit

Permalink
Merge pull request #70 from afk11/improve-tests
Browse files Browse the repository at this point in the history
Improve tests
  • Loading branch information
afk11 authored Dec 27, 2017
2 parents d81eb3f + 68b0adb commit 5c2c8c0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 25 deletions.
20 changes: 13 additions & 7 deletions tests/BufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,25 @@ public function testGetSize()
$this->assertEquals(4, Buffer::hex('41', 4)->getSize());
}

public function getIntVectors()
/**
* @return array
*/
public function getIntVectors(): array
{
return array(
array('1', 1, '01'),
array('1', null, '01'),
array('20', 1, '14')
);
return [
['1', '01', 1, ],
['1', '01', null,],
['20', '14', 1, ]
];
}

/**
* @dataProvider getIntVectors
* @param int|string $int
* @param int|null $size
* @param string $expectedHex
*/
public function testIntConstruct($int, $size, string $expectedHex)
public function testIntConstruct($int, string $expectedHex, int $size = null)
{
$buffer = Buffer::int($int, $size);
$this->assertEquals($expectedHex, $buffer->getHex());
Expand Down
1 change: 0 additions & 1 deletion tests/BuffertoolsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ public function testFlipBytes()
$this->assertSame($flip, chr(0x08) . chr(0x07) . chr(0x06) . chr(0x05) . chr(0x04) . chr(0x03) . chr(0x02) . chr(0x01));
}


public function testConcat()
{
$a = Buffer::hex("1100");
Expand Down
9 changes: 6 additions & 3 deletions tests/TemplateFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
use BitWasp\Buffertools\TemplateFactory;
use BitWasp\Buffertools\Types\VarInt;
use BitWasp\Buffertools\Types\VarString;
use BitWasp\Buffertools\Types\Vector;

class TemplateFactoryTest extends BinaryTest
{
/**
* @return array
*/
public function getTestVectors()
public function getTestVectors(): array
{
$vectors = [];

Expand Down Expand Up @@ -45,8 +46,10 @@ public function getTestVectors()

/**
* @dataProvider getTestVectors
* @param string $function
* @param string $eClass
*/
public function testTemplateUint($function, $eClass)
public function testTemplateUint(string $function, string $eClass)
{
$factory = new TemplateFactory(null);
$factory->$function();
Expand All @@ -67,6 +70,6 @@ function () {
$template = $factory->getTemplate();
$this->assertEquals(1, count($template));
$template = $factory->getTemplate()->getItems();
$this->assertInstanceOf('BitWasp\Buffertools\Types\Vector', $template[0]);
$this->assertInstanceOf(Vector::class, $template[0]);
}
}
10 changes: 8 additions & 2 deletions tests/TypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

class TypeFactoryTest extends BinaryTest
{
public function getTypeVectors()
/**
* @return array
*/
public function getTypeVectors(): array
{
$vectors = [];
$addPlainAndLe = function ($fxnName, $class, array $params = []) use (& $vectors) {
Expand All @@ -46,7 +49,10 @@ public function getTypeVectors()
return $vectors;
}

public function getTypeFactoryVectors()
/**
* @return array
*/
public function getTypeFactoryVectors(): array
{
$vectors = [];
foreach ([new TypeFactory(), new CachingTypeFactory()] as $factory) {
Expand Down
6 changes: 4 additions & 2 deletions tests/Types/IntSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

class IntSetTest extends BinaryTest
{

public function getIntSetVectors()
/**
* @return array
*/
public function getIntSetVectors(): array
{
$int32_le = new Int32(ByteOrder::LE);
$int32_be = new Int32(ByteOrder::BE);
Expand Down
32 changes: 22 additions & 10 deletions tests/Types/VarStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,36 @@

class VarStringTest extends BinaryTest
{

public function testGetVarString()
/**
* @return array
*/
public function getSampleVarStrings(): array
{
$strings = array(
return array_map(function (string $value) {
return [$value];
}, [
'',
'00',
'00010203040506070809',
'00010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102',
);
]);
}

/**
* @param string $input
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
* @throws \Exception
* @dataProvider getSampleVarStrings
*/
public function testGetVarString(string $input)
{
$varstring = new VarString(new VarInt());
$binary = $varstring->write(Buffer::hex($input));

$parser = new Parser(new Buffer($binary));
$original = $varstring->read($parser);

foreach ($strings as $string) {
$binary = $varstring->write(Buffer::hex($string));
$parser = new Parser(new Buffer($binary));
$original = $varstring->read($parser);
$this->assertSame($string, $original->getHex());
}
$this->assertSame($input, $original->getHex());
}

/**
Expand Down

0 comments on commit 5c2c8c0

Please sign in to comment.