воскресенье, 15 марта 2015 г.

GUI-testing 2. Thinking of testing

Original in Russian: http://18delphi.blogspot.ru/2013/11/blog-post_15.html

GUI-testing. Table of contents

I have already written about checkpoints in first chapter.

I use this approach quite broadly.

I implement test code in designed files.


I’ll venture to give an example:

unit Script.Parser;
 
interface
 
uses
 Classes,
 CoreObject
 ;
 
{$IfNDef NoTesting}
 {$Define TestParser}
{$EndIf  NoTesting}
 
type
 TscriptParser = class(TCoreObject)
  private
   f_Stream : TStream;
   {$IfDef TestParser}
   f_GetCharLog : TStream;
   // - right away we think of testing, we’ll output in this stream 
   //   the result of function GetChar
   f_ReadLnLog : TStream;
   // - right away we think of testing, we’ll output in this stream
   //   the result of function ReadLn
   {$EndIf TestParser}
   f_EOF : Boolean;
   f_CurrentLine : String;
  protected
   procedure Cleanup; override;
   function ReadLn: String;
  protected
   function GetChar(out aChar: AnsiChar): Boolean;
  public
   constructor Create(const aStream : TStream; const aFileName : String); overload;
   constructor Create(const aFileName : String); overload;
   function EOF: Boolean;
   procedure NextToken;
 end;//TscriptParser
 
implementation
 
uses
 System.SysUtils
 ;
 
constructor TscriptParser.Create(const aStream : TStream; const aFileName : String);
begin
 inherited Create;
 f_EOF := false;
 f_Stream := aStream;
 {$IfDef TestParser}
 f_GetCharLog := TFileStream.Create(aFileName + '.GetChar.log', fmCreate);
 f_ReadLnLog := TFileStream.Create(aFileName + '.ReadLn.log', fmCreate);
 {$EndIf TestParser}
end;
 
constructor TscriptParser.Create(const aFileName : String);
var
 l_FileName : String;
begin
 l_FileName := ExtractFilePath(ParamStr(0)) + '\' + aFileName;
 Create(TFileStream.Create(l_FileName, fmOpenRead), l_FileName);
end;
 
procedure TscriptParser.Cleanup;
begin
 FreeAndNil(f_Stream);
 {$IfDef TestParser}
 FreeAndNil(f_GetCharLog);
 FreeAndNil(f_ReadLnLog);
 {$EndIf TestParser}
 inherited;
end;
 
function TscriptParser.GetChar(out aChar: AnsiChar): Boolean;
begin
 if (f_Stream.Read(aChar, SizeOf(aChar)) = SizeOf(aChar)) then
 begin
  Result := true;
  {$IfDef TestParser}
  f_GetCharLog.Write(aChar, SizeOf(aChar));
  {$EndIf TestParser}
 end
 else
  Result := false;
end;
 
function TscriptParser.ReadLn: String;
{$IfDef TestParser}
const
 cEOL : AnsiString = #13#10;
var
 l_Result : AnsiString;
{$EndIf TestParser}
var
 l_Char : AnsiChar;
 l_Line : String;
begin
 {$IfDef TestParser}
 try
 {$EndIf TestParser}
  l_Line := '';
  while GetChar(l_Char) do
  begin
   if (l_Char = #13) then
   begin
    if GetChar(l_Char) then
    begin
     if (l_Char = #10) then
     begin
      Result := l_Line;
      Exit;
     end//l_Char = #10
     else
      Assert(false, 'Something has gone wrong, there is no character 10 after 13');
    end//GetChar(l_Char)
    else
     Assert(false, 'Something has gone wrong, at once the file end after character 13');
   end;//l_Char = #13
   l_Line := l_Line + l_Char;
  end;//while GetChar(l_Char)
  f_EOF := true;
  Result := l_Line;
 {$IfDef TestParser}
 finally
  l_Result := Result;
  f_ReadLnLog.Write(@l_Result[1], Length(l_Result));
  f_ReadLnLog.Write(@cEOL[1], Length(cEOL));
 end;//try..finally
 {$EndIf TestParser}
end;
 
procedure TscriptParser.NextToken;
begin
 while(f_CurrentLine = '') do
 begin
  f_CurrentLine := ReadLn;
  if (f_CurrentLine = '') then
   if f_EOF then
    Exit;
 end;//while(f_NextToken = '')
 f_CurrentLine := '';
end;
 
function TscriptParser.EOF: Boolean;
begin
 Result := f_EOF;
end;
 
end.

Here two “checkpoints” f_ReadLnLog and f_GetCharLog have been introduced to the class TscriptParser.

Usually, I do such “checkpoints” at the model level instead of doing it “manually”. Anyway, I hope the idea is clear.

At the same time, I usually load the “etalons” in CVS/SVN and control the difference. On different test sets. I’ll write about it a bit later.

The example is available here - https://sourceforge.net/p/rumtmarc/code-0/HEAD/tree/trunk/Blogger/GUITests/Chapter0/

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

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