Fixed double -> int conversion (now throws) for old API

This commit is contained in:
Jesse Beder
2012-01-12 23:52:51 -06:00
parent 643ea61a98
commit fb0802097c
2 changed files with 21 additions and 4 deletions

View File

@@ -50,9 +50,8 @@ namespace YAML
inline bool Convert(const std::string& input, T& output, typename enable_if<is_numeric<T> >::type * = 0) { inline bool Convert(const std::string& input, T& output, typename enable_if<is_numeric<T> >::type * = 0) {
std::stringstream stream(input); std::stringstream stream(input);
stream.unsetf(std::ios::dec); stream.unsetf(std::ios::dec);
stream >> output; if((stream >> output) && (stream >> std::ws).eof())
if(!!stream) return true;
return true;
if(std::numeric_limits<T>::has_infinity) { if(std::numeric_limits<T>::has_infinity) {
if(IsInfinity(input)) { if(IsInfinity(input)) {

View File

@@ -903,7 +903,24 @@ namespace Test
return doc["foo"].to<std::string>() == "\n"; return doc["foo"].to<std::string>() == "\n";
} }
}
bool DoubleAsInt()
{
std::string input = "1.5";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
try {
doc.to<int>();
} catch(const YAML::InvalidScalar& e) {
return true;
}
return false;
}
}
namespace { namespace {
void RunScalarParserTest(void (*test)(std::string&, std::string&), const std::string& name, int& passed, int& total) { void RunScalarParserTest(void (*test)(std::string&, std::string&), const std::string& name, int& passed, int& total) {
@@ -1184,6 +1201,7 @@ namespace Test
RunParserTest(&Parser::NonConstKey, "non const key", passed, total); RunParserTest(&Parser::NonConstKey, "non const key", passed, total);
RunParserTest(&Parser::SingleChar, "single char", passed, total); RunParserTest(&Parser::SingleChar, "single char", passed, total);
RunParserTest(&Parser::QuotedNewline, "quoted newline", passed, total); RunParserTest(&Parser::QuotedNewline, "quoted newline", passed, total);
RunParserTest(&Parser::DoubleAsInt, "double as int", passed, total);
RunEncodingTest(&EncodeToUtf8, false, "UTF-8, no BOM", passed, total); RunEncodingTest(&EncodeToUtf8, false, "UTF-8, no BOM", passed, total);
RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed, total); RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed, total);