[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

@@ -8,7 +8,7 @@ using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Timers; using System.Timers;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Shapes; using System.Windows.Shapes;

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

@@ -1,17 +1,15 @@
using Client; 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;
@@ -23,7 +21,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
{ {
@@ -42,11 +39,11 @@ namespace SharedClientServer
NEXT_ROUND NEXT_ROUND
} }
public static (string,string) GetUsernameAndMessage(byte[] json) public static (string, string) GetUsernameAndMessage(byte[] json)
{ {
string msg = Encoding.UTF8.GetString(json); string msg = Encoding.UTF8.GetString(json);
dynamic payload = JsonConvert.DeserializeObject(msg); dynamic payload = JsonConvert.DeserializeObject(msg);
return (payload.username, payload.message); return (payload.username, payload.message);
} }
@@ -88,7 +85,7 @@ namespace SharedClientServer
{ {
identifier = LobbyIdentifier.HOST, identifier = LobbyIdentifier.HOST,
id = lobbyID id = lobbyID
}) ; });
} }
public static byte[] ConstructLobbyRequestMessage() public static byte[] ConstructLobbyRequestMessage()
@@ -102,13 +99,12 @@ namespace SharedClientServer
public static byte[] ConstructLobbyListMessage(Lobby[] lobbiesList) public static byte[] ConstructLobbyListMessage(Lobby[] lobbiesList)
{ {
return GetMessageToSend(LOBBY, new return GetMessageToSend(LOBBY, new
{ {
identifier = LobbyIdentifier.LIST, identifier = LobbyIdentifier.LIST,
lobbies = lobbiesList lobbies = lobbiesList
}); });
} }
public static byte[] ConstructLobbyJoinMessage(int lobbyID) public static byte[] ConstructLobbyJoinMessage(int lobbyID)
{ {
return GetMessageToSend(LOBBY, new return GetMessageToSend(LOBBY, new
@@ -125,7 +121,8 @@ namespace SharedClientServer
identifier = LobbyIdentifier.LEAVE, identifier = LobbyIdentifier.LEAVE,
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,
@@ -192,8 +191,8 @@ namespace SharedClientServer
coords = buffer, coords = buffer,
color = colorToSend color = colorToSend
}); });
} }
public static byte[] ConstructCanvasReset() public static byte[] ConstructCanvasReset()
{ {
dynamic payload = new dynamic payload = new
@@ -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,
@@ -276,13 +274,14 @@ namespace SharedClientServer
// put the identifier at the start of the payload part // put the identifier at the start of the payload part
res[4] = identifier; res[4] = identifier;
// put the length of the payload at the start of the res array // put the length of the payload at the start of the res array
Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4); Array.Copy(BitConverter.GetBytes(payloadBytes.Length + 5), 0, res, 0, 4);
return res; return res;
} }
/* /*
* 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;
@@ -291,29 +290,27 @@ namespace SharedClientServer
string projDir = Directory.GetParent(workingDir).Parent.Parent.FullName; string projDir = Directory.GetParent(workingDir).Parent.Parent.FullName;
string filePath = projDir += $@"\resources\{filename}"; string filePath = projDir += $@"\resources\{filename}";
using(StreamReader reader = new StreamReader(filePath)) using (StreamReader reader = new StreamReader(filePath))
{ {
string json = reader.ReadToEnd(); string json = reader.ReadToEnd();
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}");
return words.words[index]; return words.words[index];
} }
/* /*
* 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

@@ -10,4 +10,4 @@
<PropertyGroup /> <PropertyGroup />
<Import Project="SharedClientServer.projitems" Label="Shared" /> <Import Project="SharedClientServer.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project> </Project>

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;