[Locale] fixed StubNumberFormatter::parse() to behave like the NumberFormatter::parse() regarding to error flagging

This commit is contained in:
Eriksen Costa 2012-04-15 19:08:47 -03:00
parent 0a606642b7
commit f16ff892bb
2 changed files with 31 additions and 6 deletions

View File

@ -501,7 +501,7 @@ class StubNumberFormatter
// Any string before the numeric value causes error in the parsing
if (isset($matches[1]) && !empty($matches[1])) {
StubIntl::setError(StubIntl::U_PARSE_ERROR);
StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Number parsing failed');
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();
@ -510,8 +510,14 @@ class StubNumberFormatter
// Remove everything that is not number or dot (.)
$value = preg_replace('/[^0-9\.\-]/', '', $value);
$value = $this->convertValueDataType($value, $type);
return $this->convertValueDataType($value, $type);
// behave like the intl extension
StubIntl::setError(StubIntl::U_ZERO_ERROR);
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();
return $value;
}
/**

View File

@ -669,10 +669,19 @@ class StubNumberFormatterTest extends LocaleTestCase
$this->assertSame($expected, $parsedValue, $message);
if ($expected === false) {
$this->assertSame(StubIntl::U_PARSE_ERROR, $formatter->getErrorCode());
$errorCode = StubIntl::U_PARSE_ERROR;
$errorMessage = 'Number parsing failed: U_PARSE_ERROR';
} else {
$this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode());
$errorCode = StubIntl::U_ZERO_ERROR;
$errorMessage = 'U_ZERO_ERROR';
}
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
$this->assertSame($errorCode, StubIntl::getErrorCode());
$this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
$this->assertSame($errorMessage, $formatter->getErrorMessage());
$this->assertSame($errorCode, $formatter->getErrorCode());
$this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode()));
}
/**
@ -682,15 +691,25 @@ class StubNumberFormatterTest extends LocaleTestCase
{
$this->skipIfIntlExtensionIsNotLoaded();
$this->skipIfICUVersionIsTooOld();
$formatter = $this->getIntlFormatterWithDecimalStyle();
$parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE);
$this->assertSame($expected, $parsedValue, $message);
if ($expected === false) {
$this->assertSame(U_PARSE_ERROR, $formatter->getErrorCode());
$errorCode = StubIntl::U_PARSE_ERROR;
$errorMessage = 'Number parsing failed: U_PARSE_ERROR';
} else {
$this->assertEquals(U_ZERO_ERROR, $formatter->getErrorCode());
$errorCode = StubIntl::U_ZERO_ERROR;
$errorMessage = 'U_ZERO_ERROR';
}
$this->assertSame($errorMessage, intl_get_error_message());
$this->assertSame($errorCode, intl_get_error_code());
$this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
$this->assertSame($errorMessage, $formatter->getErrorMessage());
$this->assertSame($errorCode, $formatter->getErrorCode());
$this->assertSame($errorCode != 0, intl_is_failure($formatter->getErrorCode()));
}
public function parseProvider()