Initial example on how to compare regexps for steps

Display exceptions when a step fails
This commit is contained in:
Emmanuel Briot 2014-02-07 22:01:46 +01:00
parent 3d047da00b
commit d81d6939ea
7 changed files with 188 additions and 35 deletions

View File

@ -378,13 +378,24 @@ package body BDD.Features is
----------------
procedure Set_Status
(Self : not null access Step_Record;
Status : BDD.Scenario_Status)
(Self : not null access Step_Record;
Status : BDD.Scenario_Status;
Error_Msg : String := "")
is
begin
Self.Status := Status;
Self.Error_Msg := To_Unbounded_String (Error_Msg);
end Set_Status;
---------------
-- Error_Msg --
---------------
function Error_Msg (Self : not null access Step_Record) return String is
begin
return To_String (Self.Error_Msg);
end Error_Msg;
------------
-- Status --
------------

View File

@ -60,10 +60,12 @@ package BDD.Features is
-- Return the components of the step
procedure Set_Status
(Self : not null access Step_Record;
Status : BDD.Scenario_Status);
(Self : not null access Step_Record;
Status : BDD.Scenario_Status;
Error_Msg : String := "");
function Status
(Self : not null access Step_Record) return BDD.Scenario_Status;
function Error_Msg (Self : not null access Step_Record) return String;
-- Set the status for a specific step
-------------
@ -149,6 +151,7 @@ private
Line : Positive;
Text : Ada.Strings.Unbounded.Unbounded_String;
Multiline : Ada.Strings.Unbounded.Unbounded_String;
Error_Msg : Ada.Strings.Unbounded.Unbounded_String;
Status : BDD.Scenario_Status;
Table : BDD.Tables.Table;
end record;

View File

@ -259,6 +259,25 @@ package body BDD.Formatters is
Scenario : BDD.Features.Scenario;
Step : not null access BDD.Features.Step_Record'Class)
is
procedure Indent (Text : String);
-- Display text on multiple lines, and indent each line as needed
procedure Indent (Text : String) is
Start, Last : Integer;
begin
Start := Text'First;
while Start <= Text'Last loop
Last := Line_End (Text, Start);
if Last < Start then -- empty line
New_Line;
Start := Last + 2;
else
Put (" " & Text (Start .. Last));
Start := Last + 1;
end if;
end loop;
end Indent;
begin
Self.Term.Set_Color
(Term => Ada.Text_IO.Standard_Output,
@ -282,35 +301,33 @@ package body BDD.Formatters is
declare
Multi : constant String := Step.Multiline;
Start, Last : Integer;
begin
if Multi /= "" then
Self.Term.Set_Color
(Term => Ada.Text_IO.Standard_Output,
Foreground => BDD.Step_Colors (Step.Status));
Put_Line (" """"""");
Start := Multi'First;
while Start <= Multi'Last loop
Last := Line_End (Multi, Start);
if Last < Start then -- empty line
New_Line;
Start := Last + 2;
else
Put (" " & Multi (Start .. Last));
Start := Last + 1;
end if;
end loop;
Indent (Multi);
Put_Line (" """"""");
end if;
end;
if Step.Table /= No_Table then
if Step.Table /= No_Table then
Self.Term.Set_Color
(Term => Ada.Text_IO.Standard_Output,
Foreground => BDD.Step_Colors (Step.Status));
Step.Table.Display
(Ada.Text_IO.Standard_Output, Prefix => " ");
end if;
declare
Msg : constant String := Step.Error_Msg;
begin
if Msg /= "" then
Self.Term.Set_Color
(Term => Ada.Text_IO.Standard_Output,
Foreground => BDD.Step_Colors (Step.Status));
Step.Table.Display
(Ada.Text_IO.Standard_Output, Prefix => " ");
Indent (Msg);
end if;
end;

View File

@ -21,6 +21,9 @@
-- --
------------------------------------------------------------------------------
with Ada.Exceptions; use Ada.Exceptions;
with BDD.Steps; use BDD.Steps;
package body BDD.Runner is
--------------
@ -144,23 +147,29 @@ package body BDD.Runner is
procedure Run_Step
(Scenario : BDD.Features.Scenario;
Step : not null access Step_Record'Class) is
Step : not null access Step_Record'Class)
is
Status : Scenario_Status;
begin
case Scenario.Status is
when Status_Passed =>
-- ??? Simulate a run
-- delay 0.2;
if Step.Line >= 25 and then Step.Line <= 26 then
Step.Set_Status (Status_Passed);
elsif Step.Line = 47 then
Step.Set_Status (Status_Failed);
elsif Step.Line = 13 then
Step.Set_Status (Status_Undefined);
elsif Step.Line = 7 then
Step.Set_Status (Status_Passed);
else
Step.Set_Status (Status_Passed);
end if;
Step.Set_Status (Status_Passed);
begin
Status := BDD.Steps.Run_Step (Step.Text);
-- if Status = Status_Undefined then
-- -- ??? Could run some predefined steps here
-- null;
-- end if;
Step.Set_Status (Status);
exception
when E : others =>
Step.Set_Status
(Status_Failed, Exception_Information (E));
end;
if Show_Steps then
Self.Steps_Stats (Step.Status) :=

77
src/bdd-steps.adb Normal file
View File

@ -0,0 +1,77 @@
-----------------------------------------------------------------------------
-- G N A T C O L L --
-- --
-- Copyright (C) 2014, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
with GNAT.Regpat; use GNAT.Regpat;
package body BDD.Steps is
Re_1 : constant Pattern_Matcher := Compile
("^Given a user named '(.*)'$");
Re_2 : constant Pattern_Matcher := Compile
("^Given I am sitting at my desk$");
procedure Do_Step_1 (Name : String);
procedure Do_Step_2;
---------------
-- Do_Step_1 --
---------------
procedure Do_Step_1 (Name : String) is
pragma Unreferenced (Name);
begin
null;
end Do_Step_1;
---------------
-- Do_Step_2 --
---------------
procedure Do_Step_2 is
begin
raise Constraint_Error;
end Do_Step_2;
--------------
-- Run_Step --
--------------
function Run_Step (Step : String) return Scenario_Status is
Matches : Match_Array (0 .. 20);
begin
Match (Re_1, Step, Matches);
if Matches (0) /= No_Match then
Do_Step_1 (Name => Step (Matches (1).First .. Matches (1).Last));
return Status_Passed;
end if;
Match (Re_2, Step, Matches);
if Matches (0) /= No_Match then
Do_Step_2;
return Status_Passed;
end if;
return Status_Undefined;
end Run_Step;
end BDD.Steps;

33
src/bdd-steps.ads Normal file
View File

@ -0,0 +1,33 @@
-----------------------------------------------------------------------------
-- G N A T C O L L --
-- --
-- Copyright (C) 2014, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
-- This package provides support for matching steps with actual subprograms
-- registered by the user
package BDD.Steps is
function Run_Step (Step : String) return Scenario_Status;
-- Run a step.
-- This procedure is expected to raise exceptions when a test fails.
end BDD.Steps;

View File

@ -21,6 +21,9 @@
-- --
------------------------------------------------------------------------------
-- This package provides support for manipulating, comparing and displaying
-- tables.
with Ada.Containers.Indefinite_Vectors;
with Ada.Containers.Vectors;
with Ada.Text_IO; use Ada.Text_IO;