Skip to content

Commit

Permalink
allow multi-line matching
Browse files Browse the repository at this point in the history
  • Loading branch information
sebgod committed May 20, 2015
1 parent f652817 commit 38543e7
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 16 deletions.
1 change: 1 addition & 0 deletions tests/.gitignore
@@ -0,0 +1 @@
*.res
19 changes: 10 additions & 9 deletions tests/Makefile
@@ -1,37 +1,38 @@
include ../Make.options

EXPS := $(wildcard internal/*.exp)
EXPS := $(wildcard manual/*.exp)
RESS := $(patsubst %.exp,%.res,$(EXPS))
TEST_MODULE := test_mercury_slre
TEST_EXE := $(TEST_MODULE)$(EXE_EXT)

.PHONY: runtests
runtests: $(RESS)
runtests: $(TEST_EXE) $(RESS)

Mercury.modules: $(wildcard *.m) $(wildcard ../src/*.m)
@echo Call test clean to ensure testing of changed code
$(MAKE) tests.clean
$(MMC) -f $^

test_mercury_slre: Mercury.modules $(SLRE_OBJ)
$(TEST_EXE): Mercury.modules $(SLRE_OBJ) $(wildcard ../src/*.[chm])
$(MMC) $(MCFLAGS) --make $@ --link-object $(SLRE_OBJ) $(MLLIBS)

$(SLRE_OBJ):
cd ../src && $(MAKE) $@
$(HARDLINK) ../src/$@ $@

%.res: %.exp
@echo testing $*
$(CURL) $(CURL_FLAGS) http://127.0.0.1:$(SLRE_TEST_PORT)/$* | \
$(DIFF) $(DIFF_FLAGS) $< - >$*.res
%.res: %.exp %.inp
@echo testing $*.inp
@./$(TEST_MODULE) < $*.inp | $(DIFF) $(DIFF_FLAGS) $< - >$*.res

.PHONY: tests.clean
tests.clean:
$(DEL_FILE) $(RESS)

.PHONY: clean
clean: tests.clean
$(MMC) --make test_mercury_slre.clean
$(MMC) --make $(TEST_MODULE).clean
$(DEL_DIR) Mercury
$(DEL_FILE) Mercury.modules
$(DEL_FILE) FAILED_TESTS ABORTED_TESTS
$(DEL_FILE) $(SLRE_OBJ)
$(DEL_FILE) test_mercury_slre$(EXE_EXT)
$(DEL_FILE) $(TEST_EXE)
1 change: 1 addition & 0 deletions tests/manual/assert.exp
@@ -0,0 +1 @@
ok x|$ abcd NULL 0 0 4
2 changes: 2 additions & 0 deletions tests/manual/assert.inp
@@ -0,0 +1,2 @@
ASSERT\(slre_match\("([^"]+)",\s*"([^"]+)",\s*\d+,\s+(NULL|caps),\s*(\d+),\s*(\d+)\)\s*==\s*(\d+|[A_Z]\s+).*
ASSERT(slre_match("x|$", "abcd", 4, NULL, 0, 0) == 4;
1 change: 1 addition & 0 deletions tests/manual/digits.exp
@@ -0,0 +1 @@
ok
2 changes: 2 additions & 0 deletions tests/manual/digits.inp
@@ -0,0 +1,2 @@
\d+
123
Empty file.
1 change: 1 addition & 0 deletions tests/manual/not_match_empty.inp
@@ -0,0 +1 @@
^\s+$
41 changes: 34 additions & 7 deletions tests/test_mercury_slre.m
Expand Up @@ -27,22 +27,49 @@

:- import_module mercury_slre.

:- import_module bool.
:- import_module int.
:- import_module list.
:- import_module maybe.
:- import_module string.

%----------------------------------------------------------------------------%

main(!IO) :-
( if matches("^(\\d+)$", "123", match(Captures, _)) then
print_line("ok", !IO),
foldl(print_line, Captures, !IO)
else
print_line("no match", !IO)
io.read_line_as_string(RegExRes, !IO),
( RegExRes = ok(RegEx),
match_lines(RegEx, !IO)
; RegExRes = error(Error : io.error),
print_line(stderr_stream, error_message(Error) : string, !IO)
; RegExRes = eof,
print_line(stderr_stream, "premature end of file", !IO)
).

:- pred match_lines(string::in, io::di, io::uo) is det.

match_lines(RegEx, !IO) :-
PrintWithTab = (pred(T::in, !.IO::di, !:IO::uo) is det :-
io.print("\t", !IO),
io.print(T, !IO)
),
io.read_line_as_string(TextRes, !IO),
( TextRes = ok(Text),
( if
matches(RegEx, Text, match(Captures, ScannedCodeUnits)),
ScannedCodeUnits >= 0
then
print("ok", !IO),
foldl(PrintWithTab, Captures, !IO),
nl(!IO)
else
true
),
match_lines(RegEx, !IO)
; TextRes = error(Error : io.error),
print_line(stderr_stream, error_message(Error) : string, !IO)
; TextRes = eof,
true
).


%----------------------------------------------------------------------------%
:- end_module test_mercury_slre.
%----------------------------------------------------------------------------%

0 comments on commit 38543e7

Please sign in to comment.