Add support for Background

This commit is contained in:
Emmanuel Briot 2014-02-07 16:44:49 +01:00
parent 2bc5926b78
commit 424668575d
7 changed files with 124 additions and 40 deletions

View File

@ -213,11 +213,12 @@ package body BDD.Features is
procedure Foreach_Step
(Self : Scenario;
Callback : not null access procedure
(S : not null access Step_Record'Class))
(Scenario : BDD.Features.Scenario;
Step : not null access Step_Record'Class))
is
begin
for S of Self.Get.Steps loop
Callback (S);
Callback (Self, S);
end loop;
end Foreach_Step;
@ -394,4 +395,17 @@ package body BDD.Features is
return Self.Status;
end Status;
------------
-- Prefix --
------------
function Prefix (Self : Scenario) return String is
begin
case Self.Get.Kind is
when Kind_Scenario => return Cst_Scenario;
when Kind_Background => return Cst_Background;
when Kind_Outline => return Cst_Scenario_Outline;
end case;
end Prefix;
end BDD.Features;

View File

@ -118,13 +118,17 @@ package BDD.Features is
function Get_Feature (Self : Scenario) return Feature'Class;
-- Retrieve the attributes of Self
function Prefix (Self : Scenario) return String;
-- Return the "Scenario:", "Background:",... prefix to use for Self
procedure Add (Self : Scenario; S : not null access Step_Record'Class);
-- Add a new step
procedure Foreach_Step
(Self : Scenario;
Callback : not null access procedure
(S : not null access Step_Record'Class));
(Scenario : BDD.Features.Scenario;
Step : not null access Step_Record'Class));
-- Iterate over each step
procedure Set_Status (Self : Scenario; Status : BDD.Scenario_Status);

View File

@ -94,7 +94,9 @@ package body BDD.Formatters is
function Scenario_Name (Scenario : BDD.Features.Scenario) return String is
begin
return +Scenario.Get_Feature.File.Relative_Path (Features_Directory)
& "#" & Image (Scenario.Index, 1)
& (if Scenario.Index /= Positive'Last
then "#" & Image (Scenario.Index, 1)
else "")
& ":" & Image (Scenario.Line, 1);
end Scenario_Name;
@ -107,11 +109,13 @@ package body BDD.Formatters is
Scenario : BDD.Features.Scenario)
is
procedure Show_Step
(Step : not null access BDD.Features.Step_Record'Class);
(Scenario : BDD.Features.Scenario;
Step : not null access BDD.Features.Step_Record'Class);
-- Display a step for a scenario
procedure Show_Step
(Step : not null access BDD.Features.Step_Record'Class) is
(Scenario : BDD.Features.Scenario;
Step : not null access BDD.Features.Step_Record'Class) is
begin
Display_Step (Self, Scenario, Step);
end Show_Step;
@ -241,7 +245,7 @@ package body BDD.Formatters is
end if;
Put_And_Align
(Scenario, Scenario_Indent & Cst_Scenario & ' ' & Scenario.Name);
(Scenario, Scenario_Indent & Scenario.Prefix & ' ' & Scenario.Name);
Display_Location (Self, Scenario);
end Display_Scenario_Header;

View File

@ -39,11 +39,13 @@ package body BDD.Parser is
pragma Unreferenced (Self);
type State_Type is (None, In_Feature,
In_Scenario,
In_Background,
In_String,
In_Outline, In_Examples);
State : State_Type := None;
F : BDD.Features.Feature := No_Feature;
Scenar : BDD.Features.Scenario := No_Scenario;
State : State_Type := None;
F : BDD.Features.Feature := No_Feature;
Scenar : BDD.Features.Scenario := No_Scenario;
Background : BDD.Features.Scenario := No_Scenario;
Step : BDD.Features.Step;
Buffer : GNAT.Strings.String_Access := File.Read_File;
Index : Integer := Buffer'First;
@ -68,9 +70,14 @@ package body BDD.Parser is
procedure Finish_Scenario is
begin
case State is
when In_Background =>
Step := null;
Runner.Scenario_End (No_Scenario, Background);
State := In_Feature;
when In_Scenario | In_Outline | In_Examples =>
Step := null;
Runner.Scenario_End (Scenar);
Runner.Scenario_End (Background, Scenar);
Scenar := No_Scenario;
State := In_Feature;
@ -89,6 +96,8 @@ package body BDD.Parser is
if State /= None then
Runner.Feature_End (F);
F := No_Feature;
Scenar := No_Scenario;
Background := No_Scenario;
end if;
State := None;
@ -126,7 +135,8 @@ package body BDD.Parser is
if Active (Me) then
Trace (Me, "Line " & Image (Line, 3, Padding => ' ')
& " " & Buffer (Line_S .. Line_E));
& " " & Buffer (Line_S .. Line_E)
& " " & State'Img);
end if;
First_Char := Line_S;
@ -134,7 +144,11 @@ package body BDD.Parser is
if Starts_With (Buffer (First_Char .. Line_E), """""""") then
if State = In_String then
State := In_Scenario;
if Scenar /= No_Scenario then
State := In_Scenario;
else
State := In_Background;
end if;
elsif Step /= null then
State := In_String;
String_Indent := First_Char - Line_S + 1;
@ -161,7 +175,7 @@ package body BDD.Parser is
elsif Buffer (First_Char) = '|' then
case State is
when In_Scenario | In_Outline =>
when In_Background | In_Scenario | In_Outline =>
if Step /= null then
Trace (Me, "MANU Table=" & Buffer (First_Char .. Line_E));
else
@ -210,16 +224,23 @@ package body BDD.Parser is
& Image (Line, 1);
end if;
if Background /= No_Scenario then
raise Syntax_Error with
"A single Background can be defined, at "
& File.Display_Full_Name & ":"
& Image (Line, 1);
end if;
Finish_Scenario;
Scenar := Create
Background := Create
(Feature => F,
Name => Get_Line_End (First_Char + Cst_Background'Length),
Kind => Kind_Background,
Line => Line,
Index => Positive'Last);
Runner.Scenario_Start (Scenar);
State := In_Scenario;
Runner.Scenario_Start (Background);
State := In_Background;
elsif Starts_With (Buffer (First_Char .. Line_E), Cst_Scenario) then
if State = None then
@ -283,8 +304,8 @@ package body BDD.Parser is
or else Starts_With (Buffer (First_Char .. Line_E), Cst_But)
or else Starts_With (Buffer (First_Char .. Line_E), Cst_When)
then
if Scenar = No_Scenario then
raise Syntax_Error with "Step must be defined with in a"
if State = In_Feature then
raise Syntax_Error with "Step must be defined within a"
& " Scenario at " & File.Display_Full_Name & ":"
& Image (Line, 1);
end if;
@ -292,7 +313,12 @@ package body BDD.Parser is
Step := Create
(Text => Buffer (First_Char .. Line_E),
Line => Line);
Scenar.Add (Step);
if State = In_Scenario then
Scenar.Add (Step);
else
Background.Add (Step);
end if;
else
if State = None then
@ -304,6 +330,7 @@ package body BDD.Parser is
F.Add_To_Description (Buffer (First_Char .. Line_E));
elsif State = In_Scenario
or else State = In_Background
or else State = In_Outline
then
raise Syntax_Error with

View File

@ -49,9 +49,11 @@ package BDD.Parser is
-- none of its steps.
procedure Scenario_End
(Self : in out Abstract_Feature_Runner;
Scenario : BDD.Features.Scenario) is null;
(Self : in out Abstract_Feature_Runner;
Background : BDD.Features.Scenario;
Scenario : BDD.Features.Scenario) is null;
-- Called when the last step in a scenario has been seen.
-- Background contains extra steps to run the scenario's own steps
procedure Feature_End
(Self : in out Abstract_Feature_Runner;

View File

@ -131,42 +131,74 @@ package body BDD.Runner is
------------------
overriding procedure Scenario_End
(Self : in out Feature_Runner;
Scenario : BDD.Features.Scenario)
(Self : in out Feature_Runner;
Background : BDD.Features.Scenario;
Scenario : BDD.Features.Scenario)
is
procedure Run_Step (S : not null access Step_Record'Class);
Show_Steps : Boolean;
procedure Run_Step
(Scenario : BDD.Features.Scenario;
Step : not null access Step_Record'Class);
-- Run a specific step of the scenario
procedure Run_Step (S : not null access Step_Record'Class) is
procedure Run_Step
(Scenario : BDD.Features.Scenario;
Step : not null access Step_Record'Class) is
begin
case Scenario.Status is
when Status_Passed =>
-- ??? Simulate a run
delay 0.2;
if Scenario.Line = 5 then
S.Set_Status (Status_Passed);
elsif Scenario.Line = 10 then
S.Set_Status (Status_Failed);
elsif Scenario.Line = 11 then
S.Set_Status (Status_Undefined);
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
S.Set_Status (Status_Passed);
Step.Set_Status (Status_Passed);
end if;
Self.Steps_Stats (S.Status) := Self.Steps_Stats (S.Status) + 1;
Scenario.Set_Status (S.Status);
Self.Steps_Stats (Step.Status) :=
Self.Steps_Stats (Step.Status) + 1;
Scenario.Set_Status (Step.Status);
when Status_Failed | Status_Skipped | Status_Undefined =>
S.Set_Status (Status_Skipped);
Step.Set_Status (Status_Skipped);
end case;
Self.Format.Step_Completed (Scenario, S);
if Show_Steps then
Self.Format.Step_Completed (Scenario, Step);
end if;
end Run_Step;
begin
if Scenario.Kind = Kind_Scenario then
Scenario.Set_Status (Status_Passed);
if Background /= No_Scenario then
Show_Steps := Scenario.Get_Feature.Id /= Self.Current_Feature_Id;
Background.Set_Status (Status_Passed);
if Show_Steps then
Self.Format.Scenario_Start (Background);
Background.Foreach_Step (Run_Step'Access);
Self.Format.Scenario_Completed (Background);
else
Background.Foreach_Step (Run_Step'Access);
end if;
if Background.Status = Status_Passed then
Scenario.Set_Status (Status_Passed);
else
Scenario.Set_Status (Status_Skipped);
end if;
end if;
Show_Steps := True;
Self.Format.Scenario_Start (Scenario);
Scenario.Foreach_Step (Run_Step'Access);
Self.Format.Scenario_Completed (Scenario);

View File

@ -71,8 +71,9 @@ package BDD.Runner is
-- Called after the last feature has been run.
overriding procedure Scenario_End
(Self : in out Feature_Runner;
Scenario : BDD.Features.Scenario);
(Self : in out Feature_Runner;
Background : BDD.Features.Scenario;
Scenario : BDD.Features.Scenario);
private
type Feature_Runner is new BDD.Parser.Abstract_Feature_Runner with record