unit TestCases;

interface

uses
  Classes, SysUtils, Test, TestCase;

function InitTestCases: TTestCases;


implementation

uses
  VCard, Table;

const
  VCardVersion = 'VERSION:2.1';
  UnixLineEnding = #10;
  MacLineEnding = #13;

function BeginEnd(Text: string; WithVersion: Boolean = True): string;
begin
  Result := VCardBegin + VCardLineEnding;
  if WithVersion then Result := Result + VCardVersion + VCardLineEnding;
  Result := Result + Text + VCardEnd + VCardLineEnding;
end;

function InitTestCases: TTestCases;
begin
  Result := TTestCases.Create;
  with Result do begin
    with TTestCaseLoadSave(AddNew('Load and save', TTestCaseLoadSave)) do begin
      Input := BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding);
      Output := Input;
    end;
    with TTestCaseLoadSave(AddNew('Long text', TTestCaseLoadSave)) do begin
      Input := BeginEnd(
        'NOTE:This is some long test which is really multi-lined each line is on dif' + VCardLineEnding +
        ' ferent line so it is on multiple lines.' + VCardLineEnding +
        VCardLineEnding);
      Output := Input;
    end;
    with TTestCaseLoadSave(AddNew('Multi-line', TTestCaseLoadSave)) do begin
      Input := BeginEnd(
        'NOTE:First line\nsecond line\nempty line\n\nlast line' + VCardLineEnding);
     Output := Input;
    end;
    with TTestCaseLoadSave(AddNew('Quoted-printable load-save', TTestCaseLoadSave)) do begin
      Input := BeginEnd(
        'FN;ENCODING=QUOTED-PRINTABLE:Jm=C3=A9no=20P=C5=99=C3=ADjmen=C3=AD' + VCardLineEnding);
      Output := Input;
    end;
    with TTestCaseLoadSave(AddNew('Quoted-printable load-save multi-line', TTestCaseLoadSave)) do begin
      Input := BeginEnd(
        'FN;ENCODING=QUOTED-PRINTABLE:Jm=C3=A9no=20P=C5=99=C3=ADjmen=C3=ADJm=C3=A9n=' + VCardLineEnding +
        'o=20P=C5=99=C3=ADjmen=C3=AD' + VCardLineEnding +
        VCardLineEnding);
      Output := Input;
    end;
    with TTestCaseLoadSave(AddNew('Base64 load-save (encoding=base64)', TTestCaseLoadSave)) do begin
      Input := BeginEnd(
        'FN;ENCODING=BASE64:VGVzdCBzdHJpbmc=' + VCardLineEnding);
      Output := Input;
    end;
    with TTestCaseLoadSave(AddNew('Base64 load-save (base64)', TTestCaseLoadSave)) do begin
      Input := BeginEnd(
        'FN;BASE64:VGVzdCBzdHJpbmc=' + VCardLineEnding);
      Output := Input;
    end;
    with TTestCaseLoadSave(AddNew('Base64 load-save (encoding=b)', TTestCaseLoadSave)) do begin
      Input := BeginEnd(
        'FN;ENCODING=B:VGVzdCBzdHJpbmc=' + VCardLineEnding);
      Output := Input;
    end;
    with TTestCaseLoadSave(AddNew('Base64 load-save multi-line', TTestCaseLoadSave)) do begin
      Input := BeginEnd(
        'FN;ENCODING=BASE64:U29tZSB2ZXJ5IGxvbmcgc3RyaW5nIFNvbWUgdmVyeSBsb25nIHN0cmlu' + VCardLineEnding +
        ' ZyBTb21lIHZlcnkgbG9uZyBzdHJpbmcgU29tZSB2ZXJ5IGxvbmcgc3RyaW5n' + VCardLineEnding +
        VCardLineEnding);
      Output := Input;
    end;
    //AddNew('Image format', TTestCaseLoadSave);
    with TTestCaseLoadSave(AddNew('Empty', TTestCaseLoadSave)) do begin
      Input := '';
      Output := '';
    end;
    with TTestCaseLoadSave(AddNew('Begin only', TTestCaseLoadSave)) do begin
      Input := VCardBegin;
      Output := '';
    end;
    with TTestCaseLoadSave(AddNew('Missing end', TTestCaseLoadSave)) do begin
      Input := VCardBegin + VCardLineEnding +
        VCardVersion + VCardLineEnding +
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding;
      Output := '';
    end;
    with TTestCaseLoadSave(AddNew('Missing start', TTestCaseLoadSave)) do begin
      Input := VCardVersion + VCardLineEnding +
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding +
        VCardEnd + VCardLineEnding;
      Output := '';
    end;
    with TTestCaseCheckProperty(AddNew('Property FN', TTestCaseCheckProperty)) do begin
      Index := cfFullName;
      Value := 'Name Surname';
      Input := BeginEnd('FN:' + Value + VCardLineEnding);
    end;
    with TTestCaseCheckProperty(AddNew('Escaped new lines in text', TTestCaseCheckProperty)) do begin
      Index := cfNote;
      Value := 'Line' + #13#10 + 'Line';
      Input := BeginEnd('NOTE:Line\nLine' + VCardLineEnding);
    end;
    with TTestCaseCheckProperty(AddNew('Compound value', TTestCaseCheckProperty)) do begin
      Index := cfFirstName;
      Value := 'FirstName';
      Input := BeginEnd('N:Surname;FirstName;;;' + VCardLineEnding);
    end;
    with TTestCaseCheckProperty(AddNew('Quoted-printable special symbols', TTestCaseCheckProperty)) do begin
      Index := cfFullName;
      Value := 'Jméno Příjmení';
      Input := BeginEnd(
        'FN;ENCODING=QUOTED-PRINTABLE:Jm=C3=A9no=20P=C5=99=C3=ADjmen=C3=AD' + VCardLineEnding);
    end;
    with TTestCaseCheckProperty(AddNew('Base64 special symbols', TTestCaseCheckProperty)) do begin
      Index := cfFullName;
      Value := 'Jméno Příjmení';
      Input := BeginEnd('FN;ENCODING=BASE64:Sm3DqW5vIFDFmcOtam1lbsOt' + VCardLineEnding);
    end;
    with TTestCaseLoadSave(AddNew('Remove exact duplicates', TTestCaseLoadSave)) do begin
      Input := BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd(
        'N:Surname2;Name2' + VCardLineEnding +
        'FN:Name2 Surname2' + VCardLineEnding) +
        BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding +
        'NOTE:Note' + VCardLineEnding) +
        BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding);
      Action := akRemoveExactDuplicates;
      Output := BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd(
        'N:Surname2;Name2' + VCardLineEnding +
        'FN:Name2 Surname2' + VCardLineEnding) +
        BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding +
        'NOTE:Note' + VCardLineEnding);
    end;
    with TTestCaseLoadSave(AddNew('Normalize Unix line endings', TTestCaseLoadSave)) do begin
      Input := VCardBegin + UnixLineEnding +
        VCardVersion + UnixLineEnding +
        'N:Surname;Name' + UnixLineEnding +
        'FN:Name Surname' + UnixLineEnding +
        VCardEnd + UnixLineEnding;
      Output := VCardBegin + VCardLineEnding +
        VCardVersion + VCardLineEnding +
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding +
        VCardEnd + VCardLineEnding;
    end;
    with TTestCaseLoadSave(AddNew('Normalize Mac line endings', TTestCaseLoadSave)) do begin
      Input := VCardBegin + MacLineEnding +
        VCardVersion + MacLineEnding +
        'N:Surname;Name' + MacLineEnding +
        'FN:Name Surname' + MacLineEnding +
        VCardEnd + MacLineEnding;
      Output := VCardBegin + VCardLineEnding +
        VCardVersion + VCardLineEnding +
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding +
        VCardEnd + VCardLineEnding;
    end;

    with TTestCaseVCardProcessor(AddNew('VCardProcessor - Remove exact duplicates', TTestCaseVCardProcessor)) do begin
      Processor.RemoveExactDuplicates := True;
      Input := BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd(
        'N:Surname2;Name2' + VCardLineEnding +
        'FN:Name2 Surname2' + VCardLineEnding) +
        BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding +
        'NOTE:Note' + VCardLineEnding) +
        BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding);
      Output := BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd(
        'N:Surname2;Name2' + VCardLineEnding +
        'FN:Name2 Surname2' + VCardLineEnding) +
        BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding +
        'NOTE:Note' + VCardLineEnding);
    end;
    with TTestCaseVCardProcessor(AddNew('VCardProcessor - Remove phone spaces', TTestCaseVCardProcessor)) do begin
      Processor.RemovePhoneSpaces := True;
      Input := BeginEnd('TEL: 123 456 789 ' + VCardLineEnding) +
        BeginEnd('TEL;CELL:919 191 919' + VCardLineEnding);
      Output := BeginEnd('TEL:123456789' + VCardLineEnding) +
        BeginEnd('TEL;CELL:919191919' + VCardLineEnding);
    end;
    with TTestCaseVCardProcessor(AddNew('VCardProcessor - Remove photos', TTestCaseVCardProcessor)) do begin
      Processor.RemovePhotos := True;
      Input := BeginEnd('N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding +
        'PHOTO:dadsa' + VCardLineEnding) +
        BeginEnd('N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding +
        'PHOTO:dadsa' + VCardLineEnding +
        'PHOTO:adsadadsa' + VCardLineEnding);
      Output := BeginEnd('N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd('N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding);
    end;
    with TTestCaseVCardProcessor(AddNew('VCardProcessor - Order', TTestCaseVCardProcessor)) do begin
      Processor.Order := True;
      Input := BeginEnd('N:Surname2;Name2' + VCardLineEnding +
        'FN:Name2 Surname2' + VCardLineEnding) +
        BeginEnd('N:Surname1;Name1' + VCardLineEnding +
        'FN:Name1 Surname1' + VCardLineEnding) +
        BeginEnd('N:Surname3;Name3' + VCardLineEnding +
        'FN:Name3 Surname3' + VCardLineEnding);
      Output := BeginEnd('FN:Name1 Surname1' + VCardLineEnding +
        'N:Surname1;Name1' + VCardLineEnding) +
        BeginEnd('FN:Name2 Surname2' + VCardLineEnding +
        'N:Surname2;Name2'+ VCardLineEnding) +
        BeginEnd('FN:Name3 Surname3' + VCardLineEnding +
        'N:Surname3;Name3' + VCardLineEnding);
    end;
    with TTestCaseVCardProcessor(AddNew('VCardProcessor - Add phone country code', TTestCaseVCardProcessor)) do begin
      Processor.AddDefaultPhoneCountryPrefix := True;
      Processor.DefaultPhoneCountryCode := '+470';
      Processor.NonPrefixedPhoneLength := 9;
      Input := BeginEnd('TEL: 123 456 789 ' + VCardLineEnding) +
        BeginEnd('TEL;CELL:919 191 919' + VCardLineEnding) +
        BeginEnd('TEL;CELL:+421 919 191 919' + VCardLineEnding) +
        BeginEnd('TEL;CELL:*#1234' + VCardLineEnding);
      Output := BeginEnd('TEL:+470 123 456 789 ' + VCardLineEnding) +
        BeginEnd('TEL;CELL:+470919 191 919' + VCardLineEnding) +
        BeginEnd('TEL;CELL:+421 919 191 919' + VCardLineEnding) +
        BeginEnd('TEL;CELL:*#1234' + VCardLineEnding);
    end;
    with TTestCaseVCardProcessor(AddNew('VCardProcessor - Convert international call prefix to country code', TTestCaseVCardProcessor)) do begin
      Processor.ConvertInternationalCallPrefixToCountryCode := True;
      Processor.DefaultInternationalCallPrefix := '00420';
      Processor.DefaultPhoneCountryCode := '+470';
      Input := BeginEnd('TEL:00420123456789' + VCardLineEnding) +
        BeginEnd('TEL;CELL:00420 919 191 919 ' + VCardLineEnding) +
        BeginEnd('TEL;CELL:00421919191919' + VCardLineEnding);
      Output := BeginEnd('TEL:+470123456789' + VCardLineEnding) +
        BeginEnd('TEL;CELL:+470 919 191 919 ' + VCardLineEnding) +
        BeginEnd('TEL;CELL:00421919191919' + VCardLineEnding);
    end;

    with TTestCaseVCardExportImport(AddNew('Export/Import CSV', TTestCaseVCardExportImport)) do begin
      Format := tfCsv;
      HumanReadableHeader := False;
      Input := BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd(
        'N:Surname2;Name2' + VCardLineEnding +
        'FN:Name2 Surname2' + VCardLineEnding);
      Output := Input;
    end;
    with TTestCaseVCardExportImport(AddNew('Export/Import CSV human header', TTestCaseVCardExportImport)) do begin
      Format := tfCsv;
      HumanReadableHeader := True;
      Input := BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd(
        'N:Surname2;Name2' + VCardLineEnding +
        'FN:Name2 Surname2' + VCardLineEnding);
      Output := Input;
    end;
    with TTestCaseVCardExportImport(AddNew('Export/Import MediaWiki', TTestCaseVCardExportImport)) do begin
      Format := tfMediaWiki;
      HumanReadableHeader := False;
      Input := BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd(
        'N:Surname2;Name2' + VCardLineEnding +
        'FN:Name2 Surname2' + VCardLineEnding);
      Output := Input;
    end;
    with TTestCaseVCardExportImport(AddNew('Export/Import XML', TTestCaseVCardExportImport)) do begin
      Format := tfXml;
      HumanReadableHeader := False;
      Input := BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd(
        'N:Surname2;Name2' + VCardLineEnding +
        'FN:Name2 Surname2' + VCardLineEnding);
      Output := Input;
    end;
    with TTestCaseVCardExportImport(AddNew('Export/Import JSON', TTestCaseVCardExportImport)) do begin
      Format := tfJSON;
      HumanReadableHeader := False;
      Input := BeginEnd(
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding) +
        BeginEnd(
        'N:Surname2;Name2' + VCardLineEnding +
        'FN:Name2 Surname2' + VCardLineEnding);
      Output := Input;
    end;

    with TTestCaseLoadSave(AddNew('Merge same cell phone', TTestCaseLoadSave)) do begin
      Input := VCardBegin + MacLineEnding +
        VCardVersion + MacLineEnding +
        'N:Surname;Name' + MacLineEnding +
        'FN:Name Surname' + MacLineEnding +
        'TEL;CELL:123456789' + MacLineEnding +
        VCardEnd + MacLineEnding +

        VCardBegin + MacLineEnding +
        VCardVersion + MacLineEnding +
        'N:Surname2;Name' + MacLineEnding +
        'FN:Name Surname2' + MacLineEnding +
        'TEL;CELL:123456789' + MacLineEnding +
        VCardEnd + MacLineEnding;
      Output := VCardBegin + VCardLineEnding +
        VCardVersion + VCardLineEnding +
        'N:Surname;Name' + VCardLineEnding +
        'FN:Name Surname' + VCardLineEnding +
        'TEL;CELL:123456789' + MacLineEnding +
        VCardEnd + VCardLineEnding;
    end;
  end;
end;

end.

