unit Test;

interface

uses
  Classes, SysUtils, VCard, VCardProcessor, TestCase, Table;

type

  TTestCaseActionKind = (akNone, akRemoveExactDuplicates);

  { TTestCaseLoadSave }

  TTestCaseLoadSave = class(TTestCase)
    Input: string;
    Output: string;
    Action: TTestCaseActionKind;
    procedure Run; override;
  end;

  { TTestCaseCheckProperty }

  TTestCaseCheckProperty = class(TTestCase)
    Input: string;
    ContactIndex: Integer;
    Index: TContactFieldIndex;
    Value: string;
    procedure Run; override;
  end;

  { TTestCaseVCardProcessor }

  TTestCaseVCardProcessor = class(TTestCase)
    Input: string;
    Output: string;
    Processor: TVCardProcessor;
    procedure Run; override;
    constructor Create; override;
    destructor Destroy; override;
  end;

  { TTestCaseVCardExportImport }

  TTestCaseVCardExportImport = class(TTestCase)
    Input: string;
    Output: string;
    Format: TTableFormat;
    HumanReadableHeader: Boolean;
    procedure Run; override;
  end;


implementation

uses
  VCardFile;

resourcestring
  SExpected = 'Expected:';
  SOutput = 'Output:';
  SExport = 'Export:';

{ TTestCaseVCardExportImport }

procedure TTestCaseVCardExportImport.Run;
var
  Lines: TStringList;
  ExportedLines: TStringList;
begin
  Lines := TStringList.Create;
  ExportedLines := TStringList.Create;
  try
    with TVCardFile.Create(nil) do
    try
      Lines.Text := Input;
      VCard.LoadFromStrings(Lines);

      VCard.ExportToStrings(ExportedLines, Format, HumanReadableHeader);
      VCard.Contacts.Clear;
      VCard.ImportFromStrings(ExportedLines, Format, HumanReadableHeader);

      Lines.Text := '';
      VCard.SaveToStrings(Lines);
      Evaluate(Lines.Text = Output);
      Log := SExpected + LineEnding +
        '"' + Output + '"' + LineEnding + LineEnding +
        SExport + LineEnding +
        '"' + ExportedLines.Text + '"' + LineEnding + LineEnding +
        SOutput + LineEnding +
        '"' + Lines.Text + '"';
    finally
      Free;
    end;
  finally
    FreeAndNil(Lines);
    FreeAndNil(ExportedLines);
  end;
end;

{ TTestCaseVCardProcessor }

procedure TTestCaseVCardProcessor.Run;
var
  Lines: TStringList;
begin
  Lines := TStringList.Create;
  try
    with TVCardFile.Create(nil) do
    try
      Lines.Text := Input;
      VCard.LoadFromStrings(Lines);

      Processor.Process(VCard);

      Lines.Text := '';
      VCard.SaveToStrings(Lines);
      Evaluate(Lines.Text = Output);
      Log := SExpected + LineEnding +
        '"' + Output + '"' + LineEnding + LineEnding +
        SOutput + LineEnding +
        '"' + Lines.Text + '"';
    finally
      Free;
    end;
  finally
    Lines.Free;
  end;
end;

constructor TTestCaseVCardProcessor.Create;
begin
  inherited;
  Processor := TVCardProcessor.Create(nil);
end;

destructor TTestCaseVCardProcessor.Destroy;
begin
  FreeAndNil(Processor);
  inherited;
end;

{ TTestCaseCheckProperty }

procedure TTestCaseCheckProperty.Run;
var
  Lines: TStringList;
  PropertyValue: string;
begin
  Lines := TStringList.Create;
  try
    with TVCardFile.Create(nil) do
    try
      Lines.Text := Input;
      VCard.LoadFromStrings(Lines);
      if ContactIndex < VCard.Contacts.Count then begin
        PropertyValue := VCard.Contacts[ContactIndex].Fields[Index];
        Evaluate(PropertyValue = Value);
      end else Fail;
      Log := SExpected + LineEnding +
        '"' + Value + '"' + LineEnding + LineEnding +
        SOutput + LineEnding +
        '"' + PropertyValue + '"';
    finally
      Free;
    end;
  finally
    Lines.Free;
  end;
end;

{ TTestCaseLoadSave }

procedure TTestCaseLoadSave.Run;
var
  Lines: TStringList;
begin
  Lines := TStringList.Create;
  try
    with TVCardFile.Create(nil) do
    try
      Lines.Text := Input;
      VCard.LoadFromStrings(Lines);

      if Action = akRemoveExactDuplicates then
        VCard.Contacts.RemoveExactDuplicates;

      Lines.Text := '';
      VCard.SaveToStrings(Lines);
      Evaluate(Lines.Text = Output);
      Log := SExpected + LineEnding +
        '"' + Output + '"' + LineEnding + LineEnding +
        SOutput + LineEnding +
        '"' + Lines.Text + '"';
    finally
      Free;
    end;
  finally
    Lines.Free;
  end;
end;

end.

