Skip to content

Commit

Permalink
Turn some semi-redundant methods into extensions
Browse files Browse the repository at this point in the history
Keeps having to reimplement them for every IBasicLogger implementation
  • Loading branch information
blowfishpro committed Mar 18, 2018
1 parent 94af6b8 commit 30fd490
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 72 deletions.
13 changes: 13 additions & 0 deletions ModuleManager/Extensions/IBasicLoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using UnityEngine;
using ModuleManager.Logging;

namespace ModuleManager.Extensions
{
public static class IBasicLoggerExtensions
{
public static void Info(this IBasicLogger logger, string message) => logger.Log(LogType.Log, message);
public static void Warning(this IBasicLogger logger, string message) => logger.Log(LogType.Warning, message);
public static void Error(this IBasicLogger logger, string message) => logger.Log(LogType.Error, message);
}
}
3 changes: 0 additions & 3 deletions ModuleManager/Logging/IBasicLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ namespace ModuleManager.Logging
public interface IBasicLogger
{
void Log(LogType logType, string message);
void Info(string message);
void Warning(string message);
void Error(string message);
void Exception(string message, Exception exception);
}
}
7 changes: 2 additions & 5 deletions ModuleManager/Logging/ModLogger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using UnityEngine;
using ModuleManager.Extensions;

namespace ModuleManager.Logging
{
Expand All @@ -15,14 +16,10 @@ public ModLogger(string prefix, ILogger logger)
}

public void Log(LogType logType, string message) => logger.Log(logType, prefix + message);

public void Info(string message) => Log(LogType.Log, message);
public void Warning(string message) => Log(LogType.Warning, message);
public void Error(string message) => Log(LogType.Error, message);

public void Exception(string message, Exception exception)
{
Error(message);
this.Error(message);
logger.LogException(exception);
}
}
Expand Down
3 changes: 0 additions & 3 deletions ModuleManager/Logging/QueueLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public QueueLogger(IMessageQueue<ILogMessage> queue)
}

public void Log(LogType logType, string message) => queue.Add(new NormalMessage(logType, message));
public void Info(string message) => Log(LogType.Log, message);
public void Warning(string message) => Log(LogType.Warning, message);
public void Error(string message) => Log(LogType.Error, message);
public void Exception(string message, Exception exception) => queue.Add(new ExceptionMessage(message, exception));
}
}
1 change: 1 addition & 0 deletions ModuleManager/ModuleManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Compile Include="Command.cs" />
<Compile Include="CommandParser.cs" />
<Compile Include="Extensions\ConfigNodeExtensions.cs" />
<Compile Include="Extensions\IBasicLoggerExtensions.cs" />
<Compile Include="Extensions\NodeStackExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Extensions\UrlConfigExtensions.cs" />
Expand Down
40 changes: 40 additions & 0 deletions ModuleManagerTests/Extensions/IBasicLoggerExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Xunit;
using NSubstitute;
using UnityEngine;
using ModuleManager.Logging;
using ModuleManager.Extensions;

namespace ModuleManagerTests.Extensions
{
public class IBasicLoggerExtensionsTest
{
private IBasicLogger logger;

public IBasicLoggerExtensionsTest()
{
logger = Substitute.For<IBasicLogger>();
}

[Fact]
public void TestInfo()
{
logger.Info("well hi there");
logger.Received().Log(LogType.Log, "well hi there");
}

[Fact]
public void TestWarning()
{
logger.Warning("I'm warning you");
logger.Received().Log(LogType.Warning, "I'm warning you");
}

[Fact]
public void TestError()
{
logger.Error("You have made a grave mistake");
logger.Received().Log(LogType.Error, "You have made a grave mistake");
}
}
}
21 changes: 6 additions & 15 deletions ModuleManagerTests/Logging/ModLoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,27 @@ public ModLoggerTest()
innerLogger = Substitute.For<ILogger>();
logger = new ModLogger("MyMod", innerLogger);
}
[Fact]
public void TestLog()
{
logger.Log(LogType.Log, "this is a log message");
logger.Log(LogType.Error, "this is another log message");

innerLogger.Received().Log(LogType.Log, "[MyMod] this is a log message");
innerLogger.Received().Log(LogType.Error, "[MyMod] this is another log message");
}

[Fact]
public void TestInfo()
public void TestLog__Info()
{
logger.Info("well hi there");
logger.Log(LogType.Log, "well hi there");

innerLogger.Received().Log(LogType.Log, "[MyMod] well hi there");
}

[Fact]
public void TestWarning()
public void TestLog__Warning()
{
logger.Warning("I'm warning you");
logger.Log(LogType.Warning, "I'm warning you");

innerLogger.Received().Log(LogType.Warning, "[MyMod] I'm warning you");
}

[Fact]
public void TestError()
public void TestLog__Error()
{
logger.Error("You have made a grave mistake");
logger.Log(LogType.Error, "You have made a grave mistake");

innerLogger.Received().Log(LogType.Error, "[MyMod] You have made a grave mistake");
}
Expand Down
19 changes: 6 additions & 13 deletions ModuleManagerTests/Logging/QueueLoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,23 @@ public QueueLoggerTest()
}

[Fact]
public void TestLog()
public void TestLog__Info()
{
logger.Log(LogType.Log, "this is a log message");
queue.Received().Add(Arg.Is<NormalMessage>(m => m.logType == LogType.Log && m.message == "this is a log message"));
}

[Fact]
public void TestInfo()
{
logger.Info("useful information");
logger.Log(LogType.Log, "useful information");
queue.Received().Add(Arg.Is<NormalMessage>(m => m.logType == LogType.Log && m.message == "useful information"));
}

[Fact]
public void TestWarning()
public void TestLog__Warning()
{
logger.Warning("not to alarm you, but something might be wrong");
logger.Log(LogType.Warning, "not to alarm you, but something might be wrong");
queue.Received().Add(Arg.Is<NormalMessage>(m => m.logType == LogType.Warning && m.message == "not to alarm you, but something might be wrong"));
}

[Fact]
public void TestError()
public void TestLog__Error()
{
logger.Error("you broke everything");
logger.Log(LogType.Error, "you broke everything");
queue.Received().Add(Arg.Is<NormalMessage>(m => m.logType == LogType.Error && m.message == "you broke everything"));
}

Expand Down
1 change: 1 addition & 0 deletions ModuleManagerTests/ModuleManagerTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Extensions\IBasicLoggerExtensionsTest.cs" />
<Compile Include="Extensions\StringExtensionsTest.cs" />
<Compile Include="Extensions\UrlConfigExtensionsTest.cs" />
<Compile Include="NeedsCheckerTest.cs" />
Expand Down
23 changes: 12 additions & 11 deletions ModuleManagerTests/PatchApplierTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using Xunit;
using NSubstitute;
using UnityEngine;
using TestUtils;
using ModuleManager;
using ModuleManager.Logging;
Expand Down Expand Up @@ -753,7 +754,7 @@ public void TestApplyPatches__Loop()
progress.Received(1).PatchApplied();
progress.Received(4).ApplyingUpdate(config1, patch1);

logger.Received().Info("Looping on abc/def/@PART:HAS[~aaa[>10]] to abc/def/PART");
logger.Received().Log(LogType.Log, "Looping on abc/def/@PART:HAS[~aaa[>10]] to abc/def/PART");

UrlDir.UrlConfig[] allConfigs = databaseRoot.AllConfigs.ToArray();
Assert.Equal(1, allConfigs.Length);
Expand Down Expand Up @@ -796,14 +797,14 @@ public void TestApplyPatches__InvalidOperator()
progress.DidNotReceiveWithAnyArgs().Exception(null, null);
progress.DidNotReceiveWithAnyArgs().Exception(null, null, null);

logger.DidNotReceiveWithAnyArgs().Error(null);
logger.DidNotReceive().Log(LogType.Error, Arg.Any<string>());
logger.DidNotReceiveWithAnyArgs().Exception(null, null);

logger.Received().Warning("Invalid command encountered on a patch: abc/def/%PART");
logger.Received().Warning("Invalid command encountered on a patch: abc/def/|PART");
logger.Received().Warning("Invalid command encountered on a patch: abc/def/#PART");
logger.Received().Warning("Invalid command encountered on a patch: abc/def/*PART");
logger.Received().Warning("Invalid command encountered on a patch: abc/def/&PART");
logger.Received().Log(LogType.Warning, "Invalid command encountered on a patch: abc/def/%PART");
logger.Received().Log(LogType.Warning, "Invalid command encountered on a patch: abc/def/|PART");
logger.Received().Log(LogType.Warning, "Invalid command encountered on a patch: abc/def/#PART");
logger.Received().Log(LogType.Warning, "Invalid command encountered on a patch: abc/def/*PART");
logger.Received().Log(LogType.Warning, "Invalid command encountered on a patch: abc/def/&PART");

UrlDir.UrlConfig[] allConfigs = databaseRoot.AllConfigs.ToArray();
Assert.Equal(1, allConfigs.Length);
Expand Down Expand Up @@ -840,8 +841,8 @@ public void TestApplyPatches__Copy__NameNotChanged()

progress.Received().Error(patch1, "Error - when applying copy abc/def/+PART to abc/def/PART - the copy needs to have a different name than the parent (use @name = xxx)");

logger.DidNotReceiveWithAnyArgs().Warning(null);
logger.DidNotReceiveWithAnyArgs().Error(null);
logger.DidNotReceive().Log(LogType.Warning, Arg.Any<string>());
logger.DidNotReceive().Log(LogType.Error, Arg.Any<string>());
logger.DidNotReceiveWithAnyArgs().Exception(null, null);

progress.Received(1).PatchApplied();
Expand All @@ -863,8 +864,8 @@ private void EnsureNoErrors()
progress.DidNotReceiveWithAnyArgs().Exception(null, null);
progress.DidNotReceiveWithAnyArgs().Exception(null, null, null);

logger.DidNotReceiveWithAnyArgs().Warning(null);
logger.DidNotReceiveWithAnyArgs().Error(null);
logger.DidNotReceive().Log(LogType.Warning, Arg.Any<string>());
logger.DidNotReceive().Log(LogType.Error, Arg.Any<string>());
logger.DidNotReceiveWithAnyArgs().Exception(null, null);
}

Expand Down
Loading

0 comments on commit 30fd490

Please sign in to comment.