are_1.2.0_16239a8b/regtests/are-generator-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
269
270
271
272
273
274
275
276
277
278
-----------------------------------------------------------------------
--  are-generator-tests -- Tests for generator
--  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.Directories;

with Util.Test_Caller;
with Are.Generator.C.Tests;
with Are.Generator.Ada2012.Tests;
with Are.Generator.Go.Tests;
package body Are.Generator.Tests is

   function Tool return String;

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

   function Tool return String is
   begin
      return "bin/are";
   end Tool;

   procedure Add_Tests (Suite : in Util.Tests.Access_Test_Suite) is
   begin
      Caller.Add_Test (Suite, "Test are (usage)",
                       Test_Wrong_Usage'Access);
      Caller.Add_Test (Suite, "Test are (wrong directory)",
                       Test_Wrong_Directory'Access);
      Caller.Add_Test (Suite, "Test are (exec wrong command)",
                       Test_Exec_Error_1'Access);
      Caller.Add_Test (Suite, "Test are (missing rule file)",
                       Test_Missing_Rule'Access);
      Caller.Add_Test (Suite, "Test are (wrong include)",
                       Test_Exec_Error_2'Access);
      Caller.Add_Test (Suite, "Test are (wrong exclude)",
                       Test_Exec_Error_3'Access);
      Caller.Add_Test (Suite, "Test are (wrong fileset)",
                       Test_Exec_Error_4'Access);
      Caller.Add_Test (Suite, "Test are (wrong install)",
                       Test_Exec_Error_5'Access);
      Caller.Add_Test (Suite, "Test are (XML format error)",
                       Test_Exec_Error_6'Access);
      Caller.Add_Test (Suite, "Test are (wrong resource format)",
                       Test_Exec_Error_7'Access);
      Caller.Add_Test (Suite, "Test are (missing <line-separator>)",
                       Test_Exec_Error_8'Access);
      Caller.Add_Test (Suite, "Test are (invalid <line-filter>)",
                       Test_Exec_Error_9'Access);
      Caller.Add_Test (Suite, "Test are (verbose mode with error)",
                       Test_Verbose'Access);
      Caller.Add_Test (Suite, "Test are (webmerge with errors)",
                       Test_Merge_Error_1'Access);
      Are.Generator.Ada2012.Tests.Add_Tests (Suite);
      Are.Generator.C.Tests.Add_Tests (Suite);
      Are.Generator.Go.Tests.Add_Tests (Suite);
   end Add_Tests;

   procedure Test_Wrong_Usage (T : in out Test) is
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool, Result, Status => 1);
      T.Execute (Tool & " --zorro", Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: unrecognized option '--zorro'", Result,
                                 "Invalid error message with --zorro");

      T.Execute (Tool & " --lang=plop .", Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: language plop not recognized", Result,
                                 "Invalid error message with --lang=plop");
   end Test_Wrong_Usage;

   procedure Test_Wrong_Directory (T : in out Test) is
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " regtests/toto", Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: path regtests/toto does not exist", Result,
                                 "Invalid error message");
      if Ada.Directories.Exists ("/dev/null") then
         T.Execute (Tool & " /dev/null", Result, Status => 1);
         Util.Tests.Assert_Matches (T, "are: error: path /dev/null is not a directory", Result,
                                    "Invalid error message");
      end if;
   end Test_Wrong_Directory;

   procedure Test_Missing_Rule (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/test-ada-4";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -o " & Dir
                 & " --rule=package-missing-rule.xml " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: package file "
                                 & "package-missing-rule.xml does not exist",
                                 Result,
                                 "Invalid error message");
   end Test_Missing_Rule;

   procedure Test_Exec_Error_1 (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/test-ada-4";
      Rule   : constant String := "regtests/files/package-error-1.xml";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, ".*missing-command", Result,
                                 "Invalid error message");

      T.Assert (not Ada.Directories.Exists (Ada.Directories.Compose (Dir, "error1.ads")),
                "Unexpected file error1.ads was created");
   end Test_Exec_Error_1;

   procedure Test_Exec_Error_2 (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/test-ada-4";
      Rule   : constant String := "regtests/files/package-error-2.xml";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: .*package-error-2.xml: empty include", Result,
                                 "Invalid error message");

      T.Assert (not Ada.Directories.Exists (Ada.Directories.Compose (Dir, "error1.ads")),
                "Unexpected file error1.ads was created");
   end Test_Exec_Error_2;

   procedure Test_Exec_Error_3 (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/test-ada-4";
      Rule   : constant String := "regtests/files/package-error-3.xml";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: .*package-error-3.xml: empty exclude", Result,
                                 "Invalid error message");

      T.Assert (not Ada.Directories.Exists (Ada.Directories.Compose (Dir, "error1.ads")),
                "Unexpected file error1.ads was created");
   end Test_Exec_Error_3;

   procedure Test_Exec_Error_4 (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/test-ada-4";
      Rule   : constant String := "regtests/files/package-error-4.xml";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: .*package-error-4.xml: empty fileset",
                                 Result,
                                 "Invalid error message");

      T.Assert (not Ada.Directories.Exists (Ada.Directories.Compose (Dir, "error1.ads")),
                "Unexpected file error1.ads was created");
   end Test_Exec_Error_4;

   procedure Test_Exec_Error_5 (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/test-ada-4";
      Rule   : constant String := "regtests/files/package-error-5.xml";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: .*package-error-5.xml: rule 'some-plugin'",
                                 Result,
                                 "Invalid error message");

      T.Assert (not Ada.Directories.Exists (Ada.Directories.Compose (Dir, "error1.ads")),
                "Unexpected file error1.ads was created");
   end Test_Exec_Error_5;

   procedure Test_Exec_Error_6 (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/test-ada-4";
      Rule   : constant String := "regtests/files/package-error-6.xml";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: package-error-6.xml:2:0: "
                                 & "Node <package> is not closed",
                                 Result,
                                 "Invalid error message");
   end Test_Exec_Error_6;

   procedure Test_Exec_Error_7 (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/test-ada-4";
      Rule   : constant String := "regtests/files/package-error-7.xml";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: .*: invalid resource format 'bad-format'",
                                 Result,
                                 "Invalid error message");
   end Test_Exec_Error_7;

   procedure Test_Exec_Error_8 (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/test-ada-4";
      Rule   : constant String := "regtests/files/package-error-8.xml";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: .*: missing 'line-separator'"
                                 & " for resource 'Error1'",
                                 Result,
                                 "Invalid error message");
   end Test_Exec_Error_8;

   procedure Test_Exec_Error_9 (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/test-ada-4";
      Rule   : constant String := "regtests/files/package-error-9.xml";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: .*: invalid pattern '",
                                 Result,
                                 "Invalid error message");
   end Test_Exec_Error_9;

   procedure Test_Merge_Error_1 (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/web-error-1";
      Rule   : constant String := "regtests/files/package-webmerge-1.xml";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, "are: error: .*: cannot read ",
                                 Result,
                                 "Invalid error message");
      Util.Tests.Assert_Matches (T, "are: error: .*: expecting a 'href=' ",
                                 Result,
                                 "Invalid error message");
      Util.Tests.Assert_Matches (T, "are: error: .*: may be the end marker 'RESOURCE-MERGE-END' ",
                                 Result,
                                 "Invalid error message");
   end Test_Merge_Error_1;

   procedure Test_Verbose (T : in out Test) is
      Dir    : constant String := Util.Tests.Get_Test_Path ("");
      Web    : constant String := "regtests/files/test-ada-4";
      Rule   : constant String := "regtests/files/package-error-1.xml";
      Result : Ada.Strings.Unbounded.Unbounded_String;
   begin
      T.Execute (Tool & " -v -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, ".*missing-command", Result,
                                 "Invalid error message");

      T.Assert (not Ada.Directories.Exists (Ada.Directories.Compose (Dir, "error1.ads")),
                "Unexpected file error1.ads was created");

      T.Execute (Tool & " -vv -o " & Dir & " --rule=" & Rule & " " & Web, Result, Status => 1);
      Util.Tests.Assert_Matches (T, ".*DEBUG.*Process rule", Result,
                                 "Invalid debug message");
      Util.Tests.Assert_Matches (T, ".*INFO  - Util.Processes", Result,
                                 "Missing debug message");

      T.Assert (not Ada.Directories.Exists (Ada.Directories.Compose (Dir, "error1.ads")),
                "Unexpected file error1.ads was created");

      T.Execute (Tool & " -V", Result, Status => 0);
      Util.Tests.Assert_Matches (T, "Advanced Resource Embedder 1.*",
                                 Result,
                                 "Invalid version");
   end Test_Verbose;

end Are.Generator.Tests;