Skip to content

Commit

Permalink
Extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
304NotModified committed Jan 21, 2017
1 parent 42cb9ad commit 4daa8e3
Showing 1 changed file with 122 additions and 2 deletions.
124 changes: 122 additions & 2 deletions tests/NLog.UnitTests/Common/InternalLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void WriteToConsoleOutTests()
Console.SetOut(consoleOutWriter1);

// Named (based on LogLevel) public methods.
InternalLogger.Warn(()=>"WWW");
InternalLogger.Warn(() => "WWW");
InternalLogger.Error(() => "EEE");
InternalLogger.Fatal(() => "FFF");
InternalLogger.Trace(() => "TTT");
Expand Down Expand Up @@ -491,13 +491,57 @@ public void ExceptionTests()

// Named (based on LogLevel) public methods.

InternalLogger.Warn(ex1, () =>"WWW");
InternalLogger.Warn(ex1, () => "WWW");
InternalLogger.Error(ex2, () => "EEE");
InternalLogger.Fatal(ex3, () => "FFF");
InternalLogger.Trace(ex4, () => "TTT");
InternalLogger.Debug(ex5, () => "DDD");
InternalLogger.Info(ex6, () => "III");

consoleOutWriter.Flush();
var strings = consoleOutWriter.ToString();
Assert.Equal(expected, strings);
}
{
StringWriter consoleOutWriter = new StringWriter()
{
NewLine = Environment.NewLine
};

// Redirect the console output to a StringWriter.
Console.SetOut(consoleOutWriter);

// Named (based on LogLevel) public methods.

InternalLogger.Log(ex1, LogLevel.Warn, "WWW");
InternalLogger.Log(ex2, LogLevel.Error, "EEE");
InternalLogger.Log(ex3, LogLevel.Fatal, "FFF");
InternalLogger.Log(ex4, LogLevel.Trace, "TTT");
InternalLogger.Log(ex5, LogLevel.Debug, "DDD");
InternalLogger.Log(ex6, LogLevel.Info, "III");

consoleOutWriter.Flush();
var strings = consoleOutWriter.ToString();
Assert.Equal(expected, strings);
}
{
StringWriter consoleOutWriter = new StringWriter()
{
NewLine = Environment.NewLine
};

// Redirect the console output to a StringWriter.
Console.SetOut(consoleOutWriter);

// Named (based on LogLevel) public methods.

InternalLogger.Log(ex1, LogLevel.Warn, () => "WWW");
InternalLogger.Log(ex2, LogLevel.Error, () => "EEE");
InternalLogger.Log(ex3, LogLevel.Fatal, () => "FFF");
InternalLogger.Log(ex4, LogLevel.Trace, () => "TTT");
InternalLogger.Log(ex5, LogLevel.Debug, () => "DDD");
InternalLogger.Log(ex6, LogLevel.Info, () => "III");

consoleOutWriter.Flush();
var strings = consoleOutWriter.ToString();
Assert.Equal(expected, strings);
Expand All @@ -506,6 +550,82 @@ public void ExceptionTests()

}

[Theory]
[InlineData("trace", 6)]
[InlineData("debug", 5)]
[InlineData("info", 4)]
[InlineData("warn", 3)]
[InlineData("error", 2)]
[InlineData("fatal", 1)]
[InlineData("off", 0)]
public void TestMinLevelSwitch_log(string rawLogLevel, int count)
{
Action log = () =>
{
InternalLogger.Log(LogLevel.Fatal, "L1");
InternalLogger.Log(LogLevel.Error, "L2");
InternalLogger.Log(LogLevel.Warn, "L3");
InternalLogger.Log(LogLevel.Info, "L4");
InternalLogger.Log(LogLevel.Debug, "L5");
InternalLogger.Log(LogLevel.Trace, "L6");
};

TestMinLevelSwitch_inner(rawLogLevel, count, log);
}

[Theory]
[InlineData("trace", 6)]
[InlineData("debug", 5)]
[InlineData("info", 4)]
[InlineData("warn", 3)]
[InlineData("error", 2)]
[InlineData("fatal", 1)]
[InlineData("off", 0)]
public void TestMinLevelSwitch(string rawLogLevel, int count)
{
Action log = () =>
{
InternalLogger.Fatal("L1");
InternalLogger.Error("L2");
InternalLogger.Warn("L3");
InternalLogger.Info("L4");
InternalLogger.Debug("L5");
InternalLogger.Trace("L6");
};

TestMinLevelSwitch_inner(rawLogLevel, count, log);
}

private static void TestMinLevelSwitch_inner(string rawLogLevel, int count, Action log)
{
//set minimal
InternalLogger.LogLevel = LogLevel.FromString(rawLogLevel);
InternalLogger.IncludeTimestamp = false;

StringWriter consoleOutWriter = new StringWriter()
{
NewLine = ";"
};

InternalLogger.LogWriter = consoleOutWriter;

// Redirect the console output to a StringWriter.
Console.SetOut(consoleOutWriter);

var expected = "";
var logLevel = LogLevel.Fatal.Ordinal;
for (int i = 0; i < count; i++, logLevel--)
{
expected += LogLevel.FromOrdinal(logLevel) + " L" + (i + 1) + ";";
}

log();

consoleOutWriter.Flush();
var strings = consoleOutWriter.ToString();
Assert.Equal(expected, strings);
}

[Theory]
[InlineData("trace", true)]
[InlineData("debug", true)]
Expand Down

0 comments on commit 4daa8e3

Please sign in to comment.