Skip to content

Commit 2f31a4a

Browse files
committed
v19.1.0 ⬩ Make more explicit null-safety in some Array[byte] extensions (Possibly breaking ABI) ⬩ Add some new unit tests ⬩ Upgrade nugets for testing dependencies
1 parent dc88e9b commit 2f31a4a

File tree

7 files changed

+343
-20
lines changed

7 files changed

+343
-20
lines changed

Directory.Build.props

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Project>
33
<PropertyGroup>
4-
<Version>19.0.2</Version>
5-
<PackageReleaseNotes>Renamed version.props to Directory.Build.props</PackageReleaseNotes>
4+
<Version>19.1.0</Version>
5+
<PackageReleaseNotes>
6+
- Make more explicit null-safety in some Array[byte] extensions (Possibly breaking ABI)
7+
- Add some new unit tests
8+
- Upgrade nugets for testing dependencies
9+
</PackageReleaseNotes>
610
</PropertyGroup>
711
</Project>

InterlockLedger.Commons.NUnit.Tests/InterlockLedger.Commons.NUnit.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
<Target Name="TagSources" />
1010
<Target Name="NugetOrg" />
1111
<ItemGroup>
12-
<PackageReference Include="NUnit" Version="4.3.2" />
13-
<PackageReference Include="NUnit.Analyzers" Version="4.6.0">
12+
<PackageReference Include="NUnit" Version="4.4.0" />
13+
<PackageReference Include="NUnit.Analyzers" Version="4.10.0">
1414
<PrivateAssets>all</PrivateAssets>
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
</PackageReference>
17-
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
18-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
17+
<PackageReference Include="NUnit3TestAdapter" Version="5.1.0" />
18+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
1919
</ItemGroup>
2020
<ItemGroup>
2121
<ProjectReference Include="..\InterlockLedger.Commons\InterlockLedger.Commons.csproj" />
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// ******************************************************************************************************************************
2+
//
3+
// Copyright (c) 2018-2023 InterlockLedger Network
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met
8+
//
9+
// * Redistributions of source code must retain the above copyright notice, this
10+
// list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of the copyright holder nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//
31+
// ******************************************************************************************************************************
32+
33+
namespace System;
34+
35+
[TestFixture]
36+
public class ArrayOfByteExtensionsTests
37+
{
38+
[Test]
39+
public void Append() {
40+
byte[] bytes = [1, 2, 3];
41+
byte[] newBytes = [4, 5, 6];
42+
byte[]? nullBytes = null;
43+
byte[]? nullNewBytes = null;
44+
byte[]? appended = bytes.Append(newBytes);
45+
Assert.That(appended, Is.EqualTo(new byte[] { 1, 2, 3, 4, 5, 6 }));
46+
appended = bytes.Append(nullNewBytes!);
47+
Assert.That(appended, Is.EqualTo(bytes));
48+
appended = nullBytes.Append(newBytes);
49+
Assert.That(appended, Is.EqualTo(newBytes));
50+
appended = nullBytes.Append(nullNewBytes);
51+
Assert.That(appended, Is.Null);
52+
}
53+
54+
[Test]
55+
public void AsLiteral() {
56+
byte[] bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
57+
using (Assert.EnterMultipleScope()) {
58+
Assert.That(bytes.AsLiteral(5), Is.EqualTo("new byte[] { 1, 2, 3, 4, 5 ...}"));
59+
Assert.That(bytes.AsLiteral(), Is.EqualTo("new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }"));
60+
}
61+
62+
}
63+
64+
[Test]
65+
public void AsLong() {
66+
byte[] bytes = [0, 0, 0, 0, 0, 0, 0, 1];
67+
Assert.That(bytes.AsLong(), Is.EqualTo(1L));
68+
}
69+
70+
[Test]
71+
public void AsULong() {
72+
byte[] bytes = [0, 0, 0, 0, 0, 0, 0, 1];
73+
Assert.That(bytes.AsULong(), Is.EqualTo(1UL));
74+
}
75+
76+
[Test]
77+
public void AsUTF8String() {
78+
byte[] bytes = "Test"u8.ToArray();
79+
Assert.That(bytes.AsUTF8String(), Is.EqualTo("Test"));
80+
}
81+
82+
[Test]
83+
public void CompareTo() {
84+
byte[] bytes1 = [1, 2, 3];
85+
byte[] bytes2 = [1, 2, 4];
86+
byte[] bytes3 = [1, 2, 3, 4];
87+
using (Assert.EnterMultipleScope()) {
88+
Assert.That(bytes1.CompareTo(bytes2), Is.EqualTo(-1));
89+
Assert.That(bytes2.CompareTo(bytes1), Is.EqualTo(1));
90+
Assert.That(bytes1.CompareTo(bytes1), Is.Zero);
91+
Assert.That(bytes1.CompareTo(bytes3), Is.EqualTo(-1));
92+
Assert.That(bytes3.CompareTo(bytes1), Is.EqualTo(1));
93+
}
94+
95+
}
96+
97+
[Test]
98+
public void FromSafeBase64() {
99+
string base64 = "AQID";
100+
byte[] bytes = base64.FromSafeBase64();
101+
Assert.That(bytes, Is.EqualTo(new byte[] { 1, 2, 3 }));
102+
}
103+
104+
[Test]
105+
public void HasSameBytesAs() {
106+
byte[] bytes1 = [1, 2, 3];
107+
byte[] bytes2 = [1, 2, 3];
108+
byte[] bytes3 = [1, 2, 4];
109+
using (Assert.EnterMultipleScope()) {
110+
Assert.That(bytes1.HasSameBytesAs(bytes2), Is.True);
111+
Assert.That(bytes1.HasSameBytesAs(bytes3), Is.False);
112+
}
113+
114+
}
115+
116+
[Test]
117+
public void PartOf() {
118+
byte[] bytes = [1, 2, 3, 4, 5];
119+
byte[] part = bytes.PartOf(3, 1);
120+
Assert.That(part, Is.EqualTo(new byte[] { 2, 3, 4 }));
121+
}
122+
123+
[Test]
124+
public void ToSafeBase64() {
125+
byte[] bytes = [1, 2, 3];
126+
string base64 = bytes.ToSafeBase64();
127+
Assert.That(base64, Is.EqualTo("AQID"));
128+
}
129+
130+
[Test]
131+
public void SafeLength() {
132+
byte[]? bytes = null;
133+
Assert.That(bytes.SafeLength(), Is.Zero);
134+
bytes = [];
135+
Assert.That(bytes.SafeLength(), Is.Zero);
136+
bytes = [1, 2, 3];
137+
Assert.That(bytes.SafeLength(), Is.EqualTo(3));
138+
}
139+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// ******************************************************************************************************************************
2+
//
3+
// Copyright (c) 2018-2023 InterlockLedger Network
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met
8+
//
9+
// * Redistributions of source code must retain the above copyright notice, this
10+
// list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of the copyright holder nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//
31+
// ******************************************************************************************************************************
32+
33+
namespace System;
34+
35+
[TestFixture]
36+
public class IntegerExtensionsTests
37+
{
38+
[Test]
39+
public void In() {
40+
Assert.That(1.In(1, 2, 3), Is.True);
41+
Assert.That(4.In(1, 2, 3), Is.False);
42+
Assert.That(1.In(), Is.False);
43+
}
44+
45+
[Test]
46+
public void ToBytes() {
47+
byte[] bytes = 1.ToBytes();
48+
byte[] expected = [0, 0, 0, 1];
49+
Assert.That(bytes, Is.EqualTo(expected));
50+
}
51+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// ******************************************************************************************************************************
2+
//
3+
// Copyright (c) 2018-2023 InterlockLedger Network
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are met
8+
//
9+
// * Redistributions of source code must retain the above copyright notice, this
10+
// list of conditions and the following disclaimer.
11+
//
12+
// * Redistributions in binary form must reproduce the above copyright notice,
13+
// this list of conditions and the following disclaimer in the documentation
14+
// and/or other materials provided with the distribution.
15+
//
16+
// * Neither the name of the copyright holder nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//
31+
// ******************************************************************************************************************************
32+
33+
namespace System;
34+
35+
[TestFixture]
36+
public class ResultTests
37+
{
38+
[Test]
39+
public void ResultOk()
40+
{
41+
var result = Result.Ok;
42+
Assert.That(result.Success, Is.True);
43+
}
44+
45+
[Test]
46+
public void ResultFromException()
47+
{
48+
var exception = new Exception("Test Exception");
49+
Result result = exception;
50+
Assert.That(result.Success, Is.False);
51+
Assert.That(result, Is.TypeOf<Error>());
52+
var error = (Error)result;
53+
Assert.That(error.ErrorMessage, Is.EqualTo("Test Exception"));
54+
Assert.That(error.Exception, Is.EqualTo(exception));
55+
}
56+
57+
[Test]
58+
public void ResultBoolConversion()
59+
{
60+
bool success = Result.Ok;
61+
Assert.That(success, Is.True);
62+
Result error = new Exception();
63+
success = error;
64+
Assert.That(success, Is.False);
65+
}
66+
67+
[Test]
68+
public void ErrorFromException()
69+
{
70+
var exception = new Exception("Test Exception");
71+
var error = new Error(exception);
72+
Assert.That(error.Success, Is.False);
73+
Assert.That(error.ErrorMessage, Is.EqualTo("Test Exception"));
74+
Assert.That(error.Exception, Is.EqualTo(exception));
75+
Assert.That(error.ErrorType, Is.EqualTo(IError.DefaultErrorType));
76+
}
77+
78+
[Test]
79+
public void ErrorFromString()
80+
{
81+
var error = new Error("Test Error");
82+
Assert.That(error.Success, Is.False);
83+
Assert.That(error.ErrorMessage, Is.EqualTo("Test Error"));
84+
Assert.That(error.Exception, Is.Null);
85+
Assert.That(error.ErrorType, Is.EqualTo(IError.DefaultErrorType));
86+
}
87+
88+
[Test]
89+
public void ResultOfTFromValue()
90+
{
91+
Result<int> result = 123;
92+
Assert.That(result.Success, Is.True);
93+
Assert.That(result.Value, Is.EqualTo(123));
94+
}
95+
96+
[Test]
97+
public void ResultOfTFromException()
98+
{
99+
var exception = new Exception("Test Exception");
100+
Result<int> result = exception;
101+
Assert.That(result.Success, Is.False);
102+
Assert.Throws<InvalidOperationException>(() => { var _ = result.Value; });
103+
}
104+
105+
[Test]
106+
public void ResultOfTToT()
107+
{
108+
Result<int> result = 123;
109+
int value = result;
110+
Assert.That(value, Is.EqualTo(123));
111+
}
112+
113+
[Test]
114+
public void ErrorOfTFromException()
115+
{
116+
var exception = new Exception("Test Exception");
117+
var error = new Error<int>(exception);
118+
Assert.That(error.Success, Is.False);
119+
Assert.That(error.ErrorMessage, Is.EqualTo("Test Exception"));
120+
Assert.That(error.Exception, Is.EqualTo(exception));
121+
}
122+
123+
[Test]
124+
public void ResultExtensionsToConvertedResult()
125+
{
126+
Result<int> result = 123;
127+
Result<string> convertedResult = result.ToConvertedResult(v => v.ToString());
128+
Assert.That(convertedResult.Success, Is.True);
129+
Assert.That(convertedResult.Value, Is.EqualTo("123"));
130+
131+
Result<int> errorResult = new Exception("Error");
132+
convertedResult = errorResult.ToConvertedResult(v => v.ToString());
133+
Assert.That(convertedResult.Success, Is.False);
134+
}
135+
}

0 commit comments

Comments
 (0)