mirror of https://github.com/briot/gnatbdd
Add support for Formatters
This commit is contained in:
parent
b6e2af0284
commit
e52dd57fc4
|
@ -220,6 +220,9 @@ feature above would be better written as::
|
|||
Scenario: Logging with invalid user account
|
||||
When I log in as "Henry"
|
||||
Then I should see the login page
|
||||
|
||||
|
||||
The background must be defined before any scenario.
|
||||
|
||||
|
||||
Scenario outlines
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
-----------------------------------------------------------------------------
|
||||
-- g N A T C O L L --
|
||||
-- G N A T C O L L --
|
||||
-- --
|
||||
-- Copyright (C) 2014, AdaCore --
|
||||
-- --
|
||||
|
@ -32,7 +32,9 @@ package body BDD.Features is
|
|||
procedure Free (Self : in out Feature) is
|
||||
begin
|
||||
Self.File := No_File;
|
||||
Self.Displayed := False;
|
||||
Free (Self.Name);
|
||||
Self.Description := Null_Unbounded_String;
|
||||
end Free;
|
||||
|
||||
--------------
|
||||
|
@ -76,14 +78,23 @@ package body BDD.Features is
|
|||
return Self.File;
|
||||
end File;
|
||||
|
||||
---------
|
||||
-- Add --
|
||||
---------
|
||||
-------------------
|
||||
-- Set_Displayed --
|
||||
-------------------
|
||||
|
||||
procedure Add (Self : in out Feature; Scenar : Scenario'Class) is
|
||||
procedure Set_Displayed (Self : in out Feature) is
|
||||
begin
|
||||
Self.Scenarios.Append (Scenar);
|
||||
end Add;
|
||||
Self.Displayed := True;
|
||||
end Set_Displayed;
|
||||
|
||||
---------------
|
||||
-- Displayed --
|
||||
---------------
|
||||
|
||||
function Displayed (Self : Feature) return Boolean is
|
||||
begin
|
||||
return Self.Displayed;
|
||||
end Displayed;
|
||||
|
||||
----------
|
||||
-- Free --
|
||||
|
@ -91,18 +102,55 @@ package body BDD.Features is
|
|||
|
||||
procedure Free (Self : in out Scenario) is
|
||||
begin
|
||||
Self.Name := Null_Unbounded_String;
|
||||
Self.Line := 1;
|
||||
Self.Index := 1;
|
||||
Self.Outline := False;
|
||||
Self.Name := Null_Unbounded_String;
|
||||
Self.Line := 1;
|
||||
Self.Index := 1;
|
||||
Self.Kind := Kind_Scenario;
|
||||
end Free;
|
||||
|
||||
--------------
|
||||
-- Set_Name --
|
||||
--------------
|
||||
----------
|
||||
-- Name --
|
||||
----------
|
||||
|
||||
procedure Set_Name
|
||||
function Name (Self : Scenario) return String is
|
||||
begin
|
||||
return To_String (Self.Name);
|
||||
end Name;
|
||||
|
||||
----------
|
||||
-- Line --
|
||||
----------
|
||||
|
||||
function Line (Self : Scenario) return Positive is
|
||||
begin
|
||||
return Self.Line;
|
||||
end Line;
|
||||
|
||||
-----------
|
||||
-- Index --
|
||||
-----------
|
||||
|
||||
function Index (Self : Scenario) return Positive is
|
||||
begin
|
||||
return Self.Index;
|
||||
end Index;
|
||||
|
||||
----------
|
||||
-- Kind --
|
||||
----------
|
||||
|
||||
function Kind (Self : Scenario) return Scenario_Kind is
|
||||
begin
|
||||
return Self.Kind;
|
||||
end Kind;
|
||||
|
||||
--------------------
|
||||
-- Set_Attributes --
|
||||
--------------------
|
||||
|
||||
procedure Set_Attributes
|
||||
(Self : in out Scenario;
|
||||
Kind : Scenario_Kind;
|
||||
Name : String;
|
||||
Line : Positive;
|
||||
Index : Positive)
|
||||
|
@ -111,15 +159,26 @@ package body BDD.Features is
|
|||
Self.Name := To_Unbounded_String (Trim (Name, Both));
|
||||
Self.Line := Line;
|
||||
Self.Index := Index;
|
||||
end Set_Name;
|
||||
Self.Kind := Kind;
|
||||
end Set_Attributes;
|
||||
|
||||
--------------------
|
||||
-- Set_Is_Outline --
|
||||
--------------------
|
||||
-----------------
|
||||
-- Description --
|
||||
-----------------
|
||||
|
||||
procedure Set_Is_Outline (Self : in out Scenario) is
|
||||
function Description (Self : Feature) return String is
|
||||
begin
|
||||
Self.Outline := True;
|
||||
end Set_Is_Outline;
|
||||
return To_String (Self.Description);
|
||||
end Description;
|
||||
|
||||
---------------------
|
||||
-- Add_Description --
|
||||
---------------------
|
||||
|
||||
procedure Add_Description (Self : in out Feature; Descr : String) is
|
||||
begin
|
||||
-- Indent the description as it will be displayed
|
||||
Append (Self.Description, " " & Descr & ASCII.LF);
|
||||
end Add_Description;
|
||||
|
||||
end BDD.Features;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
-----------------------------------------------------------------------------
|
||||
-- g N A T C O L L --
|
||||
-- G N A T C O L L --
|
||||
-- --
|
||||
-- Copyright (C) 2014, AdaCore --
|
||||
-- --
|
||||
|
@ -23,10 +23,7 @@
|
|||
|
||||
-- A feature and its scenarios.
|
||||
|
||||
with Ada.Containers.Indefinite_Doubly_Linked_Lists;
|
||||
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
|
||||
with GNAT.Strings; use GNAT.Strings;
|
||||
with GNATCOLL.VFS; use GNATCOLL.VFS;
|
||||
|
||||
package BDD.Features is
|
||||
|
||||
|
@ -40,17 +37,22 @@ package BDD.Features is
|
|||
procedure Free (Self : in out Scenario);
|
||||
-- Free the memory associated with Self
|
||||
|
||||
procedure Set_Name
|
||||
type Scenario_Kind is (Kind_Scenario, Kind_Background, Kind_Outline);
|
||||
|
||||
procedure Set_Attributes
|
||||
(Self : in out Scenario;
|
||||
Kind : Scenario_Kind;
|
||||
Name : String;
|
||||
Line : Positive;
|
||||
Index : Positive);
|
||||
-- Set the line of the feature file at which the scenario is defined, and
|
||||
-- its index within its Feature.
|
||||
|
||||
procedure Set_Is_Outline (Self : in out Scenario);
|
||||
-- The scenario is in fact an outline, which will run itself multiple
|
||||
-- times and use values from a table to generate each test.
|
||||
function Name (Self : Scenario) return String;
|
||||
function Line (Self : Scenario) return Positive;
|
||||
function Index (Self : Scenario) return Positive;
|
||||
function Kind (Self : Scenario) return Scenario_Kind;
|
||||
-- Retrieve the attributes of Self
|
||||
|
||||
-------------
|
||||
-- Feature --
|
||||
|
@ -73,25 +75,32 @@ package BDD.Features is
|
|||
function File (Self : Feature) return GNATCOLL.VFS.Virtual_File;
|
||||
-- The file in which the feature is defined
|
||||
|
||||
procedure Add (Self : in out Feature; Scenar : Scenario'Class);
|
||||
-- Add a copy of the scenario to the feature
|
||||
procedure Set_Displayed (Self : in out Feature);
|
||||
function Displayed (Self : Feature) return Boolean;
|
||||
-- Whether the feature has already been displayed on the screen.
|
||||
-- This is used when displaying the output, because some of the tests
|
||||
-- could be filtered out, and we only want to display the feature info
|
||||
-- when at least one scenario is run.
|
||||
|
||||
function Description (Self : Feature) return String;
|
||||
procedure Add_Description (Self : in out Feature; Descr : String);
|
||||
-- Add some description information. Add_Description will be called once
|
||||
-- for each line in the description.
|
||||
|
||||
private
|
||||
type Scenario is tagged record
|
||||
Name : Ada.Strings.Unbounded.Unbounded_String;
|
||||
Line : Positive := 1;
|
||||
Index : Positive := 1;
|
||||
Outline : Boolean := False;
|
||||
Kind : Scenario_Kind := Kind_Scenario;
|
||||
end record;
|
||||
-- Make sure this type can be put in a list and automatically reclaim
|
||||
-- storage when the list is clearer.
|
||||
|
||||
package Scenario_Lists is new Ada.Containers.Indefinite_Doubly_Linked_Lists
|
||||
(Scenario'Class);
|
||||
|
||||
type Feature is tagged limited record
|
||||
File : GNATCOLL.VFS.Virtual_File;
|
||||
Name : GNAT.Strings.String_Access;
|
||||
Scenarios : Scenario_Lists.List;
|
||||
File : GNATCOLL.VFS.Virtual_File;
|
||||
Name : GNAT.Strings.String_Access;
|
||||
Displayed : Boolean := False;
|
||||
Description : Ada.Strings.Unbounded.Unbounded_String;
|
||||
end record;
|
||||
end BDD.Features;
|
||||
|
|
|
@ -0,0 +1,112 @@
|
|||
-----------------------------------------------------------------------------
|
||||
-- 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 Ada.Text_IO; use Ada.Text_IO;
|
||||
with GNATCOLL.Utils; use GNATCOLL.Utils;
|
||||
|
||||
package body BDD.Formatters is
|
||||
|
||||
procedure Display_Location
|
||||
(Self : Formatter'Class;
|
||||
Feature : BDD.Features.Feature'Class;
|
||||
Scenario : BDD.Features.Scenario'Class);
|
||||
-- Display location information for the scenario
|
||||
|
||||
----------
|
||||
-- Init --
|
||||
----------
|
||||
|
||||
procedure Init
|
||||
(Self : in out Formatter;
|
||||
Term : GNATCOLL.Terminal.Terminal_Info_Access)
|
||||
is
|
||||
begin
|
||||
Self.Term := Term;
|
||||
end Init;
|
||||
|
||||
----------------------
|
||||
-- Display_Location --
|
||||
----------------------
|
||||
|
||||
procedure Display_Location
|
||||
(Self : Formatter'Class;
|
||||
Feature : BDD.Features.Feature'Class;
|
||||
Scenario : BDD.Features.Scenario'Class)
|
||||
is
|
||||
begin
|
||||
Self.Term.Set_Color
|
||||
(Term => Ada.Text_IO.Standard_Output,
|
||||
Foreground => Grey);
|
||||
Put ("# "
|
||||
& (+Feature.File.Relative_Path (Features_Directory))
|
||||
& "#" & Image (Scenario.Index, 1)
|
||||
& ":" & Image (Scenario.Line, 1));
|
||||
Self.Term.Set_Color
|
||||
(Term => Ada.Text_IO.Standard_Output,
|
||||
Style => Reset_All);
|
||||
New_Line;
|
||||
end Display_Location;
|
||||
|
||||
---------------------
|
||||
-- Display_Feature --
|
||||
---------------------
|
||||
|
||||
overriding procedure Display_Feature
|
||||
(Self : Formatter_Full;
|
||||
Feature : BDD.Features.Feature'Class)
|
||||
is
|
||||
pragma Unreferenced (Self);
|
||||
begin
|
||||
Put_Line (Cst_Features & ' ' & Feature.Name);
|
||||
Put_Line (Feature.Description);
|
||||
end Display_Feature;
|
||||
|
||||
----------------------
|
||||
-- Display_Scenario --
|
||||
----------------------
|
||||
|
||||
overriding procedure Display_Scenario
|
||||
(Self : Formatter_Full;
|
||||
Feature : BDD.Features.Feature'Class;
|
||||
Scenario : BDD.Features.Scenario'Class)
|
||||
is
|
||||
begin
|
||||
Put (" " & Cst_Scenario & ' ' & Scenario.Name & " ");
|
||||
Display_Location (Self, Feature, Scenario);
|
||||
end Display_Scenario;
|
||||
|
||||
------------------------
|
||||
-- Scenario_Completed --
|
||||
------------------------
|
||||
|
||||
overriding procedure Scenario_Completed
|
||||
(Self : Formatter_Full;
|
||||
Feature : BDD.Features.Feature'Class;
|
||||
Scenario : BDD.Features.Scenario'Class)
|
||||
is
|
||||
pragma Unreferenced (Self, Feature, Scenario);
|
||||
begin
|
||||
New_Line;
|
||||
end Scenario_Completed;
|
||||
|
||||
end BDD.Formatters;
|
|
@ -0,0 +1,80 @@
|
|||
-----------------------------------------------------------------------------
|
||||
-- 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/>. --
|
||||
-- --
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
-- The formatters are used to display output for the user
|
||||
|
||||
with BDD.Features; use BDD.Features;
|
||||
with GNATCOLL.Terminal; use GNATCOLL.Terminal;
|
||||
|
||||
package BDD.Formatters is
|
||||
|
||||
type Formatter is abstract tagged private;
|
||||
|
||||
procedure Init
|
||||
(Self : in out Formatter;
|
||||
Term : GNATCOLL.Terminal.Terminal_Info_Access);
|
||||
-- Prepare the output for a specific terminal.
|
||||
-- This controls, among other things, whether the output supports colors.
|
||||
|
||||
procedure Display_Feature
|
||||
(Self : Formatter;
|
||||
Feature : BDD.Features.Feature'Class) is null;
|
||||
-- Display information about a feature just before it is run, for instance
|
||||
-- its name and description.
|
||||
|
||||
procedure Display_Scenario
|
||||
(Self : Formatter;
|
||||
Feature : BDD.Features.Feature'Class;
|
||||
Scenario : BDD.Features.Scenario'Class) is null;
|
||||
-- Display information about a scenario just before it is run
|
||||
|
||||
procedure Scenario_Completed
|
||||
(Self : Formatter;
|
||||
Feature : BDD.Features.Feature'Class;
|
||||
Scenario : BDD.Features.Scenario'Class) is null;
|
||||
-- Called when a scenario has completed
|
||||
|
||||
type Formatter_Full is new Formatter with private;
|
||||
-- A formatter that displays all the features, scenarios and steps that
|
||||
-- are executed
|
||||
|
||||
overriding procedure Display_Feature
|
||||
(Self : Formatter_Full;
|
||||
Feature : BDD.Features.Feature'Class);
|
||||
overriding procedure Display_Scenario
|
||||
(Self : Formatter_Full;
|
||||
Feature : BDD.Features.Feature'Class;
|
||||
Scenario : BDD.Features.Scenario'Class);
|
||||
overriding procedure Scenario_Completed
|
||||
(Self : Formatter_Full;
|
||||
Feature : BDD.Features.Feature'Class;
|
||||
Scenario : BDD.Features.Scenario'Class);
|
||||
|
||||
private
|
||||
type Formatter is abstract tagged record
|
||||
Term : GNATCOLL.Terminal.Terminal_Info_Access;
|
||||
end record;
|
||||
|
||||
type Formatter_Full is new Formatter with null record;
|
||||
|
||||
end BDD.Formatters;
|
|
@ -1,5 +1,5 @@
|
|||
-----------------------------------------------------------------------------
|
||||
-- g N A T C O L L --
|
||||
-- G N A T C O L L --
|
||||
-- --
|
||||
-- Copyright (C) 2014, AdaCore --
|
||||
-- --
|
||||
|
@ -23,41 +23,30 @@
|
|||
|
||||
with GNATCOLL.Traces; use GNATCOLL.Traces;
|
||||
with GNATCOLL.Utils; use GNATCOLL.Utils;
|
||||
with GNAT.Strings; use GNAT.Strings;
|
||||
|
||||
package body BDD.Parser is
|
||||
Me : constant Trace_Handle := Create ("BDD.PARSER");
|
||||
|
||||
Cst_Features : constant String := "Feature:";
|
||||
Cst_Scenario : constant String := "Scenario:";
|
||||
Cst_Scenario_Outline : constant String := "Scenario Outline:";
|
||||
Cst_Given : constant String := "Given";
|
||||
Cst_And : constant String := "And";
|
||||
Cst_Then : constant String := "Then";
|
||||
Cst_But : constant String := "But";
|
||||
Cst_When : constant String := "When";
|
||||
Cst_Background : constant String := "Background";
|
||||
Cst_Examples : constant String := "Examples:";
|
||||
Cst_Scenarios : constant String := "Scenarios:";
|
||||
-- The keywords when parsing a file
|
||||
|
||||
-----------
|
||||
-- Parse --
|
||||
-----------
|
||||
|
||||
procedure Parse
|
||||
(Self : Feature_Parser;
|
||||
File : GNATCOLL.VFS.Virtual_File;
|
||||
Callback : access procedure (F : BDD.Features.Feature))
|
||||
(Self : Feature_Parser;
|
||||
File : GNATCOLL.VFS.Virtual_File;
|
||||
Runner : in out Abstract_Feature_Runner'Class)
|
||||
is
|
||||
pragma Unreferenced (Self);
|
||||
|
||||
type State_Type is (None, In_Feature, In_Scenario, In_String,
|
||||
type State_Type is (None, In_Feature,
|
||||
In_Scenario,
|
||||
In_String,
|
||||
In_Outline, In_Examples);
|
||||
State : State_Type := None;
|
||||
F : Feature;
|
||||
Scenar : Scenario;
|
||||
Buffer : GNAT.Strings.String_Access := File.Read_File;
|
||||
Seen_Scenar : Boolean := False; -- Whether we have one scenario in F
|
||||
Index : Integer := Buffer'First;
|
||||
Line : Natural := 0;
|
||||
Index_In_Feature : Natural := 0;
|
||||
|
@ -70,6 +59,9 @@ package body BDD.Parser is
|
|||
procedure Finish_Feature;
|
||||
-- Called when the end of a scenario or a feature are seen
|
||||
|
||||
function Get_Line_End (After : Positive) return String;
|
||||
-- Return the line text after the given character
|
||||
|
||||
---------------------
|
||||
-- Finish_Scenario --
|
||||
---------------------
|
||||
|
@ -78,9 +70,10 @@ package body BDD.Parser is
|
|||
begin
|
||||
case State is
|
||||
when In_Scenario | In_Outline | In_Examples =>
|
||||
F.Add (Scenar);
|
||||
Runner.Scenario_End (F, Scenar);
|
||||
Free (Scenar);
|
||||
State := In_Feature;
|
||||
Seen_Scenar := True;
|
||||
|
||||
when others =>
|
||||
null;
|
||||
|
@ -95,12 +88,27 @@ package body BDD.Parser is
|
|||
begin
|
||||
Finish_Scenario;
|
||||
if State /= None then
|
||||
Callback (F);
|
||||
Runner.Feature_End (F);
|
||||
Free (F);
|
||||
end if;
|
||||
|
||||
State := None;
|
||||
Seen_Scenar := False;
|
||||
end Finish_Feature;
|
||||
|
||||
------------------
|
||||
-- Get_Line_End --
|
||||
------------------
|
||||
|
||||
function Get_Line_End (After : Positive) return String is
|
||||
begin
|
||||
if After <= Line_E then
|
||||
return Buffer (After .. Line_E);
|
||||
else
|
||||
return "";
|
||||
end if;
|
||||
end Get_Line_End;
|
||||
|
||||
begin
|
||||
Trace (Me, "Parsing " & File.Display_Full_Name);
|
||||
|
||||
|
@ -180,6 +188,7 @@ package body BDD.Parser is
|
|||
Finish_Feature;
|
||||
F.Set_File (File);
|
||||
F.Set_Name (Buffer (First_Char + Cst_Features'Length .. Line_E));
|
||||
Runner.Feature_Start (F);
|
||||
Index_In_Feature := 0;
|
||||
State := In_Feature;
|
||||
|
||||
|
@ -190,11 +199,19 @@ package body BDD.Parser is
|
|||
& Image (Line, 1);
|
||||
end if;
|
||||
|
||||
if Seen_Scenar then
|
||||
raise Syntax_Error with "Background must be defined before all"
|
||||
& " Scenario, at " & File.Display_Full_Name & ":"
|
||||
& Image (Line, 1);
|
||||
end if;
|
||||
|
||||
Finish_Scenario;
|
||||
|
||||
-- ??? handle background
|
||||
null;
|
||||
|
||||
Scenar.Set_Attributes
|
||||
(Name => Get_Line_End (First_Char + Cst_Background'Length),
|
||||
Kind => Kind_Background,
|
||||
Line => Line,
|
||||
Index => Positive'Last);
|
||||
Runner.Scenario_Start (F, Scenar);
|
||||
State := In_Scenario;
|
||||
|
||||
elsif Starts_With (Buffer (First_Char .. Line_E), Cst_Scenario) then
|
||||
|
@ -206,10 +223,12 @@ package body BDD.Parser is
|
|||
|
||||
Finish_Scenario;
|
||||
Index_In_Feature := Index_In_Feature + 1;
|
||||
Scenar.Set_Name
|
||||
(Name => Buffer (First_Char + Cst_Scenario'Length .. Line_E),
|
||||
Scenar.Set_Attributes
|
||||
(Name => Get_Line_End (First_Char + Cst_Scenario'Length),
|
||||
Kind => Kind_Scenario,
|
||||
Line => Line,
|
||||
Index => Index_In_Feature);
|
||||
Runner.Scenario_Start (F, Scenar);
|
||||
State := In_Scenario;
|
||||
|
||||
elsif Starts_With (Buffer (First_Char .. Line_E),
|
||||
|
@ -223,11 +242,12 @@ package body BDD.Parser is
|
|||
|
||||
Finish_Scenario;
|
||||
Index_In_Feature := Index_In_Feature + 1;
|
||||
Scenar.Set_Is_Outline;
|
||||
Scenar.Set_Name
|
||||
(Name => Buffer (First_Char + Cst_Scenario'Length .. Line_E),
|
||||
Scenar.Set_Attributes
|
||||
(Name => Get_Line_End (First_Char + Cst_Scenario_Outline'Length),
|
||||
Kind => Kind_Outline,
|
||||
Line => Line,
|
||||
Index => Index_In_Feature);
|
||||
Runner.Scenario_Start (F, Scenar);
|
||||
State := In_Outline;
|
||||
|
||||
elsif Starts_With (Buffer (First_Char .. Line_E), Cst_Scenarios) then
|
||||
|
@ -270,14 +290,18 @@ package body BDD.Parser is
|
|||
& Image (Line, 1);
|
||||
|
||||
elsif State = In_Feature then
|
||||
-- Ignored, these are the comments for the feature
|
||||
null;
|
||||
F.Add_Description (Buffer (First_Char .. Line_E));
|
||||
|
||||
elsif State = In_Scenario
|
||||
or else State = In_Outline
|
||||
then
|
||||
raise Syntax_Error with "Expected line starting with Given/And/"
|
||||
& "But/When/Then at " & File.Display_Full_Name & ":"
|
||||
raise Syntax_Error with
|
||||
"Expected line starting with "
|
||||
& Cst_Given & "/"
|
||||
& Cst_When & "/"
|
||||
& Cst_Then & "/"
|
||||
& Cst_And & "/"
|
||||
& Cst_But & ", at " & File.Display_Full_Name & ":"
|
||||
& Image (Line, 1);
|
||||
end if;
|
||||
end if;
|
||||
|
|
|
@ -24,21 +24,52 @@
|
|||
-- A parser for the features files
|
||||
|
||||
with BDD.Features; use BDD.Features;
|
||||
with GNATCOLL.VFS; use GNATCOLL.VFS;
|
||||
|
||||
package BDD.Parser is
|
||||
|
||||
Syntax_Error : exception;
|
||||
-- Raised when reading one of the features file raises a syntax error.
|
||||
|
||||
type Abstract_Feature_Runner is interface;
|
||||
-- The type that is responsible for running the features and scenarios
|
||||
-- found by the parser.
|
||||
|
||||
procedure Feature_Start
|
||||
(Self : in out Abstract_Feature_Runner;
|
||||
Feature : in out BDD.Features.Feature'Class) is null;
|
||||
-- Called on the first line of a feature.
|
||||
-- At this stage, only the name and file or the feature are known, but none
|
||||
-- of its scenarios
|
||||
|
||||
procedure Scenario_Start
|
||||
(Self : in out Abstract_Feature_Runner;
|
||||
Feature : in out BDD.Features.Feature'Class;
|
||||
Scenario : in out BDD.Features.Scenario'Class) is null;
|
||||
-- Called on the first line of a scenario.
|
||||
-- At this stage, only the name and location of the scenario are known, but
|
||||
-- none of its steps.
|
||||
|
||||
procedure Scenario_End
|
||||
(Self : in out Abstract_Feature_Runner;
|
||||
Feature : in out BDD.Features.Feature'Class;
|
||||
Scenario : in out BDD.Features.Scenario'Class) is null;
|
||||
-- Called when the last step in a scenario has been seen.
|
||||
|
||||
procedure Feature_End
|
||||
(Self : in out Abstract_Feature_Runner;
|
||||
Feature : in out BDD.Features.Feature'Class) is null;
|
||||
-- Called when the last line of a feature has been seen.
|
||||
|
||||
type Feature_Parser is tagged private;
|
||||
|
||||
procedure Parse
|
||||
(Self : Feature_Parser;
|
||||
File : GNATCOLL.VFS.Virtual_File;
|
||||
Callback : access procedure (F : BDD.Features.Feature));
|
||||
Runner : in out Abstract_Feature_Runner'Class);
|
||||
-- Parses a .feature file.
|
||||
-- Calls Callback for each of the features found.
|
||||
--
|
||||
-- Calls Runner.Run_Scenario for each scenario found.
|
||||
--
|
||||
-- Raises Syntax_Error when the file does not contain valid syntax.
|
||||
|
||||
private
|
||||
|
|
|
@ -21,8 +21,6 @@
|
|||
-- --
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
with BDD.Parser; use BDD.Parser;
|
||||
|
||||
package body BDD.Runner is
|
||||
|
||||
--------------
|
||||
|
@ -79,22 +77,46 @@ package body BDD.Runner is
|
|||
Append (Self.Files, Files);
|
||||
end Register;
|
||||
|
||||
--------------
|
||||
-- For_Each --
|
||||
--------------
|
||||
---------
|
||||
-- Run --
|
||||
---------
|
||||
|
||||
procedure For_Each
|
||||
(Self : in out Feature_Runner;
|
||||
Callback : access procedure (F : Feature))
|
||||
procedure Run
|
||||
(Self : in out Feature_Runner;
|
||||
Format : not null access BDD.Formatters.Formatter'Class;
|
||||
Parser : BDD.Parser.Feature_Parser'Class)
|
||||
is
|
||||
Parser : Feature_Parser;
|
||||
begin
|
||||
if Self.Files /= null then
|
||||
Self.Format := Format;
|
||||
Sort (Self.Files.all);
|
||||
for F in Self.Files'Range loop
|
||||
Parser.Parse (Self.Files (F), Callback);
|
||||
Parser.Parse (Self.Files (F), Self);
|
||||
end loop;
|
||||
Self.Format := null;
|
||||
end if;
|
||||
end For_Each;
|
||||
end Run;
|
||||
|
||||
------------------
|
||||
-- Scenario_End --
|
||||
------------------
|
||||
|
||||
overriding procedure Scenario_End
|
||||
(Self : in out Feature_Runner;
|
||||
Feature : in out BDD.Features.Feature'Class;
|
||||
Scenario : in out BDD.Features.Scenario'Class)
|
||||
is
|
||||
begin
|
||||
if Scenario.Kind = Kind_Scenario then
|
||||
if not Feature.Displayed then
|
||||
Self.Format.Display_Feature (Feature);
|
||||
Feature.Set_Displayed;
|
||||
end if;
|
||||
|
||||
Self.Format.Display_Scenario (Feature, Scenario);
|
||||
|
||||
Self.Format.Scenario_Completed (Feature, Scenario);
|
||||
end if;
|
||||
end Scenario_End;
|
||||
|
||||
end BDD.Runner;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
-----------------------------------------------------------------------------
|
||||
-- g N A T C O L L --
|
||||
-- G N A T C O L L --
|
||||
-- --
|
||||
-- Copyright (C) 2014, AdaCore --
|
||||
-- --
|
||||
|
@ -24,11 +24,12 @@
|
|||
-- Manipulating features files
|
||||
|
||||
with BDD.Features; use BDD.Features;
|
||||
with GNATCOLL.VFS; use GNATCOLL.VFS;
|
||||
with BDD.Formatters; use BDD.Formatters;
|
||||
with BDD.Parser; use BDD.Parser;
|
||||
|
||||
package BDD.Runner is
|
||||
|
||||
type Feature_Runner is tagged private;
|
||||
type Feature_Runner is new BDD.Parser.Abstract_Feature_Runner with private;
|
||||
-- This type is responsible for running each of the features that are
|
||||
-- registered.
|
||||
-- You can either register features files explicitly, or by using the
|
||||
|
@ -49,14 +50,28 @@ package BDD.Runner is
|
|||
Files : GNATCOLL.VFS.File_Array);
|
||||
-- Register one or more features file explicitly.
|
||||
|
||||
procedure For_Each
|
||||
(Self : in out Feature_Runner;
|
||||
Callback : access procedure (F : Feature));
|
||||
-- Calls Callback for each of the registered features file.
|
||||
procedure Run
|
||||
(Self : in out Feature_Runner;
|
||||
Format : not null access BDD.Formatters.Formatter'Class;
|
||||
Parser : BDD.Parser.Feature_Parser'Class);
|
||||
-- Run all features and their scenarios.
|
||||
--
|
||||
-- Each of the features file is parsed through Parser. This allows you to
|
||||
-- support various syntaxes for the files.
|
||||
--
|
||||
-- The features are run in alphabetical order of the file name, and the
|
||||
-- scenarios are run in the order they were defined in in the features
|
||||
-- file.
|
||||
|
||||
overriding procedure Scenario_End
|
||||
(Self : in out Feature_Runner;
|
||||
Feature : in out BDD.Features.Feature'Class;
|
||||
Scenario : in out BDD.Features.Scenario'Class);
|
||||
|
||||
private
|
||||
type Feature_Runner is tagged record
|
||||
Files : GNATCOLL.VFS.File_Array_Access;
|
||||
type Feature_Runner is new BDD.Parser.Abstract_Feature_Runner with record
|
||||
Files : GNATCOLL.VFS.File_Array_Access;
|
||||
Format : access BDD.Formatters.Formatter'Class;
|
||||
end record;
|
||||
|
||||
end BDD.Runner;
|
||||
|
|
23
src/bdd.ads
23
src/bdd.ads
|
@ -21,6 +21,29 @@
|
|||
-- --
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
with GNAT.Strings; use GNAT.Strings;
|
||||
with GNATCOLL.VFS; use GNATCOLL.VFS;
|
||||
|
||||
package BDD is
|
||||
|
||||
Cst_Features : constant String := "Feature:";
|
||||
Cst_Scenario : constant String := "Scenario:";
|
||||
Cst_Scenario_Outline : constant String := "Scenario Outline:";
|
||||
Cst_Given : constant String := "Given";
|
||||
Cst_And : constant String := "And";
|
||||
Cst_Then : constant String := "Then";
|
||||
Cst_But : constant String := "But";
|
||||
Cst_When : constant String := "When";
|
||||
Cst_Background : constant String := "Background";
|
||||
Cst_Examples : constant String := "Examples:";
|
||||
Cst_Scenarios : constant String := "Scenarios:";
|
||||
-- The keywords when parsing a .feature file
|
||||
|
||||
Features_Directory : GNATCOLL.VFS.Virtual_File :=
|
||||
Create_From_Base ("features");
|
||||
-- The parent directory for all features file.
|
||||
|
||||
Features_File_Ext : GNAT.Strings.String_Access := new String'(".feature");
|
||||
-- Extension for the features file
|
||||
|
||||
end BDD;
|
||||
|
|
|
@ -1,24 +1,50 @@
|
|||
-----------------------------------------------------------------------------
|
||||
-- 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/>. --
|
||||
-- --
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
-- An example test driver.
|
||||
-- Such code would be automatically generated by gnattdd
|
||||
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with BDD.Runner; use BDD.Runner;
|
||||
with BDD.Features; use BDD.Features;
|
||||
with GNATCOLL.Traces; use GNATCOLL.Traces;
|
||||
with GNATCOLL.VFS; use GNATCOLL.VFS;
|
||||
with BDD; use BDD;
|
||||
with BDD.Formatters; use BDD.Formatters;
|
||||
with BDD.Parser; use BDD.Parser;
|
||||
with BDD.Runner; use BDD.Runner;
|
||||
with GNATCOLL.Terminal; use GNATCOLL.Terminal;
|
||||
with GNATCOLL.Traces; use GNATCOLL.Traces;
|
||||
with GNATCOLL.VFS; use GNATCOLL.VFS;
|
||||
|
||||
procedure Driver is
|
||||
procedure Run_Feature (F : Feature);
|
||||
procedure Run_Feature (F : Feature) is
|
||||
begin
|
||||
Put_Line ("MANU Found feature: " & F.File.Display_Full_Name
|
||||
& " => " & F.Name);
|
||||
end Run_Feature;
|
||||
|
||||
Features : BDD.Runner.Feature_Runner;
|
||||
Parser : BDD.Parser.Feature_Parser;
|
||||
Format : constant access BDD.Formatters.Formatter'Class :=
|
||||
new BDD.Formatters.Formatter_Full;
|
||||
Term : constant Terminal_Info_Access := new Terminal_Info;
|
||||
begin
|
||||
GNATCOLL.Traces.Parse_Config_File;
|
||||
Features.Discover (".feature", Create_From_Base ("features"));
|
||||
Features.For_Each (Run_Feature'Access);
|
||||
|
||||
Features.Discover (+BDD.Features_File_Ext.all, BDD.Features_Directory);
|
||||
|
||||
Term.Init_For_Stdout (Colors => Auto);
|
||||
Format.Init (Term);
|
||||
|
||||
Features.Run (Format, Parser);
|
||||
end Driver;
|
||||
|
|
Loading…
Reference in New Issue