Skip to content

Commit

Permalink
Updating [DEBUGGING]
Browse files Browse the repository at this point in the history
  • Loading branch information
dougpuob committed Apr 1, 2020
1 parent 7651294 commit 6799676
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 82 deletions.
7 changes: 4 additions & 3 deletions script/build-bin-win32-dbg.cmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@ECHO OFF

python --version > NUL
python --version > NUL
IF "9009"=="%ERRORLEVEL%" (GOTO :NOPY)

python cppnamelint.py chkenv
Expand All @@ -12,9 +12,10 @@ ECHO ==============================================================
ECHO Generate makefile via CMake
ECHO ==============================================================
SET ROOT_DIR=..
SET BUILD_DIR=..\build
SET BUILD_DIR=..\build\windows
SET SOURCE_DIR=..\source
SET BUILD_TYPE=DEBUG
::IF EXIST %BUILD_DIR% (RD %BUILD_DIR%)
python cppnamelint.py format %ROOT_DIR% %SOURCE_DIR%
MKDIR %BUILD_DIR%
python cppnamelint.py bldgcfg %ROOT_DIR% %BUILD_DIR% %BUILD_TYPE%
IF NOT "0"=="%ERRORLEVEL%" (GOTO :PYERR)
Expand Down
70 changes: 40 additions & 30 deletions source/Detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,10 @@ bool Detection::_IsLowerSeperatedString(const string &Name, vector<string> Ignor
return bStatus;
}

bool Detection::_IsHungarianNotationString(const string &TypeStr, const string &NameStr,
const bool &bPreferUpperCamel, const bool &bIsPtr,
bool Detection::_IsHungarianNotationString(const string &TypeStr,
const string &NameStr,
const bool &bPreferUpperCamel,
const bool &bIsPtr,
const bool &bIsArray,
const vector<string> &IgnorePrefixs,
const map<string, string> &TypeNamingMap,
Expand Down Expand Up @@ -233,9 +235,15 @@ bool Detection::_IsHungarianNotationString(const string &TypeStr, const string &
// Remove pointer chars.
// Remove hungarian prefix chars.
//
bool bSzStringMatched = false;
bool bStatus = false;
if (bIsPtr || bIsArray) {
size_t nLowerPCount = this->_RemoveHeadingPtrChar(NewNameStr);
// Remove `p` chars.
size_t nLowerPCount = this->_RemoveHeadingPtrChar(NewNameStr);
if (nLowerPCount > 0) {
bStatus = true;
}

// Remove hungarian notion of C null string.
for (auto Iter : NullStringMap) {
const auto IterType = Iter.Key;
const auto IterPrefix = Iter.Value;
Expand All @@ -245,42 +253,44 @@ bool Detection::_IsHungarianNotationString(const string &TypeStr, const string &
if (0 == nNewStrPos) {
const size_t nPrefixLen = IterPrefix.length();

bSzStringMatched = true;
bStatus = true;
NewNameStr = NewNameStr.substr(nPrefixLen, NewNameStr.length() - nPrefixLen);
break;
}
}
}
}
else
{
bStatus = true;
}

bool bStatus = true;
bool bSpecificTypeMatched = false;
if (!bSzStringMatched) {
for (map<string, string>::const_iterator Iter = TypeNamingMap.begin();
Iter != TypeNamingMap.end(); Iter++) {
const auto IterType = Iter->first;
const auto IterPrefix = Iter->second;
if (IterType == NewTypeStr) {
bStatus = false;
const size_t nNewStrPos = NewNameStr.find(IterPrefix);
if (0 == nNewStrPos) {
const size_t nPrefixLen = IterPrefix.length();
// Match Hungarian notation.
if (bStatus)
{
for (map<string, string>::const_iterator Iter = TypeNamingMap.begin();
Iter != TypeNamingMap.end(); Iter++) {

bStatus = true;
bSpecificTypeMatched = true;
NewNameStr = NewNameStr.substr(nPrefixLen, NewNameStr.length() - nPrefixLen);
break;
}
}
}
const auto IterType = Iter->first;
const auto IterPrefix = Iter->second;

if (IterType == NewTypeStr) {
const size_t nNewStrPos = NewNameStr.find(IterPrefix);
bStatus = (0 == nNewStrPos);
//if (bStatus) {
// const size_t nPrefixLen = IterPrefix.length();

// // Remove hungarian notation.
// NewNameStr = NewNameStr.substr(nPrefixLen, NewNameStr.length() - nPrefixLen);
//}

break;
}
}
}

if (!bSpecificTypeMatched) {
if (bPreferUpperCamel) {
bStatus = this->_IsUpperCamelCaseString(NewNameStr, IgnorePrefixs);
} else {
bStatus = false;
}
if (bPreferUpperCamel) {
bStatus = this->_IsUpperCamelCaseString(NewNameStr, IgnorePrefixs);
}

return bStatus;
Expand Down
105 changes: 56 additions & 49 deletions source/test/TestDetection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ TEST(Config_Detect_CheckVariable, InputParms_Good)
{
RuleOfVariable Rule;
Detection Detect;
Rule.WordListMap.insert(std::pair<string, string>("int" , "i"));
Rule.WordListMap.insert(std::pair<string, string>("uin8_t" , "u8"));
Rule.WordListMap.insert(std::pair<string, string>("uin16_t", "u16"));
Rule.WordListMap.insert(std::pair<string, string>("int" , "i"));
Rule.WordListMap.insert(std::pair<string, string>("uint8_t" , "u8"));
Rule.WordListMap.insert(std::pair<string, string>("uint16_t", "u16"));

Rule.NullStringMap.push_back(MappingPair("char*" , "sz"));
Rule.NullStringMap.push_back(MappingPair("char**" , "psz"));
Expand All @@ -304,74 +304,81 @@ TEST(Config_Detect_CheckVariable, InputParms_Good)
Rule.NullStringMap.push_back(MappingPair("char[]" , "sz"));
Rule.NullStringMap.push_back(MappingPair("wchar_t[]" , "wsz"));

bool bPrefer = true;
bool bIsPtr = false;
bool bIsArray = false;
bool bPrefer = true;
bool bIsPtr = true;
bool bNotPtr = false;
bool bIsArray = true;
bool bNotArray = false;
Detect.ApplyRuleForVariable(Rule);

// ...........................................................vvvvvvvvv <-- * character should be removed.
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "szStr" , bPrefer, true , bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "pszStr" , bPrefer, true , bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "wszStr" , bPrefer, true , bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "pwszStr" , bPrefer, true , bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "szStr" , bPrefer, bIsPtr, bNotArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "pszStr" , bPrefer, bIsPtr, bNotArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "wszStr" , bPrefer, bIsPtr, bNotArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "pwszStr" , bPrefer, bIsPtr, bNotArray));

// ...........................................................vvvvvvvvv <-- [] characters should be removed.
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "szStr" , bPrefer, false , true));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "wszStr" , bPrefer, false , true));

EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "piMyValue" , bPrefer, true , bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_DEFAULT , "uin8_t" , "MyFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uin8_t" , "MyFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uin8_t" , "myFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uin8_t" , "my_func" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "iMyFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u8MyFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin16_t" , "u16MyFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "char" , "szStr" , bPrefer, bNotPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "wchar_t" , "wszStr" , bPrefer, bNotPtr, bIsArray));

EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "piMyValue" , bPrefer, bIsPtr, bNotArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_DEFAULT , "uint8_t" , "MyFunc" , bPrefer, bNotPtr, bNotArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uint8_t" , "MyFunc" , bPrefer, bNotPtr, bNotArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uint8_t" , "myFunc" , bPrefer, bNotPtr, bNotArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uint8_t" , "my_func" , bPrefer, bNotPtr, bNotArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "iMyFunc" , bPrefer, bNotPtr, bNotArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u8MyFunc" , bPrefer, bNotPtr, bNotArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint16_t" , "u16MyFunc" , bPrefer, bNotPtr, bNotArray));

Rule.IgnorePrefixs.push_back("m_");
Detect.ApplyRuleForVariable(Rule);
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "m_iMyFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "m_iMyFunc" , bPrefer, bNotPtr, bNotArray));
}

TEST(Config_Detect_CheckVariable, PreferUpperCamelIfMissed)
{
RuleOfVariable Rule;
Detection Detect;


bool bIsPtr = true;
bool bNotPtr = false;
bool bIsArray = true;
bool bNotArray = false;
bool bPreferUpperCamel = true;

Rule.WordListMap.insert(std::pair<string, string>("uin8_t", "u8"));
Rule.WordListMap.insert(std::pair<string, string>("uin16_t", "u16"));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN, "int", "MyValue", bPreferUpperCamel, false, false));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN, "uin8_t", "MyValue", bPreferUpperCamel, false, false));
Rule.WordListMap.insert(std::pair<string, string>("uint8_t", "u8"));
Rule.WordListMap.insert(std::pair<string, string>("uint16_t", "u16"));
Detect.ApplyRuleForVariable(Rule);

EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_HUNGARIAN, "int", "MyValue", bPreferUpperCamel, bNotPtr, bNotArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN, "uint8_t", "MyValue", bPreferUpperCamel, bNotPtr, bNotArray));
}

TEST(Config_Detect_CheckVariable, InputParms_Bad)
{
RuleOfVariable Rule;
Detection Detect;

Rule.WordListMap.insert(std::pair<string, string>("uin8_t", "u8"));
Rule.WordListMap.insert(std::pair<string, string>("uint8_t", "u8"));
Rule.WordListMap.insert(std::pair<string, string>("uin16_t", "u16"));

bool bPrefer = true;
bool bIsPtr = false;
bool bIsArray = false;
Detect.ApplyRuleForVariable(Rule);
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_DEFAULT , "uin8_t" , "" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uin8_t" , "" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uin8_t" , "" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uin8_t" , "" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uin8_t" , "" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u8my_name" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u8my_Name" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u8my_Name" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u16AnyName" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin16_t" , "u8AnyName" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin8_t" , "u8_my_func" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin16_t" , "u16_my_func" , bPrefer, bIsPtr, bIsArray));

EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "iMyValue" , bPrefer, true, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_DEFAULT , "uint8_t" , "" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uint8_t" , "" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uint8_t" , "" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uint8_t" , "" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uint8_t" , "" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u8my_name" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u8my_Name" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u8my_Name" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u16AnyName" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin16_t" , "u8AnyName" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uint8_t" , "u8_my_func" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "uin16_t" , "u16_my_func" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN , "int" , "iMyValue" , bPrefer, true, bIsArray));
}

//-------------------------------------------------------------------------
Expand All @@ -383,30 +390,30 @@ TEST(Config_Detect_CheckVariable, GoodCases)
RuleOfVariable Rule;
Detection Detect;

Rule.WordListMap.insert(std::pair<string, string>("uin8_t", "u8"));
Rule.WordListMap.insert(std::pair<string, string>("uint8_t", "u8"));

bool bPrefer = true;
bool bIsPtr = false;
bool bIsArray = false;
Detect.ApplyRuleForVariable(Rule);
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_DEFAULT , "uin8_t", "MyFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uin8_t", "MyFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uin8_t", "myFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uin8_t", "my_func" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_DEFAULT , "uint8_t", "MyFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_UPPER_CAMEL , "uint8_t", "MyFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_CAMEL , "uint8_t", "myFunc" , bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(true, Detect.CheckVariable(RULETYPE_LOWER_SNAKE , "uint8_t", "my_func" , bPrefer, bIsPtr, bIsArray));
}

TEST(Config_Detect_CheckVariable, Hungarian_Bad)
{
RuleOfVariable Rule;
Detection Detect;

Rule.WordListMap.insert(std::pair<string, string>("uin8_t", "u8"));
Rule.WordListMap.insert(std::pair<string, string>("uint8_t", "u8"));

bool bPrefer = true;
bool bIsPtr = false;
bool bIsArray = false;
Detect.ApplyRuleForVariable(Rule);
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN, "uin8_t", "MyFunc", bPrefer, bIsPtr, bIsArray));
EXPECT_EQ(false, Detect.CheckVariable(RULETYPE_HUNGARIAN, "uint8_t", "MyFunc", bPrefer, bIsPtr, bIsArray));
}

} // namespace CheckVariable
Expand Down

0 comments on commit 6799676

Please sign in to comment.