Added regular expression support
- Added rx.{h,cpp} from Tegelsten.
- Added unit tests.
This commit is contained in:
1
src/tests/.gitignore
vendored
1
src/tests/.gitignore
vendored
@@ -21,4 +21,5 @@ path.t
|
||||
file.t
|
||||
directory.t
|
||||
grid.t
|
||||
rx.t
|
||||
*.log
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
PROJECT = t.t tdb.t date.t duration.t t.benchmark.t text.t autocomplete.t \
|
||||
config.t seq.t att.t stringtable.t record.t nibbler.t subst.t filt.t \
|
||||
cmd.t util.t color.t list.t path.t file.t directory.t grid.t
|
||||
cmd.t util.t color.t list.t path.t file.t directory.t grid.t rx.t
|
||||
CFLAGS = -I. -I.. -I../.. -Wall -pedantic -ggdb3 -fno-rtti
|
||||
LFLAGS = -L/usr/local/lib -lncurses -llua
|
||||
OBJECTS = ../t-TDB.o ../t-Task.o ../t-text.o ../t-Date.o ../t-Table.o \
|
||||
@@ -11,7 +11,7 @@ OBJECTS = ../t-TDB.o ../t-Task.o ../t-text.o ../t-Date.o ../t-Table.o \
|
||||
../t-Grid.o ../t-Color.o ../t-rules.o ../t-recur.o ../t-custom.o \
|
||||
../t-export.o ../t-import.o ../t-edit.o ../t-Timer.o \
|
||||
../t-Permission.o ../t-Path.o ../t-File.o ../t-Directory.o \
|
||||
../t-Hooks.o ../t-API.o
|
||||
../t-Hooks.o ../t-API.o ../t-rx.o
|
||||
|
||||
all: $(PROJECT)
|
||||
|
||||
@@ -96,3 +96,6 @@ directory.t: directory.t.o $(OBJECTS) test.o
|
||||
grid.t: grid.t.o $(OBJECTS) test.o
|
||||
g++ grid.t.o $(OBJECTS) test.o $(LFLAGS) -o grid.t
|
||||
|
||||
rx.t: rx.t.o $(OBJECTS) test.o
|
||||
g++ rx.t.o $(OBJECTS) test.o $(LFLAGS) -o rx.t
|
||||
|
||||
|
||||
72
src/tests/rx.t.cpp
Normal file
72
src/tests/rx.t.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Tegelsten - building blocks for UI
|
||||
//
|
||||
// Copyright 2010, 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 <rx.h>
|
||||
#include <test.h>
|
||||
|
||||
Context context;
|
||||
|
||||
int main (int argc, char** argv)
|
||||
{
|
||||
UnitTest ut (15);
|
||||
|
||||
std::string text = "This is a test.";
|
||||
|
||||
ut.ok (regexMatch (text, "i. ", true), text + " =~ /i. /");
|
||||
|
||||
std::vector <std::string> matches;
|
||||
ut.ok (regexMatch (matches, text, "(i.) ", false), text + " =~ /(i.) /");
|
||||
ut.ok (matches.size () == 1, "1 match");
|
||||
ut.is (matches[0], "is", "$1 == is");
|
||||
|
||||
text = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
ut.ok (regexMatch (text, "t..", true), "t..");
|
||||
ut.ok (regexMatch (text, "T..", false), "T..");
|
||||
ut.ok (!regexMatch (text, "T..", true), "! T..");
|
||||
|
||||
text = "this is a test of the regex engine.";
|
||||
// |...:....|....:....|....:....|....:
|
||||
|
||||
ut.ok (regexMatch (text, "^this"), "^this matches");
|
||||
ut.ok (regexMatch (text, "engine\\.$"), "engine\\.$ matches");
|
||||
|
||||
std::vector <std::string> results;
|
||||
std::vector <int> start;
|
||||
std::vector <int> end;
|
||||
ut.ok (regexMatch (results, text, "(e..)", true), "(e..) there are matches");
|
||||
ut.ok (regexMatch (start, end, text, "(e..)", true), "(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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user