pascalabcnet/bin/TestRunner.pas

454 lines
15 KiB
ObjectPascal
Raw Normal View History

// Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt)
2015-06-12 21:59:28 +03:00
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
2015-05-14 22:35:07 +03:00
{$reference Compiler.dll}
{$reference CodeCompletion.dll}
{$reference Errors.dll}
{$reference CompilerTools.dll}
2016-05-06 14:56:19 +03:00
{$reference Localization.dll}
2015-05-14 22:35:07 +03:00
{$reference System.Windows.Forms.dll}
uses PascalABCCompiler, System.IO, System.Diagnostics;
2018-06-03 20:49:55 +03:00
var
TestSuiteDir: string;
2021-01-05 14:32:21 +03:00
nogui: boolean;
2018-06-03 20:49:55 +03:00
var
PathSeparator: string := Path.DirectorySeparatorChar;
2015-05-14 22:35:07 +03:00
function IsUnix: boolean;
begin
Result := (System.Environment.OSVersion.Platform = System.PlatformID.Unix) or (System.Environment.OSVersion.Platform = System.PlatformID.MacOSX);
end;
2015-05-14 22:35:07 +03:00
function GetTestSuiteDir: string;
begin
var dir := Path.GetDirectoryName(GetEXEFileName());
var ind := dir.LastIndexOf('bin');
2018-06-03 20:49:55 +03:00
Result := dir.Substring(0, ind) + 'TestSuite';
2015-05-14 22:35:07 +03:00
end;
function GetLibDir: string;
begin
var dir := Path.GetDirectoryName(GetEXEFileName());
2018-06-03 20:49:55 +03:00
Result := dir + PathSeparator + 'Lib';
2015-05-14 22:35:07 +03:00
end;
procedure CompileErrorTests(withide: boolean);
begin
2021-10-31 15:48:32 +03:00
2015-05-14 22:35:07 +03:00
2018-06-03 20:49:55 +03:00
var files := Directory.GetFiles(TestSuiteDir + PathSeparator + 'errors', '*.pas');
2015-05-14 22:35:07 +03:00
for var i := 0 to files.Length - 1 do
begin
2021-10-31 15:48:32 +03:00
var comp := new Compiler();
var content := &File.ReadAllText(files[i]);
if content.StartsWith('//winonly') and IsUnix then
continue;
2021-10-17 13:04:24 +03:00
if content.StartsWith('//exclude') then
continue;
var errorMessage := '';
if content.StartsWith('//!') then
begin
errorMessage := content.Substring(3, content.IndexOf(System.Environment.NewLine)-3).Trim;
end;
2018-06-03 20:49:55 +03:00
var co: CompilerOptions := new CompilerOptions(files[i], CompilerOptions.OutputType.ConsoleApplicaton);
2015-05-14 22:35:07 +03:00
co.Debug := true;
2018-06-03 20:49:55 +03:00
co.OutputDirectory := TestSuiteDir + PathSeparator + 'errors';
2015-05-14 22:35:07 +03:00
co.UseDllForSystemUnits := false;
co.RunWithEnvironment := withide;
comp.ErrorsList.Clear();
comp.Warnings.Clear();
comp.Compile(co);
if comp.ErrorsList.Count = 0 then
begin
2021-01-05 14:32:21 +03:00
if nogui then
raise new Exception('Compilation of error sample ' + files[i] + ' was successfull');
2018-06-03 20:49:55 +03:00
System.Windows.Forms.MessageBox.Show('Compilation of error sample ' + files[i] + ' was successfull' + System.Environment.NewLine);
2016-05-15 22:01:40 +03:00
Halt();
2015-05-14 22:35:07 +03:00
end
else if comp.ErrorsList.Count = 1 then
begin
if comp.ErrorsList[0].GetType() = typeof(PascalABCCompiler.Errors.CompilerInternalError) then
2021-01-05 14:32:21 +03:00
begin
if nogui then
raise new Exception('Compilation of ' + files[i] + ' failed' + System.Environment.NewLine + comp.ErrorsList[0].ToString());
2018-06-03 20:49:55 +03:00
System.Windows.Forms.MessageBox.Show('Compilation of ' + files[i] + ' failed' + System.Environment.NewLine + comp.ErrorsList[0].ToString());
2021-01-05 14:32:21 +03:00
end;
2021-10-31 15:48:32 +03:00
if (errorMessage <> '') and (comp.ErrorsList[comp.ErrorsList.Count-1].Message.Trim <> errorMessage) then
2021-10-17 13:04:24 +03:00
begin
2021-10-31 15:48:32 +03:00
if nogui then
raise new Exception('Wrong error message in file ' + files[i] + ', should '+errorMessage+', is '+comp.ErrorsList[comp.ErrorsList.Count-1].Message);
System.Windows.Forms.MessageBox.Show('Wrong error message in file ' + files[i] + ', should '+errorMessage+', is '+comp.ErrorsList[comp.ErrorsList.Count-1].Message);
2021-10-17 13:04:24 +03:00
end;
2015-05-14 22:35:07 +03:00
end;
if i mod 50 = 0 then
2018-01-30 22:39:20 +03:00
System.GC.Collect();
2015-05-14 22:35:07 +03:00
end;
end;
procedure CompileAllRunTests(withdll: boolean; only32bit: boolean := false);
begin
var comp := new Compiler();
2018-06-03 20:49:55 +03:00
var files := Directory.GetFiles(TestSuiteDir, '*.pas');
2015-05-14 22:35:07 +03:00
for var i := 0 to files.Length - 1 do
begin
2021-04-18 18:40:56 +03:00
if IsUnix then
writeln('Compile file '+files[i]);
var content := &File.ReadAllText(files[i]);
if content.StartsWith('//winonly') and IsUnix then
continue;
2019-06-30 15:22:24 +03:00
if content.StartsWith('//nopabcrtl') and withdll then
continue;
2018-06-03 20:49:55 +03:00
var co: CompilerOptions := new CompilerOptions(files[i], CompilerOptions.OutputType.ConsoleApplicaton);
2015-05-14 22:35:07 +03:00
co.Debug := true;
2018-06-03 20:49:55 +03:00
co.OutputDirectory := TestSuiteDir + PathSeparator + 'exe';
2023-08-14 00:17:35 +03:00
Directory.CreateDirectory(co.OutputDirectory);
2015-05-14 22:35:07 +03:00
co.UseDllForSystemUnits := withdll;
co.RunWithEnvironment := false;
co.IgnoreRtlErrors := false;
co.Only32Bit := only32bit;
comp.ErrorsList.Clear();
comp.Warnings.Clear();
comp.Compile(co);
if comp.ErrorsList.Count > 0 then
begin
2021-01-05 14:32:21 +03:00
if nogui then
raise new Exception('Compilation of ' + files[i] + ' failed' + System.Environment.NewLine + comp.ErrorsList[0].ToString());
2018-06-03 20:49:55 +03:00
System.Windows.Forms.MessageBox.Show('Compilation of ' + files[i] + ' failed' + System.Environment.NewLine + comp.ErrorsList[0].ToString());
2018-05-13 15:19:54 +03:00
Halt();
2015-05-14 22:35:07 +03:00
end;
if i mod 50 = 0 then
begin
2018-01-30 22:39:20 +03:00
System.GC.Collect();
end;
2015-05-14 22:35:07 +03:00
end;
2018-05-20 15:18:29 +03:00
//Println;
2015-05-14 22:35:07 +03:00
end;
procedure CompileAllCompilationTests(dir: string; withdll: boolean);
begin
var comp := new Compiler();
2018-06-03 20:49:55 +03:00
var files := Directory.GetFiles(TestSuiteDir + PathSeparator + dir, '*.pas');
2015-05-14 22:35:07 +03:00
for var i := 0 to files.Length - 1 do
begin
var content := &File.ReadAllText(files[i]);
if content.StartsWith('//winonly') and IsUnix then
continue;
2018-06-03 20:49:55 +03:00
var co: CompilerOptions := new CompilerOptions(files[i], CompilerOptions.OutputType.ConsoleApplicaton);
2015-05-14 22:35:07 +03:00
co.Debug := true;
2018-06-03 20:49:55 +03:00
co.OutputDirectory := TestSuiteDir + PathSeparator + dir;
2015-05-14 22:35:07 +03:00
co.UseDllForSystemUnits := withdll;
co.RunWithEnvironment := false;
co.IgnoreRtlErrors := false;
comp.ErrorsList.Clear();
comp.Warnings.Clear();
comp.Compile(co);
if comp.ErrorsList.Count > 0 then
begin
2021-01-05 14:32:21 +03:00
if nogui then
raise new Exception('Compilation of ' + files[i] + ' failed' + System.Environment.NewLine + comp.ErrorsList[0].ToString());
2018-06-03 20:49:55 +03:00
System.Windows.Forms.MessageBox.Show('Compilation of ' + files[i] + ' failed' + System.Environment.NewLine + comp.ErrorsList[0].ToString());
2015-05-14 22:35:07 +03:00
Halt();
end;
if i mod 50 = 0 then
2018-01-30 22:39:20 +03:00
System.GC.Collect();
2015-05-14 22:35:07 +03:00
end;
end;
procedure CompileAllUnits;
begin
var comp := new Compiler();
2018-06-03 20:49:55 +03:00
var files := Directory.GetFiles(TestSuiteDir + PathSeparator + 'units', '*.pas');
var dir := TestSuiteDir + PathSeparator + 'units' + PathSeparator;
2015-05-14 22:35:07 +03:00
for var i := 0 to files.Length - 1 do
begin
var content := &File.ReadAllText(files[i]);
if content.StartsWith('//winonly') and IsUnix then
continue;
2018-06-03 20:49:55 +03:00
var co: CompilerOptions := new CompilerOptions(files[i], CompilerOptions.OutputType.ConsoleApplicaton);
2015-05-14 22:35:07 +03:00
co.Debug := true;
co.OutputDirectory := dir;
co.UseDllForSystemUnits := false;
comp.ErrorsList.Clear();
comp.Warnings.Clear();
comp.Compile(co);
if comp.ErrorsList.Count > 0 then
begin
2021-01-05 14:32:21 +03:00
if nogui then
raise new Exception('Compilation of ' + files[i] + ' failed' + System.Environment.NewLine + comp.ErrorsList[0].ToString());
2018-06-03 20:49:55 +03:00
System.Windows.Forms.MessageBox.Show('Compilation of ' + files[i] + ' failed' + System.Environment.NewLine + comp.ErrorsList[0].ToString());
2015-05-14 22:35:07 +03:00
Halt();
end;
end;
2018-01-30 22:39:20 +03:00
System.GC.Collect;
2015-05-14 22:35:07 +03:00
end;
procedure CompileAllUsesUnits;
begin
System.Environment.CurrentDirectory := Path.GetDirectoryName(GetEXEFileName());
var comp := new Compiler();
2018-06-03 20:49:55 +03:00
var files := Directory.GetFiles(TestSuiteDir + PathSeparator + 'usesunits', '*.pas');
2015-05-14 22:35:07 +03:00
for var i := 0 to files.Length - 1 do
begin
var content := &File.ReadAllText(files[i]);
if content.StartsWith('//winonly') and IsUnix then
continue;
2018-06-03 20:49:55 +03:00
var co: CompilerOptions := new CompilerOptions(files[i], CompilerOptions.OutputType.ConsoleApplicaton);
2015-05-14 22:35:07 +03:00
co.Debug := true;
2018-06-03 20:49:55 +03:00
co.OutputDirectory := TestSuiteDir + PathSeparator + 'exe';
2015-05-14 22:35:07 +03:00
co.UseDllForSystemUnits := false;
comp.ErrorsList.Clear();
comp.Warnings.Clear();
comp.Compile(co);
if comp.ErrorsList.Count > 0 then
begin
2021-01-05 14:32:21 +03:00
if nogui then
raise new Exception('Compilation of ' + files[i] + ' failed' + System.Environment.NewLine + comp.ErrorsList[0].ToString());
2018-06-03 20:49:55 +03:00
System.Windows.Forms.MessageBox.Show('Compilation of ' + files[i] + ' failed' + System.Environment.NewLine + comp.ErrorsList[0].ToString());
2015-05-14 22:35:07 +03:00
Halt();
end;
end;
2018-01-30 22:39:20 +03:00
System.GC.Collect;
2015-05-14 22:35:07 +03:00
end;
procedure CopyPCUFiles;
begin
System.Environment.CurrentDirectory := Path.GetDirectoryName(GetEXEFileName());
2018-06-03 20:49:55 +03:00
var files := Directory.GetFiles(TestSuiteDir + PathSeparator + 'units', '*.pcu');
2015-05-14 22:35:07 +03:00
foreach fname: string in files do
begin
2018-06-03 20:49:55 +03:00
&File.Move(fname, TestSuiteDir + PathSeparator + 'usesunits' + PathSeparator + Path.GetFileName(fname));
2015-05-14 22:35:07 +03:00
end;
end;
procedure RunAllTests(redirectIO: boolean);
begin
2020-12-26 20:05:31 +03:00
var dlls := Directory.GetFiles(TestSuiteDir, '*.dll');
foreach var dll in dlls do
begin
System.IO.File.Copy(dll, TestSuiteDir + PathSeparator + 'exe' + PathSeparator + Path.GetFileName(dll), true);
end;
2018-06-03 20:49:55 +03:00
var files := Directory.GetFiles(TestSuiteDir + PathSeparator + 'exe', '*.exe');
2015-05-14 22:35:07 +03:00
for var i := 0 to files.Length - 1 do
begin
//Println(files[i]);
2018-06-03 20:49:55 +03:00
var psi := new System.Diagnostics.ProcessStartInfo(files[i]);
psi.CreateNoWindow := true;
psi.UseShellExecute := false;
psi.WorkingDirectory := TestSuiteDir + PathSeparator + 'exe';
2015-05-14 22:35:07 +03:00
{psi.RedirectStandardInput := true;
psi.RedirectStandardOutput := true;
psi.RedirectStandardError := true;}
2018-06-03 20:49:55 +03:00
var p: Process := new Process();
p.StartInfo := psi;
p.Start();
if redirectIO then
p.StandardInput.WriteLine('GO');
2015-05-14 22:35:07 +03:00
//p.StandardInput.AutoFlush := true;
//var p := System.Diagnostics.Process.Start(psi);
2021-01-05 15:50:31 +03:00
if nogui then
begin
var success := p.WaitForExit(60000);
if not success then
begin
raise new Exception('Running of ' + files[i] + ' failed.');
end;
end
else
p.WaitForExit();
2021-01-02 16:49:05 +03:00
//while not p.HasExited do
// Sleep(5);
2018-06-03 20:49:55 +03:00
if p.ExitCode <> 0 then
begin
2021-01-05 14:32:21 +03:00
if nogui then
raise new Exception('Running of ' + files[i] + ' failed. Exit code is not 0');
2018-06-03 20:49:55 +03:00
System.Windows.Forms.MessageBox.Show('Running of ' + files[i] + ' failed. Exit code is not 0');
Halt;
end;
2015-05-14 22:35:07 +03:00
end;
end;
procedure RunExpressionsExtractTests;
begin
CodeCompletion.CodeCompletionTester.Test();
end;
2016-05-06 14:56:19 +03:00
function GetLineByPos(lines: array of string; pos: integer): integer;
begin
var cum_pos := 0;
for var i := 0 to lines.Length - 1 do
2018-06-03 20:49:55 +03:00
for var j := 0 to lines[j].Length - 1 do
2016-05-06 14:56:19 +03:00
begin
2018-06-03 20:49:55 +03:00
if cum_pos = pos then
begin
Result := i + 1;
exit;
end;
Inc(cum_pos);
2016-05-06 14:56:19 +03:00
end;
end;
function GetColByPos(lines: array of string; pos: integer): integer;
begin
var cum_pos := 0;
for var i := 0 to lines.Length - 1 do
2018-06-03 20:49:55 +03:00
for var j := 0 to lines[j].Length - 1 do
2016-05-06 14:56:19 +03:00
begin
2018-06-03 20:49:55 +03:00
if cum_pos = pos then
begin
Result := j + 1;
exit;
end;
Inc(cum_pos);
2016-05-06 14:56:19 +03:00
end;
end;
procedure RunIntellisenseTests;
begin
PascalABCCompiler.StringResourcesLanguage.CurrentTwoLetterISO := 'ru';
2018-06-03 20:49:55 +03:00
CodeCompletion.CodeCompletionTester.TestIntellisense(TestSuiteDir + PathSeparator + 'intellisense_tests');
2016-05-06 14:56:19 +03:00
end;
2015-05-14 22:35:07 +03:00
procedure RunFormatterTests;
begin
CodeCompletion.FormatterTester.Test();
2018-06-03 20:49:55 +03:00
var errors := &File.ReadAllText(TestSuiteDir + PathSeparator + 'formatter_tests' + PathSeparator + 'output' + PathSeparator + 'log.txt');
2015-05-14 22:35:07 +03:00
if not string.IsNullOrEmpty(errors) then
begin
2018-06-03 20:49:55 +03:00
System.Windows.Forms.MessageBox.Show(errors + System.Environment.NewLine + 'more info at TestSuite/formatter_tests/output/log.txt');
2015-05-14 22:35:07 +03:00
Halt;
end;
end;
procedure ClearDirByPattern(dir, pattern: string);
begin
2023-08-14 00:17:35 +03:00
if not Directory.Exists(dir) then exit;
2015-05-14 22:35:07 +03:00
var files := Directory.GetFiles(dir, pattern);
for var i := 0 to files.Length - 1 do
begin
try
2015-05-15 12:35:40 +03:00
if Path.GetFileName(files[i]) <> '.gitignore' then
&File.Delete(files[i]);
2015-05-14 22:35:07 +03:00
except
end;
end;
end;
procedure ClearExeDir;
begin
2018-06-03 20:49:55 +03:00
ClearDirByPattern(TestSuiteDir + PathSeparator + 'exe', '*.*');
ClearDirByPattern(TestSuiteDir + PathSeparator + 'CompilationSamples', '*.exe');
ClearDirByPattern(TestSuiteDir + PathSeparator + 'CompilationSamples', '*.mdb');
ClearDirByPattern(TestSuiteDir + PathSeparator + 'CompilationSamples', '*.pdb');
ClearDirByPattern(TestSuiteDir + PathSeparator + 'CompilationSamples', '*.pcu');
ClearDirByPattern(TestSuiteDir + PathSeparator + 'pabcrtl_tests', '*.exe');
ClearDirByPattern(TestSuiteDir + PathSeparator + 'pabcrtl_tests', '*.pdb');
ClearDirByPattern(TestSuiteDir + PathSeparator + 'pabcrtl_tests', '*.mdb');
ClearDirByPattern(TestSuiteDir + PathSeparator + 'pabcrtl_tests', '*.pcu');
2015-05-14 22:35:07 +03:00
end;
procedure DeletePCUFiles;
begin
2018-06-03 20:49:55 +03:00
ClearDirByPattern(TestSuiteDir + PathSeparator + 'usesunits', '*.pcu');
2015-05-14 22:35:07 +03:00
end;
procedure DeletePABCSystemPCU;
begin
2018-06-03 20:49:55 +03:00
var dir := Path.Combine(Path.GetDirectoryName(GetEXEFileName()), 'Lib');
var pcu := Path.Combine(dir, 'PABCSystem.pcu');
2015-05-14 22:35:07 +03:00
end;
procedure CopyLibFiles;
begin
2018-06-03 20:49:55 +03:00
var files := Directory.GetFiles(GetLibDir(), '*.pas');
2015-05-14 22:35:07 +03:00
foreach f: string in files do
begin
2018-06-03 20:49:55 +03:00
&File.Copy(f, TestSuiteDir + PathSeparator + 'CompilationSamples' + PathSeparator + Path.GetFileName(f), true);
2015-05-14 22:35:07 +03:00
end;
end;
begin
//DeletePABCSystemPCU;
try
TestSuiteDir := GetTestSuiteDir;
System.Environment.CurrentDirectory := Path.GetDirectoryName(GetEXEFileName());
2021-01-05 14:32:21 +03:00
if (ParamCount = 2) and (ParamStr(2) = '1') then
nogui := true;
2018-06-03 20:49:55 +03:00
if (ParamCount = 0) or (ParamStr(1) = '1') then
begin
DeletePCUFiles;
ClearExeDir;
CompileAllRunTests(false);
2021-01-02 16:49:05 +03:00
writeln('Tests to run: '+Milliseconds()+'ms');
2018-06-03 20:49:55 +03:00
end;
if (ParamCount = 0) or (ParamStr(1) = '2') then
begin
CopyLibFiles;
CompileAllCompilationTests('CompilationSamples', false);
2021-01-02 16:49:05 +03:00
writeln('CompilationSamples: '+Milliseconds()+'ms');
2018-06-03 20:49:55 +03:00
end;
if (ParamCount = 0) or (ParamStr(1) = '3') then
begin
CompileAllUnits;
CopyPCUFiles;
CompileAllUsesUnits;
CompileErrorTests(false);
2021-01-02 16:49:05 +03:00
writeln('Other tests '+Milliseconds()+'ms'+'. Tests compiled successfully ');
2018-06-03 20:49:55 +03:00
end;
if (ParamCount = 0) or (ParamStr(1) = '4') then
begin
RunAllTests(false);
2021-01-02 16:49:05 +03:00
writeln('Tests run successfully: '+Milliseconds()+'ms');
2018-06-03 20:49:55 +03:00
ClearExeDir;
DeletePCUFiles;
end;
if (ParamCount = 0) or (ParamStr(1) = '5') then
begin
2019-10-06 13:38:27 +03:00
CompileAllRunTests(false, true);
writeln('Tests in 32bit mode compiled successfully');
RunAllTests(false);
writeln('Tests in 32bit run successfully');
ClearExeDir;
CompileAllRunTests(true);
writeln('Tests with pabcrtl compiled successfully');
CompileAllCompilationTests('pabcrtl_tests', true);
2019-10-06 13:38:27 +03:00
end;
if (ParamCount = 0) or (ParamStr(1) = '6') then
begin
RunAllTests(false);
writeln('Tests with pabcrtl run successfully');
2018-06-03 20:49:55 +03:00
System.Environment.CurrentDirectory := Path.GetDirectoryName(GetEXEFileName());
RunExpressionsExtractTests;
writeln('Intellisense expression tests run successfully');
RunIntellisenseTests;
writeln('Intellisense tests run successfully');
RunFormatterTests;
writeln('Formatter tests run successfully');
end;
except
on e: Exception do
2021-01-05 14:53:09 +03:00
begin
if nogui then
raise new Exception(e.ToString());
2018-06-03 20:49:55 +03:00
assert(false, e.ToString());
2021-01-05 14:53:09 +03:00
end;
end;
2015-05-14 22:35:07 +03:00
end.