Unit Tests

- Because taskDifferences now uses ucFirst on attribute names, three
  tests fail.
This commit is contained in:
Paul Beckingham 2011-03-30 00:35:06 -04:00
parent 628fbd6b64
commit 38576ba08a
3 changed files with 5 additions and 255 deletions

View file

@ -147,7 +147,6 @@ std::string taskDifferences (const Task& before, const Task& after)
<< "'.\n";
}
// Shouldn't just say nothing.
if (out.str ().length () == 0)
out << " - No changes will be made.\n";

View file

@ -323,256 +323,6 @@ int flock (int fd, int operation)
}
#endif
////////////////////////////////////////////////////////////////////////////////
bool taskDiff (const Task& before, const Task& after)
{
// Attributes are all there is, so figure the different attribute names
// between before and after.
std::vector <std::string> beforeAtts;
foreach (att, before)
beforeAtts.push_back (att->first);
std::vector <std::string> afterAtts;
foreach (att, after)
afterAtts.push_back (att->first);
std::vector <std::string> beforeOnly;
std::vector <std::string> afterOnly;
listDiff (beforeAtts, afterAtts, beforeOnly, afterOnly);
if (beforeOnly.size () !=
afterOnly.size ())
return true;
foreach (name, beforeAtts)
if (*name != "uuid" &&
before.get (*name) != after.get (*name))
return true;
return false;
}
////////////////////////////////////////////////////////////////////////////////
std::string taskDifferences (const Task& before, const Task& after)
{
// Attributes are all there is, so figure the different attribute names
// between before and after.
std::vector <std::string> beforeAtts;
foreach (att, before)
beforeAtts.push_back (att->first);
std::vector <std::string> afterAtts;
foreach (att, after)
afterAtts.push_back (att->first);
std::vector <std::string> beforeOnly;
std::vector <std::string> afterOnly;
listDiff (beforeAtts, afterAtts, beforeOnly, afterOnly);
// Now start generating a description of the differences.
std::stringstream out;
foreach (name, beforeOnly)
out << " - "
<< ucFirst(*name)
<< " will be deleted.\n";
foreach (name, afterOnly)
{
if (*name == "depends")
{
std::vector <int> deps_after;
after.getDependencies (deps_after);
std::string to;
join (to, ", ", deps_after);
out << " - "
<< "Dependencies"
<< " will be set to '"
<< to
<< "'.\n";
}
else
out << " - "
<< ucFirst(*name)
<< " will be set to '"
<< renderAttribute (*name, after.get (*name))
<< "'.\n";
}
foreach (name, beforeAtts)
if (*name != "uuid" &&
before.get (*name) != after.get (*name))
{
if (*name == "depends")
{
std::vector <int> deps_before;
before.getDependencies (deps_before);
std::string from;
join (from, ", ", deps_before);
std::vector <int> deps_after;
after.getDependencies (deps_after);
std::string to;
join (to, ", ", deps_after);
out << " - "
<< "Dependencies"
<< " will be changed from '"
<< from
<< "' to '"
<< to
<< "'.\n";
}
else
out << " - "
<< ucFirst(*name)
<< " will be changed from '"
<< renderAttribute (*name, before.get (*name))
<< "' to '"
<< renderAttribute (*name, after.get (*name))
<< "'.\n";
}
// Shouldn't just say nothing.
if (out.str ().length () == 0)
out << " - No changes will be made.\n";
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
std::string taskInfoDifferences (const Task& before, const Task& after)
{
// Attributes are all there is, so figure the different attribute names
// between before and after.
std::vector <std::string> beforeAtts;
foreach (att, before)
beforeAtts.push_back (att->first);
std::vector <std::string> afterAtts;
foreach (att, after)
afterAtts.push_back (att->first);
std::vector <std::string> beforeOnly;
std::vector <std::string> afterOnly;
listDiff (beforeAtts, afterAtts, beforeOnly, afterOnly);
// Now start generating a description of the differences.
std::stringstream out;
foreach (name, beforeOnly)
{
if (*name == "depends")
{
std::vector <int> deps_before;
before.getDependencies (deps_before);
std::string from;
join (from, ", ", deps_before);
out << "Dependencies '"
<< from
<< "' deleted.\n";
}
else if (name->substr (0, 11) == "annotation_")
{
out << "Annotation '"
<< before.get (*name)
<< "' deleted.\n";
}
else
{
out << ucFirst (*name)
<< " deleted.\n";
}
}
foreach (name, afterOnly)
{
if (*name == "depends")
{
std::vector <int> deps_after;
after.getDependencies (deps_after);
std::string to;
join (to, ", ", deps_after);
out << "Dependencies"
<< " set to '"
<< to
<< "'.\n";
}
else if (name->substr (0, 11) == "annotation_")
{
out << "Annotation of '"
<< after.get (*name)
<< "' added.\n";
}
else
out << ucFirst(*name)
<< " set to '"
<< renderAttribute (*name, after.get (*name))
<< "'.\n";
}
foreach (name, beforeAtts)
if (*name != "uuid" &&
before.get (*name) != after.get (*name) &&
before.get (*name) != "" && after.get (*name) != "")
{
if (*name == "depends")
{
std::vector <int> deps_before;
before.getDependencies (deps_before);
std::string from;
join (from, ", ", deps_before);
std::vector <int> deps_after;
after.getDependencies (deps_after);
std::string to;
join (to, ", ", deps_after);
out << "Dependencies"
<< " changed from '"
<< from
<< "' to '"
<< to
<< "'.\n";
}
else if (name->substr (0, 11) == "annotation_")
{
out << "Annotation changed to '"
<< after.get (*name)
<< "'.\n";
}
else
out << ucFirst(*name)
<< " changed from '"
<< renderAttribute (*name, before.get (*name))
<< "' to '"
<< renderAttribute (*name, after.get (*name))
<< "'.\n";
}
// Shouldn't just say nothing.
if (out.str ().length () == 0)
out << "No changes made.\n";
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////
std::string renderAttribute (const std::string& name, const std::string& value)
{
Att a;
if (a.type (name) == "date" &&
value != "")
{
Date d ((time_t)::atoi (value.c_str ()));
return d.toString (context.config.get ("dateformat"));
}
return value;
}
////////////////////////////////////////////////////////////////////////////////
// The vector must be sorted first. This is a modified version of the run-
// length encoding algorithm.

View file

@ -60,6 +60,7 @@ int main (int argc, char** argv)
// TODO const std::string uuid ();
// TODO These are in feedback.cpp, not util.cpp.
// std::string taskDiff (const Task&, const Task&);
Task left;
left.set ("zero", "0");
@ -75,10 +76,10 @@ int main (int argc, char** argv)
std::string output = taskDifferences (left, right);
t.ok (taskDiff (left, right), "Detected changes");
t.ok (output.find ("zero will be changed from '0' to '00'") != std::string::npos, "Detected change zero:0 -> zero:00");
t.ok (output.find ("one will be deleted") != std::string::npos, "Detected deletion one:1 ->");
t.ok (output.find ("two") == std::string::npos, "Detected no change two:2 -> two:2");
t.ok (output.find ("three will be set to '3'") != std::string::npos, "Detected addition -> three:3");
t.ok (output.find ("Zero will be changed from '0' to '00'") != std::string::npos, "Detected change zero:0 -> zero:00");
t.ok (output.find ("One will be deleted") != std::string::npos, "Detected deletion one:1 ->");
t.ok (output.find ("Two") == std::string::npos, "Detected no change two:2 -> two:2");
t.ok (output.find ("Three will be set to '3'") != std::string::npos, "Detected addition -> three:3");
t.notok (taskDiff (right, rightAgain), "No changes detected");
output = taskDifferences (right, rightAgain);