-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSerialDecoderTests.cs
More file actions
97 lines (84 loc) · 3.24 KB
/
Copy pathSerialDecoderTests.cs
File metadata and controls
97 lines (84 loc) · 3.24 KB
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
95
96
97
using System.Collections;
using PostCodeSerialMonitor.Models;
using PostCodeSerialMonitor.Services;
using System.IO;
using Xunit;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Text.Json;
using Moq;
namespace PostCodeSerialMonitor.Tests;
public class TestDataGenerator : IEnumerable<object[]>
{
private readonly List<object[]> _data = new List<object[]>
{
new object[] {"CPU: 0x14ff (+0.000 mS)", new DecodedCode(){
Flavor = CodeFlavor.CPU,
Code = 0x14ff
}},
new object[] {"CPU: 0x14ff", new DecodedCode(){
Flavor = CodeFlavor.CPU,
Code = 0x14ff
}},
new object[] {"SP : 0x75 (+4252.064 mS)", new DecodedCode(){
Flavor = CodeFlavor.SP,
Code = 0x0075
}},
new object[] {"SP : 0x75", new DecodedCode(){
Flavor = CodeFlavor.SP,
Code = 0x0075
}}
};
public IEnumerator<object[]> GetEnumerator() => _data.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public class SerialDecoderTests
{
private readonly SerialLineDecoder _decoder;
private readonly string _testDataPath;
public SerialDecoderTests()
{
_testDataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "postcodes.csv");
var csvContent = File.ReadAllText(_testDataPath);
// Create configuration
var config = new AppConfiguration();
var optionsMonitor = new Mock<IOptionsMonitor<AppConfiguration>>();
optionsMonitor.Setup(m => m.CurrentValue).Returns(config);
// Create logger factory
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var loggerMeta = new Mock<ILogger<MetaDefinitionService>>().Object;
var loggerDecoder = new Mock<ILogger<SerialLineDecoder>>().Object;
// Create configuration service
var configService = new ConfigurationService(
optionsMonitor.Object,
loggerFactory.CreateLogger<ConfigurationService>(),
Path.GetDirectoryName(_testDataPath) ?? "."
);
// Create JSON options
var jsonOptions = new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
AllowTrailingCommas = true,
WriteIndented = true
};
// Create MetaUpdateService
var metaUpdateService = new MetaUpdateService(
configService,
jsonOptions,
loggerFactory.CreateLogger<MetaUpdateService>()
);
// Create MetaDefinitionService
var metaDefinitionService = new MetaDefinitionService(configService, metaUpdateService, loggerMeta);
metaDefinitionService.RefreshMetaDefinitionsAsync().GetAwaiter().GetResult();
_decoder = new SerialLineDecoder(metaDefinitionService, loggerDecoder);
}
[Theory]
[ClassData(typeof(TestDataGenerator))]
public void TestDecoding(string input, DecodedCode expected)
{
var result = _decoder.DecodeLine(input, ConsoleType.XboxOnePhat);
Assert.NotNull(result);
Assert.Equal(expected.Flavor, result.Flavor);
Assert.Equal(expected.Code, result.Code);
}
}