Skip to content

Commit

Permalink
Added unit tests (WIP)
Browse files Browse the repository at this point in the history
Implemented methods TJOSEBytes.Contains
  • Loading branch information
paolo-rossi committed Jan 16, 2020
1 parent 4baf55f commit a441e74
Show file tree
Hide file tree
Showing 12 changed files with 1,251 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Source/Common/JOSE.Types.Bytes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
interface

uses
System.SysUtils,
System.Classes;
System.SysUtils, System.Classes;

type
TJOSEBytes = record
Expand Down Expand Up @@ -127,18 +126,32 @@ procedure TJOSEBytes.Clear;
end;

function TJOSEBytes.Contains(const AByte: Byte): Boolean;
var
LIndex: Integer;
begin
Result := False;
for LIndex := 0 to Length(FPayload) - 1 do
if FPayload[LIndex] = AByte then
Exit(True);
end;

function TJOSEBytes.Contains(const ABytes: TBytes): Boolean;
var
LIndex: Integer;
begin
Result := False;
if (Length(ABytes) > Length(FPayload)) or (Length(ABytes) = 0) then
Exit;

for LIndex := 0 to Length(FPayload) - 1 do
if FPayload[LIndex] = ABytes[0] then
if CompareMem(@FPayload[LIndex], @ABytes[0], Length(ABytes)) then
Exit(True);
end;

function TJOSEBytes.Contains(const ABytes: TJOSEBytes): Boolean;
begin
Result := False;
Result := Contains(ABytes.AsBytes);
end;

class function TJOSEBytes.Empty: TJOSEBytes;
Expand Down
89 changes: 89 additions & 0 deletions Tests/JOSE.Tests.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{******************************************************************************}
{ }
{ Delphi JOSE Library }
{ Copyright (c) 2015-2019 Paolo Rossi }
{ https://github.com/paolo-rossi/delphi-jose-jwt }
{ }
{******************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{******************************************************************************}
program JOSE.Tests;

{$IFNDEF TESTINSIGHT}
{$APPTYPE CONSOLE}
{$ENDIF}

{$STRONGLINKTYPES ON}
uses
System.SysUtils,
{$IFDEF TESTINSIGHT}
TestInsight.DUnitX,
{$ENDIF }
DUnitX.Loggers.Console,
DUnitX.Loggers.Xml.NUnit,
DUnitX.TestFramework,
JOSE.Tests.Builder in 'Source\JOSE.Tests.Builder.pas',
JOSE.Tests.Consumer in 'Source\JOSE.Tests.Consumer.pas',
JOSE.Tests.JWA in 'Source\JOSE.Tests.JWA.pas',
JOSE.Tests.JWK in 'Source\JOSE.Tests.JWK.pas',
JOSE.Tests.JWS in 'Source\JOSE.Tests.JWS.pas',
JOSE.Tests.JWT in 'Source\JOSE.Tests.JWT.pas',
JOSE.Tests.Utils in 'Source\JOSE.Tests.Utils.pas',
JOSE.Tests.Common in 'Source\JOSE.Tests.Common.pas';

var
LRunner : ITestRunner;
LResults : IRunResults;
LLogger : ITestLogger;
LNUnitLogger : ITestLogger;
begin
{$IFDEF TESTINSIGHT}
TestInsight.DUnitX.RunRegisteredTests;
Exit;
{$ENDIF}
try
//Check command line options, will exit if invalid
TDUnitX.CheckCommandLine;
//Create the test runner
LRunner := TDUnitX.CreateRunner;
//Tell the runner to use RTTI to find Fixtures
LRunner.UseRTTI := True;
//tell the runner how we will log things
//Log to the console window
LLogger := TDUnitXConsoleLogger.Create(true);
LRunner.AddLogger(LLogger);
//Generate an NUnit compatible XML File
LNUnitLogger := TDUnitXXMLNUnitFileLogger.Create(TDUnitX.Options.XMLOutputFile);
LRunner.AddLogger(LNUnitLogger);
LRunner.FailsOnNoAsserts := False; //When true, Assertions must be made during tests;

//Run tests
LResults := LRunner.Execute;
if not LResults.AllPassed then
System.ExitCode := EXIT_ERRORS;

{$IFNDEF CI}
//We don't want this happening when running under CI.
if TDUnitX.Options.ExitBehavior = TDUnitXExitBehavior.Pause then
begin
System.Write('Done.. press <Enter> key to quit.');
System.Readln;
end;
{$ENDIF}
except
on E: Exception do
System.Writeln(E.ClassName, ': ', E.Message);
end;
end.
Loading

0 comments on commit a441e74

Please sign in to comment.