Update config parser tests

This commit is contained in:
genteure 2022-05-17 00:36:54 +08:00
parent 34bf5c31f5
commit d29471406b
2 changed files with 72 additions and 60 deletions

View File

@ -1,80 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BililiveRecorder.Core.Config;
using BililiveRecorder.Core.Config.V1;
using BililiveRecorder.Core.Config.V2;
using Newtonsoft.Json;
using BililiveRecorder.Core.Config.V3;
using VerifyXunit;
using Xunit;
namespace BililiveRecorder.Core.UnitTests.Config
{
[UsesVerify]
public class ConfigTests
{
private const string V2TestString1 = "{\"version\":2,\"global\":{\"EnabledFeature\":{\"HasValue\":false,\"Value\":0},\"ClipLengthPast\":{\"HasValue\":false,\"Value\":0},\"ClipLengthFuture\":{\"HasValue\":false,\"Value\":0},\"TimingStreamRetry\":{\"HasValue\":false,\"Value\":0},\"TimingStreamConnect\":{\"HasValue\":false,\"Value\":0},\"TimingDanmakuRetry\":{\"HasValue\":false,\"Value\":0},\"TimingCheckInterval\":{\"HasValue\":false,\"Value\":0},\"TimingWatchdogTimeout\":{\"HasValue\":false,\"Value\":0},\"RecordDanmakuFlushInterval\":{\"HasValue\":false,\"Value\":0},\"Cookie\":{\"HasValue\":false,\"Value\":null},\"WebHookUrls\":{\"HasValue\":false,\"Value\":null},\"LiveApiHost\":{\"HasValue\":false,\"Value\":null},\"RecordFilenameFormat\":{\"HasValue\":false,\"Value\":null},\"ClipFilenameFormat\":{\"HasValue\":false,\"Value\":null},\"CuttingMode\":{\"HasValue\":false,\"Value\":0},\"CuttingNumber\":{\"HasValue\":false,\"Value\":0},\"RecordDanmaku\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuRaw\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuSuperChat\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuGift\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuGuard\":{\"HasValue\":false,\"Value\":false}},\"rooms\":[{\"RoomId\":{\"HasValue\":true,\"Value\":1},\"AutoRecord\":{\"HasValue\":false,\"Value\":false},\"CuttingMode\":{\"HasValue\":false,\"Value\":0},\"CuttingNumber\":{\"HasValue\":false,\"Value\":0},\"RecordDanmaku\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuRaw\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuSuperChat\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuGift\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuGuard\":{\"HasValue\":false,\"Value\":false}},{\"RoomId\":{\"HasValue\":true,\"Value\":2},\"AutoRecord\":{\"HasValue\":false,\"Value\":false},\"CuttingMode\":{\"HasValue\":false,\"Value\":0},\"CuttingNumber\":{\"HasValue\":false,\"Value\":0},\"RecordDanmaku\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuRaw\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuSuperChat\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuGift\":{\"HasValue\":false,\"Value\":false},\"RecordDanmakuGuard\":{\"HasValue\":false,\"Value\":false}}]}";
private static readonly List<object[]> V1 = new()
{
new object[] { "{\"version\":1}", 1 },
new object[] { "{\"version\":1,\"data\":\"\"}", 1 },
};
private static readonly List<object[]> V2 = new()
{
new object[] { "{\"version\":2}", 2 },
new object[] { "{\"version\":2,\"data\":{}}", 2 },
new object[] { V2TestString1, 2 },
};
public static IEnumerable<object[]> GetTestData(int version)
=> version switch
{
0 => V1.Concat(V2).AsEnumerable(),
1 => V1.AsEnumerable(),
2 => V2.AsEnumerable(),
_ => throw new ArgumentException()
};
[Theory, MemberData(nameof(GetTestData), 0)]
public void Parse(string data, int ver)
{
var result = JsonConvert.DeserializeObject<ConfigBase>(data);
var type = ver switch
{
1 => typeof(ConfigV1Wrapper),
2 => typeof(ConfigV2),
_ => throw new Exception("not supported")
};
Assert.Equal(type, result.GetType());
}
[Fact]
public void V2Test1()
public Task CanSerializeToJsonAsync()
{
var obj = JsonConvert.DeserializeObject<ConfigBase>(V2TestString1);
var v2 = Assert.IsType<ConfigV2>(obj);
Assert.Equal(2, v2.Rooms.Count);
Assert.Equal(1, v2.Rooms[0].RoomId);
Assert.Equal(2, v2.Rooms[1].RoomId);
}
[Fact]
public void Save()
{
ConfigBase config = new ConfigV2()
var config = new ConfigV3()
{
Rooms = new List<RoomConfig>
{
new RoomConfig { RoomId = 1 },
new RoomConfig { RoomId = 2 }
new RoomConfig { RoomId = 1, AutoRecord = true },
new RoomConfig { RoomId = 2 },
new RoomConfig { RoomId = int.MaxValue, RecordDanmaku = false }
},
Global = new GlobalConfig()
{
FileNameRecordTemplate = "TEST TEMPLATE VALUE",
RecordDanmakuRaw = true,
RecordMode = RecordMode.RawData,
}
};
var json = JsonConvert.SerializeObject(config);
var json = ConfigParser.SaveJson(config);
return Verifier.Verify(json);
}
[Theory]
[InlineData(@"{""version"":1,""data"":""{\""roomlist\"":[{\""enabled\"":true,\""id\"":1},{\""enabled\"":true,\""id\"":3},{\""enabled\"":true,\""id\"":5},{\""enabled\"":false,\""id\"":999},{\""enabled\"":false,\""id\"":2147483647}],\""record_filename_format\"":\""TEST_FILE_NAME_TEMPLATE!!\""}""}")]
[InlineData(@"{""version"":2,""global"":{""RecordFilenameFormat"":{""HasValue"":true,""Value"":""TEST_FILE_NAME_TEMPLATE!!""}},""rooms"":[{""RoomId"":{""HasValue"":true,""Value"":1},""AutoRecord"":{""HasValue"":true,""Value"":true}},{""RoomId"":{""HasValue"":true,""Value"":3},""AutoRecord"":{""HasValue"":true,""Value"":true}},{""RoomId"":{""HasValue"":true,""Value"":5},""AutoRecord"":{""HasValue"":true,""Value"":true}},{""RoomId"":{""HasValue"":true,""Value"":999},""AutoRecord"":{""HasValue"":true,""Value"":false}},{""RoomId"":{""HasValue"":true,""Value"":2147483647},""AutoRecord"":{""HasValue"":true,""Value"":false}}]}")]
[InlineData(@"{""version"":3,""global"":{""FileNameRecordTemplate"":{""HasValue"":true,""Value"":""TEST_FILE_NAME_TEMPLATE!!""}},""rooms"":[{""RoomId"":{""HasValue"":true,""Value"":1},""AutoRecord"":{""HasValue"":true,""Value"":true}},{""RoomId"":{""HasValue"":true,""Value"":3},""AutoRecord"":{""HasValue"":true,""Value"":true}},{""RoomId"":{""HasValue"":true,""Value"":5},""AutoRecord"":{""HasValue"":true,""Value"":true}},{""RoomId"":{""HasValue"":true,""Value"":999},""AutoRecord"":{""HasValue"":true,""Value"":false}},{""RoomId"":{""HasValue"":true,""Value"":2147483647},""AutoRecord"":{""HasValue"":true,""Value"":false}}]}")]
public void CanLoadEveryVersion(string configString)
{
var config = ConfigParser.LoadJson(configString)!;
Assert.NotNull(config);
Assert.True(config.Global.HasFileNameRecordTemplate);
Assert.Equal("TEST_FILE_NAME_TEMPLATE!!", config.Global.FileNameRecordTemplate);
Assert.False(config.Global.HasRecordMode);
Assert.Collection(config.Rooms,
room =>
{
Assert.True(room.AutoRecord);
Assert.Equal(1, room.RoomId);
Assert.False(room.HasRecordDanmaku);
},
room =>
{
Assert.True(room.AutoRecord);
Assert.Equal(3, room.RoomId);
Assert.False(room.HasRecordDanmaku);
},
room =>
{
Assert.True(room.AutoRecord);
Assert.Equal(5, room.RoomId);
Assert.False(room.HasRecordDanmaku);
},
room =>
{
Assert.False(room.AutoRecord);
Assert.Equal(999, room.RoomId);
Assert.False(room.HasRecordDanmaku);
},
room =>
{
Assert.False(room.AutoRecord);
Assert.Equal(int.MaxValue, room.RoomId);
Assert.False(room.HasRecordDanmaku);
}
);
}
}
}

View File

@ -0,0 +1 @@
{"version":3,"global":{"RecordMode":{"HasValue":true,"Value":1},"RecordDanmakuRaw":{"HasValue":true,"Value":true},"FileNameRecordTemplate":{"HasValue":true,"Value":"TEST TEMPLATE VALUE"}},"rooms":[{"RoomId":{"HasValue":true,"Value":1},"AutoRecord":{"HasValue":true,"Value":true}},{"RoomId":{"HasValue":true,"Value":2}},{"RoomId":{"HasValue":true,"Value":2147483647},"RecordDanmaku":{"HasValue":true}}]}