Refactored tests in to facilitate loading example data from file

This commit is contained in:
Stefan Müller 2023-12-05 12:19:41 +01:00 committed by Stefan Müller
parent 5c0aa1e584
commit e04577725b
3 changed files with 75 additions and 77 deletions

View File

@ -26,32 +26,75 @@ uses
type
{ TBaseTestCase }
{ TSolverTestCase }
TBaseTestCase = class(TTestCase)
TSolverTestCase = class(TTestCase)
protected
FEngine: TSolverEngine;
FSolver: ISolver;
procedure Setup; override;
procedure TearDown; override;
function CreateSolver: ISolver; virtual; abstract;
end;
{ TEngineBaseTest }
TEngineBaseTest = class(TSolverTestCase)
protected
FEngine: TSolverEngine;
procedure Setup; override;
procedure TearDown; override;
function GetDataPath: string; virtual;
end;
{ TExampleEngineBaseTest }
TExampleEngineBaseTest = class(TEngineBaseTest)
protected
function GetDataPath: string; override;
end;
implementation
{ TBaseTestCase }
{ TSolverTestCase }
procedure TBaseTestCase.Setup;
procedure TSolverTestCase.Setup;
begin
inherited Setup;
FEngine := TSolverEngine.Create(ConcatPaths(['..', '..', 'bin', 'data']));
FSolver := CreateSolver;
end;
procedure TBaseTestCase.TearDown;
procedure TSolverTestCase.TearDown;
begin
FSolver.Free;
inherited TearDown;
end;
{ TEngineBaseTest }
procedure TEngineBaseTest.Setup;
begin
inherited Setup;
FEngine := TSolverEngine.Create(GetDataPath);
FEngine.ProcessData(FSolver);
end;
procedure TEngineBaseTest.TearDown;
begin
FEngine.Free;
inherited TearDown;
end;
function TEngineBaseTest.GetDataPath: string;
begin
Result := ConcatPaths(['..', '..', 'bin', 'data']);
end;
{ TExampleEngineBaseTest }
function TExampleEngineBaseTest.GetDataPath: string;
begin
Result := 'example_data';
end;
end.

View File

@ -26,18 +26,11 @@ uses
type
{ TGearRatiosBaseTestCase }
TGearRatiosBaseTestCase = class(TBaseTestCase)
protected
procedure Setup; override;
end;
{ TGearRatiosFullDataTestCase }
TGearRatiosFullDataTestCase = class(TGearRatiosBaseTestCase)
TGearRatiosFullDataTestCase = class(TEngineBaseTest)
protected
procedure Setup; override;
function CreateSolver: ISolver; override;
published
procedure TestPart1;
procedure TestPart2;
@ -45,9 +38,9 @@ type
{ TGearRatiosExampleTestCase }
TGearRatiosExampleTestCase = class(TGearRatiosBaseTestCase)
TGearRatiosExampleTestCase = class(TExampleEngineBaseTest)
protected
procedure Setup; override;
function CreateSolver: ISolver; override;
published
procedure TestPart1;
procedure TestPart2;
@ -55,27 +48,20 @@ type
{ TGearRatiosTestCase }
TGearRatiosTestCase = class(TGearRatiosBaseTestCase)
TGearRatiosTestCase = class(TSolverTestCase)
protected
function CreateSolver: ISolver; override;
published
procedure TestEndOfLineNumber;
end;
implementation
{ TGearRatiosBaseTestCase }
procedure TGearRatiosBaseTestCase.Setup;
begin
inherited Setup;
FSolver := TGearRatios.Create;
end;
{ TGearRatiosFullDataTestCase }
procedure TGearRatiosFullDataTestCase.Setup;
function TGearRatiosFullDataTestCase.CreateSolver: ISolver;
begin
inherited Setup;
FEngine.ProcessData(FSolver);
Result := TGearRatios.Create;
end;
procedure TGearRatiosFullDataTestCase.TestPart1;
@ -90,21 +76,9 @@ end;
{ TGearRatiosExampleTestCase }
procedure TGearRatiosExampleTestCase.Setup;
function TGearRatiosExampleTestCase.CreateSolver: ISolver;
begin
inherited Setup;
FSolver.Init;
FSolver.ProcessDataLine('467..114..');
FSolver.ProcessDataLine('...*......');
FSolver.ProcessDataLine('..35..633.');
FSolver.ProcessDataLine('......#...');
FSolver.ProcessDataLine('617*......');
FSolver.ProcessDataLine('.....+.58.');
FSolver.ProcessDataLine('..592.....');
FSolver.ProcessDataLine('......755.');
FSolver.ProcessDataLine('...$.*....');
FSolver.ProcessDataLine('.664.598..');
FSolver.Finish;
Result := TGearRatios.Create;
end;
procedure TGearRatiosExampleTestCase.TestPart1;
@ -119,12 +93,17 @@ end;
{ TGearRatiosTestCase }
function TGearRatiosTestCase.CreateSolver: ISolver;
begin
Result := TGearRatios.Create;
end;
procedure TGearRatiosTestCase.TestEndOfLineNumber;
begin
FSolver.Init;
FSolver.ProcessDataLine('...$541');
FSolver.Finish;
AssertEquals('Result of part 1 calculation incorrect.', 541, FSolver.GetResultPart1);
AssertEquals(541, FSolver.GetResultPart1);
end;
initialization

View File

@ -26,18 +26,11 @@ uses
type
{ TScratchcardsBaseTestCase }
TScratchcardsBaseTestCase = class(TBaseTestCase)
protected
procedure Setup; override;
end;
{ TScratchcardsFullDataTestCase }
TScratchcardsFullDataTestCase = class(TScratchcardsBaseTestCase)
TScratchcardsFullDataTestCase = class(TEngineBaseTest)
protected
procedure Setup; override;
function CreateSolver: ISolver; override;
published
procedure TestPart1;
procedure TestPart2;
@ -45,9 +38,9 @@ type
{ TScratchcardsExampleTestCase }
TScratchcardsExampleTestCase = class(TScratchcardsBaseTestCase)
TScratchcardsExampleTestCase = class(TExampleEngineBaseTest)
protected
procedure Setup; override;
function CreateSolver: ISolver; override;
published
procedure TestPart1;
procedure TestPart2;
@ -55,20 +48,11 @@ type
implementation
{ TScratchcardsBaseTestCase }
procedure TScratchcardsBaseTestCase.Setup;
begin
inherited Setup;
FSolver := TScratchcards.Create;
end;
{ TScratchcardsFullDataTestCase }
procedure TScratchcardsFullDataTestCase.Setup;
function TScratchcardsFullDataTestCase.CreateSolver: ISolver;
begin
inherited Setup;
FEngine.ProcessData(FSolver);
Result := TScratchcards.Create;
end;
procedure TScratchcardsFullDataTestCase.TestPart1;
@ -83,17 +67,9 @@ end;
{ TScratchcardsExampleTestCase }
procedure TScratchcardsExampleTestCase.Setup;
function TScratchcardsExampleTestCase.CreateSolver: ISolver;
begin
inherited Setup;
FSolver.Init;
FSolver.ProcessDataLine('Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53');
FSolver.ProcessDataLine('Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19');
FSolver.ProcessDataLine('Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1');
FSolver.ProcessDataLine('Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83');
FSolver.ProcessDataLine('Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36');
FSolver.ProcessDataLine('Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11');
FSolver.Finish;
Result := TScratchcards.Create;
end;
procedure TScratchcardsExampleTestCase.TestPart1;