-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageIndicesReaderTests.cs
94 lines (78 loc) · 2.65 KB
/
MessageIndicesReaderTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
namespace CSharpInteractive.Tests;
using System.Buffers;
using Core;
public class MessageIndicesReaderTests
{
private readonly Mock<ILog<MessageIndicesReader>> _log = new();
private readonly Mock<IFileSystem> _fileSystem = new();
[Fact]
public void ShouldReadIndices()
{
// Given
var reader = CreateInstance();
using var ms = new MemoryStream();
Write(ms, 1UL);
Write(ms, 123456789UL);
ms.Seek(0, SeekOrigin.Begin);
_fileSystem.Setup(i => i.OpenReader("data")).Returns(new StreamReader(ms));
// When
var expectedIndices = reader.Read("data").ToArray();
// Then
expectedIndices.ShouldBe([1UL, 123456789UL]);
}
[Fact]
public void ShouldReadIndicesWhenEmpty()
{
// Given
var reader = CreateInstance();
using var ms = new MemoryStream();
ms.Seek(0, SeekOrigin.Begin);
_fileSystem.Setup(i => i.OpenReader("data")).Returns(new StreamReader(ms));
// When
var expectedIndices = reader.Read("data").ToArray();
// Then
expectedIndices.ShouldBe([]);
}
[Fact]
public void ShouldShowWarningWhenCorrupted()
{
// Given
var reader = CreateInstance();
using var ms = new MemoryStream();
Write(ms, 1UL);
Write(ms, 123456789UL);
ms.Write([12]);
ms.Seek(0, SeekOrigin.Begin);
_fileSystem.Setup(i => i.OpenReader("data")).Returns(new StreamReader(ms));
// When
var expectedIndices = reader.Read("data").ToArray();
// Then
_log.Verify(i => i.Warning(It.Is<Text[]>(warning => warning.Single().Value.Contains("invalid size"))));
expectedIndices.ShouldBe([1UL, 123456789UL]);
}
[Fact]
public void ShouldShowWarningWhenInvalidValues()
{
// Given
var reader = CreateInstance();
using var ms = new MemoryStream();
Write(ms, 123UL);
Write(ms, 1UL);
Write(ms, 1234UL);
ms.Seek(0, SeekOrigin.Begin);
_fileSystem.Setup(i => i.OpenReader("data")).Returns(new StreamReader(ms));
// When
var expectedIndices = reader.Read("data").ToArray();
// Then
_log.Verify(i => i.Warning(It.Is<Text[]>(warning => warning.Single().Value.Contains("invalid index"))));
expectedIndices.ShouldBe([123UL]);
}
private static void Write(Stream stream, ulong index)
{
var bytes = BitConverter.GetBytes(index);
Array.Reverse(bytes);
stream.Write(bytes);
}
private MessageIndicesReader CreateInstance() =>
new(_log.Object, MemoryPool<byte>.Shared, _fileSystem.Object);
}