From a60d54be61e9ff113e50f8761f664ce78794a1a5 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Fri, 5 Jun 2009 00:15:00 -0400 Subject: [PATCH] Enhancement - Filters - Filter implemented. - Unit tests started, need more as Att::match matures. --- src/Att.cpp | 12 ++------ src/Att.h | 8 ++--- src/Filter.cpp | 44 ++++++--------------------- src/Filter.h | 7 +---- src/tests/.gitignore | 1 + src/tests/Makefile | 5 ++- src/tests/filt.t.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+), 54 deletions(-) create mode 100644 src/tests/filt.t.cpp diff --git a/src/Att.cpp b/src/Att.cpp index da68453ce..1e6d223d9 100644 --- a/src/Att.cpp +++ b/src/Att.cpp @@ -140,7 +140,7 @@ bool Att::parse (Nibbler& n) } //////////////////////////////////////////////////////////////////////////////// -bool Att::validMod (const std::string& mod) +bool Att::validMod (const std::string& mod) const { if (mod == "before" || mod == "after" || // i18n: TODO mod == "not" || // i18n: TODO @@ -159,7 +159,7 @@ bool Att::validMod (const std::string& mod) } //////////////////////////////////////////////////////////////////////////////// -bool Att::evalMod (Att& other) +bool Att::match (const Att& other) const { // No modifier means automatic pass. /* @@ -196,12 +196,6 @@ bool Att::evalMod (Att& other) return false; } -//////////////////////////////////////////////////////////////////////////////// -bool Att::match (const Att& other) -{ - return false; -} - //////////////////////////////////////////////////////////////////////////////// // name : " value " std::string Att::composeF4 () const @@ -230,7 +224,7 @@ void Att::addMod (const std::string& mod) } //////////////////////////////////////////////////////////////////////////////// -void Att::mods (std::vector & all) +void Att::mods (std::vector & all) const { all = mMods; } diff --git a/src/Att.h b/src/Att.h index f66c066ba..c5e069ba0 100644 --- a/src/Att.h +++ b/src/Att.h @@ -42,13 +42,13 @@ public: ~Att (); // Destructor bool parse (Nibbler&); - bool validMod (const std::string&); - bool evalMod (Att&); - bool match (const Att&); + bool validMod (const std::string&) const; + bool match (const Att&) const; + std::string composeF4 () const; void addMod (const std::string&); - void mods (std::vector &); + void mods (std::vector &) const; std::string name () const; void name (const std::string&); diff --git a/src/Filter.cpp b/src/Filter.cpp index 5d4985a1d..1ac864af0 100644 --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -26,45 +26,21 @@ //////////////////////////////////////////////////////////////////////////////// #include "Filter.h" +#include "util.h" //////////////////////////////////////////////////////////////////////////////// -Filter::Filter () +// For every Att in the filter, lookup the equivalent in Record, and perform a +// match. Aren't filters easy now that everything is an attribute? +bool Filter::pass (const Record& record) const { -} + Record::const_iterator r; -//////////////////////////////////////////////////////////////////////////////// -Filter::Filter (const Filter& other) -{ - throw std::string ("unimplemented Filter::Filter"); - *this = other; -} - -//////////////////////////////////////////////////////////////////////////////// -Filter& Filter::operator= (const Filter& other) -{ - throw std::string ("unimplemented Filter::operator="); - if (this != &other) - { - *this = other; - } - - return *this; -} - -//////////////////////////////////////////////////////////////////////////////// -Filter::~Filter () -{ -} - -//////////////////////////////////////////////////////////////////////////////// -bool Filter::pass (Record& record) -{ - throw std::string ("unimplemented Filter::pass"); -/* + // If record doesn't have the attribute, fail. If it does have the attribute + // but it doesn't match, fail. foreach (att, (*this)) - if (! att->match (record)) - return false; -*/ + if ((r = record.find (att->name ())) == record.end () || + ! att->match (r->second)) + return false; return true; } diff --git a/src/Filter.h b/src/Filter.h index 05b173ce3..ef730e7fd 100644 --- a/src/Filter.h +++ b/src/Filter.h @@ -34,12 +34,7 @@ class Filter : public std::vector { public: - Filter (); // Default constructor - Filter (const Filter&); // Copy constructor - Filter& operator= (const Filter&); // Assignment operator - ~Filter (); // Destructor - - bool pass (Record&); + bool pass (const Record&) const; }; #endif diff --git a/src/tests/.gitignore b/src/tests/.gitignore index 1d02ea303..2e028dfa2 100644 --- a/src/tests/.gitignore +++ b/src/tests/.gitignore @@ -13,3 +13,4 @@ record.t stringtable.t nibbler.t subst.t +filt.t diff --git a/src/tests/Makefile b/src/tests/Makefile index 6990a2898..d012f9640 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -1,5 +1,5 @@ PROJECT = t.t t2.t tdb.t date.t duration.t t.benchmark.t text.t autocomplete.t \ - parse.t seq.t att.t stringtable.t record.t nibbler.t subst.t + parse.t seq.t att.t stringtable.t record.t nibbler.t subst.t filt.t CFLAGS = -I. -I.. -Wall -pedantic -ggdb3 -fno-rtti LFLAGS = -L/usr/local/lib OBJECTS = ../TDB.o ../TDB2.o ../T.o ../T2.o ../parse.o ../text.o ../Date.o \ @@ -66,3 +66,6 @@ subst.t: subst.t.o $(OBJECTS) test.o nibbler.t: nibbler.t.o $(OBJECTS) test.o g++ nibbler.t.o $(OBJECTS) test.o $(LFLAGS) -o nibbler.t +filt.t: filt.t.o $(OBJECTS) test.o + g++ filt.t.o $(OBJECTS) test.o $(LFLAGS) -o filt.t + diff --git a/src/tests/filt.t.cpp b/src/tests/filt.t.cpp new file mode 100644 index 000000000..1560bdcb1 --- /dev/null +++ b/src/tests/filt.t.cpp @@ -0,0 +1,72 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// +#include "task.h" +#include "test.h" +#include "Filter.h" +#include "T2.h" + +Context context; + +//////////////////////////////////////////////////////////////////////////////// +int main (int argc, char** argv) +{ + UnitTest test (6); + + // Create a filter consisting of two Att criteria. + Filter f; + f.push_back (Att ("name1", "value1")); + f.push_back (Att ("name2", "value2")); + test.is (f.size (), (size_t)2, "Filter created"); + + // Create a T2 to match against. + T2 yes; + yes.set ("name1", "value1"); + yes.set ("name2", "value2"); + test.ok (f.pass (yes), "full match"); + + yes.set ("name3", "value3"); + test.ok (f.pass (yes), "over match"); + + // Negative tests. + T2 no0; + test.notok (f.pass (no0), "no match against default T2"); + + T2 no1; + no1.set ("name3", "value3"); + test.notok (f.pass (no0), "no match against mismatch T2"); + + T2 partial; + partial.set ("name1", "value1"); + test.notok (f.pass (no0), "no match against partial T2"); + + // TODO Modifiers. + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +