1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-10 15:20:48 -05:00

add ability to register custom event generators for event parsers / truncate long client names fix

This commit is contained in:
RaidMax
2020-04-04 12:40:23 -05:00
parent d45d99454b
commit 3b695a0d2c
24 changed files with 334 additions and 69 deletions

View File

@ -1,4 +1,5 @@
using ApplicationTests.Fixtures;
using FakeItEasy;
using IW4MAdmin.Application.EventParsers;
using IW4MAdmin.Application.Factories;
using Microsoft.Extensions.DependencyInjection;
@ -7,6 +8,7 @@ using NUnit.Framework;
using SharedLibraryCore;
using SharedLibraryCore.Interfaces;
using System;
using static SharedLibraryCore.GameEvent;
namespace ApplicationTests
{
@ -15,15 +17,19 @@ namespace ApplicationTests
{
private EventLogTest eventLogData;
private IServiceProvider serviceProvider;
private ILogger fakeLogger;
[SetUp]
public void Setup()
{
eventLogData = JsonConvert.DeserializeObject<EventLogTest>(System.IO.File.ReadAllText("Files/GameEvents.json"));
fakeLogger = A.Fake<ILogger>();
serviceProvider = new ServiceCollection()
.AddSingleton<BaseEventParser>()
.AddTransient<IParserPatternMatcher, ParserPatternMatcher>()
.AddSingleton<IParserRegexFactory, ParserRegexFactory>()
.AddSingleton(fakeLogger)
.BuildServiceProvider();
}
@ -54,5 +60,54 @@ namespace ApplicationTests
AssertMatch(parsedEvent, e);
}
}
[Test]
public void TestCustomEvents()
{
var eventParser = serviceProvider.GetService<BaseEventParser>();
string eventMessage = "Hello this is my test event message";
string triggerValue = "testTrigger";
string eventType = "testType";
eventParser.RegisterCustomEvent(eventType, triggerValue, (logLine, config, generatedEvent) =>
{
generatedEvent.Message = eventMessage;
return generatedEvent;
});
var customEvent = eventParser.GenerateGameEvent($"23:14 {triggerValue}");
Assert.AreEqual(EventType.Other, customEvent.Type);
Assert.AreEqual(eventType, customEvent.Subtype);
Assert.AreEqual(eventMessage, customEvent.Message);
}
[Test]
public void TestCustomEventRegistrationArguments()
{
var eventParser = serviceProvider.GetService<BaseEventParser>();
Assert.Throws<ArgumentException>(() => eventParser.RegisterCustomEvent(null, null, null));
Assert.Throws<ArgumentException>(() => eventParser.RegisterCustomEvent("test", null, null));
Assert.Throws<ArgumentException>(() => eventParser.RegisterCustomEvent("test", "test2", null));
Assert.Throws<ArgumentException>(() =>
{
// testing duplicate registers
eventParser.RegisterCustomEvent("test", "test", (a, b, c) => new GameEvent());
eventParser.RegisterCustomEvent("test", "test", (a, b, c) => new GameEvent());
});
}
[Test]
public void TestCustomEventParsingLogsWarningOnException()
{
var eventParser = serviceProvider.GetService<BaseEventParser>();
eventParser.RegisterCustomEvent("test", "test", (a, b, c) => throw new Exception());
eventParser.GenerateGameEvent("12:12 test");
A.CallTo(() => fakeLogger.WriteWarning(A<string>.Ignored))
.MustHaveHappenedOnceExactly();
}
}
}