Skip to content

Commit

Permalink
feat: Start of Source File class
Browse files Browse the repository at this point in the history
  • Loading branch information
gcarreno committed Jan 14, 2024
1 parent 3bf46b1 commit 2abd5db
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.lazarus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: gcarreno/re-usable-workflows/.github/workflows/test.lazarus.yml@main
with:
config: '{
"app-name": "TestObjectPascalParser",
"app-name": "TestObjectPascalParserCLI",
"lpi-path": "tests",
"bin-path": "bin",
"build-mode": "Release",
Expand Down
32 changes: 32 additions & 0 deletions src/text/opp.text.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
unit OPP.Text;

{$mode ObjFPC}{$H+}

interface

uses
Classes
, SysUtils
;

type
{ TTextFileType }
TTextFileType = (tftUnknnown, tftAnsi, tftUTF8, tftUTF16, tftUTF32);

{ TTextCharType }
TTextCharType = (tctUnknown, tctAnsi, tctUTF8, tctUTF16, tctUTF32);

{ #todo 999 -ogcarreno : Determine if a union is possible or best }
{ TTextCharacter }
TTextCharacter = record
&Type: TTextCharType;
Ansi: Char;
UTF8: String;
UTF16: String;
UTF32: String;
end;

implementation

end.

68 changes: 68 additions & 0 deletions src/text/opp.text.sourcefile.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
unit OPP.Text.SourceFile;

{$mode ObjFPC}{$H+}

interface

uses
Classes
, SysUtils
, OPP.Text
;

type
{ ETextSourceFileDoesNotExist }
ETextSourceFileDoesNotExist = class(Exception);

{ TTextSourceFile }
TTextSourceFile = class(TObject)
private
FSourceFileStream: TFileStream;
protected
public
constructor Create(const AFileName: String);
destructor Destroy; override;

function GetNextChar: TTextCharacter;
published
end;

resourcestring
rsETextSourceFileDoesNotExist = 'File "%s" does not exist';

implementation

{ TTextSourceFile }

constructor TTextSourceFile.Create(const AFileName: String);
begin
FSourceFileStream:= nil;
if not FileExists(AFileName) then raise ETextSourceFileDoesNotExist.Create(
Format(
rsETextSourceFileDoesNotExist,
[ AFileName ]
)
);

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

destructor TTextSourceFile.Destroy;
begin
if Assigned(FSourceFileStream) then FSourceFileStream.Free;
inherited Destroy;
end;

function TTextSourceFile.GetNextChar: TTextCharacter;
begin
Result.&Type:= tctUnknown;
Result.Ansi := #0;
Result.UTF8 := '';
Result.UTF16:= '';
Result.UTF32:= '';

// Get next char(s) and fill record
end;

end.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<CompatibilityMode Value="True"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<Title Value="TestObjectPascalParser"/>
<Title Value="TestObjectPascalParserCLI"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
Expand All @@ -20,11 +20,11 @@
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="../bin/TestObjectPascalParser"/>
<Filename Value="../bin/TestObjectPascalParserCLI"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="states;../src/states;tokenizing;../src/tokenizing"/>
<OtherUnitFiles Value="text;../src/text;states;../src/states;tokenizing;../src/tokenizing"/>
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Parsing>
Expand Down Expand Up @@ -55,11 +55,11 @@
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="../bin/TestObjectPascalParser"/>
<Filename Value="../bin/TestObjectPascalParserCLI"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="states;../src/states;tokenizing;../src/tokenizing"/>
<OtherUnitFiles Value="text;../src/text;states;../src/states;tokenizing;../src/tokenizing"/>
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<CodeGeneration>
Expand Down Expand Up @@ -90,76 +90,41 @@
<PackageName Value="FCL"/>
</Item1>
</RequiredPackages>
<Units Count="10">
<Units Count="4">
<Unit0>
<Filename Value="TestObjectPascalParser.lpr"/>
<Filename Value="TestObjectPascalParserCLI.lpr"/>
<IsPartOfProject Value="True"/>
</Unit0>
<Unit1>
<Filename Value="states/testobjectpascalparserstatesstack.pas"/>
<Filename Value="text/testobjectpascalparsertextsourcefile.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="TestObjectPascalParserStatesStack"/>
<UnitName Value="TestObjectPascalParserTextSourceFile"/>
</Unit1>
<Unit2>
<Filename Value="../src/states/opp.states.pas"/>
<Filename Value="../src/text/opp.text.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="OPP.States"/>
<UnitName Value="OPP.Text"/>
</Unit2>
<Unit3>
<Filename Value="../src/states/opp.states.stack.pas"/>
<Filename Value="../src/text/opp.text.sourcefile.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="OPP.States.Stack"/>
<UnitName Value="OPP.Text.SourceFile"/>
</Unit3>
<Unit4>
<Filename Value="states/testobjectpascalparserstatesstacktokens.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="TestObjectPascalParserStatesStackTokens"/>
</Unit4>
<Unit5>
<Filename Value="../src/states/opp.states.stacktokens.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="OPP.States.StackTokens"/>
</Unit5>
<Unit6>
<Filename Value="tokenizing/testobjectpascalparsertokenizingtokenizer.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="TestObjectPascalParserTokenizingTokenizer"/>
</Unit6>
<Unit7>
<Filename Value="../src/tokenizing/opp.tokenizing.tokens.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="OPP.Tokenizing.Tokens"/>
</Unit7>
<Unit8>
<Filename Value="../src/tokenizing/opp.tokenizing.tokenizer.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="OPP.Tokenizing.Tokenizer"/>
</Unit8>
<Unit9>
<Filename Value="tokenizing/testobjectpascalparsertokenizingtokenizereof.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="TestObjectPascalParserTokenizingTokenizerEOF"/>
</Unit9>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="../bin/TestObjectPascalParser"/>
<Filename Value="../bin/TestObjectPascalParserCLI"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="states;../src/states;tokenizing;../src/tokenizing"/>
<OtherUnitFiles Value="text;../src/text;states;../src/states;tokenizing;../src/tokenizing"/>
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Debugging>
<DebugInfoType Value="dsDwarf3"/>
</Debugging>
</Linking>
</CompilerOptions>
<Debugging>
<Exceptions Count="4">
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
Expand All @@ -169,9 +134,6 @@
<Item3>
<Name Value="EFOpenError"/>
</Item3>
<Item4>
<Name Value="EStatesStackEmpty"/>
</Item4>
</Exceptions>
</Debugging>
</CONFIG>
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
program TestObjectPascalParser;
program TestObjectPascalParserCLI;

{$mode objfpc}{$H+}

uses
Classes
, consoletestrunner
, TestObjectPascalParserStatesStack
, TestObjectPascalParserStatesStackTokens
, TestObjectPascalParserTokenizingTokenizer
, TestObjectPascalParserTokenizingTokenizerEOF
, TestObjectPascalParserTextSourceFile
//, TestObjectPascalParserStatesStack
//, TestObjectPascalParserStatesStackTokens
//, TestObjectPascalParserTokenizingTokenizer
//, TestObjectPascalParserTokenizingTokenizerEOF
;

type
Expand Down
85 changes: 85 additions & 0 deletions tests/text/testobjectpascalparsertextsourcefile.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
unit TestObjectPascalParserTextSourceFile;

{$mode objfpc}{$H+}

interface

uses
Classes
, SysUtils
, fpcunit
//, testutils
, testregistry
, OPP.Text
, OPP.Text.SourceFile
;

type

{ TTestObjectPascalParserTextSourceFile }

TTestObjectPascalParserTextSourceFile= class(TTestCase)
private
FSourceFile: TTextSourceFile;

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

implementation

const
cSourceFileContentUTF8 = 'program Test🌟';

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

procedure TTestObjectPascalParserTextSourceFile.TestObjectPascalParserTextSourceFileCreate;
var
stringStream: TstringStream;
tmpFilename: String;
begin
stringStream:= TStringStream.Create(cSourceFileContentUTF8);
try
tmpFilename:= GetTempFileName;
stringStream.SaveToFile(tmpFilename);
try

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

finally
DeleteFile(tmpFilename);
end;
finally
stringStream.Free;
end;
end;

procedure TTestObjectPascalParserTextSourceFile.TestObjectPascalParserTextSourceFileCreateError;
begin
FSourceFile:= nil;
AssertException(
'Text Source File Doesn Not Exists Exception',
ETextSourceFileDoesNotExist,
@TestSourceFileCreateException,
Format(
rsETextSourceFileDoesNotExist,
[ '' ]
)
);
if Assigned(FSourceFile) then FSourceFile.Free;
end;



initialization

RegisterTest(TTestObjectPascalParserTextSourceFile);
end.

0 comments on commit 2abd5db

Please sign in to comment.