Files
Garry Newman d95169d8fa Unit Test Cleanup (#5064)
* Move all unit tests to Engine/Tests
* Fix Margin.EdgeSubtract
* Fix Capsule.Contains
* EnvironmentVariables.Remove if it's null
* Fixed ray trace never returning startedsolid
* Fix HistoryList.Navigate on empty list
* Fix GameObject.WorldPosition accepting NaNs
* Fix flex: initial expanding to the wrong grow/shrink
* Translation.TryConvert shouldn't throw on invalid enum strings
* Skip sound file tests on machines with no audio device
2026-06-12 13:23:50 +01:00

145 lines
2.7 KiB
C#

using System;
using System.Collections.Specialized;
namespace NetworkTests;
[TestClass]
public class NetListTest
{
[TestMethod]
public void AddRemoveAndCount()
{
using var list = new NetList<int>();
Assert.IsTrue( list.Count == 0 );
list.Add( 3 );
Assert.IsTrue( list.Count == 1 );
Assert.AreEqual( 3, list[0] );
list.Remove( 3 );
Assert.IsTrue( list.Count == 0 );
}
[TestMethod]
public void Iterate()
{
using var list = new NetList<int>();
list.Add( 1 );
list.Add( 2 );
list.Add( 3 );
var current = 0;
foreach ( var item in list )
{
current++;
Assert.AreEqual( item, current );
}
Assert.AreEqual( 1, list[0] );
Assert.AreEqual( 2, list[1] );
Assert.AreEqual( 3, list[2] );
}
[TestMethod]
public void OnChangedIsInvokedWhenItemIsAdded()
{
using var list = new NetList<int>();
var callCount = 0;
NetListChangeEvent<int> receivedEvent = default;
list.OnChanged = change =>
{
callCount++;
receivedEvent = change;
};
list.Add( 42 );
Assert.AreEqual( 1, callCount );
Assert.AreEqual( NotifyCollectionChangedAction.Add, receivedEvent.Type );
Assert.AreEqual( 0, receivedEvent.Index );
Assert.AreEqual( 42, receivedEvent.NewValue );
}
[TestMethod]
public void OnChangedIsInvokedWhenItemIsRemoved()
{
using var list = new NetList<int>();
list.Add( 10 );
list.Add( 20 );
var callCount = 0;
NetListChangeEvent<int> receivedEvent = default;
list.OnChanged = change =>
{
callCount++;
receivedEvent = change;
};
list.Remove( 10 );
Assert.AreEqual( 1, callCount );
Assert.AreEqual( NotifyCollectionChangedAction.Remove, receivedEvent.Type );
Assert.AreEqual( 0, receivedEvent.Index ); // 10 was at index 0
Assert.AreEqual( 10, receivedEvent.OldValue ); // removed value
}
[TestMethod]
public void OnChangedIsInvokedWhenListIsCleared()
{
using var list = new NetList<int>();
list.Add( 1 );
list.Add( 2 );
var callCount = 0;
NetListChangeEvent<int> receivedEvent = default;
list.OnChanged = change =>
{
callCount++;
receivedEvent = change;
};
list.Clear();
Assert.AreEqual( 1, callCount );
Assert.AreEqual( NotifyCollectionChangedAction.Reset, receivedEvent.Type );
Assert.AreEqual( 0, list.Count );
}
[TestMethod]
public void OnChangedIsNotInvokedWhenNoChangeOccurs()
{
using var list = new NetList<int>();
var callCount = 0;
list.OnChanged = _ =>
{
callCount++;
};
list.Add( 5 );
// Removing an item that does not exist should not trigger a change
list.Remove( 999 );
Assert.AreEqual( 1, callCount );
}
[TestMethod]
public void ValidAccess()
{
using var list = new NetList<int>();
Assert.ThrowsException<ArgumentOutOfRangeException>( () =>
{
list[0] = 1;
} );
}
}