[ADDITION] Added username tests, as well as random word tests.
This commit is contained in:
59
Eindproject/Tests/JSONConvertRandomWord.cs
Normal file
59
Eindproject/Tests/JSONConvertRandomWord.cs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using SharedClientServer;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Tests
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class JSONConvertRandomWord
|
||||||
|
{
|
||||||
|
public byte[] GetPayload(byte[] message)
|
||||||
|
{
|
||||||
|
byte[] payload = new byte[message.Length - 5];
|
||||||
|
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] RandomWord()
|
||||||
|
{
|
||||||
|
byte identifier = 0x07;
|
||||||
|
dynamic payload = new
|
||||||
|
{
|
||||||
|
word = "teacher"
|
||||||
|
};
|
||||||
|
|
||||||
|
byte[] res = JSONConvert.GetMessageToSend(identifier, payload);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public dynamic GetDynamic(byte[] payload)
|
||||||
|
{
|
||||||
|
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(GetPayload(payload)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestSendRandomWord()
|
||||||
|
{
|
||||||
|
string randomWord = JSONConvert.SendRandomWord("WordsForGame.json");
|
||||||
|
|
||||||
|
string result = "teacher";
|
||||||
|
|
||||||
|
Assert.AreEqual(result, randomWord);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestGetRandomWord()
|
||||||
|
{
|
||||||
|
byte[] data = GetPayload(RandomWord());
|
||||||
|
string result = JSONConvert.GetRandomWord(data);
|
||||||
|
|
||||||
|
string word = "teacher";
|
||||||
|
|
||||||
|
Assert.AreEqual(word, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
89
Eindproject/Tests/JSONConvertUserMessages.cs
Normal file
89
Eindproject/Tests/JSONConvertUserMessages.cs
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using SharedClientServer;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Tests
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class JSONConvertUserMessages
|
||||||
|
{
|
||||||
|
|
||||||
|
public byte[] GetPayload(byte[] message)
|
||||||
|
{
|
||||||
|
byte[] payload = new byte[message.Length - 5];
|
||||||
|
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
public dynamic GetDynamic(byte[] payload)
|
||||||
|
{
|
||||||
|
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(GetPayload(payload)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] ComboArray()
|
||||||
|
{
|
||||||
|
byte identifier = 0x02;
|
||||||
|
dynamic payload = new
|
||||||
|
{
|
||||||
|
username = "testName",
|
||||||
|
message = "message"
|
||||||
|
};
|
||||||
|
|
||||||
|
byte[] result = JSONConvert.GetMessageToSend(identifier, payload);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] LoginArray()
|
||||||
|
{
|
||||||
|
byte identifier = 0x01;
|
||||||
|
dynamic payload = new
|
||||||
|
{
|
||||||
|
username = "testname"
|
||||||
|
};
|
||||||
|
|
||||||
|
byte[] result = JSONConvert.GetMessageToSend(identifier, payload);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestGetUsernameAndMessage()
|
||||||
|
{
|
||||||
|
byte[] data = GetPayload(ComboArray());
|
||||||
|
(string,string) result = JSONConvert.GetUsernameAndMessage(data);
|
||||||
|
|
||||||
|
(string, string) testCombo = ("testName", "message");
|
||||||
|
|
||||||
|
Assert.AreEqual(testCombo, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestGetUsernameLogin()
|
||||||
|
{
|
||||||
|
byte[] data = GetPayload(LoginArray());
|
||||||
|
string result = JSONConvert.GetUsernameLogin(data);
|
||||||
|
string username = "testname";
|
||||||
|
|
||||||
|
Assert.AreEqual(username, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestConstructUsernameMessage()
|
||||||
|
{
|
||||||
|
byte[] res = JSONConvert.ConstructUsernameMessage("testname");
|
||||||
|
dynamic payload = GetDynamic(res);
|
||||||
|
|
||||||
|
string username = "testname";
|
||||||
|
|
||||||
|
Assert.AreEqual(0x01, res[4]);
|
||||||
|
Assert.AreEqual(username, (string)payload.username);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,16 @@
|
|||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="resources\WordsForGame.json" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="resources\WordsForGame.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.0.1" />
|
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.0.1" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
||||||
|
|||||||
31
Eindproject/Tests/resources/WordsForGame.json
Normal file
31
Eindproject/Tests/resources/WordsForGame.json
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"filename": "wordsForGame",
|
||||||
|
"words": [
|
||||||
|
"teacher",
|
||||||
|
"love",
|
||||||
|
"engineer",
|
||||||
|
"supermarket",
|
||||||
|
"disaster",
|
||||||
|
"studio",
|
||||||
|
"restaurant",
|
||||||
|
"music",
|
||||||
|
"chocolate",
|
||||||
|
"dirt",
|
||||||
|
"thought",
|
||||||
|
"virus",
|
||||||
|
"lieutenant",
|
||||||
|
"painter",
|
||||||
|
"kiwi",
|
||||||
|
"power ranger",
|
||||||
|
"computer",
|
||||||
|
"people",
|
||||||
|
"candidate",
|
||||||
|
"security guard",
|
||||||
|
"Canada",
|
||||||
|
"teeth",
|
||||||
|
"army",
|
||||||
|
"airport",
|
||||||
|
"president",
|
||||||
|
"bedroom"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -300,7 +300,7 @@ namespace SharedClientServer
|
|||||||
|
|
||||||
Debug.WriteLine($"[SERVERCLIENT] Sending random words {words}");
|
Debug.WriteLine($"[SERVERCLIENT] Sending random words {words}");
|
||||||
|
|
||||||
return words.words[index];
|
return words.words[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user