Calculator

Пример тестирования класса Calculator

Пример использования в модуле Calculator теста

В примере показана прозрачная интеграция кода тестирования помощью {$REGION '<TESTME>'}.

Код тестирования модуля Calculator

{ Example TestMe Calculate}
unit Calculator;
interface
   uses  Classes,SysUtils,db
{$REGION '<TESTME>'}      {$IFDEF TESTME}
     ,TestMeType,TestMeLevelType
     ,Dialogs
     {$ENDIF}  {$ENDREGION}
     ;
      type
       TCalc=class
{$REGION '<TESTME>'}     {$IFDEF TESTME}
        private
         class Procedure _RegisterMethod; // Register TestMethods
         class  Function _TestMethods( IMethod:IRunMethodParam    // Test Methods
                            ):TTestResultParam;// Result Test
     {$ENDIF}  {$ENDREGION}
        private
          F_Value:integer;
        public
         constructor Create;
          function OpPlus(dValue:integer):integer; virtual;
          function OpMinus(dValue:integer):integer; virtual;
          function OpMul(dValue:integer):integer; virtual;
          function OpDiv(dValue:integer):integer; virtual;
         Property  Value:integer read F_Value  write F_Value ;
       end;
implementation
constructor TCalc.Create;
begin
 F_Value:=0;
end;
function TCalc.OpDiv(dValue: integer): integer;
begin
 F_Value:=F_Value div dValue;
 Result:=F_Value;
end;
function TCalc.OpMinus(dValue: integer): integer;
begin
 F_Value:=F_Value - dValue;
 Result:=F_Value;
end;
function TCalc.OpMul(dValue: integer): integer;
begin
 F_Value:=F_Value * dValue;
 Result:=F_Value;
end;
function TCalc.OpPlus(dValue: integer): integer;
begin
 F_Value:=F_Value + dValue;
 Result:=F_Value;
end;
{$REGION '<TESTME>'} {$IFDEF TESTME}
 var
  L_pIndexUnit
   ,L_pIndexTCalculate:TPIndexValue;
 
   L_oCalc:TCalc;
 
    Type
     TTestMethod=(
       tmCreate
      ,tmFree
      ,tmPlus
      ,tmMinus
      ,tmMul
      ,tmDiv
     );
 
class procedure TCalc._RegisterMethod;
begin
 if GetTestMe.bIsTestCode then exit;
  with GetTestMe.ITestCode do begin
   // Set position
  if not  ISetDefaultIndUnitClass(L_pIndexUnit,L_pIndexTCalculate)  then exit;
 
 // Create class for test
   RegMethod(tmCreate,'tmCreate','Test Create component'
   ,pAddOneEmptyParam    // Auto add test parameter
    ,1);                 // First index test position
 
 // Free class
   RegMethod(tmFree  ,'tmFree'  ,'Test Free component'
   ,pAddOneEmptyParam    // Auto add test parameter
   ,MAX_POS_TESTMETHOD); // Last index test position
 
 // Test methods
   RegMethod(tmPlus  ,'tmPlus'  ,'Test Plus operation');
    RegParam([2,2],[4],'2+2=4');
    RegParam([5,5],[10],'5+5=10');
    RegParam([4,4],[4],'4+4=4 trError it''s Ok',trError);
 
   RegMethod(tmMinus  ,'tmMinus'  ,'Test Minus operation');
    RegParam([4,1],[3],'4-1=3');
    RegParam([15,5],[10],'15-5=10');
    RegParam([4,4],[4],'4-4=4  trError it''s Ok',trError);
 
   RegMethod(tmMul  ,'tmMul'  ,'Test Multiplication operation');
    RegParam([5,5],[25],'5*5=25');
    RegParam([2,2],[4],'2*2=4');
    RegParam([4,4],[4],'4*4=4 trError it''s Ok',trError);
 
   RegMethod(tmDiv  ,'tmDiv'  ,'Test Div operation');// Test methods
    RegParam([15,5],[3],'15 div 5=3'); // True result
    RegParam([2,2],[1],'2 div 2=1');   // True result
    RegParam([4,4],[4],'4 div 4<>4 True=Error'); // Error
    RegParam([4,4],[4],'4 div 4=4 trError it''s Ok',trError); // Error = Ok
    RegParam([4,0],[4],'4 div 0 (!) Exception'); // Exception
    RegParam([4,0],[4],'4 div 0 (!) trException it''s Ok',trException); // Exception = Ok
  end; //withend
end;
 
class function TCalc._TestMethods(
  IMethod: IRunMethodParam): TTestResultParam;
begin
  Result:=trNone; // No WARN
 case TTestMethod(IMethod.IGetNumParamUser)  of
   tmCreate :begin
       if L_oCalc=nil then begin
        L_oCalc:=TCalc.Create;
        result:=trOk;
       end else
        result:=trError;
     end;
   tmFree  :begin
       if L_oCalc<>nil then begin
        FreeAndNil(L_oCalc);
        result:=trOk;
       end else
        result:=trError;
     end;
   tmPlus:
     begin
        L_oCalc.Value:=IMethod.IGetArrParam(0).AsInteger;
           if L_oCalc.OpPlus(IMethod.IGetArrParam(1).AsInteger)= IMethod.IGetArrResultForTestMethod(0).AsInteger then
            result:=trOk
           else
            result:=trError;
     end;
   tmMinus:
     begin
        L_oCalc.Value:=IMethod.IGetArrParam(0).AsInteger;
           if L_oCalc.OpMinus(IMethod.IGetArrParam(1).AsInteger)= IMethod.IGetArrResultForTestMethod(0).AsInteger then
            result:=trOk
           else
            result:=trError;
     end;
   tmMul:
     begin
        L_oCalc.Value:=IMethod.IGetArrParam(0).AsInteger;
           if L_oCalc.OpMul(IMethod.IGetArrParam(1).AsInteger)= IMethod.IGetArrResultForTestMethod(0).AsInteger then
            result:=trOk
           else
            result:=trError;
     end;
   tmDiv:
     begin
        L_oCalc.Value:=IMethod.IGetArrParam(0).AsInteger;
           if L_oCalc.OpDiv(IMethod.IGetArrParam(1).AsInteger) = IMethod.IGetArrResultForTestMethod(0).AsInteger then
            result:=trOk
           else
            result:=trError;
     end;
 end;
end;
 
initialization
if GetTestMe.bIsTestCode then begin
  with GetTestMe.ITestCode do begin
    L_pIndexUnit:=IRegisterUnit(ltComponents,'Calculator','unit Calculator components');
    L_pIndexTCalculate   := IRegisterClass(TCalc._RegisterMethod,TCalc._TestMethods,'TCalc','Class Calculater');
  end;
end else begin
  ShowMessage('Not find  TestMeDllxx.dll');
end;
{$ENDIF} {$ENDREGION}
end.