refactoring testcases for Tokenizer::simplifyAddBraces

This commit is contained in:
Daniel Marjamäki 2009-06-20 13:20:51 +02:00
parent af994d23e1
commit 13b8f10906
1 changed files with 41 additions and 73 deletions

View File

@ -80,6 +80,8 @@ private:
TEST_CASE(ifAddBraces5); TEST_CASE(ifAddBraces5);
TEST_CASE(ifAddBraces6); TEST_CASE(ifAddBraces6);
TEST_CASE(whileAddBraces);
TEST_CASE(numeric_true_condition); TEST_CASE(numeric_true_condition);
TEST_CASE(simplifyKnownVariables1); TEST_CASE(simplifyKnownVariables1);
@ -365,18 +367,11 @@ private:
" if (a);\n" " if (a);\n"
" else ;\n" " else ;\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( )\n"
// tokenize.. "{\n"
OurTokenizer tokenizer; "if ( a ) { ; }\n"
std::istringstream istr(code); "else { ; }\n"
tokenizer.tokenize(istr, "test.cpp"); "}", tokenizeAndStringify(code, true));
ASSERT_EQUALS(true, tokenizer.simplifyIfAddBraces());
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void f ( ) { if ( a ) { ; } else { ; } }", ostr.str());
} }
void ifAddBraces2() void ifAddBraces2()
@ -385,18 +380,10 @@ private:
"{\n" "{\n"
" if (a) if (b) { }\n" " if (a) if (b) { }\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( )\n"
// tokenize.. "{\n"
OurTokenizer tokenizer; "if ( a ) { if ( b ) { } }\n"
std::istringstream istr(code); "}", tokenizeAndStringify(code, true));
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS(true, tokenizer.simplifyIfAddBraces());
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void f ( ) { if ( a ) { if ( b ) { } } }", ostr.str());
} }
void ifAddBraces3() void ifAddBraces3()
@ -405,18 +392,10 @@ private:
"{\n" "{\n"
" if (a) for (;;) { }\n" " if (a) for (;;) { }\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( )\n"
// tokenize.. "{\n"
OurTokenizer tokenizer; "if ( a ) { for ( ; ; ) { } }\n"
std::istringstream istr(code); "}", tokenizeAndStringify(code, true));
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS(true, tokenizer.simplifyIfAddBraces());
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void f ( ) { if ( a ) { for ( ; ; ) { } } }", ostr.str());
} }
void ifAddBraces4() void ifAddBraces4()
@ -429,19 +408,14 @@ private:
" { }\n" " { }\n"
" return str;\n" " return str;\n"
"}\n"; "}\n";
ASSERT_EQUALS("char * foo ( )\n"
"{\n"
// tokenize.. "char * str ; str = malloc ( 10 ) ;\n"
OurTokenizer tokenizer; "if ( somecondition ) {\n"
std::istringstream istr(code); "for ( ; ; )\n"
tokenizer.tokenize(istr, "test.cpp"); "{ } }\n"
"return str ;\n"
ASSERT_EQUALS(true, tokenizer.simplifyIfAddBraces()); "}", tokenizeAndStringify(code, true));
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" char * foo ( ) { char * str ; str = malloc ( 10 ) ; if ( somecondition ) { for ( ; ; ) { } } return str ; }", ostr.str());
} }
void ifAddBraces5() void ifAddBraces5()
@ -455,37 +429,31 @@ private:
"return;\n" "return;\n"
"}\n"; "}\n";
ASSERT_EQUALS("void f ( )\n"
// tokenize.. "{\n"
OurTokenizer tokenizer; "for ( int i = 0 ; i < 2 ; i ++ ) {\n"
std::istringstream istr(code); "{\n"
tokenizer.tokenize(istr, "test.cpp"); "return ; } }\n\n"
"return ;\n"
ASSERT_EQUALS(true, tokenizer.simplifyIfAddBraces()); "}", tokenizeAndStringify(code, true));
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void f ( ) { for ( int i = 0 ; i < 2 ; i ++ ) { if ( true ) { return ; } } return ; }", ostr.str());
} }
void ifAddBraces6() void ifAddBraces6()
{ {
const char code[] = "if()"; const char code[] = "if()";
ASSERT_EQUALS("if ( )", tokenizeAndStringify(code, true));
// tokenize..
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS(false, tokenizer.simplifyIfAddBraces());
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" if ( )", ostr.str());
} }
void whileAddBraces()
{
{
const char code[] = ";while(a);";
ASSERT_EQUALS("; while ( a ) { ; }", tokenizeAndStringify(code, true));
}
}
void simplifyKnownVariables1() void simplifyKnownVariables1()
{ {
{ {