pascalabcnet/bin/TestRunner.pas

370 lines
11 KiB
ObjectPascal
Raw Permalink Normal View History

2018-01-30 22:39:20 +03:00
// Copyright (c) Ivan Bondarev, Stanislav Mihalkovich (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;
var TestSuiteDir: string;
2015-12-29 17:56:29 +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');
Result := dir.Substring(0,ind)+'TestSuite';
end;
function GetLibDir: string;
begin
var dir := Path.GetDirectoryName(GetEXEFileName());
2015-12-29 17:56:29 +03:00
Result := dir+PathSeparator+'Lib';
2015-05-14 22:35:07 +03:00
end;
procedure CompileErrorTests(withide: boolean);
begin
var comp := new Compiler();
2015-12-29 17:56:29 +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
var content := &File.ReadAllText(files[i]);
if content.StartsWith('//winonly') and IsUnix then
continue;
2015-05-14 22:35:07 +03:00
var co: CompilerOptions := new CompilerOptions(files[i],CompilerOptions.OutputType.ConsoleApplicaton);
co.Debug := true;
2015-12-29 17:56:29 +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
2016-05-15 22:01:40 +03:00
System.Windows.Forms.MessageBox.Show('Compilation of error sample '+files[i]+' was successfull'+System.Environment.NewLine);
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
System.Windows.Forms.MessageBox.Show('Compilation of '+files[i]+' failed'+System.Environment.NewLine+comp.ErrorsList[0].ToString());
end;
2018-01-30 22:39:20 +03:00
if i mod 20 = 0 then
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();
var files := Directory.GetFiles(TestSuiteDir,'*.pas');
for var i := 0 to files.Length - 1 do
begin
var content := &File.ReadAllText(files[i]);
if content.StartsWith('//winonly') and IsUnix then
continue;
2015-05-14 22:35:07 +03:00
var co: CompilerOptions := new CompilerOptions(files[i],CompilerOptions.OutputType.ConsoleApplicaton);
co.Debug := true;
2015-12-29 17:56:29 +03:00
co.OutputDirectory := TestSuiteDir+PathSeparator+'exe';
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
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;
2018-01-30 22:39:20 +03:00
if i mod 20 = 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();
2015-12-29 17:56:29 +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;
2015-05-14 22:35:07 +03:00
var co: CompilerOptions := new CompilerOptions(files[i],CompilerOptions.OutputType.ConsoleApplicaton);
co.Debug := true;
2015-12-29 17:56:29 +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
System.Windows.Forms.MessageBox.Show('Compilation of '+files[i]+' failed'+System.Environment.NewLine+comp.ErrorsList[0].ToString());
Halt();
end;
2018-01-30 22:39:20 +03:00
if i mod 20 = 0 then
System.GC.Collect();
2015-05-14 22:35:07 +03:00
end;
end;
procedure CompileAllUnits;
begin
var comp := new Compiler();
2015-12-29 17:56:29 +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;
2015-05-14 22:35:07 +03:00
var co: CompilerOptions := new CompilerOptions(files[i],CompilerOptions.OutputType.ConsoleApplicaton);
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
System.Windows.Forms.MessageBox.Show('Compilation of '+files[i]+' failed'+System.Environment.NewLine+comp.ErrorsList[0].ToString());
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();
2015-12-29 17:56:29 +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;
2015-05-14 22:35:07 +03:00
var co: CompilerOptions := new CompilerOptions(files[i],CompilerOptions.OutputType.ConsoleApplicaton);
co.Debug := true;
2015-12-29 17:56:29 +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
System.Windows.Forms.MessageBox.Show('Compilation of '+files[i]+' failed'+System.Environment.NewLine+comp.ErrorsList[0].ToString());
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());
2015-12-29 17:56:29 +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
2015-12-29 17:56:29 +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
2015-12-29 17:56:29 +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
var psi := new System.Diagnostics.ProcessStartInfo(files[i]);
psi.CreateNoWindow := true;
psi.UseShellExecute := false;
2015-12-29 17:56:29 +03:00
psi.WorkingDirectory := TestSuiteDir+PathSeparator+'exe';
2015-05-14 22:35:07 +03:00
{psi.RedirectStandardInput := true;
psi.RedirectStandardOutput := true;
psi.RedirectStandardError := true;}
var p: Process := new Process();
p.StartInfo := psi;
p.Start();
if redirectIO then
p.StandardInput.WriteLine('GO');
//p.StandardInput.AutoFlush := true;
//var p := System.Diagnostics.Process.Start(psi);
while not p.HasExited do
Sleep(10);
if p.ExitCode <> 0 then
begin
System.Windows.Forms.MessageBox.Show('Running of '+files[i]+' failed. Exit code is not 0');
Halt;
end;
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
for var j := 0 to lines[j].Length - 1 do
begin
if cum_pos = pos then
begin
Result := i + 1;
exit;
end;
Inc(cum_pos);
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
for var j := 0 to lines[j].Length - 1 do
begin
if cum_pos = pos then
begin
Result := j + 1;
exit;
end;
Inc(cum_pos);
end;
end;
procedure RunIntellisenseTests;
begin
PascalABCCompiler.StringResourcesLanguage.CurrentTwoLetterISO := 'ru';
CodeCompletion.CodeCompletionTester.TestIntellisense(TestSuiteDir+PathSeparator+'intellisense_tests');
end;
2015-05-14 22:35:07 +03:00
procedure RunFormatterTests;
begin
CodeCompletion.FormatterTester.Test();
2015-12-29 17:56:29 +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
2015-12-29 17:56:29 +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
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
2015-12-29 17:56:29 +03:00
ClearDirByPattern(TestSuiteDir+PathSeparator+'exe','*.*');
ClearDirByPattern(TestSuiteDir+PathSeparator+'CompilationSamples','*.exe');
2015-12-30 16:43:28 +03:00
ClearDirByPattern(TestSuiteDir+PathSeparator+'CompilationSamples','*.mdb');
2015-12-29 17:56:29 +03:00
ClearDirByPattern(TestSuiteDir+PathSeparator+'CompilationSamples','*.pdb');
ClearDirByPattern(TestSuiteDir+PathSeparator+'CompilationSamples','*.pcu');
ClearDirByPattern(TestSuiteDir+PathSeparator+'pabcrtl_tests','*.exe');
ClearDirByPattern(TestSuiteDir+PathSeparator+'pabcrtl_tests','*.pdb');
2015-12-30 16:43:28 +03:00
ClearDirByPattern(TestSuiteDir+PathSeparator+'pabcrtl_tests','*.mdb');
2015-12-29 17:56:29 +03:00
ClearDirByPattern(TestSuiteDir+PathSeparator+'pabcrtl_tests','*.pcu');
2015-05-14 22:35:07 +03:00
end;
procedure DeletePCUFiles;
begin
2015-12-29 17:56:29 +03:00
ClearDirByPattern(TestSuiteDir+PathSeparator+'usesunits','*.pcu');
2015-05-14 22:35:07 +03:00
end;
procedure DeletePABCSystemPCU;
begin
var dir := Path.Combine(Path.GetDirectoryName(GetEXEFileName()),'Lib');
var pcu := Path.Combine(dir,'PABCSystem.pcu');
end;
procedure CopyLibFiles;
begin
var files := Directory.GetFiles(GetLibDir(),'*.pas');
foreach f: string in files do
begin
2015-12-29 17:56:29 +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());
DeletePCUFiles;
ClearExeDir;
CompileAllRunTests(false);
CopyLibFiles;
CompileAllCompilationTests('CompilationSamples',false);
CompileAllUnits;
CopyPCUFiles;
CompileAllUsesUnits;
CompileErrorTests(false);
writeln('Tests compiled successfully');
RunAllTests(false);
writeln('Tests run successfully');
ClearExeDir;
DeletePCUFiles;
CompileAllRunTests(true);
writeln('Tests with pabcrtl compiled successfully');
CompileAllCompilationTests('pabcrtl_tests',true);
RunAllTests(false);
writeln('Tests with pabcrtl run successfully');
ClearExeDir;
CompileAllRunTests(false, true);
writeln('Tests in 32bit mode compiled successfully');
RunAllTests(false);
writeln('Tests in 32bit run successfully');
System.Environment.CurrentDirectory := Path.GetDirectoryName(GetEXEFileName());
RunExpressionsExtractTests;
writeln('Intellisense expression tests run successfully');
2016-05-08 21:27:29 +03:00
RunIntellisenseTests;
writeln('Intellisense tests run successfully');
RunFormatterTests;
writeln('Formatter tests run successfully');
except
on e: Exception do
2015-12-28 21:46:36 +03:00
assert(false,e.ToString());
end;
2015-05-14 22:35:07 +03:00
end.