TStringList

Top  Previous  Next

Unit Tests > TStringList

A frequently used Delphi class is TStringList.

 

 

using System.Classes;

using System.Sysutils;

using System;

using static D7_string_list.D7_string_listImplementation;

using static D7_string_list.D7_string_listInterface;

using static System.Classes.ClassesInterface;

using static System.SystemInterface;

using static System.Sysutils.SysutilsInterface;

 

 

namespace D7_string_list

{

 

 

 

   public class D7_string_listInterface

   {

       public static 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;

       }

 

   } // class D7_string_listInterface

 

 

   file class D7_string_listImplementation

   {

 

 

       public static bool CheckStageList()

       {

           bool result = false;

           TStringList Stages = default;

           Stages = new TStringList();

           try

           {

               Stages.Add("scan");

               Stages.Add("emit");

               Stages.Insert(1, "validate");

               Stages.Exchange(0, 2);

               result = (Stages.Count == 3) && (Stages[0] == "emit") && (Stages[1] == "validate") && (Stages[2] == "scan") && (Stages.IndexOf("validate") == 1) && (Stages.IndexOf("missing") == -1);

           }

           finally

           {

               TObject.Free(Stages);

           }

           return result;

       }

 

       public static bool CheckOptionValues()

       {

           bool result = false;

           TStringList Options = default;

           Options = new TStringList();

           try

           {

               Options.WritePropertyValues("mode", "safe");

               Options.WritePropertyValues("workers", "6");

               Options.WritePropertyValues("trace", "off");

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

           }

           finally

           {

               TObject.Free(Options);

           }

           return result;

       }

 

       public static bool CheckDelimitedFields()

       {

           bool result = false;

           TStringList Fields = default;

           Fields = new TStringList();

           try

           {

               Fields.Delimiter = ';';

               Fields.QuoteChar = '\"';

               Fields.StrictDelimiter = true;

               Fields.DelimitedText = "alpha;\"two words\";omega";

               result = (Fields.Count == 3) && (Fields[0] == "alpha") && (Fields[1] == "two words") && (Fields[2] == "omega");

           }

           finally

           {

               TObject.Free(Fields);

           }

           return result;

       }

 

       public static bool CheckFileRoundTrip()

       {

           bool result = false;

           TStringList SourceLines = default;

           TStringList LoadedLines = default;

           string FileName = string.Empty;

           FileName = "d7_string_list.tmp";

           SourceLines = new TStringList();

           LoadedLines = new TStringList();

           try

           {

               SourceLines.Add("first=alpha");

               SourceLines.Add("second=beta");

               SourceLines.SaveToFile(FileName);

               LoadedLines.LoadFromFile(FileName);

               result = (LoadedLines.Count == 2) && (LoadedLines.ReadPropertyValues("first") == "alpha") && (LoadedLines.ReadPropertyValues("second") == "beta");

           }

           finally

           {

               TObject.Free(LoadedLines);

               TObject.Free(SourceLines);

               if (FileExists(FileName))

                   DeleteFile(FileName);

           }

           return result;

       }

   } // class D7_string_listImplementation

 

}  // namespace D7_string_list

 

 

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 Delphi2C# Documentation

Delphi2C# home  Content