[FIX] tests??

This commit is contained in:
Sem van der Hoeven
2020-10-23 22:04:36 +02:00
parent 0e6a2bab59
commit 27e70d1ec0
10 changed files with 78 additions and 51 deletions

View File

@@ -9,11 +9,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "..\Client\Client.
EndProject EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "SharedClientServer", "..\SharedClientServer\SharedClientServer.shproj", "{6D26F969-9CB1-414F-AC3E-7253D449AC5A}" Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "SharedClientServer", "..\SharedClientServer\SharedClientServer.shproj", "{6D26F969-9CB1-414F-AC3E-7253D449AC5A}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{4CB2CD76-07D9-44D0-A559-A7A301621D30}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{4CB2CD76-07D9-44D0-A559-A7A301621D30}"
EndProject EndProject
Global Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\SharedClientServer\SharedClientServer.projitems*{4cb2cd76-07d9-44d0-a559-a7a301621d30}*SharedItemsImports = 5
..\SharedClientServer\SharedClientServer.projitems*{67a9bf1a-d317-47ca-9f07-c3480d1360ff}*SharedItemsImports = 5 ..\SharedClientServer\SharedClientServer.projitems*{67a9bf1a-d317-47ca-9f07-c3480d1360ff}*SharedItemsImports = 5
..\SharedClientServer\SharedClientServer.projitems*{6d26f969-9cb1-414f-ac3e-7253d449ac5a}*SharedItemsImports = 13 ..\SharedClientServer\SharedClientServer.projitems*{6d26f969-9cb1-414f-ac3e-7253d449ac5a}*SharedItemsImports = 13
..\SharedClientServer\SharedClientServer.projitems*{83768edb-097e-4089-a5de-208cb252d1a0}*SharedItemsImports = 5 ..\SharedClientServer\SharedClientServer.projitems*{83768edb-097e-4089-a5de-208cb252d1a0}*SharedItemsImports = 5

View File

@@ -0,0 +1,10 @@
using SharedClientServer;
using System;
namespace TestHelper
{
public class Helper
{
public static JSONConvert JSONConvert;
}
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<Import Project="..\..\SharedClientServer\SharedClientServer.projitems" Label="Shared" />
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>

View File

@@ -6,12 +6,15 @@ using Xunit.Sdk;
namespace Tests namespace Tests
{ {
[TestClass] [TestClass]
public class JSONConvertTest public class JSONConvertTest
{ {
[TestMethod] [TestMethod]
public void TestGetMessageToSendLength() public void TestGetMessageToSendLength()
{ {
byte identifier = 0x01; byte identifier = 0x01;
dynamic payload = new dynamic payload = new
{ {
@@ -22,9 +25,9 @@ namespace Tests
byte[] result = JSONConvert.GetMessageToSend(identifier, payload); byte[] result = JSONConvert.GetMessageToSend(identifier, payload);
Assert.AreEqual(payloadToBytes.Length + 5, result.Length); Assert.AreEqual(payloadToBytes.Length + 5, result.Length);
Assert.AreEqual(0x01, result[4]);
} }
[TestMethod]
public void TestGetMessageToSendIdentifier() public void TestGetMessageToSendIdentifier()
{ {
byte identifier = 0x01; byte identifier = 0x01;
@@ -32,8 +35,6 @@ namespace Tests
{ {
value = "test" value = "test"
}; };
string payloadToJson = JsonConvert.SerializeObject(payload);
byte[] payloadToBytes = Encoding.UTF8.GetBytes(payloadToJson);
byte[] result = JSONConvert.GetMessageToSend(identifier, payload); byte[] result = JSONConvert.GetMessageToSend(identifier, payload);
Assert.AreEqual(0x01, result[4]); Assert.AreEqual(0x01, result[4]);

View File

@@ -7,13 +7,19 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> <PackageReference Include="Extended.Wpf.Toolkit" Version="4.0.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" /> <PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="coverlet.collector" Version="1.2.0" /> <PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.extensibility.execution" Version="2.4.1" /> <PackageReference Include="xunit.extensibility.execution" Version="2.4.1" />
</ItemGroup> </ItemGroup>
<Import Project="..\..\SharedClientServer\SharedClientServer.projitems" Label="Shared" /> <ItemGroup>
<ProjectReference Include="..\..\Client\Client.csproj" />
</ItemGroup>
</Project> </Project>

View File

@@ -2,16 +2,14 @@ using Client;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Windows.Media; using System.Windows.Media;
namespace SharedClientServer namespace SharedClientServer
{ {
class JSONConvert public class JSONConvert
{ {
public const byte LOGIN = 0x01; public const byte LOGIN = 0x01;
public const byte MESSAGE = 0x02; public const byte MESSAGE = 0x02;
@@ -24,7 +22,6 @@ namespace SharedClientServer
public const int CANVAS_WRITING = 0; public const int CANVAS_WRITING = 0;
public const int CANVAS_RESET = 1; public const int CANVAS_RESET = 1;
public enum LobbyIdentifier public enum LobbyIdentifier
{ {
HOST, HOST,
@@ -108,7 +105,6 @@ namespace SharedClientServer
}); });
} }
public static byte[] ConstructLobbyJoinMessage(int lobbyID) public static byte[] ConstructLobbyJoinMessage(int lobbyID)
{ {
return GetMessageToSend(LOBBY, new return GetMessageToSend(LOBBY, new
@@ -126,6 +122,7 @@ namespace SharedClientServer
id = lobbyID id = lobbyID
}); });
} }
public static LobbyIdentifier GetLobbyIdentifier(byte[] json) public static LobbyIdentifier GetLobbyIdentifier(byte[] json)
{ {
dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json)); dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
@@ -161,8 +158,11 @@ namespace SharedClientServer
public static byte[] ConstructLobbyJoinSuccessMessage(bool isHost) public static byte[] ConstructLobbyJoinSuccessMessage(bool isHost)
{ {
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS, return GetMessageToSend(LOBBY, new
host = isHost}); {
identifier = LobbyIdentifier.JOIN_SUCCESS,
host = isHost
});
} }
public static bool GetLobbyJoinIsHost(byte[] json) public static bool GetLobbyJoinIsHost(byte[] json)
@@ -171,11 +171,10 @@ namespace SharedClientServer
return payload.host; return payload.host;
} }
#endregion #endregion lobby messages
public static byte[] ConstructCanvasDataSend(int typeToSend, double[][] buffer, Color colorToSend) public static byte[] ConstructCanvasDataSend(int typeToSend, double[][] buffer, Color colorToSend)
{ {
return GetMessageToSend(CANVAS, new return GetMessageToSend(CANVAS, new
{ {
canvasType = typeToSend, canvasType = typeToSend,
@@ -229,7 +228,6 @@ namespace SharedClientServer
public static byte[] ConstructGameStartData(int lobbyID) public static byte[] ConstructGameStartData(int lobbyID)
{ {
return GetMessageToSend(GAME, new return GetMessageToSend(GAME, new
{ {
command = GameCommand.START_GAME, command = GameCommand.START_GAME,
@@ -283,6 +281,7 @@ namespace SharedClientServer
/* /*
* This method sends a random word from the json file, this happens when the client joins a lobby. * This method sends a random word from the json file, this happens when the client joins a lobby.
*/ */
public static string SendRandomWord(string filename) public static string SendRandomWord(string filename)
{ {
dynamic words; dynamic words;
@@ -297,7 +296,6 @@ namespace SharedClientServer
words = JsonConvert.DeserializeObject(json); words = JsonConvert.DeserializeObject(json);
} }
int index = random.Next(0, 24); int index = random.Next(0, 24);
Debug.WriteLine($"[SERVERCLIENT] Sending random words {words}"); Debug.WriteLine($"[SERVERCLIENT] Sending random words {words}");
@@ -308,12 +306,11 @@ namespace SharedClientServer
/* /*
* Client gets the payload and retrieves the word from the payload * Client gets the payload and retrieves the word from the payload
*/ */
public static string GetRandomWord(byte[] json) public static string GetRandomWord(byte[] json)
{ {
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
return payload.word; return payload.word;
} }
} }
} }

View File

@@ -6,7 +6,7 @@ using System.Text;
namespace Client namespace Client
{ {
internal class Lobby : INotifyPropertyChanged public class Lobby : INotifyPropertyChanged
{ {
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;

View File

@@ -6,7 +6,7 @@ using System.Text;
namespace SharedClientServer namespace SharedClientServer
{ {
class User : IEquatable<User> public class User : IEquatable<User>
{ {
private string _username; private string _username;
private int _score; private int _score;