вторник, 17 марта 2015 г.

Testing of calculator №4. Changing of application’s architecture

Original in Russian: http://programmingmindstream.blogspot.ru/2014/02/4.html

Table of contents

We follow up the posts.

All source code is available here

We’ll assume that we have “ensured the minimal test coverage” of all precedents of the application.

Having assumed this, we can set about CHANGING the architecture of the application.

We introduce a “business class” TCalculator implementing the business logic of the application.

The “business class”:

unit Calculator;
 
interface
 
type
 TCalculator = class
  public
   class function Add(const A, B: string): string;
   class function Sub(const A, B: string): string;
   class function Mul(const A, B: string): string;
   class function Divide(const A, B: string): string;
 end;//TCalculator
 
implementation
 
uses
  SysUtils
  ;
 
class function TCalculator.Add(const A, B: string): string;
var
  x1, x2, x3 : single;
begin
  x1 := StrToFloat(A);
  x2 := StrToFloat(B);
  x3 := x1 + x2;
  Result := FloatToStr(x3);
end;
 
class function TCalculator.Sub(const A, B: string): string;
var
  x1, x2, x3 : single;
begin
  x1 := StrToFloat(A);
  x2 := StrToFloat(B);
  x3 := x1 - x2;
  Result := FloatToStr(x3);
end;
 
class function TCalculator.Mul(const A, B: string): string;
var
  x1, x2, x3 : single;
begin
  x1 := StrToFloat(A);
  x2 := StrToFloat(B);
  x3 := x1 * x2;
  Result := FloatToStr(x3);
end;
 
class function TCalculator.Divide(const A, B: string): string;
var
  x1, x2, x3 : single;
begin
  x1 := StrToFloat(A);
  x2 := StrToFloat(B);
  x3 := x1 / x2;
  Result := FloatToStr(x3);
end;
 
end.

Now the project’s main form code becomes as follows:

unit MainForm;
 
interface
 
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
 
type
  TfmMain = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  fmMain: TfmMain;
 
implementation
 
uses
  Calculator
  ;
 
{$R *.dfm}
 
procedure TfmMain.Button1Click(Sender: TObject);
begin
 Edit3.Text := TCalculator.Add(Edit1.Text, Edit2.Text);
end;
 
procedure TfmMain.Button2Click(Sender: TObject);
begin
 Edit3.Text := TCalculator.Sub(Edit1.Text, Edit2.Text);
end;
 
procedure TfmMain.Button3Click(Sender: TObject);
begin
 Edit3.Text := TCalculator.Mul(Edit1.Text, Edit2.Text);
end;
 
procedure TfmMain.Button4Click(Sender: TObject);
begin
 Edit3.Text := TCalculator.Divide(Edit1.Text, Edit2.Text);
end;
 
end.

We run our tests and the tests pass.

So what is the result? We have changed the architecture of the application PAIN-FREE and tested if “something” is wrong.

Is the idea clear?

FIRST we’ve ensured the “minimal test coverage” of ALL precedents of the application and ONLY AFTER we have changed the architecture.

We’ve defined “business-logic” class and checked if “SOMETHING is wrong”.

We need MINIMAL coverage because there are some questions, for example, about random data - https://bitbucket.org/ingword/lulinproject/src/573b39b33e09de0d68964f841d69d94df902ab9e/DummyCalculator/Chapter4/Tests/GUI/RandomPlusTest.pas?at=Release

Later on, we’ll discuss “whys” and “whats”.

Meanwhile, have a look at what has already been done. I hope, you will find it useful.

Repository

Комментариев нет:

Отправить комментарий