Skip to content

Commit fa75efb

Browse files
committed
Add Utils.Conversions unit & tests to DUnitX tests
1 parent 25dc8a6 commit fa75efb

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed

cupola/src/CSLE.Utils.Conversions.pas

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
This Source Code Form is subject to the terms of the Mozilla Public License,
3+
v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
obtain one at https://mozilla.org/MPL/2.0/
5+
6+
Copyright (C) 2024, Peter Johnson (gravatar.com/delphidabbler).
7+
8+
Various conversion routines.
9+
}
10+
11+
unit CSLE.Utils.Conversions;
12+
13+
interface
14+
15+
function TryStrToUint16(const AStr: string; out Value: UInt16): Boolean;
16+
17+
implementation
18+
19+
uses
20+
System.SysUtils;
21+
22+
function TryStrToUInt16(const AStr: string; out Value: UInt16): Boolean;
23+
begin
24+
var IntValue: Integer;
25+
if not TryStrToInt(AStr, IntValue) then
26+
Exit(False);
27+
if (IntValue < 0) or (IntValue > High(UInt16)) then
28+
Exit(False);
29+
Value := UInt16(IntValue);
30+
Result := True;
31+
end;
32+
33+
end.

cupola/tests/CodeSnip.Cupola.Tests.dpr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ uses
1212
DUnitX.Loggers.Console,
1313
DUnitX.Loggers.Xml.NUnit,
1414
{$ENDIF }
15-
DUnitX.TestFramework;
15+
DUnitX.TestFramework,
16+
CSLE.Utils.Conversions in '..\src\CSLE.Utils.Conversions.pas',
17+
Test.Utils.Conversions in 'Test.Utils.Conversions.pas';
1618

1719
{$IFNDEF TESTINSIGHT}
1820
var

cupola/tests/CodeSnip.Cupola.Tests.dproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
<DelphiCompile Include="$(MainSource)">
7070
<MainSource>MainSource</MainSource>
7171
</DelphiCompile>
72+
<DCCReference Include="..\src\CSLE.Utils.Conversions.pas"/>
73+
<DCCReference Include="Test.Utils.Conversions.pas"/>
7274
<BuildConfiguration Include="Base">
7375
<Key>Base</Key>
7476
</BuildConfiguration>
@@ -103,6 +105,7 @@
103105
</Platform>
104106
</DeployFile>
105107
<DeployFile LocalName="Win32\Debug\CodeSnip.Cupola.Tests.exe" Configuration="Debug" Class="ProjectOutput"/>
108+
<DeployFile LocalName="Win32\Debug\CodeSnip.Cupola.Tests.exe" Configuration="Debug" Class="ProjectOutput"/>
106109
<DeployClass Name="AdditionalDebugSymbols">
107110
<Platform Name="iOSSimulator">
108111
<Operation>1</Operation>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
This unit is dedicated to public domain under the CC0 license.
3+
See https://creativecommons.org/public-domain/cc0/
4+
}
5+
6+
unit Test.Utils.Conversions;
7+
8+
interface
9+
10+
uses
11+
DUnitX.TestFramework,
12+
13+
System.SysUtils,
14+
15+
CSLE.Utils.Conversions;
16+
17+
type
18+
[TestFixture]
19+
TTestConversionRoutines = class
20+
public
21+
[Setup]
22+
procedure Setup;
23+
[TearDown]
24+
procedure TearDown;
25+
26+
[Test]
27+
[TestCase('0','0,0')]
28+
[TestCase('42','42,42')]
29+
[TestCase('Max','65535,65535')]
30+
[TestCase('Plus 56','+56,56')]
31+
[TestCase('Hex','$face,64206')]
32+
procedure TryStrToUInt16_succeeds_on_strings_valid_valid_UInt16_values(const Str: string; Expected: UInt16);
33+
[Test]
34+
[TestCase('Empty str','')]
35+
[TestCase('Punctuation','*&^%$')]
36+
[TestCase('Letters','FooBar')]
37+
[TestCase('Digits then letters','999aaa')]
38+
procedure TryStrToUInt16_fails_on_non_numeric_strings(const Str: string);
39+
[Test]
40+
[TestCase('-1','-1')]
41+
[TestCase('Max+1','65536')]
42+
[TestCase('Massive +ve','68719476720')]
43+
[TestCase('Massive -ve','-68719476720')]
44+
procedure TryStrToUInt16_fails_on_strings_with_out_of_bounds_values(const Str: string);
45+
end;
46+
47+
implementation
48+
49+
procedure TTestConversionRoutines.Setup;
50+
begin
51+
end;
52+
53+
procedure TTestConversionRoutines.TearDown;
54+
begin
55+
end;
56+
57+
procedure TTestConversionRoutines.TryStrToUInt16_fails_on_non_numeric_strings(const Str: string);
58+
begin
59+
var Value: UInt16;
60+
Assert.IsFalse(TryStrToUint16(Str, Value));
61+
end;
62+
63+
procedure TTestConversionRoutines.TryStrToUInt16_fails_on_strings_with_out_of_bounds_values(
64+
const Str: string);
65+
begin
66+
var Value: UInt16;
67+
Assert.IsFalse(TryStrToUint16(Str, Value));
68+
end;
69+
70+
procedure TTestConversionRoutines.TryStrToUInt16_succeeds_on_strings_valid_valid_UInt16_values(
71+
const Str: string; Expected: UInt16);
72+
begin
73+
var Actual: UInt16;
74+
var Res := TryStrToUint16(Str, Actual);
75+
Assert.IsTrue(Res, 'Returns True');
76+
Assert.AreEqual(Expected, Actual, 'Value');
77+
end;
78+
79+
initialization
80+
TDUnitX.RegisterTestFixture(TTestConversionRoutines);
81+
82+
end.

0 commit comments

Comments
 (0)