Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize tests code #1297

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions SteamKit2/Tests/CDNClientFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task ThrowsSteamKitWebExceptionOnUnsuccessfulWebResponseForManifest
{
var configuration = SteamConfiguration.Create( x => x.WithHttpClientFactory( () => new HttpClient( new TeapotHttpMessageHandler() ) ) );
var steam = new SteamClient( configuration );
var client = new Client( steam );
using var client = new Client( steam );
var server = new Server
{
Protocol = Server.ConnectionProtocol.HTTP,
Expand All @@ -34,7 +34,7 @@ public async Task ThrowsSteamKitWebExceptionOnUnsuccessfulWebResponseForChunk()
{
var configuration = SteamConfiguration.Create( x => x.WithHttpClientFactory( () => new HttpClient( new TeapotHttpMessageHandler() ) ) );
var steam = new SteamClient( configuration );
var client = new Client( steam );
using var client = new Client( steam );
var server = new Server
{
Protocol = Server.ConnectionProtocol.HTTP,
Expand All @@ -44,7 +44,7 @@ public async Task ThrowsSteamKitWebExceptionOnUnsuccessfulWebResponseForChunk()
};
var chunk = new DepotManifest.ChunkData
{
ChunkID = new byte[] { 0xFF },
ChunkID = [0xFF],
};

var ex = await Assert.ThrowsAsync<SteamKitWebRequestException>( () => client.DownloadDepotChunkAsync( depotId: 0, chunk, server ) );
Expand All @@ -56,7 +56,7 @@ public async Task ThrowsWhenNoChunkIDIsSet()
{
var configuration = SteamConfiguration.Create( x => x.WithHttpClientFactory( () => new HttpClient( new TeapotHttpMessageHandler() ) ) );
var steam = new SteamClient( configuration );
var client = new Client( steam );
using var client = new Client( steam );
var server = new Server
{
Protocol = Server.ConnectionProtocol.HTTP,
Expand Down
8 changes: 3 additions & 5 deletions SteamKit2/Tests/CMClientFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ public void GetPacketMsgFailsWithTinyArray()

static byte[] Serialize(ISteamSerializableHeader hdr)
{
using (var ms = new MemoryStream())
{
hdr.Serialize(ms);
return ms.ToArray();
}
using var ms = new MemoryStream();
hdr.Serialize( ms );
return ms.ToArray();
}

class DummyCMClient : CMClient
Expand Down
70 changes: 35 additions & 35 deletions SteamKit2/Tests/CallbackManagerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public void PostedCallbackTriggersAction()
var callback = new CallbackForTest { UniqueID = Guid.NewGuid() };

var didCall = false;
Action<CallbackForTest> action = delegate(CallbackForTest cb)
void action( CallbackForTest cb )
yaakov-h marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.Equal(callback.UniqueID, cb.UniqueID);
Assert.Equal( callback.UniqueID, cb.UniqueID );
didCall = true;
};
}

using (mgr.Subscribe(action))
using (mgr.Subscribe<CallbackForTest>( action ))
{
PostAndRunCallback(callback);
}
Expand All @@ -46,15 +46,15 @@ public void PostedCallbackTriggersAction_CatchAll()
var callback = new CallbackForTest { UniqueID = Guid.NewGuid() };

var didCall = false;
Action<CallbackMsg> action = delegate(CallbackMsg cb)
void action( CallbackMsg cb )
yaakov-h marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.IsType<CallbackForTest>(cb);
var cft = (CallbackForTest)cb;
Assert.Equal(callback.UniqueID, cft.UniqueID);
Assert.IsType<CallbackForTest>( cb );
var cft = ( CallbackForTest )cb;
Assert.Equal( callback.UniqueID, cft.UniqueID );
didCall = true;
};
}

using (mgr.Subscribe(action))
using ( mgr.Subscribe<CallbackMsg>( action ) )
{
PostAndRunCallback(callback);
}
Expand All @@ -69,14 +69,14 @@ public void PostedCallbackTriggersActionForExplicitJobIDInvalid()
var callback = new CallbackForTest { JobID = jobID, UniqueID = Guid.NewGuid() };

var didCall = false;
Action<CallbackForTest> action = delegate(CallbackForTest cb)
void action( CallbackForTest cb )
yaakov-h marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.Equal(callback.UniqueID, cb.UniqueID);
Assert.Equal(jobID, cb.JobID);
Assert.Equal( callback.UniqueID, cb.UniqueID );
Assert.Equal( jobID, cb.JobID );
didCall = true;
};
}

using (mgr.Subscribe(JobID.Invalid, action))
using ( mgr.Subscribe<CallbackForTest>( JobID.Invalid, action ) )
{
PostAndRunCallback(callback);
}
Expand All @@ -91,14 +91,14 @@ public void PostedCallbackWithJobIDTriggersActionWhenNoJobIDSpecified()
var callback = new CallbackForTest { JobID = jobID, UniqueID = Guid.NewGuid() };

var didCall = false;
Action<CallbackForTest> action = delegate(CallbackForTest cb)
void action( CallbackForTest cb )
{
Assert.Equal(callback.UniqueID, cb.UniqueID);
Assert.Equal(jobID, cb.JobID);
Assert.Equal( callback.UniqueID, cb.UniqueID );
Assert.Equal( jobID, cb.JobID );
didCall = true;
};
}

using (mgr.Subscribe(action))
using ( mgr.Subscribe<CallbackForTest>( action ) )
{
PostAndRunCallback(callback);
}
Expand All @@ -113,12 +113,12 @@ public void PostedCallbackDoesNotTriggerActionForWrongJobID()
var callback = new CallbackForTest { JobID = jobID, UniqueID = Guid.NewGuid() };

var didCall = false;
Action<CallbackForTest> action = delegate(CallbackForTest cb)
void action( CallbackForTest cb )
{
didCall = true;
};
}

using (mgr.Subscribe(123, action))
using ( mgr.Subscribe<CallbackForTest>( 123, action ) )
{
PostAndRunCallback(callback);
}
Expand All @@ -133,14 +133,14 @@ public void PostedCallbackWithJobIDTriggersCallbackForJobID()
var callback = new CallbackForTest { JobID = jobID, UniqueID = Guid.NewGuid() };

var didCall = false;
Action<CallbackForTest> action = delegate(CallbackForTest cb)
void action( CallbackForTest cb )
{
Assert.Equal(callback.UniqueID, cb.UniqueID);
Assert.Equal(jobID, cb.JobID);
Assert.Equal( callback.UniqueID, cb.UniqueID );
Assert.Equal( jobID, cb.JobID );
didCall = true;
};
}

using (mgr.Subscribe(123456, action))
using ( mgr.Subscribe<CallbackForTest>( 123456, action ) )
{
PostAndRunCallback(callback);
}
Expand All @@ -154,12 +154,12 @@ public void SubscribedFunctionDoesNotRunWhenSubscriptionIsDisposed()
var callback = new CallbackForTest();

var callCount = 0;
Action<CallbackForTest> action = delegate (CallbackForTest cb)
void action( CallbackForTest cb )
{
callCount++;
};
}

using (mgr.Subscribe(action))
using ( mgr.Subscribe<CallbackForTest>( action ) )
{
PostAndRunCallback(callback);
}
Expand All @@ -174,13 +174,13 @@ public void PostedCallbacksTriggerActions()
var callback = new CallbackForTest { UniqueID = Guid.NewGuid() };

var numCallbacksRun = 0;
Action<CallbackForTest> action = delegate (CallbackForTest cb)
void action( CallbackForTest cb )
{
Assert.Equal(callback.UniqueID, cb.UniqueID);
Assert.Equal( callback.UniqueID, cb.UniqueID );
numCallbacksRun++;
};
}

using (mgr.Subscribe(action))
using ( mgr.Subscribe<CallbackForTest>( action ) )
{
for (var i = 0; i < 10; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions SteamKit2/Tests/ClientMsgFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ClientMsgFacts
{
// this test vector is a packet meant for a ClientMsg<MsgClientChatEnter>
static byte[] structMsgData =
{
[
0x27, 0x03, 0x00, 0x00, 0x24, 0x02, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0xAC, 0x15, 0x89, 0x00, 0x01, 0x00, 0x10, 0x01,
0x8E, 0x56, 0x11, 0x00, 0xBC, 0x4E, 0x2A, 0x00, 0x00, 0x00, 0x88, 0x01, 0x00, 0x00, 0x00, 0x00,
Expand All @@ -32,7 +32,7 @@ public class ClientMsgFacts
0x72, 0x6D, 0x69, 0x73, 0x73, 0x69, 0x6F, 0x6E, 0x73, 0x00, 0x1A, 0x03, 0x00, 0x00, 0x02, 0x44,
0x65, 0x74, 0x61, 0x69, 0x6C, 0x73, 0x00, 0x02, 0x00, 0x00, 0x00, 0x08, 0x08, 0xE8, 0x03, 0x00,
0x00,
};
];

[Fact]
public void PayloadReaderReadsNullTermString()
Expand Down
3 changes: 1 addition & 2 deletions SteamKit2/Tests/CryptoHelperFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public void TestSymmetricEncryption()
const string decryptedExpected = "this is a 24 byte string";
const string encryptionKey = "encryption key";

using var sha256 = SHA256.Create();
var key = sha256.ComputeHash( Encoding.UTF8.GetBytes( encryptionKey ) );
var key = SHA256.HashData( Encoding.UTF8.GetBytes( encryptionKey ) );

var encryptedData = Encoding.UTF8.GetBytes( decryptedExpected );
encryptedData = CryptoHelper.SymmetricEncrypt( encryptedData, key );
Expand Down
2 changes: 1 addition & 1 deletion SteamKit2/Tests/FileStorageServerListProviderFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public FileStorageServerListProviderFacts()
[Fact]
public async Task ReadsUpdatedServerList()
{
var initialServers = await fileStorageProvider.FetchServerListAsync();
await fileStorageProvider.FetchServerListAsync();

await fileStorageProvider.UpdateServerListAsync(new List<ServerRecord>()
{
Expand Down
52 changes: 26 additions & 26 deletions SteamKit2/Tests/Helpers/HandlerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@

namespace Tests
{
public class HandlerTestBase<THandler>
where THandler : ClientMsgHandler
{
public HandlerTestBase()
{
client = new SteamClient();
handler = client.GetHandler<THandler>();

callbackMgr = new CallbackManager( client );
}
public class HandlerTestBase<THandler>
where THandler : ClientMsgHandler
{
public HandlerTestBase()
{
client = new SteamClient();
handler = client.GetHandler<THandler>();

readonly SteamClient client;
readonly THandler handler;
readonly CallbackManager callbackMgr;
callbackMgr = new CallbackManager( client );
}

protected SteamClient SteamClient
{
get { return client; }
}
readonly SteamClient client;
readonly THandler handler;
readonly CallbackManager callbackMgr;

protected THandler Handler
{
get { return handler; }
}
protected SteamClient SteamClient
{
get { return client; }
}

protected CallbackManager CallbackManager
{
get { return callbackMgr; }
}
}
protected THandler Handler
{
get { return handler; }
}

protected CallbackManager CallbackManager
{
get { return callbackMgr; }
}
}
}
20 changes: 7 additions & 13 deletions SteamKit2/Tests/KeyValueFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public void KeyValuesFailsToReadTruncatedBinary()
// Test every possible truncation boundary we have.
for ( int i = 0; i < TestObjectHex.Length; i += 2 )
{
var binary = Utils.DecodeHexString( TestObjectHex.Substring( 0, i ) );
var binary = Utils.DecodeHexString( TestObjectHex[ ..i ] );
var kv = new KeyValue();
bool success;
using ( var ms = new MemoryStream( binary ) )
Expand Down Expand Up @@ -423,10 +423,8 @@ public void KeyValuesSavesTextToStream()
{
kv.SaveToStream( ms, asBinary: false );
ms.Seek( 0, SeekOrigin.Begin );
using ( var reader = new StreamReader( ms ) )
{
text = reader.ReadToEnd();
}
using var reader = new StreamReader( ms );
text = reader.ReadToEnd();
}

Assert.Equal( expected, text );
Expand Down Expand Up @@ -468,10 +466,8 @@ public void KeyValuesEscapesTextWhenSerializing()
{
kv.SaveToStream( ms, asBinary: false );
ms.Seek( 0, SeekOrigin.Begin );
using ( var reader = new StreamReader( ms ) )
{
text = reader.ReadToEnd();
}
using var reader = new StreamReader( ms );
text = reader.ReadToEnd();
}

var expectedValue = "\"key\"\n{\n\t\"slashes\"\t\t\"\\\\o/\"\n\t\"newline\"\t\t\"\\r\\n\"\n}\n";
Expand All @@ -490,10 +486,8 @@ public void KeyValuesTextPreserveEmptyObjects()
{
kv.SaveToStream( ms, asBinary: false );
ms.Seek( 0, SeekOrigin.Begin );
using ( var reader = new StreamReader( ms ) )
{
text = reader.ReadToEnd();
}
using var reader = new StreamReader( ms );
text = reader.ReadToEnd();
}

var expectedValue = "\"key\"\n{\n\t\"emptyObj\"\n\t{\n\t}\n\t\"emptyString\"\t\t\"\"\n}\n";
Expand Down
Loading
Loading