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 type
{ TBaseTestCase } { TSolverTestCase }
TBaseTestCase = class(TTestCase) TSolverTestCase = class(TTestCase)
protected protected
FEngine: TSolverEngine;
FSolver: ISolver; FSolver: ISolver;
procedure Setup; override; procedure Setup; override;
procedure TearDown; 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; end;
implementation implementation
{ TBaseTestCase } { TSolverTestCase }
procedure TBaseTestCase.Setup; procedure TSolverTestCase.Setup;
begin begin
inherited Setup; inherited Setup;
FEngine := TSolverEngine.Create(ConcatPaths(['..', '..', 'bin', 'data'])); FSolver := CreateSolver;
end; end;
procedure TBaseTestCase.TearDown; procedure TSolverTestCase.TearDown;
begin begin
FSolver.Free; 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; FEngine.Free;
inherited TearDown; inherited TearDown;
end; end;
function TEngineBaseTest.GetDataPath: string;
begin
Result := ConcatPaths(['..', '..', 'bin', 'data']);
end;
{ TExampleEngineBaseTest }
function TExampleEngineBaseTest.GetDataPath: string;
begin
Result := 'example_data';
end;
end. end.

View File

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

View File

@ -26,18 +26,11 @@ uses
type type
{ TScratchcardsBaseTestCase }
TScratchcardsBaseTestCase = class(TBaseTestCase)
protected
procedure Setup; override;
end;
{ TScratchcardsFullDataTestCase } { TScratchcardsFullDataTestCase }
TScratchcardsFullDataTestCase = class(TScratchcardsBaseTestCase) TScratchcardsFullDataTestCase = class(TEngineBaseTest)
protected protected
procedure Setup; override; function CreateSolver: ISolver; override;
published published
procedure TestPart1; procedure TestPart1;
procedure TestPart2; procedure TestPart2;
@ -45,9 +38,9 @@ type
{ TScratchcardsExampleTestCase } { TScratchcardsExampleTestCase }
TScratchcardsExampleTestCase = class(TScratchcardsBaseTestCase) TScratchcardsExampleTestCase = class(TExampleEngineBaseTest)
protected protected
procedure Setup; override; function CreateSolver: ISolver; override;
published published
procedure TestPart1; procedure TestPart1;
procedure TestPart2; procedure TestPart2;
@ -55,20 +48,11 @@ type
implementation implementation
{ TScratchcardsBaseTestCase }
procedure TScratchcardsBaseTestCase.Setup;
begin
inherited Setup;
FSolver := TScratchcards.Create;
end;
{ TScratchcardsFullDataTestCase } { TScratchcardsFullDataTestCase }
procedure TScratchcardsFullDataTestCase.Setup; function TScratchcardsFullDataTestCase.CreateSolver: ISolver;
begin begin
inherited Setup; Result := TScratchcards.Create;
FEngine.ProcessData(FSolver);
end; end;
procedure TScratchcardsFullDataTestCase.TestPart1; procedure TScratchcardsFullDataTestCase.TestPart1;
@ -83,17 +67,9 @@ end;
{ TScratchcardsExampleTestCase } { TScratchcardsExampleTestCase }
procedure TScratchcardsExampleTestCase.Setup; function TScratchcardsExampleTestCase.CreateSolver: ISolver;
begin begin
inherited Setup; Result := TScratchcards.Create;
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;
end; end;
procedure TScratchcardsExampleTestCase.TestPart1; procedure TScratchcardsExampleTestCase.TestPart1;