From 0b4711c86e97fa81265d0e0ef29027d5795ee8d9 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Thu, 1 Jan 2015 15:13:02 +0100 Subject: Adapted unit tests --- test/core/common/CharReaderTest.cpp | 82 ++++++++++++++++++------------------- test/core/common/LoggerTest.cpp | 46 ++++++++++----------- test/core/model/TypesystemTest.cpp | 16 ++++---- test/plugins/css/CSSParserTest.cpp | 2 +- test/plugins/xml/XmlParserTest.cpp | 22 +++++++--- 5 files changed, 88 insertions(+), 80 deletions(-) (limited to 'test') diff --git a/test/core/common/CharReaderTest.cpp b/test/core/common/CharReaderTest.cpp index 9c1026c..dc974b3 100644 --- a/test/core/common/CharReaderTest.cpp +++ b/test/core/common/CharReaderTest.cpp @@ -454,7 +454,7 @@ TEST(CharReader, simpleRead) ASSERT_EQ(testStr, res); // We must now be at line 1, column 15 - ASSERT_EQ(1U, reader.getLine()); + ASSERT_EQ(1, reader.getLine()); ASSERT_EQ(testStr.size() + 1, reader.getColumn()); // If we call either read or peek, false is returned @@ -483,14 +483,14 @@ TEST(CharReader, simplePeek) ASSERT_EQ(testStr, res); // We must now be at line 1, column 1 and NOT at the end of the stream - ASSERT_EQ(1U, reader.getLine()); - ASSERT_EQ(1U, reader.getColumn()); + ASSERT_EQ(1, reader.getLine()); + ASSERT_EQ(1, reader.getColumn()); ASSERT_FALSE(reader.atEnd()); // If we consume the peek, we must be at line 1, column 15 and we should be // at the end of the stream reader.consumePeek(); - ASSERT_EQ(1U, reader.getLine()); + ASSERT_EQ(1, reader.getLine()); ASSERT_EQ(testStr.size() + 1, reader.getColumn()); ASSERT_TRUE(reader.atEnd()); @@ -505,27 +505,27 @@ TEST(CharReader, rowColumnCounter) CharReader reader{"1\n\r2\n3\r\n\n4"}; // We should currently be in line 1, column 1 - ASSERT_EQ(1U, reader.getLine()); - ASSERT_EQ(1U, reader.getColumn()); + ASSERT_EQ(1, reader.getLine()); + ASSERT_EQ(1, reader.getColumn()); // Read two characters char c; for (int i = 0; i < 2; i++) reader.read(c); - ASSERT_EQ(2U, reader.getLine()); - ASSERT_EQ(1U, reader.getColumn()); + ASSERT_EQ(2, reader.getLine()); + ASSERT_EQ(1, reader.getColumn()); // Read two characters for (int i = 0; i < 2; i++) reader.read(c); - ASSERT_EQ(3U, reader.getLine()); - ASSERT_EQ(1U, reader.getColumn()); + ASSERT_EQ(3, reader.getLine()); + ASSERT_EQ(1, reader.getColumn()); // Read three characters for (int i = 0; i < 3; i++) reader.read(c); - ASSERT_EQ(5U, reader.getLine()); - ASSERT_EQ(1U, reader.getColumn()); + ASSERT_EQ(5, reader.getLine()); + ASSERT_EQ(1, reader.getColumn()); } TEST(CharReader, rowColumnCounterTest) @@ -534,27 +534,27 @@ TEST(CharReader, rowColumnCounterTest) CharReader reader{"1\n\r2\n3\r\n\n4", 4, 10}; // We should currently be in line 1, column 1 - ASSERT_EQ(4U, reader.getLine()); - ASSERT_EQ(10U, reader.getColumn()); + ASSERT_EQ(4, reader.getLine()); + ASSERT_EQ(10, reader.getColumn()); // Read two characters char c; for (int i = 0; i < 2; i++) reader.read(c); - ASSERT_EQ(5U, reader.getLine()); - ASSERT_EQ(1U, reader.getColumn()); + ASSERT_EQ(5, reader.getLine()); + ASSERT_EQ(1, reader.getColumn()); // Read two characters for (int i = 0; i < 2; i++) reader.read(c); - ASSERT_EQ(6U, reader.getLine()); - ASSERT_EQ(1U, reader.getColumn()); + ASSERT_EQ(6, reader.getLine()); + ASSERT_EQ(1, reader.getColumn()); // Read three characters for (int i = 0; i < 3; i++) reader.read(c); - ASSERT_EQ(8U, reader.getLine()); - ASSERT_EQ(1U, reader.getColumn()); + ASSERT_EQ(8, reader.getLine()); + ASSERT_EQ(1, reader.getColumn()); } TEST(CharReader, linebreakSubstitution) @@ -584,8 +584,8 @@ TEST(CharReader, rowColumnCounterUTF8) // The sequence above equals 5 UTF-8 characters (so after reading all the // cursor is at position 6) - ASSERT_EQ(1U, reader.getLine()); - ASSERT_EQ(6U, reader.getColumn()); + ASSERT_EQ(1, reader.getLine()); + ASSERT_EQ(6, reader.getColumn()); } TEST(CharReader, stream) @@ -626,8 +626,8 @@ TEST(CharReader, fork) { CharReaderFork fork = reader.fork(); - ASSERT_EQ(1U, fork.getLine()); - ASSERT_EQ(5U, fork.getColumn()); + ASSERT_EQ(1, fork.getLine()); + ASSERT_EQ(5, fork.getColumn()); fork.peek(c); ASSERT_EQ('i', c); @@ -635,11 +635,11 @@ TEST(CharReader, fork) fork.read(c); ASSERT_EQ('t', c); - ASSERT_EQ(1U, fork.getLine()); - ASSERT_EQ(6U, fork.getColumn()); + ASSERT_EQ(1, fork.getLine()); + ASSERT_EQ(6, fork.getColumn()); - ASSERT_EQ(1U, reader.getLine()); - ASSERT_EQ(5U, reader.getColumn()); + ASSERT_EQ(1, reader.getLine()); + ASSERT_EQ(5, reader.getColumn()); reader.read(c); reader.read(c); @@ -647,8 +647,8 @@ TEST(CharReader, fork) fork.commit(); } - ASSERT_EQ(1U, reader.getLine()); - ASSERT_EQ(6U, reader.getColumn()); + ASSERT_EQ(1, reader.getLine()); + ASSERT_EQ(6, reader.getColumn()); } TEST(CharReaderTest, context) @@ -660,7 +660,7 @@ TEST(CharReaderTest, context) // Retrieval at beginning of stream { CharReader reader{testStr}; - TextCursor::Context ctx = reader.getContext(80); + SourceContext ctx = reader.getContext(80); ASSERT_EQ("first line", ctx.text); ASSERT_EQ(0U, ctx.relPos); ASSERT_FALSE(ctx.truncatedStart); @@ -670,7 +670,7 @@ TEST(CharReaderTest, context) // Retrieval in middle of line { CharReader reader{testStr}; - TextCursor::Context ctx = reader.getContext(80); + SourceContext ctx = reader.getContext(80); char c; for (int i = 0; i < 5; i++) @@ -690,7 +690,7 @@ TEST(CharReaderTest, context) for (int i = 0; i < 11; i++) reader.read(c); - TextCursor::Context ctx = reader.getContext(80); + SourceContext ctx = reader.getContext(80); ASSERT_EQ("first line", ctx.text); ASSERT_EQ(10U, ctx.relPos); ASSERT_FALSE(ctx.truncatedStart); @@ -705,7 +705,7 @@ TEST(CharReaderTest, context) for (int i = 0; i < 5; i++) reader.read(c); - TextCursor::Context ctx = reader.getContext(3); + SourceContext ctx = reader.getContext(3); ASSERT_EQ("t l", ctx.text); ASSERT_EQ(1U, ctx.relPos); ASSERT_TRUE(ctx.truncatedStart); @@ -720,7 +720,7 @@ TEST(CharReaderTest, context) for (int i = 0; i < 12; i++) reader.read(c); - TextCursor::Context ctx = reader.getContext(80); + SourceContext ctx = reader.getContext(80); ASSERT_EQ("second line", ctx.text); ASSERT_EQ(0U, ctx.relPos); ASSERT_FALSE(ctx.truncatedStart); @@ -735,7 +735,7 @@ TEST(CharReaderTest, context) for (int i = 0; i < 23; i++) reader.read(c); - TextCursor::Context ctx = reader.getContext(80); + SourceContext ctx = reader.getContext(80); ASSERT_EQ("second line", ctx.text); ASSERT_EQ(11U, ctx.relPos); ASSERT_FALSE(ctx.truncatedStart); @@ -750,7 +750,7 @@ TEST(CharReaderTest, context) for (int i = 0; i < 24; i++) reader.read(c); - TextCursor::Context ctx = reader.getContext(80); + SourceContext ctx = reader.getContext(80); ASSERT_EQ("last line", ctx.text); ASSERT_EQ(0U, ctx.relPos); ASSERT_FALSE(ctx.truncatedStart); @@ -765,7 +765,7 @@ TEST(CharReaderTest, context) for (int i = 0; i < 28; i++) reader.read(c); - TextCursor::Context ctx = reader.getContext(80); + SourceContext ctx = reader.getContext(80); ASSERT_EQ("last line", ctx.text); ASSERT_EQ(4U, ctx.relPos); ASSERT_FALSE(ctx.truncatedStart); @@ -780,7 +780,7 @@ TEST(CharReaderTest, context) for (int i = 0; i < 28; i++) reader.read(c); - TextCursor::Context ctx = reader.getContext(3); + SourceContext ctx = reader.getContext(3); ASSERT_EQ("t l", ctx.text); ASSERT_EQ(1U, ctx.relPos); ASSERT_TRUE(ctx.truncatedStart); @@ -795,7 +795,7 @@ TEST(CharReaderTest, context) for (int i = 0; i < 100; i++) reader.read(c); - TextCursor::Context ctx = reader.getContext(80); + SourceContext ctx = reader.getContext(80); ASSERT_EQ("last line", ctx.text); ASSERT_EQ(9U, ctx.relPos); ASSERT_FALSE(ctx.truncatedStart); @@ -810,7 +810,7 @@ TEST(CharReaderTest, context) for (int i = 0; i < 100; i++) reader.read(c); - TextCursor::Context ctx = reader.getContext(4); + SourceContext ctx = reader.getContext(4); ASSERT_EQ("line", ctx.text); ASSERT_EQ(4U, ctx.relPos); ASSERT_TRUE(ctx.truncatedStart); diff --git a/test/core/common/LoggerTest.cpp b/test/core/common/LoggerTest.cpp index ee1fb6f..66e49cd 100644 --- a/test/core/common/LoggerTest.cpp +++ b/test/core/common/LoggerTest.cpp @@ -25,38 +25,40 @@ namespace ousia { struct Pos { - TextCursor::Position pos; - TextCursor::Context ctx; + SourceLocation pos; - Pos(TextCursor::Position pos = TextCursor::Position{}, - TextCursor::Context ctx = TextCursor::Context{}) - : pos(pos), ctx(ctx){}; + Pos(SourceLocation pos = SourceLocation{}) + : pos(pos) {}; - TextCursor::Position getPosition() { return pos; } - TextCursor::Context getContext() { return ctx; } + SourceLocation getLocation() { return pos; } }; +static SourceContext contextCallback(const SourceLocation &location, + void *) +{ + return SourceContext{"int bla = blub;", 10, true, false}; +} + TEST(TerminalLogger, log) { // Test for manual visual expection only -- no assertions TerminalLogger logger{std::cerr, true}; logger.pushFile("test.odp"); - logger.debug("This is a test debug message", TextCursor::Position{10, 20}); + logger.debug("This is a test debug message", SourceLocation{10, 20}); logger.debug("This is a test debug message with no column", - TextCursor::Position{10}); + SourceLocation{10}); logger.debug("This is a test debug message with no line"); - logger.note("This is a test note", TextCursor::Position{10, 20}); - logger.warning("This is a test warning", TextCursor::Position{10, 20}); - logger.error("This is a test error", TextCursor::Position{10, 20}); - logger.fatalError("This is a test fatal error!", - TextCursor::Position{10, 20}); + logger.note("This is a test note", SourceLocation{10, 20}); + logger.warning("This is a test warning", SourceLocation{10, 20}); + logger.error("This is a test error", SourceLocation{10, 20}); + logger.fatalError("This is a test fatal error!", SourceLocation{10, 20}); - logger.error("This is a test error with context", - TextCursor::Position{10, 20}, - TextCursor::Context{"int bla = blub;", 10, true, false}); + logger.pushFile("test2.odp", SourceLocation{}, contextCallback); + logger.error("This is a test error with context", SourceLocation{10, 20}); + logger.popFile(); - Pos pos(TextCursor::Position{10, 20}); + Pos pos(SourceLocation{10, 20}); try { throw LoggableException{"An exception"}; @@ -82,10 +84,8 @@ TEST(TerminalLogger, fork) LoggerFork fork = logger.fork(); - fork.pushFile("test.odp"); - fork.error("This is a test error with context", - TextCursor::Position{10, 20}, - TextCursor::Context{"int bla = blub;", 10, true, false}); + fork.pushFile("test.odp", SourceLocation{}, contextCallback); + fork.error("This is a test error with context", SourceLocation{10, 20}); fork.pushFile("test2.odp"); fork.error("This is a test error without context"); fork.popFile(); @@ -96,7 +96,5 @@ TEST(TerminalLogger, fork) // Print all error messages fork.commit(); } - - } diff --git a/test/core/model/TypesystemTest.cpp b/test/core/model/TypesystemTest.cpp index b2f2600..29ab5ec 100644 --- a/test/core/model/TypesystemTest.cpp +++ b/test/core/model/TypesystemTest.cpp @@ -27,7 +27,7 @@ namespace ousia { namespace model { static TerminalLogger logger(std::cerr, true); -//static Logger logger; +//static ConcreteLogger logger; /* Class StringType */ @@ -318,28 +318,28 @@ TEST(EnumType, createValidated) Manager mgr; { - logger.resetMaxEncounteredSeverity(); + logger.reset(); Rooted enumType{EnumType::createValidated( mgr, "enum", nullptr, {"a", "b", "c"}, logger)}; ASSERT_EQ(Severity::DEBUG, logger.getMaxEncounteredSeverity()); } { - logger.resetMaxEncounteredSeverity(); + logger.reset(); Rooted enumType{EnumType::createValidated( mgr, "enum", nullptr, {"a", "a", "c"}, logger)}; ASSERT_EQ(Severity::ERROR, logger.getMaxEncounteredSeverity()); } { - logger.resetMaxEncounteredSeverity(); + logger.reset(); Rooted enumType{ EnumType::createValidated(mgr, "enum", nullptr, {}, logger)}; ASSERT_EQ(Severity::ERROR, logger.getMaxEncounteredSeverity()); } { - logger.resetMaxEncounteredSeverity(); + logger.reset(); Rooted enumType{ EnumType::createValidated(mgr, "enum", nullptr, {"a a"}, logger)}; ASSERT_EQ(Severity::ERROR, logger.getMaxEncounteredSeverity()); @@ -483,7 +483,7 @@ TEST(StructType, createValidated) Rooted intType{new IntType(mgr, nullptr)}; { - logger.resetMaxEncounteredSeverity(); + logger.reset(); Rooted structType{StructType::createValidated( mgr, "struct", nullptr, nullptr, NodeVector{ @@ -496,7 +496,7 @@ TEST(StructType, createValidated) } { - logger.resetMaxEncounteredSeverity(); + logger.reset(); Rooted structType{StructType::createValidated( mgr, "struct", nullptr, nullptr, NodeVector{ @@ -509,7 +509,7 @@ TEST(StructType, createValidated) } { - logger.resetMaxEncounteredSeverity(); + logger.reset(); Rooted structType{StructType::createValidated( mgr, "struct", nullptr, nullptr, NodeVector{ diff --git a/test/plugins/css/CSSParserTest.cpp b/test/plugins/css/CSSParserTest.cpp index e043f60..99e6424 100644 --- a/test/plugins/css/CSSParserTest.cpp +++ b/test/plugins/css/CSSParserTest.cpp @@ -276,7 +276,7 @@ void assertException(std::string css) try { instance.parse(input, ctx).cast(); } - catch (ParserException ex) { + catch (LoggableException ex) { logger.log(ex); seenException = true; } diff --git a/test/plugins/xml/XmlParserTest.cpp b/test/plugins/xml/XmlParserTest.cpp index 39b1a9d..c5a7b10 100644 --- a/test/plugins/xml/XmlParserTest.cpp +++ b/test/plugins/xml/XmlParserTest.cpp @@ -20,12 +20,16 @@ #include +#include + #include namespace ousia { namespace parser { namespace xml { +TerminalLogger logger(std::cerr, true); + TEST(XmlParser, mismatchedTagException) { StandaloneParserContext ctx; @@ -35,8 +39,8 @@ TEST(XmlParser, mismatchedTagException) try { p.parse("\n", ctx); } - catch (ParserException ex) { - ASSERT_EQ(2U, ex.pos.line); + catch (LoggableException ex) { + ASSERT_EQ(2U, ex.loc.line); hadException = true; } ASSERT_TRUE(hadException); @@ -47,8 +51,10 @@ const char *TEST_DATA = "\n" " \n" " \n" - " \n" - " \n" + " \n" + " \n" + " \n" + " \n" " \n" " \n" " \n" @@ -58,10 +64,14 @@ const char *TEST_DATA = TEST(XmlParser, namespaces) { - StandaloneParserContext ctx; + StandaloneParserContext ctx(logger); XmlParser p; - p.parse(TEST_DATA, ctx); + try { + p.parse(TEST_DATA, ctx); + } catch(LoggableException ex) { + logger.log(ex); + } } } } -- cgit v1.2.3