Format

Top  Previous  Next

Unit Tests > Format

The formatting routines account for a considerable part of the SysUtils unit. Some of them are nested and consist in about 1000 lines of code. Nevertheless the translated code works nearly perfect.

 

using System.Sysutils;

using System;

using static D7_formatting.D7_formattingImplementation;

using static D7_formatting.D7_formattingInterface;

using static System.SystemInterface;

using static System.Sysutils.SysutilsInterface;

 

 

namespace D7_formatting

{

 

 

 

    public class D7_formattingInterface

    {

        public static bool RunFormattingChecks()

        {

            bool result = false;

            result = true;

            if (!CheckBuildMessage())

                result = false;

            if (!CheckIdentifierLayout())

                result = false;

            if (!CheckFloatingPointText())

                result = false;

            return result;

        }

 

    } // class D7_formattingInterface

 

 

    file class D7_formattingImplementation

    {

 

 

        public static bool CheckBuildMessage()

        {

            bool result = false;

            string MessageText = string.Empty;

            MessageText = Format("Build %s produced %d artifacts", new TVarRec[] { "linux-x64", 18 });

            result = MessageText == "Build linux-x64 produced 18 artifacts";

            return result;

        }

 

        public static bool CheckIdentifierLayout()

        {

            bool result = false;

            result = true;

            if (Format("ID-%6.4d", new TVarRec[] { 73 }) != "ID-  0073")

                result = false;

            if (Format("HEX-%4.4x", new TVarRec[] { 0x2A }) != "HEX-002A")

                result = false;

            if (Format("%1:s/%0:s/%1:s", new TVarRec[] { "source", "target" }) != "target/source/target")

                result = false;

            return result;

        }

 

        public static bool CheckFloatingPointText()

        {

            bool result = false;

            TFormatSettings SavedSettings = TFormatSettings.CreateRecord();

            double Measurement = 0.0D;

            SavedSettings = FormatSettings;

            try

            {

                FormatSettings.DecimalSeparator = ',';

                FormatSettings.ThousandSeparator = '.';

                Measurement = 4312.24D;

                result = true;

                if (FloatToStr(12.5D) != "12,5")

                    result = false;

                if (FloatToStr(1E40D) != "1E40")

                    result = false;

                if (FormatFloat("#,##0.00", Measurement) != "4.312,24")

                    result = false;

                if (FormatFloat("000000", Measurement) != "004312")

                    result = false;

                if (FormatFloat("0.000", Measurement) != "4312,240")

                    result = false;

                if (FormatFloat("0.000E+00", Measurement) != "4,312E+03")

                    result = false;

                if (FormatFloat("0.0 \"up\";0.0 \"down\"", -Measurement) != "4312,2 down")

                    result = false;

                if (FormatFloat("0.0;-0.0;\"idle\"", 0.0F) != "idle")

                    result = false;

            }

            finally

            {

                FormatSettings = SavedSettings;

            }

            return result;

        }

    } // class D7_formattingImplementation

 

}  // namespace D7_formatting

 

 

 

generated from: 

 

 unit d7_formatting;

 

 

interface

 

function RunFormattingChecks: Boolean;

 

implementation

 

uses

  System.SysUtils;

 

function CheckBuildMessage: Boolean;

var

  MessageText: string;

begin

  MessageText := Format('Build %s produced %d artifacts',

    ['linux-x64', 18]);

 

  Result := MessageText =

    'Build linux-x64 produced 18 artifacts';

end;

 

function CheckIdentifierLayout: Boolean;

begin

  Result := True;

 

  if Format('ID-%6.4d', [73]) <> 'ID-  0073' then

    Result := False;

 

  if Format('HEX-%4.4x', [$2A]) <> 'HEX-002A' then

    Result := False;

 

  if Format('%1:s/%0:s/%1:s', ['source', 'target']) <>

    'target/source/target' then

    Result := False;

end;

 

function CheckFloatingPointText: Boolean;

var

  SavedSettings: TFormatSettings;

  Measurement: Double;

begin

  SavedSettings := FormatSettings;

  try

    FormatSettings.DecimalSeparator := ',';

    FormatSettings.ThousandSeparator := '.';

    Measurement := 4312.24;

 

    Result := True;

 

    if FloatToStr(12.5) <> '12,5' then

      Result := False;

 

    if FloatToStr(1E40) <> '1E40' then

      Result := False;

 

    if FormatFloat('#,##0.00', Measurement) <> '4.312,24' then

      Result := False;

 

    if FormatFloat('000000', Measurement) <> '004312' then

      Result := False;

 

    if FormatFloat('0.000', Measurement) <> '4312,240' then

      Result := False;

 

    if FormatFloat('0.000E+00', Measurement) <> '4,312E+03' then

      Result := False;

 

    if FormatFloat('0.0 "up";0.0 "down"', -Measurement) <>

      '4312,2 down' then

      Result := False;

 

    if FormatFloat('0.0;-0.0;"idle"', 0) <> 'idle' then

      Result := False;

  finally

    FormatSettings := SavedSettings;

  end;

end;

 

function RunFormattingChecks: Boolean;

begin

  Result := True;

 

  if not CheckBuildMessage then

    Result := False;

 

  if not CheckIdentifierLayout then

    Result := False;

 

  if not CheckFloatingPointText then

    Result := False;

end;

 

end.

 

 

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content