Fixed bug with simple keys that are quoted scalars

This commit is contained in:
jbeder
2009-07-25 18:58:41 +00:00
parent ab7c8b0df0
commit 27055f178f
4 changed files with 260 additions and 224 deletions

View File

@@ -309,8 +309,8 @@ namespace YAML
{ {
std::string scalar; std::string scalar;
// eat single or double quote // peek at single or double quote (don't eat because we need to preserve (for the time being) the input position)
char quote = INPUT.get(); char quote = INPUT.peek();
bool single = (quote == '\''); bool single = (quote == '\'');
// setup the scanning parameters // setup the scanning parameters
@@ -330,6 +330,11 @@ namespace YAML
InsertSimpleKey(); InsertSimpleKey();
int line = INPUT.line, column = INPUT.column; int line = INPUT.line, column = INPUT.column;
// now eat that opening quote
INPUT.get();
// and scan
scalar = ScanScalar(INPUT, params); scalar = ScanScalar(INPUT, params);
m_simpleKeyAllowed = false; m_simpleKeyAllowed = false;

View File

@@ -1,222 +1,251 @@
#include "tests.h" #include "tests.h"
#include "yaml.h" #include "yaml.h"
#include <sstream> #include <sstream>
#include <algorithm>
namespace Test
{ namespace Test
namespace Parser { {
void SimpleScalar(std::string& inputScalar, std::string& desiredOutput) namespace Parser {
{ void SimpleScalar(std::string& inputScalar, std::string& desiredOutput)
inputScalar = "Hello, World!"; {
desiredOutput = "Hello, World!"; inputScalar = "Hello, World!";
} desiredOutput = "Hello, World!";
}
void MultiLineScalar(std::string& inputScalar, std::string& desiredOutput)
{ void MultiLineScalar(std::string& inputScalar, std::string& desiredOutput)
inputScalar = {
"normal scalar, but\n" inputScalar =
"over several lines"; "normal scalar, but\n"
desiredOutput = "normal scalar, but over several lines"; "over several lines";
} desiredOutput = "normal scalar, but over several lines";
}
void LiteralScalar(std::string& inputScalar, std::string& desiredOutput)
{ void LiteralScalar(std::string& inputScalar, std::string& desiredOutput)
inputScalar = {
"|\n" inputScalar =
" literal scalar - so we can draw ASCII:\n" "|\n"
" \n" " literal scalar - so we can draw ASCII:\n"
" - -\n" " \n"
" | - |\n" " - -\n"
" -----\n"; " | - |\n"
desiredOutput = " -----\n";
"literal scalar - so we can draw ASCII:\n" desiredOutput =
"\n" "literal scalar - so we can draw ASCII:\n"
" - -\n" "\n"
" | - |\n" " - -\n"
" -----\n"; " | - |\n"
} " -----\n";
}
void FoldedScalar(std::string& inputScalar, std::string& desiredOutput)
{ void FoldedScalar(std::string& inputScalar, std::string& desiredOutput)
inputScalar = {
">\n" inputScalar =
" and a folded scalar... so we\n" ">\n"
" can just keep writing various\n" " and a folded scalar... so we\n"
" things. And if we want to keep indentation:\n" " can just keep writing various\n"
" \n" " things. And if we want to keep indentation:\n"
" we just indent a little\n" " \n"
" see, this stays indented"; " we just indent a little\n"
desiredOutput = " see, this stays indented";
"and a folded scalar... so we" desiredOutput =
" can just keep writing various" "and a folded scalar... so we"
" things. And if we want to keep indentation:\n" " can just keep writing various"
"\n" " things. And if we want to keep indentation:\n"
" we just indent a little\n" "\n"
" see, this stays indented"; " we just indent a little\n"
} " see, this stays indented";
}
void ChompedFoldedScalar(std::string& inputScalar, std::string& desiredOutput)
{ void ChompedFoldedScalar(std::string& inputScalar, std::string& desiredOutput)
inputScalar = {
">-\n" inputScalar =
" Here's a folded scalar\n" ">-\n"
" that gets chomped."; " Here's a folded scalar\n"
desiredOutput = " that gets chomped.";
"Here's a folded scalar" desiredOutput =
" that gets chomped."; "Here's a folded scalar"
} " that gets chomped.";
}
void ChompedLiteralScalar(std::string& inputScalar, std::string& desiredOutput)
{ void ChompedLiteralScalar(std::string& inputScalar, std::string& desiredOutput)
inputScalar = {
"|-\n" inputScalar =
" Here's a literal scalar\n" "|-\n"
" that gets chomped."; " Here's a literal scalar\n"
desiredOutput = " that gets chomped.";
"Here's a literal scalar\n" desiredOutput =
"that gets chomped."; "Here's a literal scalar\n"
} "that gets chomped.";
}
void FoldedScalarWithIndent(std::string& inputScalar, std::string& desiredOutput)
{ void FoldedScalarWithIndent(std::string& inputScalar, std::string& desiredOutput)
inputScalar = {
">2\n" inputScalar =
" Here's a folded scalar\n" ">2\n"
" that starts with some indentation."; " Here's a folded scalar\n"
desiredOutput = " that starts with some indentation.";
" Here's a folded scalar\n" desiredOutput =
"that starts with some indentation."; " Here's a folded scalar\n"
} "that starts with some indentation.";
}
void ColonScalar(std::string& inputScalar, std::string& desiredOutput)
{ void ColonScalar(std::string& inputScalar, std::string& desiredOutput)
inputScalar = "::vector"; {
desiredOutput = "::vector"; inputScalar = "::vector";
} desiredOutput = "::vector";
}
void QuotedScalar(std::string& inputScalar, std::string& desiredOutput)
{ void QuotedScalar(std::string& inputScalar, std::string& desiredOutput)
inputScalar = "\": - ()\""; {
desiredOutput = ": - ()"; inputScalar = "\": - ()\"";
} desiredOutput = ": - ()";
}
void CommaScalar(std::string& inputScalar, std::string& desiredOutput)
{ void CommaScalar(std::string& inputScalar, std::string& desiredOutput)
inputScalar = "Up, up, and away!"; {
desiredOutput = "Up, up, and away!"; inputScalar = "Up, up, and away!";
} desiredOutput = "Up, up, and away!";
}
void DashScalar(std::string& inputScalar, std::string& desiredOutput)
{ void DashScalar(std::string& inputScalar, std::string& desiredOutput)
inputScalar = "-123"; {
desiredOutput = "-123"; inputScalar = "-123";
} desiredOutput = "-123";
}
void URLScalar(std::string& inputScalar, std::string& desiredOutput)
{ void URLScalar(std::string& inputScalar, std::string& desiredOutput)
inputScalar = "http://example.com/foo#bar"; {
desiredOutput = "http://example.com/foo#bar"; inputScalar = "http://example.com/foo#bar";
} desiredOutput = "http://example.com/foo#bar";
}
bool SimpleSeq()
{ bool SimpleSeq()
std::string input = {
"- eggs\n" std::string input =
"- bread\n" "- eggs\n"
"- milk"; "- bread\n"
"- milk";
std::stringstream stream(input);
YAML::Parser parser(stream); std::stringstream stream(input);
YAML::Node doc; YAML::Parser parser(stream);
parser.GetNextDocument(doc); YAML::Node doc;
parser.GetNextDocument(doc);
std::string output;
doc[0] >> output; std::string output;
if(output != "eggs") doc[0] >> output;
return false; if(output != "eggs")
doc[1] >> output; return false;
if(output != "bread") doc[1] >> output;
return false; if(output != "bread")
doc[2] >> output; return false;
if(output != "milk") doc[2] >> output;
return false; if(output != "milk")
return false;
return true;
} return true;
}
bool SimpleMap()
{ bool SimpleMap()
std::string input = {
"name: Prince Fielder\n" std::string input =
"position: 1B\n" "name: Prince Fielder\n"
"bats: L"; "position: 1B\n"
"bats: L";
std::stringstream stream(input);
YAML::Parser parser(stream); std::stringstream stream(input);
YAML::Node doc; YAML::Parser parser(stream);
parser.GetNextDocument(doc); YAML::Node doc;
parser.GetNextDocument(doc);
std::string output;
doc["name"] >> output; std::string output;
if(output != "Prince Fielder") doc["name"] >> output;
return false; if(output != "Prince Fielder")
doc["position"] >> output; return false;
if(output != "1B") doc["position"] >> output;
return false; if(output != "1B")
doc["bats"] >> output; return false;
if(output != "L") doc["bats"] >> output;
return false; if(output != "L")
return false;
return true;
} return true;
}
bool FlowSeq()
{ bool FlowSeq()
std::string input = "[ 2 , 3, 5 , 7, 11]"; {
std::string input = "[ 2 , 3, 5 , 7, 11]";
std::stringstream stream(input);
YAML::Parser parser(stream); std::stringstream stream(input);
YAML::Node doc; YAML::Parser parser(stream);
parser.GetNextDocument(doc); YAML::Node doc;
parser.GetNextDocument(doc);
int output;
doc[0] >> output; int output;
if(output != 2) doc[0] >> output;
return false; if(output != 2)
doc[1] >> output; return false;
if(output != 3) doc[1] >> output;
return false; if(output != 3)
doc[2] >> output; return false;
if(output != 5) doc[2] >> output;
return false; if(output != 5)
doc[3] >> output; return false;
if(output != 7) doc[3] >> output;
return false; if(output != 7)
doc[4] >> output; return false;
if(output != 11) doc[4] >> output;
return false; if(output != 11)
return false;
return true;
} return true;
}
bool FlowMap()
{ bool FlowMap()
std::string input = "{hr: 65, avg: 0.278}"; {
std::string input = "{hr: 65, avg: 0.278}";
std::stringstream stream(input);
YAML::Parser parser(stream); std::stringstream stream(input);
YAML::Node doc; YAML::Parser parser(stream);
parser.GetNextDocument(doc); YAML::Node doc;
parser.GetNextDocument(doc);
std::string output;
doc["hr"] >> output; std::string output;
if(output != "65") doc["hr"] >> output;
return false; if(output != "65")
doc["avg"] >> output; return false;
if(output != "0.278") doc["avg"] >> output;
return false; if(output != "0.278")
return false;
return true;
} return true;
} }
}
bool QuotedSimpleKeys()
{
std::string KeyValue[3] = { "\"double\": double\n", "'single': single\n", "plain: plain\n" };
int perm[3] = { 0, 1, 2 };
do {
std::string input = KeyValue[perm[0]] + KeyValue[perm[1]] + KeyValue[perm[2]];
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
std::string output;
doc["double"] >> output;
if(output != "double")
return false;
doc["single"] >> output;
if(output != "single")
return false;
doc["plain"] >> output;
if(output != "plain")
return false;
} while(std::next_permutation(perm, perm + 3));
return true;
}
}
}

View File

@@ -262,6 +262,7 @@ namespace Test
RunParserTest(&Parser::SimpleMap, "simple map", passed); RunParserTest(&Parser::SimpleMap, "simple map", passed);
RunParserTest(&Parser::FlowSeq, "flow seq", passed); RunParserTest(&Parser::FlowSeq, "flow seq", passed);
RunParserTest(&Parser::FlowMap, "flow map", passed); RunParserTest(&Parser::FlowMap, "flow map", passed);
RunParserTest(&Parser::QuotedSimpleKeys, "quoted simple keys", passed);
RunEncodingTest(&EncodeToUtf8, false, "UTF-8, no BOM", passed); RunEncodingTest(&EncodeToUtf8, false, "UTF-8, no BOM", passed);
RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed); RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed);

View File

@@ -28,6 +28,7 @@ namespace Test {
bool SimpleMap(); bool SimpleMap();
bool FlowSeq(); bool FlowSeq();
bool FlowMap(); bool FlowMap();
bool QuotedSimpleKeys();
} }
namespace Emitter { namespace Emitter {