summaryrefslogtreecommitdiff
path: root/packages/pastojs/tests/tcunitsearch.pas
blob: e0cedea74bbc908799e752b3a7bbf5f39a0940fc (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
{
    This file is part of the Free Component Library (FCL)
    Copyright (c) 2018 by Michael Van Canneyt

    Unit tests for Pascal-to-Javascript converter class.

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program 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.

 **********************************************************************

 Examples:
    ./testpas2js --suite=TestCLI_UnitSearch.
    ./testpas2js --suite=TestUS_Program
    ./testpas2js --suite=TestUS_UsesEmptyFileFail
}
unit tcunitsearch;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, contnrs,
  fpcunit, testregistry,
  PScanner, PasTree,
  {$IFDEF CheckPasTreeRefCount}PasResolveEval,{$ENDIF}
  Pas2jsFileUtils, Pas2jsCompiler, Pas2jsfsCompiler, Pas2jsFileCache, Pas2jsLogger,
  tcmodules;

type

  { TTestCompiler }

  TTestCompiler = class(TPas2jsFSCompiler)
  private
    FExitCode: longint;
  protected
    function GetExitCode: Longint; override;
    procedure SetExitCode(Value: Longint); override;
  end;

  { TCLIFile }

  TCLIFile = class
  public
    Filename: string;
    Source: string;
    Age: TPas2jsFileAgeTime;
    Attr: TPas2jsFileAttr;
    constructor Create(const aFilename, Src: string; aAge: TPas2jsFileAgeTime;
       aAttr: TPas2jsFileAttr);
  end;

  { TCLILogMsg }

  TCLILogMsg = class
  public
    Msg: string;
    MsgTxt: string;
    MsgType: TMessageType;
    MsgNumber: integer;
    MsgFile: string;
    MsgLine: integer;
    MsgCol: integer;
  end;

  { TCustomTestCLI }

  TCustomTestCLI = class(TTestCase)
  private
    FCurDate: TDateTime;
    FErrorCol: integer;
    FErrorFile: string;
    FErrorLine: integer;
    FErrorMsg: string;
    FErrorNumber: integer;
    FWorkDir: string;
    FCompilerExe: string;
    FCompiler: TTestCompiler;
    FDefaultFileAge: longint;
    FFiles: TObjectList; // list of TCLIFile
    FLogMsgs: TObjectList; // list ot TCLILogMsg
    FParams: TStringList;
    {$IFDEF EnablePasTreeGlobalRefCount}
    FElementRefCountAtSetup: int64;
    {$ENDIF}
    function GetExitCode: integer;
    function GetFiles(Index: integer): TCLIFile;
    function GetLogMsgs(Index: integer): TCLILogMsg;
    procedure SetExitCode(const AValue: integer);
    procedure SetWorkDir(const AValue: string);
  protected
    procedure SetUp; override;
    procedure TearDown; override;
    procedure DoLog(Sender: TObject; const Msg: String);
    Function OnReadDirectory(Dir: TPas2jsCachedDirectory): boolean; virtual;
    Function OnReadFile(aFilename: string; var aSource: string): boolean; virtual;
    procedure OnWriteFile(aFilename: string; Source: string); virtual;
    procedure WriteSources;
  public
    constructor Create; override;
    destructor Destroy; override;
    procedure Compile(const Args: array of string; ExpectedExitCode: longint = 0);
    property Compiler: TTestCompiler read FCompiler;
    property CompilerExe: string read FCompilerExe write FCompilerExe;
    property Params: TStringList read FParams;
    property Files[Index: integer]: TCLIFile read GetFiles; // files an directories
    function FileCount: integer;
    function FindFile(Filename: string): TCLIFile; // files and directories
    function ExpandFilename(const Filename: string): string;
    function AddFile(Filename, Source: string): TCLIFile;
    function AddFile(Filename: string; const SourceLines: array of string): TCLIFile;
    function AddUnit(Filename: string; const Intf, Impl: array of string): TCLIFile;
    function AddDir(Filename: string): TCLIFile;
    procedure AssertFileExists(Filename: string);
    property WorkDir: string read FWorkDir write SetWorkDir;
    property DefaultFileAge: longint read FDefaultFileAge write FDefaultFileAge;
    property ExitCode: integer read GetExitCode write SetExitCode;
    property LogMsgs[Index: integer]: TCLILogMsg read GetLogMsgs;
    function GetLogCount: integer;
    property ErrorMsg: string read FErrorMsg write FErrorMsg;
    property ErrorFile: string read FErrorFile write FErrorFile;
    property ErrorLine: integer read FErrorLine write FErrorLine;
    property ErrorCol: integer read FErrorCol write FErrorCol;
    property ErrorNumber: integer read FErrorNumber write FErrorNumber;
    property CurDate: TDateTime read FCurDate write FCurDate;
  end;

  { TTestCLI_UnitSearch }

  TTestCLI_UnitSearch = class(TCustomTestCLI)
  published
    procedure TestUS_Program;
    procedure TestUS_UsesEmptyFileFail;
    procedure TestUS_Program_o;
    procedure TestUS_Program_FU;
    procedure TestUS_Program_FU_o;
    procedure TestUS_Program_FE_o;

    procedure TestUS_UsesInFile;
    procedure TestUS_UsesInFile_Duplicate;
    procedure TestUS_UsesInFile_IndirectDuplicate;
  end;

function LinesToStr(const Lines: array of string): string;

implementation

function LinesToStr(const Lines: array of string): string;
var
  i: Integer;
begin
  Result:='';
  for i:=low(Lines) to high(Lines) do
    Result:=Result+Lines[i]+LineEnding;
end;

{ TCLIFile }

constructor TCLIFile.Create(const aFilename, Src: string;
  aAge: TPas2jsFileAgeTime; aAttr: TPas2jsFileAttr);
begin
  Filename:=aFilename;
  Source:=Src;
  Age:=aAge;
  Attr:=aAttr;
end;

{ TTestCompiler }

function TTestCompiler.GetExitCode: Longint;
begin
  Result:=FExitCode;
end;

procedure TTestCompiler.SetExitCode(Value: Longint);
begin
  FExitCode:=Value;
end;

{ TCustomTestCLI }

function TCustomTestCLI.GetFiles(Index: integer): TCLIFile;
begin
  Result:=TCLIFile(FFiles[Index]);
end;

function TCustomTestCLI.GetExitCode: integer;
begin
  Result:=Compiler.ExitCode;
end;

function TCustomTestCLI.GetLogMsgs(Index: integer): TCLILogMsg;
begin
  Result:=TCLILogMsg(FLogMsgs[Index]);
end;

procedure TCustomTestCLI.SetExitCode(const AValue: integer);
begin
  Compiler.ExitCode:=AValue;
end;

procedure TCustomTestCLI.SetWorkDir(const AValue: string);
var
  NewValue: String;
begin
  NewValue:=IncludeTrailingPathDelimiter(ResolveDots(AValue));
  if FWorkDir=NewValue then Exit;
  FWorkDir:=NewValue;
end;

procedure TCustomTestCLI.SetUp;
begin
  {$IFDEF EnablePasTreeGlobalRefCount}
  FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  {$ENDIF}
  inherited SetUp;
  FDefaultFileAge:=DateTimeToFileDate(Now);
  WorkDir:=ExtractFilePath(ParamStr(0));
  {$IFDEF Windows}
  CompilerExe:='P:\bin\pas2js.exe';
  {$ELSE}
  CompilerExe:='/usr/bin/pas2js';
  {$ENDIF}
  FCompiler:=TTestCompiler.Create;
  Compiler.Log.OnLog:=@DoLog;
  Compiler.FileCache.OnReadDirectory:=@OnReadDirectory;
  Compiler.FileCache.OnReadFile:=@OnReadFile;
  Compiler.FileCache.OnWriteFile:=@OnWriteFile;
end;

procedure TCustomTestCLI.TearDown;
{$IFDEF CheckPasTreeRefCount}
var
  El: TPasElement;
  i: integer;
{$ENDIF}
begin
  FreeAndNil(FCompiler);
  FParams.Clear;
  FFiles.Clear;
  FLogMsgs.Clear;
  FErrorMsg:='';
  FErrorFile:='';
  FErrorLine:=0;
  FErrorCol:=0;
  FErrorNumber:=0;
  inherited TearDown;
  {$IFDEF EnablePasTreeGlobalRefCount}
  if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
    begin
    writeln('TCustomTestCLI.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
    {$IFDEF CheckPasTreeRefCount}
    El:=TPasElement.FirstRefEl;
    while El<>nil do
      begin
      writeln('  ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
      for i:=0 to El.RefIds.Count-1 do
        writeln('    ',El.RefIds[i]);
      El:=El.NextRefEl;
      end;
    {$ENDIF}
    Halt;
    Fail('TCustomTestCLI.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
    end;
  {$ENDIF}
end;

procedure TCustomTestCLI.DoLog(Sender: TObject; const Msg: String);
var
  LogMsg: TCLILogMsg;
begin
  {$IF defined(VerbosePasResolver) or defined(VerbosePCUFiler)}
  writeln('TCustomTestCLI.DoLog ',Msg,' File=',Compiler.Log.LastMsgFile,' Line=',Compiler.Log.LastMsgLine);
  {$ENDIF}
  LogMsg:=TCLILogMsg.Create;
  LogMsg.Msg:=Msg;
  LogMsg.MsgTxt:=Compiler.Log.LastMsgTxt;
  LogMsg.MsgType:=Compiler.Log.LastMsgType;
  LogMsg.MsgFile:=Compiler.Log.LastMsgFile;
  LogMsg.MsgLine:=Compiler.Log.LastMsgLine;
  LogMsg.MsgCol:=Compiler.Log.LastMsgCol;
  LogMsg.MsgNumber:=Compiler.Log.LastMsgNumber;
  FLogMsgs.Add(LogMsg);
  if (LogMsg.MsgType<=mtError) then
  begin
    if (ErrorFile='')
        or ((ErrorLine<1) and (LogMsg.MsgLine>0)) then
    begin
      ErrorMsg:=LogMsg.MsgTxt;
      ErrorFile:=LogMsg.MsgFile;
      ErrorLine:=LogMsg.MsgLine;
      ErrorCol:=LogMsg.MsgCol;
    end;
  end;
end;

function TCustomTestCLI.OnReadDirectory(Dir: TPas2jsCachedDirectory): boolean;
var
  i: Integer;
  aFile: TCLIFile;
  Path: String;
begin
  Path:=Dir.Path;
  //writeln('TCustomTestCLI.ReadDirectory START ',Path,' ',Dir.Count);
  Dir.Add('.',DefaultFileAge,faDirectory,4096);
  Dir.Add('..',DefaultFileAge,faDirectory,4096);
  for i:=0 to FileCount-1 do
    begin
    aFile:=Files[i];
    if CompareFilenames(ExtractFilePath(aFile.Filename),Path)<>0 then continue;
    //writeln('TCustomTestCLI.ReadDirectory ',aFile.Filename);
    Dir.Add(ExtractFileName(aFile.Filename),aFile.Age,aFile.Attr,length(aFile.Source));
    end;
  //writeln('TCustomTestCLI.ReadDirectory END ',Path,' ',Dir.Count);
  Result:=true;
end;

function TCustomTestCLI.OnReadFile(aFilename: string; var aSource: string
  ): boolean;
var
  aFile: TCLIFile;
begin
  aFile:=FindFile(aFilename);
  //writeln('TCustomTestCLI.ReadFile ',aFilename,' Found=',aFile<>nil);
  if aFile=nil then exit(false);
  if (faDirectory and aFile.Attr)>0 then
  begin
    {$IF defined(VerbosePasResolver) or defined(VerbosePCUFiler)}
    writeln('[20180224000557] TCustomTestCLI.OnReadFile ',aFilename);
    {$ENDIF}
    EPas2jsFileCache.Create('TCustomTestCLI.OnReadFile: reading directory '+aFilename);
  end;
  aSource:=aFile.Source;
  //writeln('TCustomTestCLI.OnReadFile ',aFile.Filename,' "',LeftStr(aFile.Source,50),'"');
  Result:=true;
end;

procedure TCustomTestCLI.OnWriteFile(aFilename: string; Source: string);
var
  aFile: TCLIFile;
  {$IF defined(VerboseUnitQueue) or defined(VerbosePCUFiler)}
  //i: Integer;
  {$ENDIF}
begin
  aFile:=FindFile(aFilename);
  {$IF defined(VerboseUnitQueue) or defined(VerbosePCUFiler)}
  writeln('TCustomTestCLI.WriteFile ',aFilename,' Found=',aFile<>nil,' SrcLen=',length(Source));
  {$ENDIF}
  if aFile<>nil then
  begin
    if (faDirectory and aFile.Attr)>0 then
    begin
      {$IF defined(VerboseUnitQueue) or defined(VerbosePCUFiler)}
      writeln('[20180223175616] TCustomTestCLI.OnWriteFile ',aFilename);
      {$ENDIF}
      raise EPas2jsFileCache.Create('unable to write file to directory "'+aFilename+'"');
    end;
  end else
  begin
    {$IF defined(VerboseUnitQueue) or defined(VerbosePCUFiler)}
    //writeln('TCustomTestCLI.OnWriteFile FFiles: ',FFiles.Count);
    //for i:=0 to FFiles.Count-1 do
    //begin
    //  aFile:=TCLIFile(FFiles[i]);
    //  writeln('  ',i,': Filename=',aFile.Filename,' ',CompareFilenames(aFile.Filename,aFilename),' Dir=',(aFile.Attr and faDirectory)>0,' Len=',length(aFile.Source));
    //end;
    {$ENDIF}
    aFile:=TCLIFile.Create(aFilename,'',0,0);
    FFiles.Add(aFile);
  end;
  aFile.Source:=Source;
  aFile.Attr:=faNormal;
  aFile.Age:=DateTimeToFileDate(CurDate);
  writeln('TCustomTestCLI.OnWriteFile ',aFile.Filename,' Found=',FindFile(aFilename)<>nil,' "',LeftStr(aFile.Source,50),'" ');
end;

procedure TCustomTestCLI.WriteSources;
var
  i, j, aRow, aCol: Integer;
  aFile: TCLIFile;
  SrcLines: TStringList;
  Line, aFilename: String;
  IsSrc: Boolean;
begin
  writeln('TCustomTestCLI.WriteSources START');
  aFilename:=ErrorFile;
  aRow:=ErrorLine;
  aCol:=ErrorCol;
  SrcLines:=TStringList.Create;
  try
    for i:=0 to FileCount-1 do
    begin
      aFile:=Files[i];
      if (faDirectory and aFile.Attr)>0 then continue;
      writeln('Testcode:-File="',aFile.Filename,'"----------------------------------:');
      SrcLines.Text:=aFile.Source;
      IsSrc:=ExtractFilename(aFile.Filename)=ExtractFileName(aFilename);
      for j:=1 to SrcLines.Count do
        begin
        Line:=SrcLines[j-1];
        if IsSrc and (j=aRow) then
          begin
          write('*');
          Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
          end;
        writeln(Format('%:4d: ',[j]),Line);
        end;
    end;
  finally
    SrcLines.Free;
  end;
end;

constructor TCustomTestCLI.Create;
begin
  inherited Create;
  FFiles:=TObjectList.Create(true);
  FLogMsgs:=TObjectList.Create(true);
  FParams:=TStringList.Create;
  CurDate:=Now;
end;

destructor TCustomTestCLI.Destroy;
begin
  FreeAndNil(FFiles);
  FreeAndNil(FLogMsgs);
  FreeAndNil(FParams);
  inherited Destroy;
end;

procedure TCustomTestCLI.Compile(const Args: array of string;
  ExpectedExitCode: longint);
var
  i: Integer;
begin
  AssertEquals('Initial System.ExitCode',0,system.ExitCode);
  for i:=low(Args) to High(Args) do
    Params.Add(Args[i]);
  try
    try
      //writeln('TCustomTestCLI.Compile WorkDir=',WorkDir);
      Compiler.Run(CompilerExe,WorkDir,Params,false);
    except
      on E: ECompilerTerminate do
      begin
        {$IF defined(VerbosePasResolver) or defined(VerbosePCUFiler)}
        writeln('TCustomTestCLI.Compile ',E.ClassName,':',E.Message);
        {$ENDIF}
      end;
      on E: Exception do
      begin
        {$IF defined(VerbosePasResolver) or defined(VerbosePCUFiler)}
        writeln('TCustomTestCLI.Compile ',E.ClassName,':',E.Message);
        {$ENDIF}
        Fail('TCustomTestCLI.Compile '+E.ClassName+':'+E.Message);
      end;
    end;
  finally
    Compiler.Log.CloseOutputFile;
  end;

  if ExpectedExitCode<>ExitCode then
  begin
    WriteSources;
    AssertEquals('ExitCode',ExpectedExitCode,ExitCode);
  end;
end;

function TCustomTestCLI.FileCount: integer;
begin
  Result:=FFiles.Count;
end;

function TCustomTestCLI.FindFile(Filename: string): TCLIFile;
var
  i: Integer;
begin
  Filename:=ExpandFilename(Filename);
  for i:=0 to FileCount-1 do
    if CompareFilenames(Files[i].Filename,Filename)=0 then
      exit(Files[i]);
  Result:=nil;
end;

function TCustomTestCLI.ExpandFilename(const Filename: string): string;
begin
  Result:=SetDirSeparators(Filename);
  if not FilenameIsAbsolute(Result) then
    Result:=WorkDir+Result;
  Result:=ResolveDots(Result);
end;

function TCustomTestCLI.AddFile(Filename, Source: string): TCLIFile;
begin
  Filename:=ExpandFilename(Filename);
  {$IFDEF VerbosePCUFiler}
  writeln('TCustomTestCLI.AddFile ',Filename);
  {$ENDIF}
  Result:=FindFile(Filename);
  if Result<>nil then
    raise Exception.Create('[20180224001050] TCustomTestCLI.AddFile already exists: '+Filename);
  Result:=TCLIFile.Create(Filename,Source,DefaultFileAge,faNormal);
  FFiles.Add(Result);
  AddDir(ExtractFilePath(Filename));
end;

function TCustomTestCLI.AddFile(Filename: string;
  const SourceLines: array of string): TCLIFile;
begin
  Result:=AddFile(Filename,LinesToStr(SourceLines));
end;

function TCustomTestCLI.AddUnit(Filename: string; const Intf,
  Impl: array of string): TCLIFile;
var
  Name: String;
begin
  Name:=ExtractFilenameOnly(Filename);
  Result:=AddFile(Filename,
    'unit '+Name+';'+LineEnding
    +'interface'+LineEnding
    +LinesToStr(Intf)
    +'implementation'+LineEnding
    +LinesToStr(Impl)
    +'end.'+LineEnding);
end;

function TCustomTestCLI.AddDir(Filename: string): TCLIFile;
var
  p: Integer;
  Dir: String;
  aFile: TCLIFile;
begin
  Result:=nil;
  Filename:=IncludeTrailingPathDelimiter(ExpandFilename(Filename));
  p:=length(Filename);
  while p>1 do
  begin
    if Filename[p]=PathDelim then
    begin
      Dir:=LeftStr(Filename,p-1);
      aFile:=FindFile(Dir);
      if Result=nil then
        Result:=aFile;
      if aFile=nil then
        begin
        {$IFDEF VerbosePCUFiler}
        writeln('TCustomTestCLI.AddDir add Dir=',Dir);
        {$ENDIF}
        FFiles.Add(TCLIFile.Create(Dir,'',DefaultFileAge,faDirectory));
        end
      else if (aFile.Attr and faDirectory)=0 then
      begin
        {$IFDEF VerbosePCUFiler}
        writeln('[20180224001036] TCustomTestCLI.AddDir file exists: Dir=',Dir);
        {$ENDIF}
        raise EPas2jsFileCache.Create('[20180224001036] TCustomTestCLI.AddDir Dir='+Dir);
      end;
      dec(p);
    end else
      dec(p);
  end;
end;

procedure TCustomTestCLI.AssertFileExists(Filename: string);
var
  aFile: TCLIFile;
begin
  aFile:=FindFile(Filename);
  AssertNotNull('File not found: '+Filename,aFile);
end;

function TCustomTestCLI.GetLogCount: integer;
begin
  Result:=FLogMsgs.Count;
end;

{ TTestCLI_UnitSearch }

procedure TTestCLI_UnitSearch.TestUS_Program;
begin
  AddUnit('system.pp',[''],['']);
  AddFile('test1.pas',[
    'begin',
    'end.']);
  Compile(['test1.pas','-va']);
  AssertNotNull('test1.js not found',FindFile('test1.js'));
end;

procedure TTestCLI_UnitSearch.TestUS_UsesEmptyFileFail;
begin
  AddFile('system.pp','');
  AddFile('test1.pas',[
    'begin',
    'end.']);
  Compile(['test1.pas'],ExitCodeSyntaxError);
  AssertEquals('ErrorMsg','Expected "unit"',ErrorMsg);
end;

procedure TTestCLI_UnitSearch.TestUS_Program_o;
begin
  AddUnit('system.pp',[''],['']);
  AddFile('test1.pas',[
    'begin',
    'end.']);
  Compile(['test1.pas','-obla.js']);
  AssertNotNull('bla.js not found',FindFile('bla.js'));
end;

procedure TTestCLI_UnitSearch.TestUS_Program_FU;
begin
  AddUnit('system.pp',[''],['']);
  AddFile('test1.pas',[
    'begin',
    'end.']);
  AddDir('lib');
  Compile(['test1.pas','-FUlib']);
  AssertNotNull('lib/test1.js not found',FindFile('lib/test1.js'));
end;

procedure TTestCLI_UnitSearch.TestUS_Program_FU_o;
begin
  AddUnit('system.pp',[''],['']);
  AddFile('test1.pas',[
    'begin',
    'end.']);
  AddDir('lib');
  Compile(['test1.pas','-FUlib','-ofoo.js']);
  AssertNotNull('lib/system.js not found',FindFile('lib/system.js'));
  AssertNotNull('foo.js not found',FindFile('foo.js'));
end;

procedure TTestCLI_UnitSearch.TestUS_Program_FE_o;
begin
  AddUnit('system.pp',[''],['']);
  AddFile('test1.pas',[
    'begin',
    'end.']);
  AddDir('lib');
  Compile(['test1.pas','-FElib','-ofoo.js']);
  AssertNotNull('lib/system.js not found',FindFile('lib/system.js'));
  AssertNotNull('foo.js not found',FindFile('foo.js'));
end;

procedure TTestCLI_UnitSearch.TestUS_UsesInFile;
begin
  AddUnit('system.pp',[''],['']);
  AddUnit('unit1.pas',
  ['uses bird in ''unit2.pas'';',
   'var a: longint;'],
  ['']);
  AddUnit('unit2.pas',
  ['var b: longint;'],
  ['']);
  AddFile('test1.pas',[
    'uses foo in ''unit1.pas'', bar in ''unit2.pas'';',
    'begin',
    '  bar.b:=foo.a;',
    '  a:=b;',
    'end.']);
  Compile(['test1.pas','-Jc']);
end;

procedure TTestCLI_UnitSearch.TestUS_UsesInFile_Duplicate;
begin
  AddUnit('system.pp',[''],['']);
  AddUnit('unit1.pas',
  ['var a: longint;'],
  ['']);
  AddUnit('sub/unit1.pas',
  ['var b: longint;'],
  ['']);
  AddFile('test1.pas',[
    'uses foo in ''unit1.pas'', bar in ''sub/unit1.pas'';',
    'begin',
    '  bar.b:=foo.a;',
    '  a:=b;',
    'end.']);
  Compile(['test1.pas','-Jc'],ExitCodeSyntaxError);
  AssertEquals('ErrorMsg','Duplicate file found: "'+WorkDir+'sub/unit1.pas" and "'+WorkDir+'unit1.pas"',ErrorMsg);
end;

procedure TTestCLI_UnitSearch.TestUS_UsesInFile_IndirectDuplicate;
begin
  AddUnit('system.pp',[''],['']);
  AddUnit('unit1.pas',
  ['var a: longint;'],
  ['']);
  AddUnit('sub/unit1.pas',
  ['var b: longint;'],
  ['']);
  AddUnit('unit2.pas',
  ['uses unit1 in ''unit1.pas'';'],
  ['']);
  AddFile('test1.pas',[
    'uses unit2, foo in ''sub/unit1.pas'';',
    'begin',
    'end.']);
  Compile(['test1.pas','-Jc'],ExitCodeSyntaxError);
  AssertEquals('ErrorMsg','Duplicate file found: "'+WorkDir+'unit1.pas" and "'+WorkDir+'sub/unit1.pas"',ErrorMsg);
end;

Initialization
  RegisterTests([TTestCLI_UnitSearch]);
end.