Files
taskwarrior-2.x/test/rx.t.cpp
Paul Beckingham b49523c06d Regex
- Implemented RegX class to maintain a separate compile, and match
  method, thereby allowing efficient re-use of the regex.  This is
  critical to Expression::eval, where an identical regex might be
  applied to every task.
- Obsoleted rx.{h,cpp}, which combined the compile and match steps
  into a single call, and is therefore not efficient when used in
  the context of filtering.
- Fixed some unit tests that weren't building.  Now they do.  They
  don't work of course (don't be silly) but that's a problem for
  another day.
- Modified all code that relies on rx.h to use RegX.h.
2011-06-21 01:43:57 -04:00

84 lines
2.5 KiB
C++

////////////////////////////////////////////////////////////////////////////////
// taskwarrior - a command line task list manager.
//
// Copyright 2010 - 2011, Paul Beckingham, Federico Hernandez.
// 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 <Context.h>
#include <RegX.h>
#include <test.h>
Context context;
int main (int argc, char** argv)
{
UnitTest ut (15);
std::string text = "This is a test.";
RegX r1 ("i. ", true);
ut.ok (r1.match (text), text + " =~ /i. /");
std::vector <std::string> matches;
RegX r2 ("(i.) ", false);
ut.ok (r2.match (matches, text), text + " =~ /(i.) /");
ut.ok (matches.size () == 1, "1 match");
ut.is (matches[0], "is", "$1 == is");
text = "abcdefghijklmnopqrstuvwxyz";
RegX r3 ("t..", true);
ut.ok (r3.match (text), "t..");
RegX r4 ("T..", false);
ut.ok (r4.match (text), "T..");
RegX r5 ("T..", true);
ut.ok (!r5.match (text), "! T..");
text = "this is a test of the regex engine.";
// |...:....|....:....|....:....|....:
RegX r6 ("^this");
ut.ok (r6.match (text), "^this matches");
RegX r7 ("engine\\.$");
ut.ok (r7.match (text), "engine\\.$ matches");
std::vector <std::string> results;
std::vector <int> start;
std::vector <int> end;
RegX r8 ("(e..)", true);
ut.ok (r8.match (results, text), "(e..) there are matches");
ut.ok (r8.match (start, end, text), "(e..) there are matches");
ut.is (results.size (), (size_t) 1, "(e..) == 1 match");
ut.is (results[0], "est", "(e..)[0] == 'est'");
ut.is (start[0], 11, "(e..)[0] == 11->");
ut.is (end[0], 14, "(e..)[0] == ->14");
return 0;
}
////////////////////////////////////////////////////////////////////////////////