]> git.mdlowis.com Git - archive/dlang.git/commitdiff
Added unit tests for dlparser
authorMike D. Lowis <mike@mdlowis.com>
Thu, 7 Jun 2012 20:56:47 +0000 (16:56 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Thu, 7 Jun 2012 20:56:47 +0000 (16:56 -0400)
source/dlparser/dlparser.cpp
tests/test_dlparser.cpp
tools/UnitTest++/Makefile

index 8c1be4b6e7a19d9633447569818acdc157fc6499..95ebc9f9fc31a88b6608432bdbfc60569bace54b 100644 (file)
@@ -12,7 +12,7 @@ DLParser::DLParser() : BTParser()
     core_forms["if"]     = IF;
     core_forms["begin"]  = BEGIN;
     core_forms["quote"]  = QUOTE;
-    core_forms["lambda"] = LAMBDA;
+    core_forms["func"] = LAMBDA;
     core_forms["macro"]  = MACRO;
     core_forms["syntax"] = SYNTAX;
 }
index f9e372de7ac4f8362be7b086d8d79c4375f8ac1f..a8d3ca757e390c2502bd2a7790f69816bb3e126e 100644 (file)
@@ -3,8 +3,10 @@
 
 // Supporting Includes
 #include <sstream>
+#include <list>
 #include "exception.h"
 #include "ivisitor.h"
+#include "astprinter.h"
 
 // File To Test
 #include "dlparser.h"
@@ -14,31 +16,202 @@ using namespace UnitTest;
 //-----------------------------------------------------------------------------
 // Helper Functions
 //-----------------------------------------------------------------------------
-class TreeTester : public IVisitor {
-    public:
-        eTokenTypes* expected_types;
-        unsigned int cur_index;
-        TreeTester(eTokenTypes expected[]) : IVisitor(), expected_types(expected), cur_index(0) {}
-    private:
-        void afterChildren(AST* cur, int depth) {
-            CHECK_EQUAL( expected_types[ cur_index ], cur->type() );
-            cur_index = (expected_types[cur_index] == PROGRAM) ? cur_index : (cur_index + 1);
+#define CHECK_AST(left,right) CheckASTsForEquality( left, right, __LINE__ )
+void CheckASTsForEquality(AST* left, AST* right, int line)
+{
+    bool result = ( (NULL != left) && (NULL != right) );
+    if( true == result )
+    {
+        result = (bool)( (*left) == (*right) );
+        if( !result )
+        {
+            ASTPrinter printer;
+            UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), line),
+                    "Syntax trees are not equal in CHECK_AST( left ,  right )");
+            std::cout << "Printing left tree:" << std::endl;
+            (*left).process( printer );
+            std::cout << "Printing right tree:" << std::endl;
+            (*right).process( printer );
         }
-
-        // Not Used Here
-        void beforeVisit(AST* cur, int depth) {}
-        void afterVisit(AST* cur, int depth) {}
-        void beforeChildren(AST* cur, int depth) {}
-        void beforeChild(AST* cur, int depth) {}
-        void afterChild(AST* cur, int depth) {}
-};
-
-//-----------------------------------------------------------------------------
-// Helper Functions
-//-----------------------------------------------------------------------------
+    }
+    else
+    {
+        UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), line),
+                "At least one of the given syntax trees is NULL");
+    }
+}
 
 //-----------------------------------------------------------------------------
 // Begin Unit Tests
 //-----------------------------------------------------------------------------
 namespace {
+    //-------------------------------------------------------------------------
+    // Test isSyntaxName
+    //-------------------------------------------------------------------------
+    TEST(Verify_isSyntaxName_recognizes_a_registered_syntax)
+    {
+        DLParser parser;
+        std::istringstream input("foobar");
+        parser.input(new DLLexer(input));
+        CHECK( true == parser.isSyntaxName() );
+    }
+
+    TEST(Verify_isSyntaxName_returns_false_if_token_is_not_a_registered_syntax)
+    {
+        DLParser parser;
+        std::istringstream input("foobar");
+        parser.input(new DLLexer(input));
+        CHECK( false == parser.isSyntaxName() );
+    }
+
+    //-------------------------------------------------------------------------
+    // Test isCoreFormName
+    //-------------------------------------------------------------------------
+    TEST(Verify_isCoreFormName_Recognizes_def_form)
+    {
+        DLParser parser;
+        std::istringstream input("def");
+        parser.input(new DLLexer(input));
+        CHECK( true == parser.isCoreFormName() );
+    }
+
+    TEST(Verify_isCoreFormName_Recognizes_set_form)
+    {
+        DLParser parser;
+        std::istringstream input("set");
+        parser.input(new DLLexer(input));
+        CHECK( true == parser.isCoreFormName() );
+    }
+
+    TEST(Verify_isCoreFormName_Recognizes_if_form)
+    {
+        DLParser parser;
+        std::istringstream input("if");
+        parser.input(new DLLexer(input));
+        CHECK( true == parser.isCoreFormName() );
+    }
+
+    TEST(Verify_isCoreFormName_Recognizes_begin_form)
+    {
+        DLParser parser;
+        std::istringstream input("begin");
+        parser.input(new DLLexer(input));
+        CHECK( true == parser.isCoreFormName() );
+    }
+
+    TEST(Verify_isCoreFormName_Recognizes_quote_form)
+    {
+        DLParser parser;
+        std::istringstream input("quote");
+        parser.input(new DLLexer(input));
+        CHECK( true == parser.isCoreFormName() );
+    }
+
+    TEST(Verify_isCoreFormName_Recognizes_lambda_form)
+    {
+        DLParser parser;
+        std::istringstream input("func");
+        parser.input(new DLLexer(input));
+        CHECK( true == parser.isCoreFormName() );
+    }
+
+    TEST(Verify_isCoreFormName_Recognizes_macro_form)
+    {
+        DLParser parser;
+        std::istringstream input("macro");
+        parser.input(new DLLexer(input));
+        CHECK( true == parser.isCoreFormName() );
+    }
+
+    TEST(Verify_isCoreFormName_Recognizes_syntax_form)
+    {
+        DLParser parser;
+        std::istringstream input("syntax");
+        parser.input(new DLLexer(input));
+        CHECK( true == parser.isCoreFormName() );
+    }
+
+    TEST(Verify_isCoreFormName_Recognizes_a_non_core_form)
+    {
+        DLParser parser;
+        std::istringstream input("foobar");
+        parser.input(new DLLexer(input));
+        CHECK( false == parser.isCoreFormName() );
+    }
+
+    //-------------------------------------------------------------------------
+    // Test getCoreFormId
+    //-------------------------------------------------------------------------
+    TEST(Verify_getCoreFormId_Recognizes_def_form)
+    {
+        DLParser parser;
+        std::istringstream input("def");
+        parser.input(new DLLexer(input));
+        CHECK( DEFINE == parser.getCoreFormId() );
+    }
+
+    TEST(Verify_getCoreFormId_Recognizes_set_form)
+    {
+        DLParser parser;
+        std::istringstream input("set");
+        parser.input(new DLLexer(input));
+        CHECK( ASSIGN == parser.getCoreFormId() );
+    }
+
+    TEST(Verify_getCoreFormId_Recognizes_if_form)
+    {
+        DLParser parser;
+        std::istringstream input("if");
+        parser.input(new DLLexer(input));
+        CHECK( IF == parser.getCoreFormId() );
+    }
+
+    TEST(Verify_getCoreFormId_Recognizes_begin_form)
+    {
+        DLParser parser;
+        std::istringstream input("begin");
+        parser.input(new DLLexer(input));
+        CHECK( BEGIN == parser.getCoreFormId() );
+    }
+
+    TEST(Verify_getCoreFormId_Recognizes_quote_form)
+    {
+        DLParser parser;
+        std::istringstream input("quote");
+        parser.input(new DLLexer(input));
+        CHECK( QUOTE == parser.getCoreFormId() );
+    }
+
+    TEST(Verify_getCoreFormId_Recognizes_lambda_form)
+    {
+        DLParser parser;
+        std::istringstream input("func");
+        parser.input(new DLLexer(input));
+        CHECK( LAMBDA == parser.getCoreFormId() );
+    }
+
+    TEST(Verify_getCoreFormId_Recognizes_macro_form)
+    {
+        DLParser parser;
+        std::istringstream input("macro");
+        parser.input(new DLLexer(input));
+        CHECK( MACRO == parser.getCoreFormId() );
+    }
+
+    TEST(Verify_getCoreFormId_Recognizes_syntax_form)
+    {
+        DLParser parser;
+        std::istringstream input("syntax");
+        parser.input(new DLLexer(input));
+        CHECK( SYNTAX == parser.getCoreFormId() );
+    }
+
+    TEST(Verify_getCoreFormId_Recognizes_a_non_core_form)
+    {
+        DLParser parser;
+        std::istringstream input("foobar");
+        parser.input(new DLLexer(input));
+        //CHECK( false == parser.getCoreFormId() );
+        CHECK( false );
+    }
 }
index fa4fb0d3bdedb4acdde916577678a4da423bcaa0..0476af5a5e96a70ca5f3312c1b10b93a21b1627c 100644 (file)
@@ -82,7 +82,7 @@ $(test): $(lib) $(test_objects)
        @./$(test)
 
 clean:
-       -@$(RM) $(objects) $(test_objects) $(dependencies) $(test_dependencies) $(test) $(lib) 2> /dev/null
+       -@$(RM) -f $(objects) $(test_objects) $(dependencies) $(test_dependencies) $(test) $(lib) 2> /dev/null
 
 %.o : %.cpp
        @echo $<