opentoken_6.0.3_b278f6ad/Test/analyzer_buffer_test.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
-------------------------------------------------------------------------------
--
--  Copyright (C) 2014 Stephen Leake
--
--  This file is part of the OpenToken package.
--
--  The OpenToken package is free software; you can redistribute it
--  and/or modify it under the terms of the GNU General Public License
--  as published by the Free Software Foundation; either version 3, or
--  (at your option) any later version. The OpenToken package is
--  distributed in the hope that it will be useful, but WITHOUT ANY
--  WARRANTY; without even the implied warranty of MERCHANTABILITY or
--  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
--  License for more details. You should have received a copy of the
--  GNU General Public License distributed with the OpenToken package;
--  see file GPL.txt. If not, write to the Free Software Foundation,
--  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

pragma License (GPL);

with AUnit.Assertions;
with AUnit.Check;
with Ada.Directories;
with Ada.Exceptions;
with Ada.Strings.Maps.Constants;
with Ada.Text_IO;
with GNAT.OS_Lib;
with OpenToken.Recognizer.Character_Set;
with OpenToken.Recognizer.Identifier;
with OpenToken.Text_Feeder.Counted_GNAT_OS_Lib;
with OpenToken.Text_Feeder.String;
with OpenToken.Token.Enumerated.Analyzer;
package body Analyzer_Buffer_Test is

   type Token_ID is (Identifier_ID, Whitespace);

   package Master_Example_Token is new OpenToken.Token.Enumerated
     (Token_ID, Token_ID'First, Token_ID'Last, Token_ID'Image);
   package Tokenizer is new Master_Example_Token.Analyzer;

   procedure Check is new AUnit.Check.Gen_Check_Discrete (Token_ID);

   Syntax : constant Tokenizer.Syntax :=
     (Identifier_ID     => Tokenizer.Get
        (OpenToken.Recognizer.Identifier.Get
           (Start_Chars => Ada.Strings.Maps.Constants.Alphanumeric_Set,
            Body_Chars  => Ada.Strings.Maps.Constants.Alphanumeric_Set)),
      Whitespace        => Tokenizer.Get
        (OpenToken.Recognizer.Character_Set.Get (OpenToken.Recognizer.Character_Set.Standard_Whitespace)));

   String_Feeder : aliased OpenToken.Text_Feeder.String.Instance;
   Analyzer      : constant Tokenizer.Handle := Tokenizer.Initialize (Syntax, null, Buffer_Size => 10);

   procedure Step
     (Label           : in String;
      Expected_ID     : in Token_ID;
      Expected_Lexeme : in String)
   is
      use Tokenizer;
      use AUnit.Check;
   begin
      Analyzer.Find_Next (Look_Ahead => False);

      Check (Label & ".ID", Analyzer.ID, Expected_ID);
      Check (Label & ".lexeme", Analyzer.Lexeme, Expected_Lexeme);
   end Step;

   ----------
   --  Test procedures

   procedure Nominal_String (T : in out AUnit.Test_Cases.Test_Case'Class)
   is
      pragma Unreferenced (T);

      Text : constant String := "a23456 b90123";
   begin
      --  Verify that the analyzer tokenizes properly when tokens
      --  cross the internal buffer end, when reading from a
      --  String text feeder.

      OpenToken.Text_Feeder.String.Set (String_Feeder, Text);
      Analyzer.Feeder := String_Feeder'Access;
      Analyzer.Reset;

      Step ("1", Identifier_ID, "a23456");
      Step ("2", Identifier_ID, "b90123");

   end Nominal_String;

   procedure Nominal_Counted_Gnat (T : in out AUnit.Test_Cases.Test_Case'Class)
   is
      pragma Unreferenced (T);
      use Ada.Directories;
      use Ada.Text_IO;
      use GNAT.OS_Lib;

      Text      : constant String := "a23456 b90123";
      File_Name : constant String := "analyzer_buffer_test.text";
      Text_File : File_Type;
   begin
      --  Verify that the analyzer tokenizes properly when tokens
      --  cross the internal buffer end, when reading from a
      --  Counted_GNAT_OS_Lib text feeder.
      --
      --  There used to be a bug in Counted_GNAT_OS_Lib.End_Of_File
      --  that caused this to fail

      if Exists (File_Name) then
         Delete_File (File_Name);
      end if;
      Create (Text_File, Out_File, File_Name);
      Put (Text_File, Text);
      Close (Text_File);

      Analyzer.Feeder := OpenToken.Text_Feeder.Counted_GNAT_OS_Lib.Create
        (Open_Read (File_Name, Binary));

      OpenToken.Text_Feeder.Counted_GNAT_OS_Lib.Instance (Analyzer.Feeder.all).Reset (Text'Length);
      Analyzer.Reset;

      Step ("1", Identifier_ID, "a23456");
      Step ("2", Identifier_ID, "b90123");

   end Nominal_Counted_Gnat;

   procedure Buffer_Full (T : in out AUnit.Test_Cases.Test_Case'Class)
   is
      pragma Unreferenced (T);
      use AUnit.Check;

      Text : constant String := "a2345678901";
   begin
      --  Verify that the analyzer raises a nice error message when a
      --  token is bigger than the internal buffer.

      OpenToken.Text_Feeder.String.Set (String_Feeder, Text);
      Analyzer.Feeder := String_Feeder'Access;
      Analyzer.Reset;

      Analyzer.Find_Next (Look_Ahead => False);
      AUnit.Assertions.Assert (False, "did not get exception");
   exception
   when E : others =>
      Check ("exception message", Ada.Exceptions.Exception_Message (E), "token larger than buffer size of 10");
   end Buffer_Full;

   ----------
   --  Public subprograms

   overriding procedure Register_Tests (T : in out Test_Case)
   is
      use AUnit.Test_Cases.Registration;
   begin
      Register_Routine (T, Nominal_String'Access, "Nominal_String");
      Register_Routine (T, Nominal_Counted_Gnat'Access, "Nominal_Counted_Gnat");
      Register_Routine (T, Buffer_Full'Access, "Buffer_Full");
   end Register_Tests;

   overriding function Name (T : Test_Case) return AUnit.Message_String
   is
      pragma Unreferenced (T);
   begin
      return new String'("../../Test/analyzer_buffer_test.adb");
   end Name;

end Analyzer_Buffer_Test;