Calculator

An example test of the class Calculator

Example of use in the test Calculator module

The example shows transparent integration of testing code using {$REGION'<TESTME>'}.

Code unit test 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 begin
  with GetTestMe.ITestCode do begin
   // Set position
  if not  ISetDefaultIndUnitClass(L_pIndexUnit,L_pIndexTCalculate)  then exit;
 
 // Create class for test
   IRegisterMethod(tmCreate,'tmCreate','Test Create component'
   ,pAddOneEmptyParam    // Auto add test parameter
    ,1);                 // First index test position
 
 // Free class
   IRegisterMethod(tmFree  ,'tmFree'  ,'Test Free component'
   ,pAddOneEmptyParam    // Auto add test parameter
   ,MAX_POS_TESTMETHOD); // Last index test position
 
 // Test methods
   IRegisterMethod(tmPlus  ,'tmPlus'  ,'Test Plus operation');
    IRegisterMethodParams([2,2],[4],'2+2=4');
    IRegisterMethodParams([5,5],[10],'5+5=10');
    IRegisterMethodParams([4,4],[4],'4+4=4 trError it''s Ok',trError);
 
   IRegisterMethod(tmMinus  ,'tmMinus'  ,'Test Minus operation');
    IRegisterMethodParams([4,1],[3],'4-1=3');
    IRegisterMethodParams([15,5],[10],'15-5=10');
    IRegisterMethodParams([4,4],[4],'4-4=4  trError it''s Ok',trError);
 
   IRegisterMethod(tmMul  ,'tmMul'  ,'Test Multiplication operation');
    IRegisterMethodParams([5,5],[25],'5*5=25');
    IRegisterMethodParams([2,2],[4],'2*2=4');
    IRegisterMethodParams([4,4],[4],'4*4=4 trError it''s Ok',trError);
 
   IRegisterMethod(tmDiv  ,'tmDiv'  ,'Test Div operation');// Test methods
    IRegisterMethodParams([15,5],[3],'15 div 5=3'); // True result
    IRegisterMethodParams([2,2],[1],'2 div 2=1');   // True result
    IRegisterMethodParams([4,4],[4],'4 div 4<>4 True=Error'); // Error
    IRegisterMethodParams([4,4],[4],'4 div 4=4 trError it''s Ok',trError); // Error = Ok
    IRegisterMethodParams([4,0],[4],'4 div 0 (!) Exception'); // Exception
    IRegisterMethodParams([4,0],[4],'4 div 0 (!) trException it''s Ok',trException); // Exception = Ok
  end;
 end;
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.