are_1.4.0_a458cb9e/regtests/are-tests.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
-----------------------------------------------------------------------
--  are-tests -- Various tests for the are tool (based on examples)
--  Copyright (C) 2021 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Text_IO;
with Ada.Directories;
with Ada.Strings.Maps;

with Util.Test_Caller;
package body Are.Tests is

   package Caller is new Util.Test_Caller (Test, "Are.Examples");

   procedure Add_Tests (Suite : in Util.Tests.Access_Test_Suite) is
   begin
      Caller.Add_Test (Suite, "Test examples c-config",
                       Test_Example_C_Config'Access);
      Caller.Add_Test (Suite, "Test examples c-help",
                       Test_Example_C_Help'Access);
      Caller.Add_Test (Suite, "Test examples c-lines",
                       Test_Example_C_Lines'Access);
      Caller.Add_Test (Suite, "Test examples ada-config",
                       Test_Example_Ada_Config'Access);
      Caller.Add_Test (Suite, "Test examples ada-help",
                       Test_Example_Ada_Help'Access);
      Caller.Add_Test (Suite, "Test examples ada-lines",
                       Test_Example_Ada_Lines'Access);
      Caller.Add_Test (Suite, "Test examples ada-mapping",
                       Test_Example_Ada_Mapping'Access);
      if Ada.Directories.Exists ("/usr/bin/go") then
         Caller.Add_Test (Suite, "Test examples go-config",
                          Test_Example_Go_Config'Access);
         Caller.Add_Test (Suite, "Test examples go-help",
                          Test_Example_Go_Help'Access);
      end if;
      Caller.Add_Test (Suite, "Test Are.Convert_To_Lines_1",
                       Test_Convert_Lines_1'Access);
      Caller.Add_Test (Suite, "Test Are.Convert_To_Lines_2",
                       Test_Convert_Lines_2'Access);
   end Add_Tests;

   procedure Test_Example_C_Config (T : in out Test) is
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("make -C examples/c-config clean", Result, Status => 0);
      T.Execute ("make -C examples/c-config ARE=../../bin/are", Result, Status => 0);
      T.Execute ("examples/c-config/show-config" & Are.Testsuite.EXE, Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*Example of embedded configuration.*",
                                 Result,
                                 "Invalid C show-config");
   end Test_Example_C_Config;

   procedure Test_Example_Ada_Config (T : in out Test) is
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("make -C examples/ada-config clean", Result, Status => 0);
      T.Execute ("make -C examples/ada-config ARE=../../bin/are", Result, Status => 0);
      T.Execute ("examples/ada-config/show_config" & Are.Testsuite.EXE, Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*Example of embedded configuration.*",
                                 Result,
                                 "Invalid Ada show_config");
   end Test_Example_Ada_Config;

   procedure Test_Example_Ada_Mapping (T : in out Test) is
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("make -C examples/ada-mapping clean", Result, Status => 0);
      T.Execute ("make -C examples/ada-mapping ARE=../../bin/are", Result, Status => 0);
      T.Execute ("examples/ada-mapping/extension" & Are.Testsuite.EXE & " test.adb",
                 Result, Status => 0);
      Util.Tests.Assert_Matches (T, "Ada",
                                 Result,
                                 "Invalid Ada extension");
   end Test_Example_Ada_Mapping;

   procedure Test_Example_Go_Config (T : in out Test) is
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("make -C examples/go-config clean", Result, Status => 0);
      T.Execute ("make -C examples/go-config ARE=../../bin/are", Result, Status => 0);
      T.Execute ("examples/go-config/show-config", Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*Example of embedded configuration.*",
                                 Result,
                                 "Invalid Go show-config");
   end Test_Example_Go_Config;

   procedure Test_Example_C_Help (T : in out Test) is
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("make -C examples/c-help clean", Result, Status => 0);
      T.Execute ("make -C examples/c-help ARE=../../bin/are", Result, Status => 0);
      T.Execute ("examples/c-help/show-help" & Are.Testsuite.EXE, Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*sh.*",
                                 Result,
                                 "Invalid C show-help: sh missing");
      Util.Tests.Assert_Matches (T, ".*extract.*",
                                 Result,
                                 "Invalid C show-help: extract missing");

      T.Execute ("examples/c-help/show-help sh", Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*shell.*",
                                 Result,
                                 "Invalid C show-help: sh");

      T.Execute ("examples/c-help/show-help extract", Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*extract files.*",
                                 Result,
                                 "Invalid C show-help: extract");
   end Test_Example_C_Help;

   procedure Test_Example_C_Lines (T : in out Test) is
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("make -C examples/c-lines clean", Result, Status => 0);
      T.Execute ("make -C examples/c-lines ARE=../../bin/are", Result, Status => 0);
      T.Execute ("examples/c-lines/show-script" & Are.Testsuite.EXE, Result, Status => 0);
      Util.Tests.Assert_Matches (T, "Create SQLite 7 lines.*",
                                 Result,
                                 "Invalid C show-script: SQL statement");
      Util.Tests.Assert_Matches (T, "Drop SQLite 4 lines.*",
                                 Result,
                                 "Invalid C show-script: SQL statement");

   end Test_Example_C_Lines;

   procedure Test_Example_Ada_Help (T : in out Test) is
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("make -C examples/ada-help clean", Result, Status => 0);
      T.Execute ("make -C examples/ada-help ARE=../../bin/are", Result, Status => 0);
      T.Execute ("examples/ada-help/show_help" & Are.Testsuite.EXE, Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*sh.*",
                                 Result,
                                 "Invalid Ada show-help: sh missing");
      Util.Tests.Assert_Matches (T, ".*extract.*",
                                 Result,
                                 "Invalid Ada show-help: extract missing");

      T.Execute ("examples/ada-help/show_help sh", Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*shell.*",
                                 Result,
                                 "Invalid Ada show-help: sh");

      T.Execute ("examples/ada-help/show_help extract", Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*extract files.*",
                                 Result,
                                 "Invalid Ada show-help: extract");
   end Test_Example_Ada_Help;

   procedure Test_Example_Ada_Lines (T : in out Test) is
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("make -C examples/ada-lines clean", Result, Status => 0);
      T.Execute ("make -C examples/ada-lines ARE=../../bin/are", Result, Status => 0);
      T.Execute ("examples/ada-lines/show_script" & Are.Testsuite.EXE, Result, Status => 0);
      Util.Tests.Assert_Matches (T, "Create SQLite 7 lines.*",
                                 Result,
                                 "Invalid Ada show_script: SQL statement");
      Util.Tests.Assert_Matches (T, "Drop SQLite 4 lines.*",
                                 Result,
                                 "Invalid Ada show_script: SQL statement");

   end Test_Example_Ada_Lines;

   procedure Test_Example_Go_Help (T : in out Test) is
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute ("make -C examples/go-help clean", Result, Status => 0);
      T.Execute ("make -C examples/go-help ARE=../../bin/are", Result, Status => 0);
      T.Execute ("examples/go-help/show-help", Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*sh.*",
                                 Result,
                                 "Invalid Go show-help: sh missing");
      Util.Tests.Assert_Matches (T, ".*extract.*",
                                 Result,
                                 "Invalid Go show-help: extract missing");

      T.Execute ("examples/go-help/show-help sh", Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*shell.*",
                                 Result,
                                 "Invalid Go show-help: sh");

      T.Execute ("examples/go-help/show-help extract", Result, Status => 0);
      Util.Tests.Assert_Matches (T, ".*extract files.*",
                                 Result,
                                 "Invalid Go show-help: extract");
   end Test_Example_Go_Help;

   procedure Test_Convert_Lines_1 (T : in out Test) is
      use Ada.Strings.Maps;

      Resource : Resource_Type;
      Lines    : Util.Strings.Vectors.Vector;
      File     : File_Info;
   begin
      Resource.Add_File ("lines", "regtests/files/lines-1.txt");
      Resource.Separators := To_Set (ASCII.CR) or To_Set (ASCII.LF);
      File := Resource.Files.First_Element;
      Resource.Convert_To_Lines (File, Lines);
      Util.Tests.Assert_Equals (T, 9, Natural (Lines.Length),
                                "Invalid number of lines");
      for I in 1 .. 9 loop
         Util.Tests.Assert_Equals (T, "line" & Util.Strings.Image (I),
                                   Lines.Element (I), "Invalid line");
      end loop;
   end Test_Convert_Lines_1;

   procedure Test_Convert_Lines_2 (T : in out Test) is
      use Ada.Strings.Maps;

      Resource : Resource_Type;
      Lines    : Util.Strings.Vectors.Vector;
      File     : File_Info;
   begin
      Resource.Add_File ("lines", "regtests/files/lines-2.txt");
      Resource.Separators := To_Set (";");

      --  Remove newlines and contiguous spaces.
      Are.Add_Line_Filter (Resource, "[\r\n]", "");
      Are.Add_Line_Filter (Resource, "[ \t][ \t]+", " ");

      --  Remove C comments.
      Are.Add_Line_Filter (Resource, "/\*[^/]*\*/", "");

      --  Remove contiguous spaces (can happen after C comment removal).
      Are.Add_Line_Filter (Resource, "[ \t][ \t]+", " ");
      File := Resource.Files.First_Element;
      Resource.Convert_To_Lines (File, Lines);
      Util.Tests.Assert_Equals (T, 5, Natural (Lines.Length),
                                "Invalid number of lines");

      for I in 1 .. 5 loop
         Ada.Text_IO.Put_Line (Lines.Element (I));
      end loop;

      Util.Tests.Assert_Equals (T, "pragma synchronous=OFF",
                                Lines.Element (1), "Invalid line 1");
      Util.Tests.Assert_Equals (T, "CREATE TABLE IF NOT EXISTS entity_type ( `id`"
                                & " INTEGER PRIMARY KEY AUTOINCREMENT, `name`"
                                & " VARCHAR(127) UNIQUE )",
                                Lines.Element (2), "Invalid line 2");
      Util.Tests.Assert_Equals (T, "CREATE TABLE IF NOT EXISTS sequence ( `name`"
                                & " VARCHAR(127) UNIQUE NOT NULL, `version` INTEGER NOT NULL,"
                                & " `value` BIGINT NOT NULL, `block_size` BIGINT NOT NULL,"
                                & " PRIMARY KEY (`name`))",
                                Lines.Element (3), "Invalid line 3");
      Util.Tests.Assert_Equals (T, "INSERT OR IGNORE INTO entity_type (name) VALUES"
                                & " (""entity_type"")",
                                Lines.Element (4), "Invalid line 4");
      Util.Tests.Assert_Equals (T, "INSERT OR IGNORE INTO entity_type (name) VALUES"
                                & " (""sequence"")",
                                Lines.Element (5), "Invalid line 5");
   end Test_Convert_Lines_2;

end Are.Tests;