]> git.mdlowis.com Git - archive/dlang.git/commitdiff
Added unit test suite for lexer and fixed bug in the comment recognizer
authorMike D. Lowis <mike@mdlowis.com>
Tue, 3 Apr 2012 19:38:04 +0000 (15:38 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Tue, 3 Apr 2012 19:38:04 +0000 (15:38 -0400)
source/dllexer/dllexer.cpp
tests/test_dllexer.cpp [new file with mode: 0644]

index 71bfe304bb12e851eb1d5af3ae289e237e465802..4d9efe1e118e739eecdafd26e711ab574ae8d694 100644 (file)
@@ -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 (file)
index 0000000..03b4899
--- /dev/null
@@ -0,0 +1,121 @@
+// Unit Test Framework Includes
+#include "UnitTest++.h"
+
+// Supporting Includes
+#include <sstream>
+#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);
+    }
+
+}