Changeset 6


Ignore:
Timestamp:
Aug 19, 2022, 10:53:15 PM (2 years ago)
Author:
chronos
Message:
  • Modified: Improved test cases definition and evaluation.
Location:
trunk
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        44SVNZero.lps
        55heaptrclog.trc
         6Languages/*.mo
  • trunk/Languages/svnzero.cs.po

    r4 r6  
    2020msgid "Zeroed files count: %d"
    2121msgstr "Počet vynulovaných souborů: %d"
     22
     23#: utest.sfailed
     24msgid "Failed"
     25msgstr ""
     26
     27#: utest.snone
     28msgid "None"
     29msgstr ""
     30
     31#: utest.spassed
     32msgid "Passed"
     33msgstr ""
     34
  • trunk/Languages/svnzero.pot

    r4 r6  
    1111msgstr ""
    1212
     13#: utest.sfailed
     14msgid "Failed"
     15msgstr ""
     16
     17#: utest.snone
     18msgid "None"
     19msgstr ""
     20
     21#: utest.spassed
     22msgid "Passed"
     23msgstr ""
     24
  • trunk/SVNZero.lpi

    r4 r6  
    8787        <IsPartOfProject Value="True"/>
    8888      </Unit>
     89      <Unit>
     90        <Filename Value="UTestCases.pas"/>
     91        <IsPartOfProject Value="True"/>
     92      </Unit>
    8993    </Units>
    9094  </ProjectOptions>
  • trunk/SVNZero.lpr

    r5 r6  
    55  cthreads,
    66  {$ENDIF}
    7   Classes, SysUtils, CustApp, USubversion, UTest, USvnZero
     7  Classes, SysUtils, CustApp, USubversion, UTest, USvnZero, UTestCases
    88  { you can add units after this };
    99
     
    2929begin
    3030  if HasOption('t', 'test') then begin
    31     with TTest.Create do
     31    with TTestCases.Create do
    3232    try
     33      AddNew('Add', TTestCaseAdd);
     34      AddNew('Update', TTestCaseUpdate);
     35      AddNew('Modify', TTestCaseModify);
     36      AddNew('Delete', TTestCaseDelete);
    3337      Run;
    3438    finally
  • trunk/UTest.pas

    r5 r6  
    44
    55uses
    6   Classes, SysUtils, USubversion, FileUtil;
     6  Classes, SysUtils, USubversion, FileUtil, Generics.Collections;
    77
    88type
     9  TTestResult = (trNone, trPassed, trFailed);
    910
    10   { TTest }
     11  { TTestCase }
    1112
    12   TTest = class
     13  TTestCase = class
     14  public
     15    Name: string;
     16    TestResult: TTestResult;
     17    Log: string;
     18    procedure Run; virtual;
     19    procedure Evaluate(Passed: Boolean);
     20    procedure Pass;
     21    procedure Fail;
     22    constructor Create; virtual;
     23  end;
     24
     25  TTestCaseClass = class of TTestCase;
     26
     27  { TTestCases }
     28
     29  TTestCases = class(TObjectList<TTestCase>)
     30    function AddNew(Name: string; TestClass: TTestCaseClass): TTestCase;
     31    procedure Run;
     32  end;
     33
     34  { TTestCaseSvn }
     35
     36  TTestCaseSvn = class(TTestCase)
    1337  private
    1438    Subversion: TExecute;
    1539    SubversionAdmin: TSubversionAdmin;
     40    function CheckError(Text: string): Boolean;
     41  protected
    1642    RepoDir: string;
    1743    WorkDir: string;
    1844    TestDir: string;
    19     TestResult: Boolean;
    2045    procedure Svn(Parameters: array of string);
    2146    procedure SvnAdmin(Parameters: array of string);
    22     function CheckError(Text: string): Boolean;
    23     procedure Eval(TestResult: Boolean);
    24     procedure TestAdd;
    25     procedure TestModify;
    26     procedure TestUpdate;
    27     procedure TestDelete;
    2847  public
    29     procedure Run;
    30     constructor Create;
     48    procedure Run; override;
     49    constructor Create; override;
    3150    destructor Destroy; override;
    3251  end;
     52
     53resourcestring
     54  SNone = 'None';
     55  SPassed = 'Passed';
     56  SFailed = 'Failed';
     57
     58const
     59  ResultText: array[TTestResult] of string = (SNone, SPassed, SFailed);
     60
     61procedure Translate;
     62procedure SaveStringToFile(S, FileName: string);
    3363
    3464
     
    3767uses
    3868  USvnZero;
     69
     70procedure Translate;
     71begin
     72  ResultText[trNone] := SNone;
     73  ResultText[trPassed] := SPassed;
     74  ResultText[trFailed] := SFailed;
     75end;
     76
    3977
    4078procedure SaveStringToFile(S, FileName: string);
     
    5189end;
    5290
     91{ TTestCase }
    5392
    54 { TTest }
     93procedure TTestCase.Run;
     94begin
     95end;
    5596
    56 procedure TTest.Svn(Parameters: array of string);
     97procedure TTestCase.Evaluate(Passed: Boolean);
     98begin
     99  if Passed then TestResult := trPassed
     100    else TestResult := trFailed;
     101end;
     102
     103procedure TTestCase.Pass;
     104begin
     105  TestResult := trPassed;
     106end;
     107
     108procedure TTestCase.Fail;
     109begin
     110  TestResult := trFailed;
     111end;
     112
     113constructor TTestCase.Create;
     114begin
     115end;
     116
     117{ TTestCases }
     118
     119function TTestCases.AddNew(Name: string; TestClass: TTestCaseClass): TTestCase;
     120begin
     121  Result := TestClass.Create;
     122  Result.Name := Name;
     123  Add(Result);
     124end;
     125
     126procedure TTestCases.Run;
     127var
     128  I: Integer;
     129  Passed: Integer;
     130  Failed: Integer;
     131begin
     132  for I := 0 to Count - 1 do
     133  with Items[I] do begin
     134    WriteLn('== ' + Name + ' ======= ');
     135    Run;
     136  end;
     137
     138  Passed := 0;
     139  Failed := 0;
     140  for I := 0 to Count - 1 do
     141  with Items[I] do begin
     142    WriteLn(Name + ': ' + ResultText[TestResult]);
     143    if TestResult = trPassed then Inc(Passed);
     144    if TestResult = trFailed then Inc(Failed);
     145  end;
     146  WriteLn('Total: ' + IntToStr(Count) + ', Passed: ' + IntToStr(Passed) +
     147    ', Failed: ' + IntToStr(Failed));
     148end;
     149
     150{ TTestCaseSvn }
     151
     152procedure TTestCaseSvn.Svn(Parameters: array of string);
    57153begin
    58154  Subversion.ExecuteOutput(Parameters);
    59155  if CheckError(Subversion.ErrorOutput) then
    60     TestResult := False;
     156    TestResult := trFailed;
    61157end;
    62158
    63 procedure TTest.SvnAdmin(Parameters: array of string);
     159procedure TTestCaseSvn.SvnAdmin(Parameters: array of string);
    64160begin
    65161  SubversionAdmin.ExecuteOutput(Parameters);
    66162  if CheckError(SubversionAdmin.ErrorOutput) then
    67     TestResult := False;
     163    TestResult := trFailed;
    68164end;
    69165
    70 function TTest.CheckError(Text: string): Boolean;
     166function TTestCaseSvn.CheckError(Text: string): Boolean;
    71167begin
    72168  Result := Pos('svn: E', Text) > 0;
    73169end;
    74170
    75 procedure TTest.Eval(TestResult: Boolean);
     171procedure TTestCaseSvn.Run;
    76172begin
    77   if TestResult then WriteLn('PASSED')
    78     else WriteLn('FAILED');
     173  ChDir(TestDir);
    79174end;
    80175
    81 procedure TTest.TestAdd;
    82 var
    83   TestFile: string;
    84   LastDir: string;
    85 begin
    86   TestResult := True;
    87   LastDir := GetCurrentDir;
    88   TestFile := 'TestFile.txt';
    89   SvnAdmin(['create', RepoDir]);
    90   Svn(['checkout', 'file:///' + RepoDir, WorkDir]);
    91   ChDir(WorkDir);
    92   SaveStringToFile('Some text', TestFile);
    93   Svn(['add', TestFile]);
    94   Svn(['commit', '-m', '"Commit message"']);
    95   ChDir(LastDir);
    96   DeleteDirectory(WorkDir, False);
    97   DeleteDirectory(RepoDir, False);
    98   Eval(TestResult);
    99 end;
    100 
    101 procedure TTest.TestModify;
    102 var
    103   TestFile: string;
    104   LastDir: string;
    105 begin
    106   TestResult := True;
    107   LastDir := GetCurrentDir;
    108   TestFile := 'TestFile.txt';
    109   SvnAdmin(['create', RepoDir]);
    110   Svn(['checkout', 'file:///' + RepoDir, WorkDir]);
    111   ChDir(WorkDir);
    112   SaveStringToFile('Some text', TestFile);
    113   Svn(['add', TestFile]);
    114   Svn(['commit', '-m', '"Commit message"']);
    115   SaveStringToFile('Different text', TestFile);
    116   Svn(['commit', '-m', '"Commit message 2"']);
    117   ChDir(LastDir);
    118   DeleteDirectory(WorkDir, False);
    119   DeleteDirectory(RepoDir, False);
    120   Eval(TestResult);
    121 end;
    122 
    123 procedure TTest.TestUpdate;
    124 var
    125   TestFile: string;
    126   LastDir: string;
    127 begin
    128   TestResult := True;
    129   LastDir := GetCurrentDir;
    130   TestFile := 'TestFile.txt';
    131   SvnAdmin(['create', RepoDir]);
    132   Svn(['checkout', 'file:///' + RepoDir, WorkDir]);
    133   ChDir(WorkDir);
    134   SaveStringToFile('Some text', TestFile);
    135   Svn(['add', TestFile]);
    136   Svn(['commit', '-m', '"Commit message"']);
    137   SaveStringToFile('Different text', TestFile);
    138   Svn(['commit', '-m', '"Commit message 2"']);
    139   Svn(['update', '-r', '1', TestFile]);
    140   Svn(['update']);
    141   ChDir(LastDir);
    142   DeleteDirectory(WorkDir, False);
    143   DeleteDirectory(RepoDir, False);
    144   Eval(TestResult);
    145 end;
    146 
    147 procedure TTest.TestDelete;
    148 var
    149   TestFile: string;
    150   LastDir: string;
    151 begin
    152   TestResult := True;
    153   LastDir := GetCurrentDir;
    154   TestFile := 'TestFile.txt';
    155   SvnAdmin(['create', RepoDir]);
    156   Svn(['checkout', 'file:///' + RepoDir, WorkDir]);
    157   ChDir(WorkDir);
    158   SaveStringToFile('Some text', TestFile);
    159   Svn(['add', TestFile]);
    160   Svn(['commit', '-m', '"Commit message"']);
    161   Svn(['delete', TestFile]);
    162   Svn(['commit', '-m', '"Commit message 2"']);
    163   ChDir(LastDir);
    164   DeleteDirectory(WorkDir, False);
    165   DeleteDirectory(RepoDir, False);
    166   Eval(TestResult);
    167 end;
    168 
    169 procedure TTest.Run;
    170 begin
    171   ChDir(TestDir);
    172   TestAdd;
    173   TestModify;
    174   TestUpdate;
    175   TestDelete;
    176 end;
    177 
    178 constructor TTest.Create;
     176constructor TTestCaseSvn.Create;
    179177begin
    180178  TestDir := GetTempDir(False) + 'SVNZero';
     
    187185end;
    188186
    189 destructor TTest.Destroy;
     187destructor TTestCaseSvn.Destroy;
    190188begin
    191189  FreeAndNil(SubversionAdmin);
Note: See TracChangeset for help on using the changeset viewer.