TStringList

Top  Previous  Next

Unit tests > TStringList

A frequently used Delphi class is TStringList.

 

#include "pch.h"

 

 

#include "d7_string_list.h"

#include "System.Classes.h"

#include "System.SysUtils.h"

#include "OnLeavingScope.h"

 

using namespace std;

using namespace d2c_system;

using namespace System;

using namespace System::Classes;

using namespace System::Sysutils;

 

namespace D7_string_list

{

 

 

 

bool CheckStageList()

{

  bool result = false;

  TStringList* Stages = nullptr;

  Stages = new TStringList();

  {

    auto olsLambda = onLeavingScope([&] 

    {

      delete Stages;

    });

    Stages->Add(L"scan");

    Stages->Add(L"emit");

    Stages->Insert(1, L"validate");

    Stages->Exchange(0, 2);

    result = (Stages->ReadPropertyCount() == 3) && ((*Stages)[0] == L"emit") && ((*Stages)[1] == L"validate") && ((*Stages)[2] == L"scan") && (Stages->IndexOf(L"validate") == 1) && (Stages->IndexOf(L"missing") == -1);

  }

  return result;

}

 

bool CheckOptionValues()

{

  bool result = false;

  TStringList* Options = nullptr;

  Options = new TStringList();

  {

    auto olsLambda = onLeavingScope([&] 

    {

      delete Options;

    });

    Options->WritePropertyValues(L"mode", L"safe");

    Options->WritePropertyValues(L"workers", L"6");

    Options->WritePropertyValues(L"trace", L"off");

    result = (Options->ReadPropertyNames(0) == L"mode") && (Options->ReadPropertyValues(L"mode") == L"safe") && (Options->ReadPropertyValues(L"workers") == L"6") && (Options->ReadPropertyValues(L"trace") == L"off");

  }

  return result;

}

 

bool CheckDelimitedFields()

{

  bool result = false;

  TStringList* Fields = nullptr;

  Fields = new TStringList();

  {

    auto olsLambda = onLeavingScope([&] 

    {

      delete Fields;

    });

    Fields->WritePropertyDelimiter(L';');

    Fields->WritePropertyQuoteChar(L'\"');

    Fields->WritePropertyStrictDelimiter(true);

    Fields->WritePropertyDelimitedText(L"alpha;\"two words\";omega");

    result = (Fields->ReadPropertyCount() == 3) && ((*Fields)[0] == L"alpha") && ((*Fields)[1] == L"two words") && ((*Fields)[2] == L"omega");

  }

  return result;

}

 

bool CheckFileRoundTrip()

{

  bool result = false;

  TStringList* SourceLines = nullptr;

  TStringList* LoadedLines = nullptr;

  String FileName;

  FileName = L"d7_string_list.tmp";

  SourceLines = new TStringList();

  LoadedLines = new TStringList();

  {

    auto olsLambda = onLeavingScope([&] 

    {

      delete LoadedLines;

      delete SourceLines;

      if(FileExists(FileName))

        DeleteFile(FileName);

    });

    SourceLines->Add(L"first=alpha");

    SourceLines->Add(L"second=beta");

    SourceLines->SaveToFile(FileName);

    LoadedLines->LoadFromFile(FileName);

    result = (LoadedLines->ReadPropertyCount() == 2) && (LoadedLines->ReadPropertyValues(L"first") == L"alpha") && (LoadedLines->ReadPropertyValues(L"second") == L"beta");

  }

  return result;

}

 

bool RunStringListChecks()

{

  bool result = false;

  bool CheckResult1 = false;

  bool CheckResult2 = false;

  bool CheckResult3 = false;

  bool CheckResult4 = false;

  CheckResult1 = CheckStageList();

  result = CheckResult1;

  CheckResult2 = CheckOptionValues();

  result = result && CheckResult2;

  CheckResult3 = CheckDelimitedFields();

  result = result && CheckResult3;

  CheckResult4 = CheckFileRoundTrip();

  result = result && CheckResult4;

  return result;

}

 

 

}  // namespace D7_string_list

 

 

This code was generated from:

 

 

unit d7_string_list;

 

interface

 

function RunStringListChecks: Boolean;

 

implementation

 

uses

  System.Classes,

  System.SysUtils;

 

function CheckStageList: Boolean;

var

  Stages: TStringList;

begin

  Stages := TStringList.Create;

  try

    Stages.Add('scan');

    Stages.Add('emit');

    Stages.Insert(1, 'validate');

    Stages.Exchange(0, 2);

 

    Result :=

      (Stages.Count = 3) and

      (Stages[0] = 'emit') and

      (Stages[1] = 'validate') and

      (Stages[2] = 'scan') and

      (Stages.IndexOf('validate') = 1) and

      (Stages.IndexOf('missing') = -1);

  finally

    Stages.Free;

  end;

end;

 

function CheckOptionValues: Boolean;

var

  Options: TStringList;

begin

  Options := TStringList.Create;

  try

    Options.Values['mode'] := 'safe';

    Options.Values['workers'] := '6';

    Options.Values['trace'] := 'off';

 

    Result :=

      (Options.Names[0] = 'mode') and

      (Options.Values['mode'] = 'safe') and

      (Options.Values['workers'] = '6') and

      (Options.Values['trace'] = 'off');

  finally

    Options.Free;

  end;

end;

 

function CheckDelimitedFields: Boolean;

var

  Fields: TStringList;

begin

  Fields := TStringList.Create;

  try

    Fields.Delimiter := ';';

    Fields.QuoteChar := '"';

    Fields.StrictDelimiter := True;

    Fields.DelimitedText := 'alpha;"two words";omega';

 

    Result :=

      (Fields.Count = 3) and

      (Fields[0] = 'alpha') and

      (Fields[1] = 'two words') and

      (Fields[2] = 'omega');

  finally

    Fields.Free;

  end;

end;

 

function CheckFileRoundTrip: Boolean;

var

  SourceLines: TStringList;

  LoadedLines: TStringList;

  FileName: string;

begin

  FileName := 'd7_string_list.tmp';

  SourceLines := TStringList.Create;

  LoadedLines := TStringList.Create;

  try

    SourceLines.Add('first=alpha');

    SourceLines.Add('second=beta');

    SourceLines.SaveToFile(FileName);

    LoadedLines.LoadFromFile(FileName);

 

    Result :=

      (LoadedLines.Count = 2) and

      (LoadedLines.Values['first'] = 'alpha') and

      (LoadedLines.Values['second'] = 'beta');

  finally

    LoadedLines.Free;

    SourceLines.Free;

    if FileExists(FileName) then

      DeleteFile(FileName);

  end;

end;

 

function RunStringListChecks: Boolean;

var

  CheckResult1: Boolean;

  CheckResult2: Boolean;

  CheckResult3: Boolean;

  CheckResult4: Boolean;

begin

  CheckResult1 := CheckStageList;

  Result := CheckResult1;

 

  CheckResult2 := CheckOptionValues;

  Result := Result and CheckResult2;

 

  CheckResult3 := CheckDelimitedFields;

  Result := Result and CheckResult3;

 

  CheckResult4 := CheckFileRoundTrip;

  Result := Result and CheckResult4;

end;

 

end.

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content