From: Mike D. Lowis Date: Tue, 3 Apr 2012 19:38:04 +0000 (-0400) Subject: Added unit test suite for lexer and fixed bug in the comment recognizer X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=4a24a61c046f85def5e7bd94e0b19f4dc5343811;p=archive%2Fdlang.git Added unit test suite for lexer and fixed bug in the comment recognizer --- diff --git a/source/dllexer/dllexer.cpp b/source/dllexer/dllexer.cpp index 71bfe30..4d9efe1 100644 --- a/source/dllexer/dllexer.cpp +++ b/source/dllexer/dllexer.cpp @@ -131,13 +131,11 @@ void DLLexer::WS(void) void DLLexer::COMMENT(void) { match('#'); - do + while( (lookahead(1) != '\n') + && (lookahead(1) != EOF)) { consume(); } - while( (lookahead(1) != '\n') - && (lookahead(1) != EOF)); - } void DLLexer::Id(Token& tok) diff --git a/tests/test_dllexer.cpp b/tests/test_dllexer.cpp new file mode 100644 index 0000000..03b4899 --- /dev/null +++ b/tests/test_dllexer.cpp @@ -0,0 +1,121 @@ +// Unit Test Framework Includes +#include "UnitTest++.h" + +// Supporting Includes +#include +#include "exception.h" + +// File To Test +#include "dllexer.h" + +using namespace UnitTest; + +//----------------------------------------------------------------------------- +// Helper Functions +//----------------------------------------------------------------------------- +void TestLexerWithInput(std::string& input, eTokenTypes expected_types[]) +{ + // Setup + std::istringstream input_stream(input); + DLLexer* lexer = new DLLexer(input_stream); + int i = 0; + Token tok; + + // Compare tokens + do + { + tok = lexer->next(); + CHECK_EQUAL( expected_types[i], tok.type() ); + if( tok.type() != expected_types[i] ) + { + std::cout << "Test failed at index " << i << "." << std::endl; + break; + } + i++; + } + while(tok.type() != EOF); + + // Cleanup + delete lexer; +} + +void TestLexerThrowsException(std::string& input) +{ + // Setup + std::istringstream input_stream(input); + DLLexer* lexer = new DLLexer(input_stream); + + CHECK_THROW( lexer->next(), Exception ); + + // Cleanup + delete lexer; +} + +//----------------------------------------------------------------------------- +// Begin Unit Tests +//----------------------------------------------------------------------------- +namespace { + + TEST(Recognize_And_Ignore_Whitespace) + { + std::string input("foo \t\r\n foo"); + eTokenTypes expected[] = { ID, ID, (eTokenTypes)EOF }; + TestLexerWithInput( input, expected ); + } + + TEST(Recognize_And_Ignore_Comments) + { + std::string input( + "foo # Comment after valid token\r\n" + "# Comment on a line by itself\r\n" + "# Comment terminated by only a newline\n" + "bar\n" + "#\n" // An Empty comment + "foo" + "#" // A comment at the end of the file + ); + eTokenTypes expected[] = { ID, ID, ID, (eTokenTypes)EOF }; + TestLexerWithInput( input, expected ); + } + + TEST(Recognize_Valid_IDs) + { + std::string input( + // Make Sure we recognize all valid characters for an ID + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_\n" + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_\n" + "a_123\n" + "a123\n" + "a_\n" + "a_a\n" + ); + eTokenTypes expected[] = { ID, ID, ID, ID, ID, ID, (eTokenTypes)EOF }; + TestLexerWithInput( input, expected ); + } + + TEST(Recognize_Valid_Numbers) + { + CHECK(false); + } + + TEST(Recognize_Valid_Characters) + { + CHECK(false); + } + + TEST(Recognize_Valid_Strings) + { + CHECK(false); + } + + TEST(Recognize_Valid_Symbols) + { + CHECK(false); + } + + TEST(Recognize_Valid_Operators) + { + CHECK(false); + } + +}