Skip to content

Commit

Permalink
tests: Added test for Source File Filename
Browse files Browse the repository at this point in the history
  • Loading branch information
gcarreno committed Jan 14, 2024
1 parent 2abd5db commit dbcadf5
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/text/opp.text.sourcefile.pas
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ ETextSourceFileDoesNotExist = class(Exception);
{ TTextSourceFile }
TTextSourceFile = class(TObject)
private
FFilename: String;
FSourceFileStream: TFileStream;
protected
public
constructor Create(const AFileName: String);
destructor Destroy; override;

function GetNextChar: TTextCharacter;

property Filename: String
read FFilename;
published
end;

Expand All @@ -44,6 +48,7 @@ constructor TTextSourceFile.Create(const AFileName: String);
)
);

FFilename:= AFileName;
FSourceFileStream:= TFileStream.Create(AFileName, fmOpenRead);
end;

Expand Down
45 changes: 44 additions & 1 deletion tests/TestObjectPascalParserCLI.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<PackageName Value="FCL"/>
</Item1>
</RequiredPackages>
<Units Count="4">
<Units Count="13">
<Unit0>
<Filename Value="TestObjectPascalParserCLI.lpr"/>
<IsPartOfProject Value="True"/>
Expand All @@ -110,6 +110,49 @@
<IsPartOfProject Value="True"/>
<UnitName Value="OPP.Text.SourceFile"/>
</Unit3>
<Unit4>
<Filename Value="states/testobjectpascalparserstatesstack.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="TestObjectPascalParserStatesStack"/>
</Unit4>
<Unit5>
<Filename Value="../src/states/opp.states.pas"/>
<IsPartOfProject Value="True"/>
</Unit5>
<Unit6>
<Filename Value="../src/states/opp.states.stack.pas"/>
<IsPartOfProject Value="True"/>
</Unit6>
<Unit7>
<Filename Value="states/testobjectpascalparserstatesstacktokens.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="TestObjectPascalParserStatesStackTokens"/>
</Unit7>
<Unit8>
<Filename Value="../src/states/opp.states.stacktokens.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="OPP.States.StackTokens"/>
</Unit8>
<Unit9>
<Filename Value="tokenizing/testobjectpascalparsertokenizingtokenizer.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="TestObjectPascalParserTokenizingTokenizer"/>
</Unit9>
<Unit10>
<Filename Value="../src/tokenizing/opp.tokenizing.tokens.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="OPP.Tokenizing.Tokens"/>
</Unit10>
<Unit11>
<Filename Value="../src/tokenizing/opp.tokenizing.tokenizer.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="OPP.Tokenizing.Tokenizer"/>
</Unit11>
<Unit12>
<Filename Value="tokenizing/testobjectpascalparsertokenizingtokenizereof.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="TestObjectPascalParserTokenizingTokenizerEOF"/>
</Unit12>
</Units>
</ProjectOptions>
<CompilerOptions>
Expand Down
53 changes: 38 additions & 15 deletions tests/text/testobjectpascalparsertextsourcefile.pas
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,53 @@ TTestObjectPascalParserTextSourceFile= class(TTestCase)
private
FSourceFile: TTextSourceFile;

function DumpToTempFile(const AContent: String): String;

procedure TestSourceFileCreateException;
published
procedure TestObjectPascalParserTextSourceFileCreate;
procedure TestObjectPascalParserTextSourceFileCreateError;
procedure TestObjectPascalParserTextSourceFileFilename;
end;

implementation

const
cSourceFileContentUTF8 = 'program Test🌟';

procedure TTestObjectPascalParserTextSourceFile.TestSourceFileCreateException;
begin
FSourceFile:= TTextSourceFile.Create('');
end;

procedure TTestObjectPascalParserTextSourceFile.TestObjectPascalParserTextSourceFileCreate;
function TTestObjectPascalParserTextSourceFile.DumpToTempFile(
const AContent: String): String;
var
stringStream: TstringStream;
tmpFilename: String;
stringStream: TstringStream;
begin
Result:= EmptyStr;
stringStream:= TStringStream.Create(cSourceFileContentUTF8);
try
tmpFilename:= GetTempFileName;
stringStream.SaveToFile(tmpFilename);
try
finally
stringStream.Free;
end;
Result:= tmpFilename;
end;

FSourceFile:= TTextSourceFile.Create(tmpFilename);
AssertNotNull('Text Source File is not null', FSourceFile);
// Assert file is of type tftUTF8
procedure TTestObjectPascalParserTextSourceFile.TestSourceFileCreateException;
begin
FSourceFile:= TTextSourceFile.Create('');
end;

finally
DeleteFile(tmpFilename);
end;
procedure TTestObjectPascalParserTextSourceFile.TestObjectPascalParserTextSourceFileCreate;
begin
FSourceFile:= TTextSourceFile.Create(DumpToTempFile(cSourceFileContentUTF8));
try
AssertNotNull('Text Source File is not null', FSourceFile);
// Assert file is of type tftUTF8
finally
stringStream.Free;
DeleteFile(FSourceFile.Filename);
FSourceFile.Free;
end;

end;

procedure TTestObjectPascalParserTextSourceFile.TestObjectPascalParserTextSourceFileCreateError;
Expand All @@ -76,6 +86,19 @@ procedure TTestObjectPascalParserTextSourceFile.TestObjectPascalParserTextSource
if Assigned(FSourceFile) then FSourceFile.Free;
end;

procedure TTestObjectPascalParserTextSourceFile.TestObjectPascalParserTextSourceFileFilename;
var
tmpFilename: String;
begin
tmpFilename:= DumpToTempFile('');
FSourceFile:= TTextSourceFile.Create(tmpFilename);
try
AssertEquals('Text Source File Filename', tmpFilename, FSourceFile.Filename);
finally
FSourceFile.Free;
end;
end;



initialization
Expand Down

0 comments on commit dbcadf5

Please sign in to comment.