Feature - Config validation

- The 'version' command now complains about use of deprecated color names and
  duplicate entries.
- Unit tests verify duplicate detection.
- Unit tests verify deprecated color detection.
- Most validation code moved from command.cpp to Config.cpp.
This commit is contained in:
Paul Beckingham
2009-12-08 22:56:01 -05:00
parent b032a00283
commit 42c1b30c31
6 changed files with 206 additions and 25 deletions

View File

@@ -83,6 +83,7 @@ bool Config::load (const std::string& file)
std::string key = trim (line.substr (0, equal), " \t"); // no i18n
std::string value = trim (line.substr (equal+1, line.length () - equal), " \t"); // no i18n
(*this)[key] = value;
sequence.push_back (key);
}
}
}
@@ -329,6 +330,13 @@ void Config::setDefaults ()
set ("alias.rm", "delete"); // TODO i18n
}
////////////////////////////////////////////////////////////////////////////////
void Config::clear ()
{
std::map <std::string, std::string>::clear ();
sequence.clear ();
}
////////////////////////////////////////////////////////////////////////////////
// Return the configuration value given the specified key.
const std::string Config::get (const char* key)
@@ -437,3 +445,71 @@ void Config::all (std::vector<std::string>& items)
}
////////////////////////////////////////////////////////////////////////////////
void Config::getSequence (std::vector<std::string>& items)
{
items = sequence;
}
////////////////////////////////////////////////////////////////////////////////
std::string Config::checkForDuplicates ()
{
std::vector <std::string> duplicates;
std::map <std::string, int> unique;
foreach (i, sequence)
{
if (unique.find (*i) != unique.end ())
duplicates.push_back (*i);
else
unique[*i] = 0;
}
std::stringstream out;
if (duplicates.size ())
{
out << "Found duplicate entries for:" << std::endl;
foreach (i, duplicates)
out << " " << *i << std::endl;
out << std::endl;
}
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
std::string Config::checkForDeprecatedColor ()
{
int count = 0;
std::vector <std::string> deprecated;
foreach (i, *this)
{
if (i->first.find ("color.") != std::string::npos)
{
std::string value = get (i->first);
if (value.find ("_") != std::string::npos)
{
++count;
deprecated.push_back (i->first);
}
}
}
std::stringstream out;
if (count)
{
out << "Your .taskrc file contains color settings that use deprecated "
<< "underscores. Please check:"
<< std::endl;
foreach (i, deprecated)
out << " " << *i << "=" << get (*i) << std::endl;
out << std::endl;
}
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////