Add OptionalQuantityFormatter test.
This commit is contained in:
parent
a46bd2f029
commit
e5698c99ed
@ -15,6 +15,7 @@ use InvalidArgumentException;
|
||||
use Web3\Utils;
|
||||
use Web3\Formatters\IFormatter;
|
||||
use Web3\Validators\TagValidator;
|
||||
use Web3\Formatters\QuantityFormatter;
|
||||
|
||||
class OptionalQuantityFormatter implements IFormatter
|
||||
{
|
||||
@ -29,9 +30,6 @@ class OptionalQuantityFormatter implements IFormatter
|
||||
if (TagValidator::validate($value)) {
|
||||
return $value;
|
||||
}
|
||||
$value = Utils::toString($value);
|
||||
$bn = Utils::toBn($value);
|
||||
|
||||
return '0x' . $bn->toHex(true);
|
||||
return QuantityFormatter::format($value);
|
||||
}
|
||||
}
|
@ -35,7 +35,7 @@ class QuantityFormatter implements IFormatter
|
||||
$hex = preg_replace('/^0x0+(?!$)/', '', $value);
|
||||
} else {
|
||||
$bn = Utils::toBn($value);
|
||||
$hex = $bn->toHex(true);
|
||||
$hex = $bn->toHex(true);
|
||||
}
|
||||
if (empty($hex)) {
|
||||
$hex = '0';
|
||||
|
60
test/unit/OptionalQuantityFormatterTest.php
Normal file
60
test/unit/OptionalQuantityFormatterTest.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Test\Unit;
|
||||
|
||||
use Test\TestCase;
|
||||
use Web3\Formatters\OptionalQuantityFormatter;
|
||||
|
||||
class OptionalQuantityFormatterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* formatter
|
||||
*
|
||||
* @var \Web3\Formatters\OptionalQuantityFormatter
|
||||
*/
|
||||
protected $formatter;
|
||||
|
||||
/**
|
||||
* setUp
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->formatter = new OptionalQuantityFormatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* testFormat
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testFormat()
|
||||
{
|
||||
$formatter = $this->formatter;
|
||||
|
||||
$this->assertEquals('0x927c0', $formatter->format(0x0927c0));
|
||||
$this->assertEquals('0x927c0', $formatter->format('0x0927c0'));
|
||||
$this->assertEquals('0x927c0', $formatter->format('0x927c0'));
|
||||
$this->assertEquals('0x927c0', $formatter->format('600000'));
|
||||
$this->assertEquals('0x927c0', $formatter->format(600000));
|
||||
|
||||
$this->assertEquals('0xea60', $formatter->format('0x0ea60'));
|
||||
$this->assertEquals('0xea60', $formatter->format('0xea60'));
|
||||
$this->assertEquals('0xea60', $formatter->format(0x0ea60));
|
||||
$this->assertEquals('0xea60', $formatter->format('60000'));
|
||||
$this->assertEquals('0xea60', $formatter->format(60000));
|
||||
|
||||
$this->assertEquals('0x0', $formatter->format(0x00));
|
||||
$this->assertEquals('0x0', $formatter->format('0x00'));
|
||||
$this->assertEquals('0x0', $formatter->format('0x0'));
|
||||
$this->assertEquals('0x0', $formatter->format('0'));
|
||||
$this->assertEquals('0x0', $formatter->format(0));
|
||||
|
||||
$this->assertEquals('latest', $formatter->format('latest'));
|
||||
$this->assertEquals('earliest', $formatter->format('earliest'));
|
||||
$this->assertEquals('pending', $formatter->format('pending'));
|
||||
$this->assertEquals('0x0', $formatter->format('hello'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user