From: Mike D. Lowis Date: Wed, 4 Apr 2012 18:15:25 +0000 (-0400) Subject: 100% coverage of dllexer X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=6963f4944aca812de1cb49b74ca360b8e6fdeb86;p=archive%2Fdlang.git 100% coverage of dllexer --- diff --git a/rakefile.rb b/rakefile.rb index cb67e43..2eec605 100644 --- a/rakefile.rb +++ b/rakefile.rb @@ -132,6 +132,7 @@ task :coverage do #=> [ :test ] do end # For each test file + puts "" tests.each { |test| # Find the source file basename, object file location, and gcov output source = File.basename( test ).gsub( 'test_', '' ) @@ -140,7 +141,8 @@ task :coverage do #=> [ :test ] do # Generate the coverage info and display only the summary for our # source file - sh "gcov -o build/test/obj #{obj} | grep -A1 #{source}" + puts "----" + sh "gcov -b -o build/test/obj #{obj} | grep -A 4 \"#{source}'\"" # Move the coverage to our output folder FileUtils.mv( gcov, out_dir ) @@ -148,4 +150,5 @@ task :coverage do #=> [ :test ] do # Delete the unwanted coverage files FileList['*.gcov'].each { |f| File.delete(f) } } + puts "" end diff --git a/source/dllexer/dllexer.cpp b/source/dllexer/dllexer.cpp index 4d9efe1..0ff7ad9 100644 --- a/source/dllexer/dllexer.cpp +++ b/source/dllexer/dllexer.cpp @@ -184,7 +184,7 @@ void DLLexer::Number(Token& tok, bool isNegative) else { Exception ex(line,column); - ex << "Integer for floating point exponent"; + ex << "Missing integer for floating point exponent"; throw ex; } } @@ -404,7 +404,8 @@ void DLLexer::MultiCharOp(Token& tok) tok = Token(MAP, line, column); } } - else + + if (tok.type() == EOF) { Exception ex(line,column); ex << "Unexpected token"; @@ -415,36 +416,12 @@ void DLLexer::MultiCharOp(Token& tok) std::string DLLexer::EscapeSequence() { ostringstream oss; - + // consume backslash + oss << lookahead(1); + consume(); + // consume escaped char oss << lookahead(1); consume(); - - if ( lookahead(1) == 'x' ) - { - oss << lookahead(1); - consume(); - for(int i = 0; i < 2; i++) - { - if ( ((lookahead(1) >= '0') || (lookahead(1) <= '9')) || - ((lookahead(1) >= 'a') || (lookahead(1) <= 'f')) || - ((lookahead(1) >= 'A') || (lookahead(1) <= 'F'))) - { - oss << lookahead(1); - consume(); - } - else - { - Exception ex(line,column); - ex << "Invalid hexadecimal escape sequence."; - throw ex; - } - } - } - else - { - oss << lookahead(1); - consume(); - } return oss.str(); } diff --git a/tests/test_dllexer.cpp b/tests/test_dllexer.cpp index 1fcb957..ed1b86f 100644 --- a/tests/test_dllexer.cpp +++ b/tests/test_dllexer.cpp @@ -120,12 +120,28 @@ namespace { TEST(Recognize_Valid_Characters) { - CHECK(false); + std::string input( + // Make Sure we recognize characters and escaped characters + "'a' '\\a'" + ); + eTokenTypes expected[] = { + CHAR, CHAR, (eTokenTypes)EOF + }; + TestLexerWithInput( input, expected ); } TEST(Recognize_Valid_Strings) { - CHECK(false); + std::string input( + // Make Sure we recognize all valid characters for a symbol + "\"\" \n" + "\"a\" \n" + "\"\\a\" \n" + ); + eTokenTypes expected[] = { + STRING, STRING, STRING, (eTokenTypes)EOF + }; + TestLexerWithInput( input, expected ); } TEST(Recognize_Valid_Symbols) @@ -149,17 +165,40 @@ namespace { { std::string input( // Recognize single character operators - "[ ] ( ) { } , + * / . %" + "[ ] ( ) { } , + * / . % - " // Recognize multi character operators and similar single char ones "= == ! != < <= > >= | || && : := @ @=" ); eTokenTypes expected[] = { LBRACK, RBRACK, LPAR, RPAR, LBRACE, RBRACE, COMMA, ADD, MUL, DIV, - MEMB, MACRO, ASSIGN, EQ, NOT, NE, LT, LTE, GT, GTE, PIPE, OR, AND, - SEP, DEFN, MAP, IMPORT, + MEMB, MACRO, SUB, ASSIGN, EQ, NOT, NE, LT, LTE, GT, GTE, PIPE, OR, + AND, SEP, DEFN, MAP, IMPORT, (eTokenTypes)EOF }; TestLexerWithInput( input, expected ); } + TEST(Throw_Exceptions_For_Exceptional_Cases) + { + // Make sure invalud number literals throw exceptions where appropriate + std::string num_exception1("1.0e-"); + TestLexerThrowsException( num_exception1 ); + + std::string num_exception2("1.0e-a"); + TestLexerThrowsException( num_exception2 ); + + std::string num_exception3("1.0e-"); + TestLexerThrowsException( num_exception3 ); + + std::string num_exception4("1.a"); + TestLexerThrowsException( num_exception4 ); + + // Make sure we throw an exception for an invalid operator + std::string op_exception1("^"); + TestLexerThrowsException( op_exception1 ); + + // Make sure we throw an exception for an invalid operator + std::string multi_op_exception1("&"); + TestLexerThrowsException( multi_op_exception1 ); + } }