Skip to content

Commit 8a0c326

Browse files
committed
coverage
1 parent 42d27c4 commit 8a0c326

7 files changed

Lines changed: 331 additions & 371 deletions

File tree

CSUtilities.Lib/AssemblyInfo.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Runtime.InteropServices;
3+
4+
// In SDK-style projects such as this one, several assembly attributes that were historically
5+
// defined in this file are now automatically added during build and populated with
6+
// values defined in project properties. For details of which attributes are included
7+
// and how to customise this process see: https://aka.ms/assembly-info-properties
8+
9+
10+
// Setting ComVisible to false makes the types in this assembly not visible to COM
11+
// components. If you need to access a type in this assembly from COM, set the ComVisible
12+
// attribute to true on that type.
13+
14+
[assembly: ComVisible(false)]
15+
16+
// The following GUID is for the ID of the typelib if this project is exposed to COM.
17+
18+
[assembly: Guid("48364828-e5d2-4b28-bc91-b7c081c911f4")]
19+
[assembly: InternalsVisibleTo("CSUtilities.Tests")]

CSUtilities.Tests/EnvironmentVarsTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,41 @@ public void SetTest()
6464
EnvironmentVars.Set(varname, varvalue, EnvironmentVariableTarget.Process);
6565
Assert.Equal(varvalue, Environment.GetEnvironmentVariable(varname));
6666
}
67+
68+
[Fact]
69+
public void Set_WithoutTarget_SetsProcessVariable()
70+
{
71+
string varname = "TEST_SET_NO_TARGET";
72+
string varvalue = "VALUE_NO_TARGET";
73+
EnvironmentVars.Set(varname, varvalue);
74+
Assert.Equal(varvalue, Environment.GetEnvironmentVariable(varname, EnvironmentVariableTarget.Process));
75+
}
76+
77+
[Fact]
78+
public void Delete_RemovesVariable()
79+
{
80+
string varname = "TEST_DELETE";
81+
Environment.SetEnvironmentVariable(varname, "TO_BE_DELETED");
82+
EnvironmentVars.Delete(varname);
83+
Assert.Null(Environment.GetEnvironmentVariable(varname, EnvironmentVariableTarget.Process));
84+
}
85+
86+
[Fact]
87+
public void Delete_WithTarget_RemovesVariable()
88+
{
89+
string varname = "TEST_DELETE_TARGET";
90+
Environment.SetEnvironmentVariable(varname, "TO_BE_DELETED", EnvironmentVariableTarget.Process);
91+
EnvironmentVars.Delete(varname, EnvironmentVariableTarget.Process);
92+
Assert.Null(Environment.GetEnvironmentVariable(varname, EnvironmentVariableTarget.Process));
93+
}
94+
95+
[Fact]
96+
public void GetGeneric_ReturnsDefaultIfNotSet()
97+
{
98+
string varname = "NOT_SET_GENERIC";
99+
Assert.Equal(0, EnvironmentVars.Get<int>(varname));
100+
Assert.Null(EnvironmentVars.Get<string>(varname));
101+
Assert.False(EnvironmentVars.Get<bool>(varname));
102+
}
67103
}
68104
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using CSUtilities.IO;
6+
using Xunit;
7+
8+
namespace CSUtilities.Tests.IO
9+
{
10+
public class StreamIOTests : IDisposable
11+
{
12+
private readonly MemoryStream _memoryStream;
13+
private readonly StreamIO _streamIO;
14+
15+
public StreamIOTests()
16+
{
17+
_memoryStream = new MemoryStream();
18+
_streamIO = new StreamIO(_memoryStream);
19+
}
20+
21+
public void Dispose()
22+
{
23+
_streamIO.Dispose();
24+
_memoryStream.Dispose();
25+
}
26+
27+
[Fact]
28+
public void WriteAndReadByte_WorksCorrectly()
29+
{
30+
_streamIO.Write<byte>(0x42);
31+
_streamIO.Position = 0;
32+
Assert.Equal(0x42, _streamIO.ReadByte());
33+
}
34+
35+
[Fact]
36+
public void WriteAndReadBytes_WorksCorrectly()
37+
{
38+
byte[] data = { 1, 2, 3, 4, 5 };
39+
_streamIO.WriteBytes(data);
40+
_streamIO.Position = 0;
41+
Assert.Equal(data, _streamIO.ReadBytes(data.Length));
42+
}
43+
44+
[Fact]
45+
public void WriteAndReadString_WorksCorrectly()
46+
{
47+
string value = "Hello, world!";
48+
_streamIO.Write(value, Encoding.UTF8);
49+
_streamIO.Position = 0;
50+
string result = _streamIO.ReadString(value.Length, Encoding.UTF8);
51+
Assert.Equal(value, result);
52+
}
53+
54+
[Fact]
55+
public void GetBytes_ReturnsCorrectBytes()
56+
{
57+
byte[] data = { 10, 20, 30, 40, 50 };
58+
_streamIO.WriteBytes(data);
59+
byte[] result = _streamIO.GetBytes(1, 3);
60+
Assert.Equal(new byte[] { 20, 30, 40 }, result);
61+
}
62+
63+
[Fact]
64+
public async Task GetBytesAsync_ReturnsCorrectBytes()
65+
{
66+
byte[] data = { 100, 101, 102, 103, 104 };
67+
_streamIO.WriteBytes(data);
68+
byte[] result = await _streamIO.GetBytesAsync(2, 2);
69+
Assert.Equal(new byte[] { 102, 103 }, result);
70+
}
71+
72+
[Fact]
73+
public void LookByte_DoesNotAdvancePosition()
74+
{
75+
_streamIO.Write<byte>(0x7F);
76+
_streamIO.Position = 0;
77+
byte b = _streamIO.LookByte();
78+
Assert.Equal(0x7F, b);
79+
Assert.Equal(0, _streamIO.Position);
80+
}
81+
82+
[Fact]
83+
public void LookBytes_DoesNotAdvancePosition()
84+
{
85+
byte[] data = { 1, 2, 3, 4 };
86+
_streamIO.WriteBytes(data);
87+
_streamIO.Position = 0;
88+
byte[] looked = _streamIO.LookBytes(2);
89+
Assert.Equal(new byte[] { 1, 2 }, looked);
90+
Assert.Equal(0, _streamIO.Position);
91+
}
92+
93+
[Fact]
94+
public void ReadChar_ReadsCorrectChar()
95+
{
96+
_streamIO.Write<byte>((byte)'A');
97+
_streamIO.Position = 0;
98+
Assert.Equal('A', _streamIO.ReadChar());
99+
}
100+
101+
[Fact]
102+
public void ReadUntil_ReadsUntilMatch()
103+
{
104+
string value = "abc;def";
105+
_streamIO.Write(value, Encoding.ASCII);
106+
_streamIO.Position = 0;
107+
string result = _streamIO.ReadUntil(';');
108+
Assert.Equal("abc;", result);
109+
}
110+
111+
[Fact]
112+
public void ReadShort_ReadsCorrectValue()
113+
{
114+
short value = 0x1234;
115+
_streamIO.Write(value);
116+
_streamIO.Position = 0;
117+
Assert.Equal(value, _streamIO.ReadShort());
118+
}
119+
120+
[Fact]
121+
public void ReadInt_ReadsCorrectValue()
122+
{
123+
int value = 0x12345678;
124+
_streamIO.Write(value);
125+
_streamIO.Position = 0;
126+
Assert.Equal(value, _streamIO.ReadInt());
127+
}
128+
129+
[Fact]
130+
public void ReadLong_ReadsCorrectValue()
131+
{
132+
long value = 0x123456789ABCDEF0;
133+
_streamIO.Write(value);
134+
_streamIO.Position = 0;
135+
Assert.Equal(value, _streamIO.ReadLong());
136+
}
137+
138+
[Fact]
139+
public void ReadSingle_ReadsCorrectValue()
140+
{
141+
float value = 123.456f;
142+
_streamIO.Write(value);
143+
_streamIO.Position = 0;
144+
Assert.Equal(value, _streamIO.ReadSingle(), 3);
145+
}
146+
147+
[Fact]
148+
public void ReadDouble_ReadsCorrectValue()
149+
{
150+
double value = 123456.789;
151+
_streamIO.Write(value);
152+
_streamIO.Position = 0;
153+
Assert.Equal(value, _streamIO.ReadDouble(), 6);
154+
}
155+
156+
[Fact]
157+
public void ReadUShort_ReadsCorrectValue()
158+
{
159+
ushort value = 0xFEDC;
160+
_streamIO.Write(value);
161+
_streamIO.Position = 0;
162+
Assert.Equal(value, _streamIO.ReadUShort());
163+
}
164+
165+
[Fact]
166+
public void ReadUInt_ReadsCorrectValue()
167+
{
168+
uint value = 0xDEADBEEF;
169+
_streamIO.Write(value);
170+
_streamIO.Position = 0;
171+
Assert.Equal(value, _streamIO.ReadUInt());
172+
}
173+
174+
[Fact]
175+
public void ReadULong_ReadsCorrectValue()
176+
{
177+
ulong value = 0x123456789ABCDEF0UL;
178+
_streamIO.Write(value);
179+
_streamIO.Position = 0;
180+
Assert.Equal(value, _streamIO.ReadULong());
181+
}
182+
183+
[Fact]
184+
public void WriteBytes_WithOffsetAndCount_WritesCorrectly()
185+
{
186+
byte[] data = { 1, 2, 3, 4, 5 };
187+
_streamIO.WriteBytes(data, 1, 3);
188+
_streamIO.Position = 0;
189+
Assert.Equal(new byte[] { 2, 3, 4 }, _streamIO.ReadBytes(3));
190+
}
191+
192+
[Fact]
193+
public void ReadBytes_ThrowsOnNegativeLength()
194+
{
195+
Assert.Throws<ArgumentOutOfRangeException>(() => _streamIO.ReadBytes(-1));
196+
}
197+
198+
[Fact]
199+
public void GetBytes_ThrowsOnNegativeLength()
200+
{
201+
Assert.Throws<ArgumentOutOfRangeException>(() => _streamIO.GetBytes(0, -1));
202+
}
203+
204+
[Fact]
205+
public void ReadBytes_ThrowsOnEndOfStream()
206+
{
207+
Assert.Throws<EndOfStreamException>(() => _streamIO.ReadBytes(1));
208+
}
209+
}
210+
}

CSUtilities.Tests/TryTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using CSUtilities;
3+
using Xunit;
4+
5+
namespace CSUtilities.Tests
6+
{
7+
public class TryTests
8+
{
9+
[Fact]
10+
public void Do_ActionSucceeds_ReturnsTrue()
11+
{
12+
// Arrange
13+
bool executed = false;
14+
Action action = () => executed = true;
15+
16+
// Act
17+
bool result = Try.Do(action);
18+
19+
// Assert
20+
Assert.True(result);
21+
Assert.True(executed);
22+
}
23+
24+
[Fact]
25+
public void Do_ActionThrows_ReturnsFalse()
26+
{
27+
// Arrange
28+
Action action = () => throw new InvalidOperationException();
29+
30+
// Act
31+
bool result = Try.Do(action);
32+
33+
// Assert
34+
Assert.False(result);
35+
}
36+
37+
[Fact]
38+
public void Do_WithExceptionOut_ActionSucceeds_ReturnsTrueAndNullException()
39+
{
40+
// Arrange
41+
Action action = () => { };
42+
43+
// Act
44+
bool result = Try.Do(action, out Exception ex);
45+
46+
// Assert
47+
Assert.True(result);
48+
Assert.Null(ex);
49+
}
50+
51+
[Fact]
52+
public void Do_WithExceptionOut_ActionThrows_ReturnsFalseAndException()
53+
{
54+
// Arrange
55+
var expected = new InvalidOperationException("fail");
56+
Action action = () => throw expected;
57+
58+
// Act
59+
bool result = Try.Do(action, out Exception ex);
60+
61+
// Assert
62+
Assert.False(result);
63+
Assert.Same(expected, ex);
64+
}
65+
}
66+
}

CSUtilities/CSUtilities.projitems

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IEnumerableExtensions.cs" />
2727
<Compile Include="$(MSBuildThisFileDirectory)Extensions\QueueExtensions.cs" />
2828
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ReflectionExtensions.cs" />
29-
<Compile Include="$(MSBuildThisFileDirectory)IO\CsvReader.cs" />
3029
<Compile Include="$(MSBuildThisFileDirectory)Extensions\EnumExtensions.cs" />
31-
<Compile Include="$(MSBuildThisFileDirectory)Mutation.cs" />
3230
<Compile Include="$(MSBuildThisFileDirectory)AppDomainUtils.cs" />
3331
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ByteExtensions.cs" />
3432
<Compile Include="$(MSBuildThisFileDirectory)Extensions\StringExtensions.cs" />

0 commit comments

Comments
 (0)