Skip to content

Commit

Permalink
test: fixing test messages
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Feb 7, 2022
1 parent b73c3fe commit 615983e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion source/SnapshotBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public T GetLinearInterpolation(double now)
if (Last.time < now)
{
if (logger.LogEnabled())
logger.Log($"No snapshots for t = {now:0.000}, using first t = {buffer[0].time:0.000}, last t = {Last.time:0.000}");
logger.Log($"No snapshots for t = {now:0.000}, using first t = {buffer[0].time:0.000}, last t = {Last.time:0.000}");
return Last.state;
}

Expand Down
51 changes: 39 additions & 12 deletions tests/runtime/SnapshotBufferTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Mirage.Logging;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
Expand Down Expand Up @@ -86,7 +87,7 @@ public void ShouldGiveErrorIfOutOfOrder()
buffer.AddSnapShot(default, time1);

ArgumentException exception = Assert.Throws<ArgumentException>(() => buffer.AddSnapShot(default, time2));
Assert.That(exception, Has.Message.EqualTo($"Can not add Snapshot to buffer out of order, last t={time1:0.000}, new t={time2:0.000}"));
Assert.That(exception, Has.Message.EqualTo($"Can not add snapshot to buffer. This would cause the buffer to be out of order. Last t={time1:0.000}, new t={time2:0.000}"));
}
}

Expand All @@ -99,7 +100,7 @@ public void ShouldGiveErrorWhenEmpty()
SnapshotBuffer<TransformState> buffer = CreateBuffer();

InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() => buffer.GetLinearInterpolation(default));
Assert.That(exception, Has.Message.EqualTo("No snapshots in buffer"));
Assert.That(exception, Has.Message.EqualTo("No snapshots in buffer."));
}

[Test]
Expand All @@ -109,9 +110,12 @@ public void ShouldReturnFirstIfOnly1Snapshot([Range(0, 10, 1f)] float now)
SnapshotBuffer<TransformState> buffer = CreateBuffer();
buffer.AddSnapShot(state, default);

LogAssert.Expect(LogType.Log, "[DEBUG] First snapshot");
TransformState value = buffer.GetLinearInterpolation(now);
Assert.That(value, Is.EqualTo(state));
using (new SetLogLevel<SnapshotBuffer<TransformState>>(LogType.Log))
{
LogAssert.Expect(LogType.Log, "First snapshot");
TransformState value = buffer.GetLinearInterpolation(now);
Assert.That(value, Is.EqualTo(state));
}
}

[Test]
Expand All @@ -126,9 +130,12 @@ public void ShouldReturnFirstIfNowIsBeforeFirst([Range(0, 1, 0.1f)] float now)
buffer.AddSnapShot(state1, time1);
buffer.AddSnapShot(state2, time2);

LogAssert.Expect(LogType.Log, $"[DEBUG] No snapshots for t={now:0.000}, using earliest t={time1:0.000}");
TransformState value = buffer.GetLinearInterpolation(now);
Assert.That(value, Is.EqualTo(state1));
using (new SetLogLevel<SnapshotBuffer<TransformState>>(LogType.Log))
{
LogAssert.Expect(LogType.Log, $"No snapshots for t = {now:0.000}, using earliest t = {time1:0.000}");
TransformState value = buffer.GetLinearInterpolation(now);
Assert.That(value, Is.EqualTo(state1));
}
}

[Test]
Expand All @@ -143,9 +150,12 @@ public void ShouldReturnLastIfNowIsAfterLast([Range(1.6f, 2.6f, 0.1f)] float now
buffer.AddSnapShot(state1, time1);
buffer.AddSnapShot(state2, time2);

LogAssert.Expect(LogType.Warning, $"[WARN] No snapshots for t={now:0.000}, using first t={time1:0.000} last t={time2:0.000}");
TransformState value = buffer.GetLinearInterpolation(now);
Assert.That(value, Is.EqualTo(state2));
using (new SetLogLevel<SnapshotBuffer<TransformState>>(LogType.Log))
{
LogAssert.Expect(LogType.Log, $"No snapshots for t = {now:0.000}, using first t = {time1:0.000}, last t = {time2:0.000}");
TransformState value = buffer.GetLinearInterpolation(now);
Assert.That(value, Is.EqualTo(state2));
}
}

[Test]
Expand Down Expand Up @@ -173,7 +183,7 @@ public class SnapshotBuffer_DebugToString : SnapshotBufferTestBase
public void ShouldSayEmptyBuffer()
{
SnapshotBuffer<TransformState> buffer = CreateBuffer();
string str = buffer.ToString();
string str = buffer.ToDebugString(0);
Assert.That(str, Is.EqualTo("Buffer Empty"));
}
[Test]
Expand All @@ -185,4 +195,21 @@ public void ShouldListSnapshotsInBuffer()
//Assert.Ignore("NotImplemented");
}
}

struct SetLogLevel<T> : IDisposable
{
private ILogger logger;
private LogType startingLevel;

public SetLogLevel(LogType targetLevel)
{
logger = LogFactory.GetLogger<T>();
startingLevel = logger.filterLogType;
logger.filterLogType = targetLevel;
}
public void Dispose()
{
logger.filterLogType = startingLevel;
}
}
}

0 comments on commit 615983e

Please sign in to comment.