summaryrefslogtreecommitdiff
path: root/test/formats/osxml/OsxmlEventParserTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/formats/osxml/OsxmlEventParserTest.cpp')
-rw-r--r--test/formats/osxml/OsxmlEventParserTest.cpp99
1 files changed, 36 insertions, 63 deletions
diff --git a/test/formats/osxml/OsxmlEventParserTest.cpp b/test/formats/osxml/OsxmlEventParserTest.cpp
index 3293370..d4e9443 100644
--- a/test/formats/osxml/OsxmlEventParserTest.cpp
+++ b/test/formats/osxml/OsxmlEventParserTest.cpp
@@ -21,6 +21,7 @@
#include <core/frontend/TerminalLogger.hpp>
#include <core/common/CharReader.hpp>
#include <core/common/Variant.hpp>
+#include <core/parser/utils/TokenizedData.hpp>
#include <formats/osxml/OsxmlEventParser.hpp>
@@ -31,10 +32,10 @@ static TerminalLogger logger(std::cerr, true);
namespace {
enum class OsxmlEvent {
- COMMAND,
+ COMMAND_START,
ANNOTATION_START,
ANNOTATION_END,
- FIELD_END,
+ RANGE_END,
DATA
};
@@ -42,9 +43,10 @@ class TestOsxmlEventListener : public OsxmlEvents {
public:
std::vector<std::pair<OsxmlEvent, Variant>> events;
- void command(const Variant &name, const Variant::mapType &args) override
+ void commandStart(const Variant &name,
+ const Variant::mapType &args) override
{
- events.emplace_back(OsxmlEvent::COMMAND,
+ events.emplace_back(OsxmlEvent::COMMAND_START,
Variant::arrayType{name, args});
}
@@ -62,25 +64,30 @@ public:
Variant::arrayType{className, elementName});
}
- void fieldEnd() override
+ void rangeEnd() override
{
- events.emplace_back(OsxmlEvent::FIELD_END, Variant::arrayType{});
+ events.emplace_back(OsxmlEvent::RANGE_END, Variant::arrayType{});
}
- void data(const Variant &data) override
+ void data(const TokenizedData &data) override
{
- events.emplace_back(OsxmlEvent::DATA, Variant::arrayType{data});
+ Token token;
+ Variant text;
+ TokenizedDataReader reader = data.reader();
+ reader.read(token, TokenSet{}, WhitespaceMode::PRESERVE);
+ EXPECT_EQ(Tokens::Data, token.id);
+ text = Variant::fromString(token.content);
+ text.setLocation(token.getLocation());
+ events.emplace_back(OsxmlEvent::DATA, Variant::arrayType{text});
}
};
static std::vector<std::pair<OsxmlEvent, Variant>> parseXml(
- const char *testString,
- WhitespaceMode whitespaceMode = WhitespaceMode::TRIM)
+ const char *testString)
{
TestOsxmlEventListener listener;
CharReader reader(testString);
OsxmlEventParser parser(reader, listener, logger);
- parser.setWhitespaceMode(whitespaceMode);
parser.parse();
return listener.events;
}
@@ -93,11 +100,11 @@ TEST(OsxmlEventParser, simpleCommandWithArgs)
// 0 1 2 3
std::vector<std::pair<OsxmlEvent, Variant>> expectedEvents{
- {OsxmlEvent::COMMAND,
+ {OsxmlEvent::COMMAND_START,
Variant::arrayType{
"a", Variant::mapType{
{"name", "test"}, {"a", 1}, {"b", 2}, {"c", "blub"}}}},
- {OsxmlEvent::FIELD_END, Variant::arrayType{}}};
+ {OsxmlEvent::RANGE_END, Variant::arrayType{}}};
auto events = parseXml(testString);
ASSERT_EQ(expectedEvents, events);
@@ -133,10 +140,12 @@ TEST(OsxmlEventParser, magicTopLevelTag)
const char *testString = "<ousia><a/><b/></ousia>";
std::vector<std::pair<OsxmlEvent, Variant>> expectedEvents{
- {OsxmlEvent::COMMAND, Variant::arrayType{{"a", Variant::mapType{}}}},
- {OsxmlEvent::FIELD_END, Variant::arrayType{}},
- {OsxmlEvent::COMMAND, Variant::arrayType{{"b", Variant::mapType{}}}},
- {OsxmlEvent::FIELD_END, Variant::arrayType{}}};
+ {OsxmlEvent::COMMAND_START,
+ Variant::arrayType{{"a", Variant::mapType{}}}},
+ {OsxmlEvent::RANGE_END, Variant::arrayType{}},
+ {OsxmlEvent::COMMAND_START,
+ Variant::arrayType{{"b", Variant::mapType{}}}},
+ {OsxmlEvent::RANGE_END, Variant::arrayType{}}};
auto events = parseXml(testString);
ASSERT_EQ(expectedEvents, events);
@@ -147,71 +156,35 @@ TEST(OsxmlEventParser, magicTopLevelTagInside)
const char *testString = "<a><ousia/></a>";
std::vector<std::pair<OsxmlEvent, Variant>> expectedEvents{
- {OsxmlEvent::COMMAND, Variant::arrayType{{"a", Variant::mapType{}}}},
- {OsxmlEvent::COMMAND,
+ {OsxmlEvent::COMMAND_START,
+ Variant::arrayType{{"a", Variant::mapType{}}}},
+ {OsxmlEvent::COMMAND_START,
Variant::arrayType{{"ousia", Variant::mapType{}}}},
- {OsxmlEvent::FIELD_END, Variant::arrayType{}},
- {OsxmlEvent::FIELD_END, Variant::arrayType{}}};
+ {OsxmlEvent::RANGE_END, Variant::arrayType{}},
+ {OsxmlEvent::RANGE_END, Variant::arrayType{}}};
auto events = parseXml(testString);
ASSERT_EQ(expectedEvents, events);
}
-TEST(OsxmlEventParser, commandWithDataPreserveWhitespace)
+TEST(OsxmlEventParser, commandWithData)
{
const char *testString = "<a> hello \n world </a>";
// 012345678901 234567890123
// 0 1 2
std::vector<std::pair<OsxmlEvent, Variant>> expectedEvents{
- {OsxmlEvent::COMMAND, Variant::arrayType{"a", Variant::mapType{}}},
+ {OsxmlEvent::COMMAND_START,
+ Variant::arrayType{"a", Variant::mapType{}}},
{OsxmlEvent::DATA, Variant::arrayType{" hello \n world "}},
- {OsxmlEvent::FIELD_END, Variant::arrayType{}}};
+ {OsxmlEvent::RANGE_END, Variant::arrayType{}}};
- auto events = parseXml(testString, WhitespaceMode::PRESERVE);
+ auto events = parseXml(testString);
ASSERT_EQ(expectedEvents, events);
// Check the location of the text
ASSERT_EQ(3U, events[1].second.asArray()[0].getLocation().getStart());
ASSERT_EQ(20U, events[1].second.asArray()[0].getLocation().getEnd());
}
-
-TEST(OsxmlEventParser, commandWithDataTrimWhitespace)
-{
- const char *testString = "<a> hello \n world </a>";
- // 012345678901 234567890123
- // 0 1 2
-
- std::vector<std::pair<OsxmlEvent, Variant>> expectedEvents{
- {OsxmlEvent::COMMAND, Variant::arrayType{"a", Variant::mapType{}}},
- {OsxmlEvent::DATA, Variant::arrayType{"hello \n world"}},
- {OsxmlEvent::FIELD_END, Variant::arrayType{}}};
-
- auto events = parseXml(testString, WhitespaceMode::TRIM);
- ASSERT_EQ(expectedEvents, events);
-
- // Check the location of the text
- ASSERT_EQ(5U, events[1].second.asArray()[0].getLocation().getStart());
- ASSERT_EQ(19U, events[1].second.asArray()[0].getLocation().getEnd());
-}
-
-TEST(OsxmlEventParser, commandWithDataCollapseWhitespace)
-{
- const char *testString = "<a> hello \n world </a>";
- // 012345678901 234567890123
- // 0 1 2
-
- std::vector<std::pair<OsxmlEvent, Variant>> expectedEvents{
- {OsxmlEvent::COMMAND, Variant::arrayType{"a", Variant::mapType{}}},
- {OsxmlEvent::DATA, Variant::arrayType{"hello world"}},
- {OsxmlEvent::FIELD_END, Variant::arrayType{}}};
-
- auto events = parseXml(testString, WhitespaceMode::COLLAPSE);
- ASSERT_EQ(expectedEvents, events);
-
- // Check the location of the text
- ASSERT_EQ(5U, events[1].second.asArray()[0].getLocation().getStart());
- ASSERT_EQ(19U, events[1].second.asArray()[0].getLocation().getEnd());
-}
}