[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.Timers;
using System.Windows;
using System.Windows.Input;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

View File

@@ -9,11 +9,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "..\Client\Client.
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "SharedClientServer", "..\SharedClientServer\SharedClientServer.shproj", "{6D26F969-9CB1-414F-AC3E-7253D449AC5A}"
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
Global
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*{6d26f969-9cb1-414f-ac3e-7253d449ac5a}*SharedItemsImports = 13
..\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
{
[TestClass]
public class JSONConvertTest
{
[TestMethod]
public void TestGetMessageToSendLength()
{
byte identifier = 0x01;
dynamic payload = new
{
@@ -22,9 +25,9 @@ namespace Tests
byte[] result = JSONConvert.GetMessageToSend(identifier, payload);
Assert.AreEqual(payloadToBytes.Length + 5, result.Length);
Assert.AreEqual(0x01, result[4]);
}
[TestMethod]
public void TestGetMessageToSendIdentifier()
{
byte identifier = 0x01;
@@ -32,8 +35,6 @@ namespace Tests
{
value = "test"
};
string payloadToJson = JsonConvert.SerializeObject(payload);
byte[] payloadToBytes = Encoding.UTF8.GetBytes(payloadToJson);
byte[] result = JSONConvert.GetMessageToSend(identifier, payload);
Assert.AreEqual(0x01, result[4]);

View File

@@ -7,13 +7,19 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<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" />
</ItemGroup>
<Import Project="..\..\SharedClientServer\SharedClientServer.projitems" Label="Shared" />
<ItemGroup>
<ProjectReference Include="..\..\Client\Client.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,17 +1,15 @@
using Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Text;
using System.Windows.Media;
namespace SharedClientServer
{
class JSONConvert
public class JSONConvert
{
public const byte LOGIN = 0x01;
public const byte MESSAGE = 0x02;
@@ -23,7 +21,6 @@ namespace SharedClientServer
public const int CANVAS_WRITING = 0;
public const int CANVAS_RESET = 1;
public enum LobbyIdentifier
{
@@ -42,11 +39,11 @@ namespace SharedClientServer
NEXT_ROUND
}
public static (string,string) GetUsernameAndMessage(byte[] json)
public static (string, string) GetUsernameAndMessage(byte[] json)
{
string msg = Encoding.UTF8.GetString(json);
dynamic payload = JsonConvert.DeserializeObject(msg);
dynamic payload = JsonConvert.DeserializeObject(msg);
return (payload.username, payload.message);
}
@@ -88,7 +85,7 @@ namespace SharedClientServer
{
identifier = LobbyIdentifier.HOST,
id = lobbyID
}) ;
});
}
public static byte[] ConstructLobbyRequestMessage()
@@ -102,13 +99,12 @@ namespace SharedClientServer
public static byte[] ConstructLobbyListMessage(Lobby[] lobbiesList)
{
return GetMessageToSend(LOBBY, new
{
{
identifier = LobbyIdentifier.LIST,
lobbies = lobbiesList
});
}
public static byte[] ConstructLobbyJoinMessage(int lobbyID)
{
return GetMessageToSend(LOBBY, new
@@ -125,7 +121,8 @@ namespace SharedClientServer
identifier = LobbyIdentifier.LEAVE,
id = lobbyID
});
}
}
public static LobbyIdentifier GetLobbyIdentifier(byte[] json)
{
dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
@@ -161,8 +158,11 @@ namespace SharedClientServer
public static byte[] ConstructLobbyJoinSuccessMessage(bool isHost)
{
return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS,
host = isHost});
return GetMessageToSend(LOBBY, new
{
identifier = LobbyIdentifier.JOIN_SUCCESS,
host = isHost
});
}
public static bool GetLobbyJoinIsHost(byte[] json)
@@ -171,11 +171,10 @@ namespace SharedClientServer
return payload.host;
}
#endregion
#endregion lobby messages
public static byte[] ConstructCanvasDataSend(int typeToSend, double[][] buffer, Color colorToSend)
{
return GetMessageToSend(CANVAS, new
{
canvasType = typeToSend,
@@ -192,8 +191,8 @@ namespace SharedClientServer
coords = buffer,
color = colorToSend
});
}
}
public static byte[] ConstructCanvasReset()
{
dynamic payload = new
@@ -229,7 +228,6 @@ namespace SharedClientServer
public static byte[] ConstructGameStartData(int lobbyID)
{
return GetMessageToSend(GAME, new
{
command = GameCommand.START_GAME,
@@ -276,13 +274,14 @@ namespace SharedClientServer
// put the identifier at the start of the payload part
res[4] = identifier;
// 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;
}
/*
* 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)
{
dynamic words;
@@ -291,29 +290,27 @@ namespace SharedClientServer
string projDir = Directory.GetParent(workingDir).Parent.Parent.FullName;
string filePath = projDir += $@"\resources\{filename}";
using(StreamReader reader = new StreamReader(filePath))
using (StreamReader reader = new StreamReader(filePath))
{
string json = reader.ReadToEnd();
words = JsonConvert.DeserializeObject(json);
}
int index = random.Next(0, 24);
Debug.WriteLine($"[SERVERCLIENT] Sending random words {words}");
return words.words[index];
}
}
/*
* Client gets the payload and retrieves the word from the payload
*/
*/
public static string GetRandomWord(byte[] json)
{
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
return payload.word;
}
}
}
}

View File

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

View File

@@ -10,4 +10,4 @@
<PropertyGroup />
<Import Project="SharedClientServer.projitems" Label="Shared" />
<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
{
class User : IEquatable<User>
public class User : IEquatable<User>
{
private string _username;
private int _score;