Source

asconfig / Test / TestASConfig.pas

unit TestASConfig;
{

  Delphi DUnit Test Case
  ----------------------
  This unit contains a skeleton test case class generated by the Test Case Wizard.
  Modify the generated code to correctly setup and call the methods from the unit
  being tested.

}

interface

uses
  Windows, TestFramework, Classes, SysUtils, ASConfig;

type
  // Test methods for class TASCustomConfig

  TestTASCustomConfig = class(TTestCase)
  strict private
    FASCustomConfig: TASCustomConfig;

  protected
    property Config: TASCustomConfig read FASCustomConfig;

  public
    procedure SetUp; override;
    procedure TearDown; override;

  published
    procedure TestSaveToIniFile;
    procedure TestLoadFromIniFile;
  end;

  TTestObject = class
  private
    Fstring: string;
    Fint: Integer;
  public
    property Astring: string read Fstring write Fstring;
    property Aint: Integer read Fint write Fint;
  end;

implementation

uses
  Forms, IniFiles;

procedure TestTASCustomConfig.SetUp;
begin
  FASCustomConfig := TASConfigIniFile.Create(Application);
end;

procedure TestTASCustomConfig.TearDown;
begin
  FASCustomConfig.Free;
  FASCustomConfig := nil;
end;

procedure TestTASCustomConfig.TestLoadFromIniFile;
var
  Obj: TTestObject;
  Ini: TIniFile;
  FileName: string;
begin
  FileName := './as-test.ini';
  if FileExists(FileName) then
    CheckTrue(DeleteFile(FileName), 'Delete existing ini file (' + FileName + ')');

  Ini := TIniFile.Create(FileName);
  try
    Ini.WriteString('Default', 'Astring', 'My string');
    Ini.WriteInteger('Default', 'Aint', 44);
  finally
    Ini.Free;
  end;

  Obj := TTestObject.Create;
  try
    (Config as TASConfigIniFile).FileName := FileName;
    Config.Load(Obj);

    CheckEqualsString('My string', Obj.Astring, '[Default]');
    CheckEquals(44, Obj.Aint, '[Default]');
  finally
    Obj.Free;
  end;
end;

procedure TestTASCustomConfig.TestSaveToIniFile;
var
  Obj: TTestObject;
  Ini: TIniFile;
  FileName: string;
begin
  FileName := './as-test.ini';
  if FileExists(FileName) then
    CheckTrue(DeleteFile(FileName), 'Delete existing ini file (' + FileName + ')');

  Obj := TTestObject.Create;
  try
    Obj.Astring := 'A string';
    Obj.Aint := 55;

    (Config as TASConfigIniFile).FileName := FileName;
    Config.Save(Obj);

    Ini := TIniFile.Create(FileName);
    try
      CheckEqualsString('A string', Ini.ReadString('Default', 'Astring', ''), '[Default]');
      CheckEquals(55, Ini.ReadInteger('Default', 'Aint', 0), '[Default]');
    finally
      Ini.Free;
    end;
  finally
    Obj.Free;
  end;
end;

initialization
  // Register any test cases with the test runner
  RegisterTest(TestTASCustomConfig.Suite);
end.