From 2e6ae00a4e7bcf497502bacbddb8752ee68add99 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Thu, 7 Jun 2012 16:56:47 -0400 Subject: [PATCH] Added unit tests for dlparser --- source/dlparser/dlparser.cpp | 2 +- tests/test_dlparser.cpp | 215 +++++++++++++++++++++++++++++++---- tools/UnitTest++/Makefile | 2 +- 3 files changed, 196 insertions(+), 23 deletions(-) diff --git a/source/dlparser/dlparser.cpp b/source/dlparser/dlparser.cpp index 8c1be4b..95ebc9f 100644 --- a/source/dlparser/dlparser.cpp +++ b/source/dlparser/dlparser.cpp @@ -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; } diff --git a/tests/test_dlparser.cpp b/tests/test_dlparser.cpp index f9e372d..a8d3ca7 100644 --- a/tests/test_dlparser.cpp +++ b/tests/test_dlparser.cpp @@ -3,8 +3,10 @@ // Supporting Includes #include +#include #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 ); + } } diff --git a/tools/UnitTest++/Makefile b/tools/UnitTest++/Makefile index fa4fb0d..0476af5 100644 --- a/tools/UnitTest++/Makefile +++ b/tools/UnitTest++/Makefile @@ -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 $< -- 2.52.0