Improved test coverage.

Refactored to extract givenACodeSampleToTokenize, reducing LOC.
This commit is contained in:
Pete Johns 2010-09-26 13:31:36 +10:00
parent 97a68bdcee
commit acad87c3e3
1 changed files with 96 additions and 108 deletions

View File

@ -40,8 +40,10 @@ private:
TEST_CASE(deleteLast); TEST_CASE(deleteLast);
TEST_CASE(match1); TEST_CASE(matchAny);
TEST_CASE(match2); TEST_CASE(matchNothingOrAnyNotElse);
TEST_CASE(matchNumeric);
TEST_CASE(matchBoolean);
} }
void nextprevious() void nextprevious()
@ -126,122 +128,108 @@ private:
} }
void matchAny()
void match1()
{ {
// Match "%var% | %var%" givenACodeSampleToTokenize varBitOrVar("abc|def");
{ ASSERT_EQUALS(true, Token::Match(varBitOrVar.tokens(), "%var% | %var%"));
const std::string code("abc|def");
// tokenize.. givenACodeSampleToTokenize varLogOrVar("abc||def");
Tokenizer tokenizer; ASSERT_EQUALS(true, Token::Match(varLogOrVar.tokens(), "%var% || %var%"));
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "%var% | %var%"));
} }
// Match "%var% || %var%" void matchNothingOrAnyNotElse()
{ {
const std::string code("abc||def"); givenACodeSampleToTokenize emptyString("");
ASSERT_EQUALS(true, Token::Match(emptyString.tokens(), "!!else"));
ASSERT_EQUALS(false, Token::Match(emptyString.tokens(), "!!else something"));
// tokenize.. givenACodeSampleToTokenize ifSemicolon("if ;");
Tokenizer tokenizer; ASSERT_EQUALS(true, Token::Match(ifSemicolon.tokens(), "!!return if"));
std::istringstream istr(code); ASSERT_EQUALS(true, Token::Match(ifSemicolon.tokens(), "if ; !!else"));
tokenizer.tokenize(istr, "test.cpp");
// Match.. givenACodeSampleToTokenize ifSemicolonSomething("if ; something");
ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "%var% || %var%")); ASSERT_EQUALS(true, Token::Match(ifSemicolonSomething.tokens(), "if ; !!else"));
}
givenACodeSampleToTokenize justElse("else");
ASSERT_EQUALS(false, Token::Match(justElse.tokens(), "!!else"));
givenACodeSampleToTokenize ifSemicolonElse("if ; else");
ASSERT_EQUALS(false, Token::Match(ifSemicolonElse.tokens(), "if ; !!else"));
} }
void match2()
void matchNumeric()
{ {
givenACodeSampleToTokenize nonNumeric("abc");
ASSERT_EQUALS(false, Token::Match(nonNumeric.tokens(), "%num%"));
givenACodeSampleToTokenize binary("101010b");
ASSERT_EQUALS(true, Token::Match(binary.tokens(), "%num%"));
givenACodeSampleToTokenize octal("0123");
ASSERT_EQUALS(true, Token::Match(octal.tokens(), "%num%"));
givenACodeSampleToTokenize decimal("4567");
ASSERT_EQUALS(true, Token::Match(decimal.tokens(), "%num%"));
givenACodeSampleToTokenize hexadecimal("0xDEADBEEF");
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
givenACodeSampleToTokenize floatingPoint("0.0f");
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
givenACodeSampleToTokenize doublePrecision("0.0d");
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
givenACodeSampleToTokenize unsignedInt("0U");
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
givenACodeSampleToTokenize unsignedLong("0UL");
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
givenACodeSampleToTokenize unsignedLongLong("0ULL");
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
givenACodeSampleToTokenize positive("+666");
TODO_ASSERT_EQUALS(true, Token::Match(positive.tokens(), "%num%"));
}
void matchBoolean()
{ {
const std::string code(""); givenACodeSampleToTokenize yes("YES");
ASSERT_EQUALS(false, Token::Match(yes.tokens(), "%bool%"));
// tokenize.. givenACodeSampleToTokenize positive("true");
Tokenizer tokenizer; ASSERT_EQUALS(true, Token::Match(positive.tokens(), "%bool%"));
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match.. givenACodeSampleToTokenize negative("false");
ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "!!else")); ASSERT_EQUALS(true, Token::Match(negative.tokens(), "%bool%"));
} }
class givenACodeSampleToTokenize
{ {
const std::string code(""); private:
std::istringstream _sample;
// tokenize.. const Token* _tokens;
Tokenizer tokenizer; Tokenizer _tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS(false, Token::Match(tokenizer.tokens(), "!!else something"));
}
public:
givenACodeSampleToTokenize(const std::string& sample)
:_sample(sample)
,_tokens(NULL)
{ {
const std::string code("if ;"); _tokenizer.tokenize(_sample, "test.cpp");
_tokens = _tokenizer.tokens();
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "!!return if"));
} }
const Token* tokens() const
{ {
const std::string code("if ;"); return _tokens;
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "if ; !!else"));
} }
};
{
const std::string code("if ; something");
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "if ; !!else"));
}
{
const std::string code("else");
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS(false, Token::Match(tokenizer.tokens(), "!!else"));
}
{
const std::string code("if ; else");
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS(false, Token::Match(tokenizer.tokens(), "if ; !!else"));
}
}
}; };
REGISTER_TEST(TestToken) REGISTER_TEST(TestToken)