Skip to content

Commit

Permalink
Allow local variables in test files
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJefferson committed Aug 25, 2017
1 parent fb9f480 commit 70ee2bf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
22 changes: 18 additions & 4 deletions lib/test.gi
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ InstallGlobalFunction(RunTests, function(arg)
s, res, fres, t, f, i;
# don't enter break loop in case of error during test
tests := arg[1];
opts := rec( breakOnError := false, showProgress := "some" );
opts := rec( breakOnError := false, showProgress := "some", localdef := false );
if Length(arg) > 1 and IsRecord(arg[2]) then
for f in RecNames(arg[2]) do
opts.(f) := arg[2].(f);
Expand Down Expand Up @@ -130,7 +130,11 @@ InstallGlobalFunction(RunTests, function(arg)
fres := OutputTextString(res, false);
SET_OUTPUT(fres, true);
t := Runtime();
READ_STREAM_LOOP(s, true);
if opts.localdef <> false then
READ_STREAM_LOOP_WITH_CONTEXT(s, true, opts.localdef);
else
READ_STREAM_LOOP(s, true);
fi;
SET_PREVIOUS_OUTPUT();
CloseStream(fres);
CloseStream(s);
Expand Down Expand Up @@ -321,7 +325,7 @@ end;
##
InstallGlobalFunction("Test", function(arg)
local fnam, nopts, opts, size, full, pf, ret, lines, ign, new, n,
cT, ok, oldtimes, thr, delta, len, c, i, j, d;
cT, ok, oldtimes, thr, delta, len, c, i, j, d, linesplit, localdef;

# get arguments and set options
fnam := arg[1];
Expand Down Expand Up @@ -370,6 +374,7 @@ InstallGlobalFunction("Test", function(arg)
Print("########\n");
end,
subsWindowsLineBreaks := true,
localdef := false,
);
for c in RecNames(nopts) do
opts.(c) := nopts.(c);
Expand Down Expand Up @@ -400,13 +405,22 @@ InstallGlobalFunction("Test", function(arg)
if opts.subsWindowsLineBreaks = true then
full := ReplacedString(full, "\r\n", "\n");
fi;

linesplit := SplitString(full, "\n", "");
if linesplit[1]{[1..3]} = "##!" then
opts.localdef := linesplit[1]{[4..Length(linesplit[1])]};
opts.localdef := Concatenation("(function() ", opts.localdef, "; return GetCurrentLVars(); end)()");
opts.localdef := EvalString(opts.localdef);
fi;


# split input into GAP input, GAP output and comments
pf := ParseTestInput(full, opts.ignoreComments);

# run the GAP inputs and collect the outputs and the timings
RunTests(pf, rec(breakOnError := opts.breakOnError,
showProgress := opts.showProgress));
showProgress := opts.showProgress,
localdef := opts.localdef));

# reset screen width
SizeScreen(size);
Expand Down
25 changes: 17 additions & 8 deletions src/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ Obj READ_AS_FUNC ( void )
}


static void READ_TEST_OR_LOOP(void)
static void READ_TEST_OR_LOOP(Obj context)
{
UInt type;
UInt oldtime;
Expand All @@ -354,7 +354,7 @@ static void READ_TEST_OR_LOOP(void)

/* read and evaluate the command */
ClearError();
type = ReadEvalCommand(STATE(BottomLVars), &dualSemicolon);
type = ReadEvalCommand(context, &dualSemicolon);

/* stop the stopwatch */
AssGVar( Time, INTOBJ_INT( SyTime() - oldtime ) );
Expand Down Expand Up @@ -397,9 +397,9 @@ static void READ_TEST_OR_LOOP(void)
**
** Read the current input as read-eval-view loop and close the input stream.
*/
static void READ_LOOP ( void )
static void READ_LOOP ( Obj context )
{
READ_TEST_OR_LOOP();
READ_TEST_OR_LOOP(context);

/* close the input file again, and return 'true' */
if ( ! CloseInput() ) {
Expand Down Expand Up @@ -1077,10 +1077,11 @@ Obj FuncREAD_STREAM (
**
*F FuncREAD_STREAM_LOOP( <self>, <stream>, <catcherrstdout> ) . read a stream
*/
Obj FuncREAD_STREAM_LOOP (
Obj FuncREAD_STREAM_LOOP_WITH_CONTEXT (
Obj self,
Obj stream,
Obj catcherrstdout )
Obj catcherrstdout,
Obj context )
{
/* try to open the file */
if ( ! OpenInputStream(stream) ) {
Expand All @@ -1093,12 +1094,19 @@ Obj FuncREAD_STREAM_LOOP (


/* read the test file */
READ_LOOP();
READ_LOOP(context);
STATE(IgnoreStdoutErrout) = NULL;
return True;
}


Obj FuncREAD_STREAM_LOOP (
Obj self,
Obj stream,
Obj catcherrstdout)
{
return FuncREAD_STREAM_LOOP_WITH_CONTEXT(self, stream, catcherrstdout,
STATE(BottomLVars));
}
/****************************************************************************
**
*F FuncREAD_AS_FUNC( <self>, <filename> ) . . . . . . . . . . . read a file
Expand Down Expand Up @@ -2222,6 +2230,7 @@ static StructGVarFunc GVarFuncs [] = {
GVAR_FUNC(READ_COMMAND, 2, "stream, echo"),
GVAR_FUNC(READ_STREAM, 1, "stream"),
GVAR_FUNC(READ_STREAM_LOOP, 2, "stream, catchstderrout"),
GVAR_FUNC(READ_STREAM_LOOP_WITH_CONTEXT, 3, "stream, catchstderrout, context"),
GVAR_FUNC(READ_AS_FUNC, 1, "filename"),
GVAR_FUNC(READ_AS_FUNC_STREAM, 1, "stream"),
GVAR_FUNC(READ_GAP_ROOT, 1, "filename"),
Expand Down
2 changes: 2 additions & 0 deletions src/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,8 @@ void PrintIsbComObjExpr (

Obj FuncGetCurrentLVars( Obj self )
{
// Need to promote to High Vars, else bag will be freed when function exits
MakeHighVars(STATE(CurrLVars));
return STATE(CurrLVars);
}

Expand Down
1 change: 1 addition & 0 deletions tst/testinstall/recordname.tst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
##! local x,r
#############################################################################
##
#W recordname.tst GAP tests Chris Jefferson
Expand Down
1 change: 1 addition & 0 deletions tst/testinstall/strings.tst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
##! local x
#############################################################################
##
#W strings.tst GAP tests Alexander Konovalov
Expand Down

0 comments on commit 70ee2bf

Please sign in to comment.